Introduction
Signum Engine is all about generating Sql commands. Usually these commands are big strings which are expensive to concatenate. StringBuilder, on the other hand, is more efficient but tends to generate code in a very imperative way.
In order to have a good balance between usability (functional style) and performance, SqlPreCommand was created.
How it works
SqlPreCommand is an abstract class, there are two implementations: SqlPreCommandSimple y SqlPreCommandConcat.

In memory, SqlPreCommands is a tree with SqlPreCommandSimple on the leaves, and SqlPreCommandConcat mixing them. At the very end, when the command is going to be executed, the tree gets flattened once in a efficient way.
SqlPreCommandSimple ¶
Contains a piece of sql text, consistent and executable by itself. Also, contains a list with all the related SqlParameter objects to execute it (to avoid
sql injection attack)
It has a pair of constructors like this:
public SqlPreCommandSimple(string sql)
public SqlPreCommandSimple(string sql, List<SqlParameter> parameters)
Internal class SqlBuilder constructs almost any SqlPreCommandSimple that is used by Signum Engine.
SqlPreCommandConcat ¶
Contains an array with all the SqlPreCommands (Simple or Concat), as well as a value of the Spacing enum to know how many blank lines will be rendered in the middle.
It has no public constructor, instead the static method Combine in SqlPreCommand should be used instead. It's defined like this:
public static SqlPreCommand Combine(Spacing spacing, params SqlPreCommand[] sentences)
{
if (sentences.Contains(null))
sentences = sentences.NotNull().ToArray();
if (sentences.Length == 0)
return null;
if (sentences.Length == 1)
return sentences[0];
return new SqlPreCommandConcat(spacing, sentences);
}
There's also another overloading over any IEnumerable<SqlPrecommand> using extension methods:
public static class SqlPreCommandExtensions
{
public static SqlPreCommand Combine(this IEnumerable<SqlPreCommand> preCommands, Spacing spacing)
{
return SqlPreCommand.Combine(spacing, preCommands.ToArray());
}
(...)
}
SqlPreCommand ¶
Finally, there are three interesting methods available on any SqlPreCommand:
- ToSimple: Turns a SqlPreCommand into a SqlPreCommandSimple. In case of being SqlPreCommandConcat, concatenates that Sql and the parameters. SqlPreCommandSimple just returns itself. Use ToSimple, not casting.
- PlainSql: Returns a string with all the SqlParameter objects embedder on it. Good for debugging or generating scripts, but not for production code cos it's sensitive to SQL Injection attacks.
- Split: Returns a IEnumerable<SqlPreCommand> making each piece smaller or equal than a provided number of parameters.
Executor is able to send SqlPreCommand objects to the current connection.
Administrator class returns SqlPreCommand trees for any administrative task you want to do on your schema.