Question

What's DBExecutor class and when is it needed?

I found that query method Execute can be run with DBExecutor passed as parameter and without it. Do you know what's the difference? I didn't find documentation explaining this.

Like

10 comments

Dear Carlos,

There is no Execute method in EntitySchemaQuery class. Could you write here a code example to show the context of your question?

I meant the literal Query class :) The one from which for example Delete inherits. Example code 1:
 

var query = new Delete(userConnection);
query.From("UsrBook");
query.Where("UsrAuthor").IsEqual(Column.Parameter(authorId));
query.Execute();

Example code 2:
 

using (DBExecutor exec = userConnection.EnsureDBConnection())
{
    var query = new Delete(userConnection);
    query.From("UsrBook");
    query.Where("UsrAuthor").IsEqual(Column.Parameter(authorId));
    query.Execute(exec);
}

 

Here you will find Query class documentation. Click on Execute method to see all overloads and theirs description.

Peter Vdovukhin,

I saw that already but it doesn't explain much. What's exactly the difference between:

 

Executes the query text and returns the number of rows affected when executing the query.

and:

Executes the query text, using the specified DBExecutor instance, and returns the number of rows affected when executing the query.

?

What is this DBExecutor class for if it's not necessary to execute queries?

DBExecutor is a class that manages connection to the database. It is called always when you work with queries. By default every time you execute ESQ query, Select, Delete or any other query a new instance of this class is created. You can pass an existing DBExecutor instance just for optimization in order not to create an extra object or for using transactions. For example, if you need to run 3 queries in a row, you can create one DBExecutor and use it again in all these quires. How to create a new instance of this class and use it correctly you can read here

Ok, thanks, that clears things up :)

Welcome. As a bonus take an implementation of these two methods in a Query class:

public virtual int Execute() {
            string sqlText = GetSqlText();
            using (DBExecutor dbExecutor = UserConnection.EnsureDBConnection()) {
                return HasParameters ? dbExecutor.Execute(sqlText, Parameters) :
                    dbExecutor.Execute(sqlText);
            }
        }

public virtual int Execute(DBExecutor dbExecutor) {
            string sqlText = GetSqlText();
            return HasParameters ? dbExecutor.Execute(sqlText, Parameters) :
                dbExecutor.Execute(sqlText);
        }

Peter Vdovukhin,

So I want to add couple of objects to database using EntitySchemaQuery and I want to reuse the same database connection. Is what I'm doing here correct?

using (dbExecutor = userConnection.EnsureDBConnection())
{
    var schema = dbExecutor.UserConnection.EntitySchemaManager.GetInstanceByName("UsrExampleSchema");
    Entity entity1 = schema.CreateEntity(dbExecutor.UserConnection);
    entity1.SetDefColumnValues();
    entity1.CreateInsert().Execute(dbExecutor);
 
    Entity entity2 = schema.CreateEntity(dbExecutor.UserConnection);
    entity2.SetDefColumnValues();
    entity2.CreateInsert().Execute(dbExecutor);
}

Or is it possible to avoid using CreateInsert method and use DBExecutor directly with EntitySchemaQuery?

Dear Carlos,

Your code seems too complex and I cannot predict if it works. You can try it to check the correctness of this code. The one thing I can tell: you cannot directly use Execute method on Entity.

 Please, be advised that in 99.9% cases you do not need a dbExecutor class. First, when using dbExecutor directly with Select, Update, Create, Delete classes you bypass any events such as running business processes or validation at application level so you may finish with incorrect object state in the DB. You also bypass rights check because you execute a direct sql query.

Second, the overhead of using every time a new dbExecutor is very small

In your case just use entity.save() without dealing with dbExecutor at all.
So after refactoring your code will look like this:
 

var schema = UserConnection.EntitySchemaManager.GetInstanceByName("UsrExampleSchema");
    Entity entity1 = schema.CreateEntity(UserConnection);
    entity1.SetDefColumnValues();
    entity1.Save();
 
    Entity entity2 = schema.CreateEntity(UserConnection);
    entity2.SetDefColumnValues();
    entity2.Save();

 

You do need dbExecutor when work only with Update, Select, Create, Delete classes. It is mandatory.

Peter Vdovukhin,

Ok, thanks.

Log in or register to comment