Sql dot is a library which allows, to embed your own sql engine (parser, compiler and runtime) into your .net application.
The parser is completly written using c#
and available under the MIT
license.
Initialy the parser is written for the ODBC-Driver
used by the Simplic Content Delivery Network
and is set as open source
project, to let other benefit from this project and create some great sql-implementation togehter.
The aim is, to create a library which allows to embed sql in your appliaction, only by implementing a single interface: IQueryExecutor
.
The complete communication with your software is written in this implementation and registered in one line.
Implementation
public class MyQueryExecutor : IQueryExecutor
{
// ...
}
Registration
// Register the query executor
SqlDotNet.Sql.RegisterExecutor<MyQueryExecutor>()
SIQL - Structured Intermidiate Query Lanugage. All sql queries will be compiled into this simple language before execution.
This language is similar to .net IL
code and other intermedate languages.
Short example: SELECT Ident, Name, ValueInCent * 100 as Dollar FROM Products WHERE Name = 'Ski'
// SQL program
@siql v1
ocur.tbl cur1
curout.cur1 ()
cursrc.cur1 Products
filter cur1
{
ldcol Name
ldc.str 'Ski'
eq
}
oresset res1
fresset.res1 (cur1)
{
crow
ldcol Ident
pnxc __col0
ldcol Name
pnxc __col1
ldcol ValueInCent
ldc.i4 100
mul
pnxc Dollar
}
The library is structured into a compiler
and runtime
. Both with be explained in this article, to get an overview.
The library is under heavy development, so changes are maybe not up-to-date here.
The compiler
compiles a sql query into SIQL
code and cache it. If the same query will be executed later, it has not to be compiled
again. The compiler is splitted into the following steps.
We are also creating some debugger tool. The debugger is not directly for debugging SQL-Statements, but for debugging the Compiler and runtime, because it visualize all generated results.
The tokenizer takes the input sql code and makes build tokens. E.g.: SELECT A FROM B
will be splitted into <SELECT> <A> <FROM> <B>
.
Involved files
- /Compiler/Tokenizer/Tokenizer.cs Tokenizer core
- /Compiler/Tokenizer/ParserConfiguration.cs Configuration files for token generating
The syntaxtree builder classifies all token in the Build
method and creates the Abstract-Syntax-Tree (AST)
. In this steps first Sql
sematic analysis will be done. The result is a tree build of SyntaxTreeNodes
. Also conditions and other expressions will be resolved
using the Shuntig-Yard
algorithm.
Involved files
- /Compiler/SyntaxTree/SyntaxTreeBuilder.cs Main tree builder
- /Compiler/SyntaxTree/Nodes/* All node definitions
- /Compiler/SyntaxTree/Factory/SyntaxTreeFactory.cs Token classification
- /Compiler/SyntaxTree/ShuntingYard.cs Shunting Yard algorithm
Not yet implemented
Takes the optimized and generated AST
and compiles it to SIQL
code.
- /Compiler/ILCompiler/SIQLCompiler.cs Main compiler
The runtime
is responsible to execute SIQL
code and call the IQueryExecutor
interface/implementation.
The runtime is implemeted in SQLRuntime.cs
and contains the execution ligic which triggers the IQueryExecutor
. The required components
for the runtime are all placed in the Runtime/
directory and consists of:
- CommandChain - SIQL as .net commands
- Scope - Contains the scope-system, for holding cursor, resultsets, arguments and other stuff
- Stack - Used for operation and resultset generating
- CLRInterface - Contains the communication interface to invoke methods when sql functions or statements were executed.