Share your own simple query expression simulation (ESQL, Linq) (1)

Source: Internet
Author: User
Tags net command

It is very difficult to fully simulate expressions, because C # currently does not support custom operators and only supports Operator Overloading (C # is expected to provide operator customization. If a language supports this, please be advised) The idea is actually very simple. It is to set up your own type and reload some operators. The purpose of simulating this is to avoid writing Database SQL statements directly under the entity model, using ESQL, to achieve this, the Linq syntax provides the development of a consistent entity application model:
1) parameter class, used to pass the actual parameters and convert them to the required parameters in the ADO. Net command:
[Csharp]
Public class Parameter
{// No specific implementation. You only need to simulate
}
2) expression interface:
[Csharp]
Public interface IDbExpression
{
String Expression {get ;}
Dictionary <string, Parameter> Parameters {get ;}
}
3) expression base class: mainly involves some operator symbol overloading, and some areas where expressions cannot be used to define similar functions
[Csharp]
Public class DbExpression: IDbExpression
{
Private StringBuilder _ sqlStatement;
Private Dictionary <string, Parameter> _ Parameters;
Public StringBuilder SQL {get {return _ sqlStatement ;}}
Public DbExpression ()
{
_ SqlStatement = new StringBuilder ();
_ Parameters = new Dictionary <string, Parameter> ();
}
Public static DbExpression operator> (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + ">" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator <(DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "<" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator> = (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "> =" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator <= (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "<=" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator = (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "=" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator! = (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "! = "+ E2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator & (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "and" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator | (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "or" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public DbExpression Parentheses (DbExpression e)
{
This. SQL. AppendLine ("(" + e. Expression + ")");
This. AddParams (e );
Return this;
}
Public DbExpression Where (DbExpression e1)
{
This. _ sqlStatement. AppendLine ("WHERE" + e1.Expression );
This. AddParams (e1 );
Return this;
}
Public DbExpression From (params DbExpression [] Tables)
{
This. SQL. AppendLine ("FROM ");
If (Tables! = Null)
{
Int I = 0;
Foreach (var item in Tables)
{
If (I = 0)
{
_ SqlStatement. Append ("" + item. Expression );
}
Else
{
_ SqlStatement. Append ("," + item. Expression );
}
This. AddParams (item );
}
}
Return this;
}
Public DbExpression Select (params DbExpression [] Fields)
{
_ SqlStatement. Append ("SELECT ");

If (Fields! = Null)
{
Int I = 0;
Foreach (var item in Fields)
{
If (I = 0)
{
_ SqlStatement. Append ("" + item. Expression );
}
Else
{
_ SqlStatement. Append ("," + item. Expression );
}
This. AddParams (item );
}
}
Else
{
_ SqlStatement. Append ("*");
}
Return this;
}
Public void AddParams (DbExpression e)
{
If (e. Parameters = null)
{
Return;
}
Foreach (var p in e. _ Parameters)
{
If (this. _ Parameters. ContainsKey (p. Key) = false)
{
This. _ Parameters. Add (p. Key, p. Value );
}
}
}
Public void AddParams (IDbExpression e)
{
If (e. Parameters = null)
{
Return;
}
Foreach (var p in e. Parameters)
{
If (this. _ Parameters. ContainsKey (p. Key) = false)
{
This. _ Parameters. Add (p. Key, p. Value );
}
}
}
Public virtual string Expression
{
Get {return _ sqlStatement. ToString ();}
}
 
Public virtual Dictionary <string, Parameter> Parameters
{
Get {return _ Parameters ;}
}
}
 
From hawksoft

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.