J
Jehu Galeahsa
Hello:
I am working on a pet project. I am playing around with some database
code. One of the things I am working on is generating SQL dynamically.
I have a pretty good architecture at this point. You can check it out
at earthworm.codeplex.com.
Does anyone have any ideas on a good way to handle provider-specific
extensions to SQL? For instance, T-SQL has a TOP N modifier and a way
of retrieving the AUTO INCREMENT value after an insert. These aren't
part of the ANSI standard. However, I don't want to prevent someone
using SQL Server from finding my library useful. Essentially, I want
to allow arbitrary modifications to the SQL without requiring code
that needs to search within the generated SQL or a lot of string
manipulating.
It would be nice to provide access to the columns, etc. that can be
used when modifying the SQL. That way the correct aliases are used.
I was thinking of breaking out each clause in the select statment
(projection, from, where, order by, group by) and providing a
"before", and "after" event that allowed some text to be manually
added in. For instance, someone might attach to a "ProjectionBuilding"
event and add a TOP 3 to the SQL before any columns are listed. But
how can I do this in a user-friendly manner? Perhaps I can't.
Here is an example of what the code looks like now:
Table table = new Table("test");
QueryBuilder builder = new QueryBuilder(table);
Column nameColumn = table.CreateColumn("name");
builder.AddProjection(column1);
Column titleColumn = table.CreateColumn("title");
builder.AddProjection(column2);
Column idColumn = table.CreateColumn("id");
builder.Where.AddFilter(new EqualToFilter(idColumn, new
NumericLiteral(1234)));
builder.AddOrderBy(new OrderBy(name, Order.Ascending,
NullPlacement.First));
string commandText = builder.CommandText;
It will generate SQL like this:
SELECT test.name, test.title
FROM test
WHERE test.id = 1234
ORDER BY test.name ASC NULLS FIRST
I'd appreciate any ideas. It is fun playing around with this stuff.
I am working on a pet project. I am playing around with some database
code. One of the things I am working on is generating SQL dynamically.
I have a pretty good architecture at this point. You can check it out
at earthworm.codeplex.com.
Does anyone have any ideas on a good way to handle provider-specific
extensions to SQL? For instance, T-SQL has a TOP N modifier and a way
of retrieving the AUTO INCREMENT value after an insert. These aren't
part of the ANSI standard. However, I don't want to prevent someone
using SQL Server from finding my library useful. Essentially, I want
to allow arbitrary modifications to the SQL without requiring code
that needs to search within the generated SQL or a lot of string
manipulating.
It would be nice to provide access to the columns, etc. that can be
used when modifying the SQL. That way the correct aliases are used.
I was thinking of breaking out each clause in the select statment
(projection, from, where, order by, group by) and providing a
"before", and "after" event that allowed some text to be manually
added in. For instance, someone might attach to a "ProjectionBuilding"
event and add a TOP 3 to the SQL before any columns are listed. But
how can I do this in a user-friendly manner? Perhaps I can't.
Here is an example of what the code looks like now:
Table table = new Table("test");
QueryBuilder builder = new QueryBuilder(table);
Column nameColumn = table.CreateColumn("name");
builder.AddProjection(column1);
Column titleColumn = table.CreateColumn("title");
builder.AddProjection(column2);
Column idColumn = table.CreateColumn("id");
builder.Where.AddFilter(new EqualToFilter(idColumn, new
NumericLiteral(1234)));
builder.AddOrderBy(new OrderBy(name, Order.Ascending,
NullPlacement.First));
string commandText = builder.CommandText;
It will generate SQL like this:
SELECT test.name, test.title
FROM test
WHERE test.id = 1234
ORDER BY test.name ASC NULLS FIRST
I'd appreciate any ideas. It is fun playing around with this stuff.