ecifying the attribute provider.
ArgIndices - an array that allows you to restrict or specify the order of the arguments concerning the method of SQL functions. For example, this is useful when the same function for different database not only has different names, but the order of the arguments.
ServerSideOnly - flag prohibiting perform the function on the client. Always leads to the generation of SQL.
The last parameter requires special explanation. The fact that BLToolkit attempts to minimize the load on the database server, and if possible, to shift the execution of certain functions on the client. But this behavior is not always useful. If SQL function flagged ServerSideOnly, the function anyway translated into SQL.
As BLToolkit contains a function which can be used to force converted to SQL expressions those which did not originally anticipated. Consider the example of everything with the same function of calculating the length of the line:
from c in db.Customer
select Sql.Length(c.ContactName); |
SQL:
SELECT
[c].[ContactName]
FROM
[Customers] [c] |
In this case, the string is passed to the client, which is already calculated its length.
In the following example, we use the Sql.AsSql, to prevent this behavior:
from c in db.Customer
select Sql.AsSql(Sql.Length(c.ContactName)); |
SQL:
SELECT
Len([c].[ContactName]) as [c1]
FROM
[Customers] [c] |
You probably guessed it should look like a function:
[SqlExpression("{0}", 0, ServerSideOnly=true)]
public static T AsSql
(T obj)
{
return obj;
}
|
Notable here is that in this case we do not use SqlFunction attribute, and attribute similar SqlExpression. It is very similar SqlFunction, but it takes a format string as a parameter by which builds the SQL statement. The arguments are a format string method parameters, and if we are dealing with a generalized method or class, and the generalized parameters. Thus function Sql.AsSql will be passed two parameters: objand T. But since the parameter T us in this case is not interested, the second parameter attribute SqlExpression in our example just says BLToolkit, which used to be only one parameter at index 0.
It should also mention one more attribute - SqlProperty. In fact, it's just a function with no parameters. This property Sql.CurrentTimestamp we've met before. That is its implementation:
[SqlProperty("CURRENT_TIMESTAMP", ServerSideOnly = true)]
[SqlProperty("Informix", "CURRENT", ServerSideOnly = true)]
[SqlProperty("Access", "Now", ServerSideOnly = true)]
[SqlFunction("SqlCe", "GetDate", ServerSideOnly = true)]
[SqlFunction("Sybase", "GetDate", ServerSideOnly = true)]
public static DateTime CurrentTimestamp
{
get { throw new LinqException("The 'CurrentTimestamp' is server side only property."); }
} |
Attributes SqlFunction, SqlExpression and SqlProperty can be applied simultaneously to the same method to different providers. Here, for example, looks like a method TrimLeft:
[SqlExpression("DB2", "Strip({0}, L, {1})")]
[SqlFunction ( "LTrim")]
public static string TrimLeft(string str, char? ch)
{
return str == null || ch == null ? null : str.TrimStart(ch.Value);
} |
Using these attributes, you can easily expand your set of SQL functions and attribute SqlExpression allows you to create almost any SQL statements that you may need.
Substitution method
SQL functions are very important and useful primitives, but unfortunately