public void BasicTest_ORDERBY()
        {
            SelectStmtInfo stmtInfo = new SelectStmtInfo();

            stmtInfo.Columns.Add(new Column()
            {
                Expression = new ColumnExpression(ExpressionType.ColumnName)
                {
                    ColumnName = "userId"
                }
            });
            stmtInfo.Columns.Add(new Column()
            {
                Expression = new ColumnExpression(ExpressionType.ColumnName)
                {
                    ColumnName = "userName"
                }
            });

            stmtInfo.TableDescriptor = new TableDescriptor()
            {
                TableName = "me", TableReadType = TableReadType.NONE
            };

            stmtInfo.OrderBy.Add(new OrderByCondition()
            {
                Expression = "userId", Direction = OrderByDirection.ASC
            });

            IGenerator g = new DefaultSqlGenerator();

            var sql = g.Generate(stmtInfo);

            Assert.AreEqual("SELECT userId, userName FROM me ORDER BY userId ASC", sql);
        }
        public void BasicTest_Alias()
        {
            SelectStmtInfo stmtInfo = new SelectStmtInfo();

            stmtInfo.Columns.Add(new Column()
            {
                Expression = new ColumnExpression(ExpressionType.ColumnName)
                {
                    ColumnName = "userId"
                }, HasAlias = true, Alias = "UID"
            });
            stmtInfo.Columns.Add(new Column()
            {
                Expression = new ColumnExpression(ExpressionType.ColumnName)
                {
                    ColumnName = "userName"
                }
            });

            stmtInfo.TableDescriptor = new TableDescriptor()
            {
                TableName = "me", TableReadType = TableReadType.NONE
            };

            IGenerator g = new DefaultSqlGenerator();

            var sql = g.Generate(stmtInfo);

            Assert.AreEqual("SELECT userId AS UID, userName FROM me", sql);
        }
        public void BasicTest_PREDICT_NO_WHERE_NO_ORDERBY()
        {
            SelectStmtInfo stmtInfo = new SelectStmtInfo();

            stmtInfo.Column_PredictExists = true;
            stmtInfo.Column_PredictWord   = "DISTINCT";

            stmtInfo.Columns.Add(new Column()
            {
                Expression = new ColumnExpression(ExpressionType.ColumnName)
                {
                    ColumnName = "userId"
                }
            });
            stmtInfo.Columns.Add(new Column()
            {
                Expression = new ColumnExpression(ExpressionType.ColumnName)
                {
                    ColumnName = "userName"
                }
            });

            stmtInfo.TableDescriptor = new TableDescriptor()
            {
                TableName = "me", TableReadType = TableReadType.NONE
            };

            IGenerator g = new DefaultSqlGenerator();

            var sql = g.Generate(stmtInfo);

            Assert.AreEqual("SELECT DISTINCT userId, userName FROM me", sql);
        }
Exemplo n.º 4
0
        private static void SetDefaultSqlGenerator()
        {
            var  currentLoadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            var  loadedTypes             = new List <Type>();
            Type defaultSqlGeneratorType = null;

            foreach (var assembly in currentLoadedAssemblies)
            {
                var loadableTypes = assembly.GetLoadableTypes();

                if (loadableTypes != null && loadableTypes.FirstOrDefault(t => t.IsSubclassOf(typeof(DefaultSqlGenerator))) != null)
                {
                    defaultSqlGeneratorType = loadableTypes.FirstOrDefault(t => t.IsSubclassOf(typeof(DefaultSqlGenerator)));
                    break;
                }
            }

            DefaultSqlGenerator defaultSqlGenerator = null;

            if (defaultSqlGeneratorType != null)
            {
                defaultSqlGenerator = (DefaultSqlGenerator)Activator.CreateInstance(defaultSqlGeneratorType);

                if (defaultSqlGenerator != null)
                {
                    defaultSqlGenerator.SetSqlGenerator();
                    SqlGenerator = defaultSqlGenerator.Generator;
                    return;
                }
            }

            SqlGenerator = new SqlServerSqlGenerator();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            string sql  = @"select * from [me]";
            var    info = SQLParser.ParseSQL(sql);

            var generator = new DefaultSqlGenerator();

            Console.WriteLine(generator.Generate(info));
        }
Exemplo n.º 6
0
        public void BasicTest_2_Condition()
        {
            SelectStmtInfo stmtInfo = new SelectStmtInfo();

            stmtInfo.Columns.Add(new Column()
            {
                Expression = new ColumnExpression(ExpressionType.ColumnName)
                {
                    ColumnName = "userId"
                }
            });
            stmtInfo.Columns.Add(new Column()
            {
                Expression = new ColumnExpression(ExpressionType.ColumnName)
                {
                    ColumnName = "userName"
                }
            });

            stmtInfo.TableDescriptor = new TableDescriptor()
            {
                TableName = "me", TableReadType = TableReadType.NONE
            };

            stmtInfo.WhereCondition = new WhereCondition(WhereConditionNodeType.Statement);

            stmtInfo.WhereCondition.Statement_Operator = "AND";

            stmtInfo.WhereCondition.Statement_LeftNode = new WhereCondition(WhereConditionNodeType.Condition);
            stmtInfo.WhereCondition.Statement_LeftNode.Condition_LeftExpression  = "userId";
            stmtInfo.WhereCondition.Statement_LeftNode.Condition_Operator        = ">";
            stmtInfo.WhereCondition.Statement_LeftNode.Condition_RightExpression = "5";

            stmtInfo.WhereCondition.Statement_RightNode = new WhereCondition(WhereConditionNodeType.Condition);
            stmtInfo.WhereCondition.Statement_RightNode.Condition_LeftExpression  = "sex";
            stmtInfo.WhereCondition.Statement_RightNode.Condition_Operator        = "=";
            stmtInfo.WhereCondition.Statement_RightNode.Condition_RightExpression = "1";

            stmtInfo.OrderBy.Add(new OrderByCondition()
            {
                Expression = "userId", Direction = OrderByDirection.ASC
            });

            IGenerator g = new DefaultSqlGenerator();

            var sql = g.Generate(stmtInfo);

            Assert.AreEqual("SELECT userId, userName FROM me WHERE (userId>5) AND (sex=1) ORDER BY userId ASC", sql);
        }