DbExtensions - Cannot Manipulate SqlBuilder

280 Views Asked by At

I have an existing SqlBuilder, which looks like this

var query = SQL
    .WHERE("Field1 = {0}", "bob");

Now i want to pass this sqlBuilder into another function, so it can be manipulated, where by a SELECT, FROM and additional WHERE clauses can be added to it, but this doesnt seem to be possible?

If i pass this sqlBuilder into a function and call .SELECT like so

public void myFunction(SqlBuilder sqlBuilder)
{
    var newBuilder = sqlBuilder.Clone()
        .SELECT("*")
        .FROM("myTable")
        .WHERE("Field2 = {0}", "something");
}

Now i would expect the SQL to be something like

SELECT * FROM myTable WHERE Field1 = 'bob' AND Field2 = 'something'

But instead i get SQL equal to

WHERE Field1 = 'bob' SELECT * FROM myTable WHERE Field2 = 'something'

Obviously this is invalid. How can i do what i want? Is this not possible with DbExtensions?

As i said, i would like to manipulate the existing SqlBuilder (clone it first) and add additional clauses to it. Do you have to add each clause in order? Thought it would hold each in some sort of background dictionary until it needed to create the SQL string?

1

There are 1 best solutions below

1
On

With SqlBuilder, order matters, just like with StringBuilder. You have to either adapt your code, or use SqlSet instead.