コード例 #1
0
ファイル: Token.cs プロジェクト: dusdong/BaseComponent
 public static Token Create(Column column)
 {
     return new Token()
     {
         Column = column,
         Type = TokenType.Column
     };
 }
コード例 #2
0
 void ParseEntityType(Type type)
 {
     var tableSechma = TableInfoManager.GetTable(type);
     var tableInfo = new Table()
     {
         DataBase = tableSechma.DataBase,
         Name = tableSechma.Name,
         Type = tableSechma.Type
     };
     var columns = tableSechma.Columns;
     foreach (var columnValue in columns.Values)
     {
         var column = new Column();
         column.Name = columnValue.Name;
         column.DataType = columnValue.PropertyInfo.PropertyType;
         column.MemberInfo = columnValue.PropertyInfo;
         column.Table = tableInfo;
         Columns.Add(column);
         Context.Columns.Add(columnValue.Name, column);
     }
 }
コード例 #3
0
 protected string FormatColumn(Column column, bool genColumnAlias = true)
 {
     var tblAlias = GetTableAlias(column.Name);
     string col = string.Format("[{0}].[{1}]", tblAlias, column.Name);
     var converter = ParserConverter(column);
     if (!string.IsNullOrWhiteSpace(converter))
     {
         col = string.Format(converter, string.Format("[{0}].[{1}]", tblAlias, column.Name));
     }
     if (!genColumnAlias)
     {
         return col;
     }
     return string.Format("{0} [{1}]", col, column.Alias ?? column.MemberInfo.Name);
 }
コード例 #4
0
 public abstract string ParserConverter(Column column);
コード例 #5
0
 protected string BuildSql(Column column)
 {
     var col = string.Empty;
     //if (string.IsNullOrWhiteSpace(column.Table.DataBase))
     //{
     col = string.Format("[{0}].[{1}]", GetTableAlias(column.Name), column.Name);
     //}
     //else
     //{
     //    col = string.Format("[{0}].dbo.[{1}].[{2}]", column.Table.DataBase, GetTableAlias(column.Name), column.Name);
     //}
     var convert = string.Empty;
     if (column.Converters.Any())
     {
         convert = ParserConverter(column);
         col = string.Format(convert, col);
     }
     return col;
 }
コード例 #6
0
ファイル: SqlBuilder.cs プロジェクト: dusdong/BaseComponent
        public override string ParserConverter(Column column)
        {
            var converter = string.Empty;
            if (column.Converters.Any())
            {
                converter = "{0}";
            }
            while (column.Converters.Count > 0)
            {
                var columnConverter = column.Converters.Pop();
                var memberInfo = columnConverter.MemberInfo;
                var args = columnConverter.Parameters;
                var paramName = "@" + ParserUtils.GenerateAlias("param");
                switch (memberInfo.MemberType)
                {
                    case MemberTypes.Property:
                        if (TypeHelper.IsNullableType(memberInfo.DeclaringType) && memberInfo.Name == "Value")
                        {
                            continue;
                        }
                        if (memberInfo.DeclaringType == ReflectorConsts.DateTimeNullableType || memberInfo.DeclaringType == ReflectorConsts.DateTimeType)
                        {
                            switch (memberInfo.Name)
                            {
                                case "Date":
                                    converter = string.Format(converter, "CONVERT(DATE,{0},211)");
                                    break;
                                case "Value":

                                    break;
                                default:
                                    throw new Exception("不支持");
                            }
                            continue;
                        }
                        else if (memberInfo.DeclaringType == ReflectorConsts.TimeSpanType)
                        {
                            var unit = string.Empty;
                            switch (memberInfo.Name)
                            {
                                case "TotalDays":
                                    unit = "DAY";
                                    break;
                                case "TotalHours":
                                    unit = "HOUR";
                                    break;
                                case "TotalMilliseconds":
                                    unit = "MILLISECOND";
                                    break;
                                case "TotalMinutes":
                                    unit = "MINUTE";
                                    break;
                                case "TotalSeconds":
                                    unit = "SECOND";
                                    break;
                                default:
                                    throw new Exception("不支持");
                            }
                            converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "DATEDIFF(" + unit + ",{1},{0})", paramName);
                            _result.Parameters.Add(paramName, args[0]);
                            continue;
                        }
                        throw new Exception("不支持");
                    case MemberTypes.Method:
                        if (memberInfo.DeclaringType == ReflectorConsts.StringType)
                        {
                            switch (memberInfo.Name)
                            {
                                case "Contains":
                                    converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "CHARINDEX({0},{1})>0", paramName);
                                    _result.Parameters.Add(paramName, args[0]);
                                    break;
                                case "StartsWith":
                                    converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "CHARINDEX({0},{1})=1", paramName);
                                    _result.Parameters.Add(paramName, args[0]);
                                    break;
                                case "Substring":
                                    if (columnConverter.Parameters.Count == 1)
                                    {
                                        if (columnConverter.IsInstanceColumn)
                                        {
                                            converter = string.Format(converter, "SUBSTRING({0}," + (Convert.ToInt32(columnConverter.Parameters[0]) + 1) + ",LEN({0})+1-" + columnConverter.Parameters[0] + ")");
                                        }
                                        else
                                        {
                                            throw new Exception("不支持");
                                        }
                                    }
                                    else if (columnConverter.Parameters.Count == 2)
                                    {
                                        if (columnConverter.IsInstanceColumn)
                                        {
                                            converter = string.Format(converter, "SUBSTRING({0}," + (Convert.ToInt32(columnConverter.Parameters[0]) + 1) + "," + columnConverter.Parameters[1] + ")");
                                        }
                                        else
                                        {
                                            throw new Exception("不支持");
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception("不支持");
                                    }
                                    break;
                                default:
                                    throw new Exception("不支持");
                            }
                            continue;
                        }
                        else if (memberInfo.DeclaringType == ReflectorConsts.DateTimeType || memberInfo.DeclaringType == ReflectorConsts.DateTimeNullableType)
                        {
                            switch (memberInfo.Name)
                            {
                                case "AddDays":
                                    converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "DATEADD(DAY,{0},{1})", paramName);
                                    _result.Parameters.Add(paramName, args[0]);
                                    break;
                                case "AddHours":
                                    converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "DATEADD(HOUR,{0},{1})", paramName);
                                    _result.Parameters.Add(paramName, args[0]);
                                    break;
                                case "AddYears":
                                    converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "DATEADD(YEAR,{0},{1})", paramName);
                                    _result.Parameters.Add(paramName, args[0]);
                                    break;
                                case "AddMonths":
                                    converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "DATEADD(MONTH,{0},{1})", paramName);
                                    _result.Parameters.Add(paramName, args[0]);
                                    break;
                                case "AddSeconds":
                                    converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "DATEADD(SECOND,{0},{1})", paramName);
                                    _result.Parameters.Add(paramName, args[0]);
                                    break;
                                case "AddMilliseconds":
                                    converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "DATEADD(MILLISECOND,{0},{1})", paramName);
                                    _result.Parameters.Add(paramName, args[0]);
                                    break;
                                case "AddMinutes":
                                    converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "DATEADD(MINUTE,{0},{1})", paramName);
                                    _result.Parameters.Add(paramName, args[0]);
                                    break;
                                default:
                                    throw new Exception("不支持");
                            }
                            continue;
                        }
                        else if (memberInfo.DeclaringType == ReflectorConsts.EnumerableType)
                        {
                            switch (memberInfo.Name)
                            {
                                case "Contains":
                                    if (columnConverter.IsInstanceColumn)
                                    {
                                        throw new Exception("不支持");
                                    }
                                    converter = string.Format(converter, "{0} in (" + string.Join(",", (IEnumerable<int>)args[0]) + ")");
                                    break;
                                default:
                                    throw new Exception("不支持");
                            }
                            continue;
                        }
                        else
                        {
                            throw new Exception("不支持");
                        }
                    default:
                        throw new Exception();
                }
            }
            return converter;
        }
コード例 #7
0
ファイル: SqlBuilder.cs プロジェクト: dusdong/BaseComponent
 public override string ParserConverter(Column column)
 {
     var converter = string.Empty;
     if (column.Converters.Any())
     {
         converter = "{0}";
     }
     while (column.Converters.Count > 0)
     {
         var columnConverter = column.Converters.Pop();
         var memberInfo = columnConverter.MemberInfo;
         var args = columnConverter.Parameters;
         var paramName = "@" + ParserUtils.GenerateAlias("param");
         switch (memberInfo.MemberType)
         {
             case MemberTypes.Property:
                 if (TypeHelper.IsNullableType(memberInfo.DeclaringType) && memberInfo.Name == "Value")
                 {
                     continue;
                 }
                 if (memberInfo.DeclaringType == ReflectorConsts.DateTimeNullableType || memberInfo.DeclaringType == ReflectorConsts.DateTimeType)
                 {
                     converter = string.Format(converter, "DATE({0})");
                     continue;
                 }
                 else if (memberInfo.DeclaringType == ReflectorConsts.TimeSpanType)
                 {
                     var unit = 1;
                     switch (memberInfo.Name)
                     {
                         case "TotalDays":
                             unit = 1;
                             break;
                         case "TotalHours":
                             unit = 12;
                             break;
                         case "TotalMilliseconds":
                             unit = 12 * 60 * 60 * 1000;
                             break;
                         case "TotalMinutes":
                             unit = 12 * 60;
                             break;
                         case "TotalSeconds":
                             unit = 12 * 60 * 60;
                             break;
                         default:
                             throw new Exception("不支持");
                     }
                     converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "((JULIANDAY({0}) - JULIANDAY({1}))*" + unit + ")", paramName);
                     //converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "DATEDIFF(" + unit + ",{1},{0})", paramName);
                     _result.Parameters.Add(paramName, args[0]);
                     continue;
                 }
                 throw new Exception("不支持");
             case MemberTypes.Method:
                 if (memberInfo.DeclaringType == ReflectorConsts.StringType)
                 {
                     switch (memberInfo.Name)
                     {
                         case "Contains":
                             converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "CHARINDEX({0},{1})>0", paramName);
                             _result.Parameters.Add(paramName, args[0]);
                             break;
                         case "StartsWith":
                             converter = FormatConverter(columnConverter.IsInstanceColumn, converter, "CHARINDEX({0},{1})=1", paramName);
                             _result.Parameters.Add(paramName, args[0]);
                             break;
                         case "Substring":
                             if (columnConverter.Parameters.Count == 1)
                             {
                                 if (columnConverter.IsInstanceColumn)
                                 {
                                     converter = string.Format(converter, "SUBSTR({0}," + (Convert.ToInt32(columnConverter.Parameters[0]) + 1) + ")");
                                 }
                                 else
                                 {
                                     throw new Exception("不支持");
                                 }
                             }
                             else if (columnConverter.Parameters.Count == 2)
                             {
                                 if (columnConverter.IsInstanceColumn)
                                 {
                                     converter = string.Format(converter, "SUBSTR({0}," + (Convert.ToInt32(columnConverter.Parameters[0]) + 1) + "," + columnConverter.Parameters[1] + ")");
                                 }
                                 else
                                 {
                                     throw new Exception("不支持");
                                 }
                             }
                             else
                             {
                                 throw new Exception("不支持");
                             }
                             break;
                         default:
                             throw new Exception("不支持");
                     }
                     continue;
                 }
                 else if (memberInfo.DeclaringType == ReflectorConsts.DateTimeType || memberInfo.DeclaringType == ReflectorConsts.DateTimeNullableType)
                 {
                     var num = args[0].ToString();
                     switch (memberInfo.Name)
                     {
                         case "AddDays":
                             converter = FormatDateConverter(columnConverter, converter, "DAYS", num);
                             break;
                         case "AddHours":
                             converter = FormatDateConverter(columnConverter, converter, "HOURS", num);
                             break;
                         case "AddYears":
                             converter = FormatDateConverter(columnConverter, converter, "YEARS", num);
                             break;
                         case "AddMonths":
                             converter = FormatDateConverter(columnConverter, converter, "MONTHS", num);
                             break;
                         case "AddSeconds":
                             converter = FormatDateConverter(columnConverter, converter, "SECONDS", num);
                             break;
                         case "AddMilliseconds":
                             converter = FormatDateConverter(columnConverter, converter, string.Format("0.{0} SECONDS", num), string.Empty);
                             break;
                         case "AddMinutes":
                             converter = FormatDateConverter(columnConverter, converter, "MINUTES", num);
                             break;
                         default:
                             throw new Exception("不支持");
                     }
                     continue;
                 }
                 else if (memberInfo.DeclaringType == ReflectorConsts.EnumerableType)
                 {
                     switch (memberInfo.Name)
                     {
                         case "Contains":
                             if (columnConverter.IsInstanceColumn)
                             {
                                 throw new Exception("不支持");
                             }
                             converter = string.Format(converter, "{0} in (" + string.Join(",", (IEnumerable<int>)args[0]) + ")");
                             break;
                         default:
                             throw new Exception("不支持");
                     }
                     continue;
                 }
                 else
                 {
                     throw new Exception("不支持");
                 }
             default:
                 throw new Exception();
         }
     }
     return converter;
 }
コード例 #8
0
 protected override Expression VisitParameter(ParameterExpression node)
 {
     var column = new Column();
     if (TableInfoManager.IsEntity(node.Type))
     {
         //弹出第一个参数,一般是列
         var table = GetTable(node.Type);
         var _memberInfo = ((MemberExpression)_memberInfos.Pop()).Member;
         column.DataType = ((PropertyInfo)_memberInfo).PropertyType;
         column.Name = table.Columns.Get(_memberInfo.Name).Name;
         column.MemberInfo = _memberInfo;
         var tableAlias = node.Name;
         if (_joins != null)
         {
             if (_joins.ContainsKey(tableAlias))
             {
                 tableAlias = _joins[tableAlias].Right.Table.Alias;
             }
         }
         else
         {
             tableAlias = table.Name;
         }
         column.Table = CreateTable(tableAlias, table.DataBase, table.Name, table.Type);
         while (_memberInfos.Count > 0)
         {
             var exp = _memberInfos.Pop();
             switch (exp.NodeType)
             {
                 case ExpressionType.MemberAccess:
                     var memberInfo = ((MemberExpression)exp).Member;
                     column.Converters.Push(new ColumnConverter(memberInfo, new List<object>()));
                     break;
                 default:
                     throw new Exception();
             }
         }
         //column.Converter = GetConverter(null);
     }
     else
     {
         SchemaModel.Table tableInfo = GetTable();
         MemberInfo columnMember = null;
         var tableAlias = string.Empty;
         Table table = null;
         if (tableInfo != null)
         {
             tableAlias = _tableMember.Name;
             columnMember = ((MemberExpression)_memberInfos.Pop()).Member;
             if (_joins != null)
             {
                 if (_joins.ContainsKey(tableAlias))
                 {
                     tableAlias = _joins[tableAlias].Right.Table.Alias;
                 }
             }
             else
             {
                 tableAlias = tableInfo.Name;
             }
             table = CreateTable(tableAlias, tableInfo.DataBase, tableInfo.Name, tableInfo.Type);
         }
         else
         {
             columnMember = ((MemberExpression)_memberInfos.Pop()).Member;
             table = _columns.Get(columnMember.Name).Table;
             tableAlias = table.Alias;
             tableInfo = GetTable(table.Type);
         }
         //if (_memberInfos.Count > 1)
         //{
         //    tableInfo = GetTable();
         //}
         //else
         //{
         //}
         var columnType = ((PropertyInfo)columnMember).PropertyType;
         var columnName = string.Empty;
         var columnSechma = tableInfo.Columns.Get(columnMember.Name);
         if (columnSechma != null)
         {
             columnName = columnSechma.Name;
         }
         else
         {
             columnName = Context.Columns.Get(columnMember.Name).Name;
         }
         column = new Column()
         {
             DataType = columnType,
             Name = columnName,
             Table = table,
             MemberInfo = columnMember
         };
         while (_memberInfos.Count > 0)
         {
             var exp = _memberInfos.Pop();
             switch (exp.NodeType)
             {
                 case ExpressionType.MemberAccess:
                     var memberInfo = ((MemberExpression)exp).Member;
                     column.Converters.Push(new ColumnConverter(memberInfo, new List<object>()));
                     break;
                 default:
                     throw new Exception();
             }
         }
         //column.Converter = GetConverter(null);
     }
     Token = Token.Create(column);
     return node;
 }
コード例 #9
0
 protected override Expression VisitParameter(ParameterExpression node)
 {
     //MemberExpressionType = MemberExpressionType.Column;
     if (TableInfoManager.IsEntity(node.Type))
     {
         //弹出第一个参数,一般是列
         var table = GetTable(node.Type);
         var _memberInfo = _memberInfoStack.Pop();
         SelectedColumn.DataType = ((PropertyInfo)_memberInfo).PropertyType;
         SelectedColumn.Name = table.Columns.Get(_memberInfo.Name).Name;
         SelectedColumn.MemberInfo = _memberInfo;
         var tableAlias = node.Name;
         if (_Joins != null)
         {
             if (_Joins.ContainsKey(tableAlias))
             {
                 tableAlias = _Joins[tableAlias].Right.Table.Alias;
             }
         }
         else
         {
             tableAlias = table.Name;
         }
         SelectedColumn.Table = CreateTable(tableAlias, table.DataBase, table.Name, table.Type);
         while (_memberInfoStack.Count > 0)
         {
             var propertyInfo = (MemberInfo)_memberInfoStack.Pop();
             SelectedColumn.Converters.Push(new ColumnConverter(propertyInfo, new List<object>()));
         }
         //SelectedColumn.Converter = GetConverter();
     }
     else
     {
         var tableInfo = GetTable();
         var columnMember = _memberInfoStack.Pop();
         var columnType = ((PropertyInfo)columnMember).PropertyType;
         var tableAlias = _tableMember.Name;
         if (_Joins != null)
         {
             if (_Joins.ContainsKey(tableAlias))
             {
                 tableAlias = _Joins[tableAlias].Right.Table.Alias;
             }
         }
         else
         {
             tableAlias = tableInfo.Name;
         }
         var table = CreateTable(tableAlias, tableInfo.DataBase, tableInfo.Name, tableInfo.Type);
         SelectedColumn = new Column()
         {
             DataType = columnType,
             Name = tableInfo.Columns.Get(columnMember.Name).Name,
             Table = table,
             MemberInfo = columnMember
         };
         while (_memberInfoStack.Count > 0)
         {
             var exp = _memberInfoStack.Pop();
             SelectedColumn.Converters.Push(new ColumnConverter(exp, new List<object>()));
         }
         //SelectedColumn.Converter = GetConverter();
     }
     return node;
 }
コード例 #10
0
 public MemberExpressionVisitor(TranslateContext context)
     : base(context)
 {
     _Joins = context.Joins;
     SelectedColumn = new Column();
 }