Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

重构 Query 架构

  • Loading branch information...
commit ba29d67d701c5f67756091b8b1749ac8f4cc6130 1 parent 9ab15c7
@Ivony authored
View
25 Sources/Ivony.Data/ParseContext.cs
@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Ivony.Data.Queries;
+
+namespace Ivony.Data
+{
+ /// <summary>
+ /// 模板解析上下文
+ /// </summary>
+ public abstract class ParseContext
+ {
+
+ /// <summary>
+ /// 创建参数 SQL 表达式
+ /// </summary>
+ /// <param name="parameter">参数表达式</param>
+ /// <returns>该参数在 SQL 语句中引用的形式</returns>
+ public abstract string CreateParameterExpression( ParameterExpression parameter );
+
+
+ public abstract string GetNameExpression( params string[] names );
+ }
+}
View
14 Sources/Ivony.Data/Queries/BinaryExpression.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ public class BinaryExpression : IConditionExpression
+ {
+ public BinaryExpression( string @operator, IScalarExpression left, IScalarExpression right )
+ {
+ }
+ }
+}
View
94 Sources/Ivony.Data/Queries/BinaryOperators.cs
@@ -0,0 +1,94 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ public static class BinaryOperators
+ {
+
+ public static IConditionExpression Equal( this IScalarExpression left, IScalarExpression right )
+ {
+ return new BinaryExpression( "==", left, right );
+ }
+
+ public static IConditionExpression Equal( this IScalarExpression left, object right )
+ {
+ return new BinaryExpression( "==", left, new ParameterExpression( right ) );
+ }
+
+
+
+ public static IConditionExpression NotEqual( this IScalarExpression left, IScalarExpression right )
+ {
+ return new BinaryExpression( "!=", left, right );
+ }
+
+ public static IConditionExpression NotEquals( this IScalarExpression left, object right )
+ {
+ return new BinaryExpression( "!=", left, new ParameterExpression( right ) );
+ }
+
+
+
+ public static IConditionExpression GreaterThan( this IScalarExpression left, IScalarExpression right )
+ {
+ return new BinaryExpression( ">", left, right );
+ }
+
+ public static IConditionExpression GreaterThan( this IScalarExpression left, object right )
+ {
+ return new BinaryExpression( ">", left, new ParameterExpression( right ) );
+ }
+
+
+
+ public static IConditionExpression LessThan( this IScalarExpression left, IScalarExpression right )
+ {
+ return new BinaryExpression( "<", left, right );
+ }
+
+ public static IConditionExpression LessThan( this IScalarExpression left, object right )
+ {
+ return new BinaryExpression( "<", left, new ParameterExpression( right ) );
+ }
+
+
+
+ public static IConditionExpression GreaterThanOrEqual( this IScalarExpression left, IScalarExpression right )
+ {
+ return new BinaryExpression( ">=", left, right );
+ }
+
+ public static IConditionExpression GreaterThanOrEqual( this IScalarExpression left, object right )
+ {
+ return new BinaryExpression( ">=", left, new ParameterExpression( right ) );
+ }
+
+
+
+ public static IConditionExpression LessThanOrEqual( this IScalarExpression left, IScalarExpression right )
+ {
+ return new BinaryExpression( "<=", left, right );
+ }
+
+ public static IConditionExpression LessThanOrEqual( this IScalarExpression left, object right )
+ {
+ return new BinaryExpression( "<=", left, new ParameterExpression( right ) );
+ }
+
+
+
+ public static IConditionExpression And( this IConditionExpression left, IConditionExpression right )
+ {
+ return new LogicalExpression( "AND", left, right );
+ }
+
+ public static IConditionExpression Or( this IConditionExpression left, IConditionExpression right )
+ {
+ return new LogicalExpression( "OR", left, right );
+ }
+
+ }
+}
View
11 Sources/Ivony.Data/Queries/Expression.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ public static class Expression
+ {
+ }
+}
View
35 Sources/Ivony.Data/Queries/FieldExpression.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+
+ /// <summary>
+ /// 表示一个字段引用表达式
+ /// </summary>
+ public class FieldExpression : IScalarExpression
+ {
+
+ /// <summary>
+ /// 创建一个字段表达式
+ /// </summary>
+ /// <param name="fieldAlias">字段别名</param>
+ public FieldExpression( string fieldAlias ) : this( fieldAlias, null ) { }
+
+ /// <summary>
+ /// 创建一个字段表达式
+ /// </summary>
+ /// <param name="fieldName">字段名</param>
+ /// <param name="tableAlias">表别名</param>
+ public FieldExpression( string fieldName, string tableAlias )
+ {
+ FieldName = fieldName;
+ TableName = tableAlias;
+ }
+
+ public string FieldName { get; private set; }
+ public string TableName { get; private set; }
+ }
+}
View
23 Sources/Ivony.Data/Queries/FilterExpression.cs
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ public class FilterExpression : ITemplatePartialExpression
+ {
+
+ public FilterExpression()
+ {
+
+ }
+
+
+
+ public string Parse( ParseContext context )
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
View
17 Sources/Ivony.Data/Queries/IConditionExpression.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ /// <summary>
+ /// 定义一个条件表达式
+ /// </summary>
+ public interface IConditionExpression : IDbExpression
+ {
+
+ }
+
+
+}
View
11 Sources/Ivony.Data/Queries/IDbQuery.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ public interface IDbQuery
+ {
+ }
+}
View
17 Sources/Ivony.Data/Queries/IScalarExpression.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ /// <summary>
+ /// 表示一个标量值的表达式
+ /// </summary>
+ public interface IScalarExpression : IDbExpression
+ {
+
+
+
+ }
+}
View
14 Sources/Ivony.Data/Queries/ISetExpression.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ /// <summary>
+ /// 集合表达式
+ /// </summary>
+ public class ISetExpression
+ {
+ }
+}
View
11 Sources/Ivony.Data/Queries/JoinExpression.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ public class JoinExpression
+ {
+ }
+}
View
14 Sources/Ivony.Data/Queries/LogicalExpression.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ class LogicalExpression : IConditionExpression
+ {
+ public LogicalExpression( string @operator, IConditionExpression left, IConditionExpression right )
+ {
+ }
+ }
+}
View
11 Sources/Ivony.Data/Queries/Query.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ public static class Query
+ {
+ }
+}
View
31 Sources/Ivony.Data/Queries/QueryBuilder.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ public class QueryBuilder
+ {
+
+ public static FieldExpression Field( string fieldName )
+ {
+ return new FieldExpression( fieldName );
+ }
+
+ public static FieldExpression Field( string fieldName, string tableAlias )
+ {
+ return new FieldExpression( fieldName, tableAlias );
+ }
+
+ public static TableExpression Table( string tableName )
+ {
+ return new TableExpression( tableName );
+ }
+ public static TableExpression Table( string tableName, string alias )
+ {
+ return new TableExpression( tableName, alias );
+ }
+
+ }
+}
View
17 Sources/Ivony.Data/Queries/SetOperators.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ public static class SetOperators
+ {
+
+ public static ISetExpression InnerJoin( this ISetExpression left, ISetExpression right, IConditionExpression condition )
+ {
+ return new JoinExpression( "INNER", left, right, condition );
+ }
+
+ }
+}
View
51 Sources/Ivony.Data/Queries/StoredProcedureExpression.cs
@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ /// <summary>
+ /// 存储过程表达式
+ /// </summary>
+ public class StoredProcedureExpression : IDbExpression
+ {
+
+ private string _name;
+ private IDictionary<string, object> _parameters;
+
+ /// <summary>
+ /// 创建 StoredProcedureExpression 对象
+ /// </summary>
+ /// <param name="name">存储过程名称</param>
+ public StoredProcedureExpression( string name ) : this( name, new Dictionary<string,object>() ) { }
+
+ /// <summary>
+ /// 创建 StoredProcedureExpression 对象
+ /// </summary>
+ /// <param name="name">存储过程名称</param>
+ /// <param name="parameters">存储过程参数列表</param>
+ public StoredProcedureExpression( string name, IDictionary<string, object> parameters )
+ {
+ _name = name;
+ _parameters = parameters;
+ }
+
+
+ /// <summary>
+ /// 存储过程名称
+ /// </summary>
+ public string Name
+ {
+ get { return _name; }
+ }
+
+ /// <summary>
+ /// 存储过程参数列表
+ /// </summary>
+ public IDictionary<string, object> Parameters
+ {
+ get { return _parameters; }
+ }
+ }
+}
View
14 Sources/Ivony.Data/Queries/TableExpression.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ public class TableExpression : ISetExpression
+ {
+ public TableExpression( string tableName ) : this( tableName, null ) { }
+
+ public TableExpression( string tableName, string alias ) { }
+ }
+}
View
58 Sources/Ivony.Data/Queries/TableQuery.cs
@@ -0,0 +1,58 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data.Queries
+{
+ /// <summary>
+ /// 数据表查询
+ /// </summary>
+ public class TableQuery : IDbExpression
+ {
+
+ private string _tableName;
+
+ public TableQuery( string tableName )
+ {
+ _tableName = tableName;
+ }
+
+ private string[] _fields = null;
+
+ public TableQuery Fields( params string[] fields )
+ {
+ _fields = fields;
+ return this;
+ }
+
+ private TemplateQuery _where;
+
+ public TableQuery Where( string whereClause, params object[] parameters )
+ {
+ _where = Template( whereClause );
+ return this;
+ }
+
+
+
+ private static TemplateQuery Template( string template, params object[] parameters )
+ {
+ return new TemplateQuery( template, parameters );
+ }
+ }
+
+ public class SelectClause
+ {
+ public SelectClause AddField( string fieldExpression, string fieldName = null )
+ {
+ throw new NotImplementedException();
+ }
+
+
+ }
+
+
+
+
+}
View
159 Sources/Ivony.Data/Queries/TemplateQuery.cs
@@ -0,0 +1,159 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace Ivony.Data.Queries
+{
+
+
+
+ /// <summary>
+ /// 定义可以作为模板的一部分被嵌入模板的表达式
+ /// </summary>
+ public interface ITemplatePartialExpression : IDbExpression
+ {
+ /// <summary>
+ /// 解析模版并提供嵌入的 SQL 表达式
+ /// </summary>
+ /// <param name="context">模版解析上下文</param>
+ /// <returns></returns>
+ string Parse( ParseContext context );
+
+ }
+
+ /// <summary>
+ /// 模板表达式
+ /// </summary>
+ public class TemplateQuery : ITemplatePartialExpression, IDbQuery
+ {
+ /// <summary>
+ /// 创建 TemplateQuery 对象
+ /// </summary>
+ /// <param name="template">模版</param>
+ /// <param name="parameters">参数列表</param>
+ public TemplateQuery( string template, params object[] parameters )
+ {
+
+ Template = template.Replace( "{...}", ParseParameterListSymbol( parameters.Length ) );
+
+ Parameters = parameters.Select( item =>
+ {
+ var partial = item as ITemplatePartialExpression;
+ if ( partial == null )
+ partial = new ParameterExpression( item );
+ return partial;
+ } ).ToArray();
+ }
+
+ /// <summary>
+ /// 模版
+ /// </summary>
+ public string Template
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ /// 参数列表
+ /// </summary>
+ public ITemplatePartialExpression[] Parameters
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ /// 解析模版表达式
+ /// </summary>
+ /// <param name="context">模版解析上下文</param>
+ /// <returns>需要嵌入在模版中的形式</returns>
+ public string Parse( ParseContext context )
+ {
+ return FormatRegexNum.Replace( Template, delegate( Match match )
+ {
+ string format = match.Groups["format"].ToString();
+ int index = int.Parse( match.Groups["index"].ToString() );
+
+ if ( index >= Parameters.Length )
+ throw new IndexOutOfRangeException();
+
+ ITemplatePartialExpression partial = Parameters[index];
+
+ return partial.Parse( context );
+ }
+ );
+ }
+
+
+ /// <summary>
+ /// 处理参数列表表达式“{...}”
+ /// </summary>
+ /// <param name="amount">参数个数</param>
+ /// <returns></returns>
+ private static string ParseParameterListSymbol( int amount )
+ {
+ StringBuilder builder = new StringBuilder();
+
+ bool begin = true;
+ for ( int i = 0; i < amount; i++ )
+ {
+ if ( !begin )
+ builder.Append( " , " );
+ builder.Append( "{" + i + "}" );
+ begin = false;
+ }
+
+ return builder.ToString();
+ }
+
+
+ internal static readonly Regex FormatRegexNum = new Regex( @"\{(?<index>[0-9]*)(,(?<alignment>[0-9]+[a-zA-Z]*))?(\:(?<format>[^{}]*))?\}", RegexOptions.Compiled );
+ }
+
+ /// <summary>
+ /// 参数表达式
+ /// </summary>
+ public class ParameterExpression : IScalarExpression, ITemplatePartialExpression
+ {
+ /// <summary>
+ /// 创建 ParameterExpression 实例
+ /// </summary>
+ /// <param name="value">参数值</param>
+ public ParameterExpression( object value )
+ {
+ Value = value;
+ }
+
+ /// <summary>
+ /// 参数值
+ /// </summary>
+ public object Value
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ /// 数据类型,可选
+ /// </summary>
+ public DbType? DbType
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ /// 解析成参数表达式
+ /// </summary>
+ /// <param name="context">模版解析上下文</param>
+ /// <returns>需要嵌入在模版中的形式</returns>
+ public string Parse( ParseContext context )
+ {
+ return context.CreateParameterExpression( this );
+ }
+ }
+}
View
2  Sources/Ivony.Data/SqlServerExpressionParser.cs
@@ -88,7 +88,7 @@ private SqlCommand CreateCommand()
}
- private class SqlTemplateParseContext : TemplateParseContext
+ private class SqlTemplateParseContext : ParseContext
{
private List<SqlParameter> list = new List<SqlParameter>();
Please sign in to comment.
Something went wrong with that request. Please try again.