Creating Truncate Table extension

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 06-Jun-2012 17:38:49   

I need to issue a Truncate Table instead of deleting individual records. I can easily do that with TypedListDAO and an ActionQuery.

However, I thought I would make an extension to do this and that code is below. Few questions:

1) Seems this would be better if I could call the extension on a collection entity - but I have no way of getting to the entity.LLBLGenProEntityName, so I had to put this extension on an entity. Is there a way to do this?

2) Is my use of the DQE and such correct below?

        public static void TruncateTable(this IEntityCore entity)
        {
            TypedListDAO typedListDAO = new TypedListDAO();

            IFieldPersistenceInfo[] persitanceInfo = PersistenceInfoProviderSingleton.GetInstance().GetAllFieldPersistenceInfos(entity.LLBLGenProEntityName);
            if (persitanceInfo == null || persitanceInfo.Length == 0)
                throw new Exception("Invalid entity for TruncateTable");
            var dqe = new SD.LLBLGen.Pro.DQE.SqlServer.DynamicQueryEngine();
            string tableToTruncate = dqe.Creator.CreateObjectName(persitanceInfo[0]);

            // ACTION QUERY
            // create an action query using a connection created by our DbUtils...
            IActionQuery query2 = new ActionQuery(new SqlCommand("truncate table " + tableToTruncate));

            // execute the query
            typedListDAO.ExecuteActionQuery(query2, null);
        }

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 06-Jun-2012 18:02:54   

3) How can I do this line so it is not SQL specific (if possible - not that I really need to, but would be nice to have it right)

        var dqe = new SD.LLBLGen.Pro.DQE.SqlServer.DynamicQueryEngine();

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Jun-2012 20:01:56   

You can use the EntityFactoryToUse property of the entityCollection, to create an entity and hence get its name.