public override void ParseInside(ExpressionParser parser)
        {
            SelectParser selectParser = new SelectParser(parser.Collection);

            selectParser.SelectExpresion = this;
            selectParser.Parse();
        }
Exemple #2
0
        public void ParsePredicates_With_Trailing_Predicates_One_Column()
        {
            //arrange
            string query = @"select truck, origin, space
                            from someTable where origin > 8
                            AND truck = '   ford' 
                            OR space = 98
                            ORDER BY truck";

            SelectParser selectParser = new SelectParser();

            //act
            var predicateResults = selectParser.ParsePredicates(query);

            predicateResults = selectParser.GetPredicateTrailers(predicateResults, query);

            var predicates = predicateResults.Predicates;



            //assert
            Assert.AreEqual("where origin > 8", predicates[0]);
            Assert.AreEqual("AND truck = '   ford'", predicates[1]);
            Assert.AreEqual("OR space = 98", predicates[2]);
            Assert.AreEqual("order truck", predicateResults.PredicateTrailer[0]);
        }
Exemple #3
0
        public void ReplaceSubqueryWithValue_Handle_New_Lines()
        {
            //arrange
            SelectParser selectParser = new SelectParser();

            string query = @"select truck, origin, space
                            from someTable where origin > 8
                            AND truck = (  
                                            select truck from someTruckTable   
                                        ) 
                            OR space = 98";

            InnerStatement subquery = selectParser.GetFirstMostInnerParantheses(query);

            var expected = @"select truck, origin, space
                            from someTable where origin > 8
                            AND truck = 'F-150' 
                            OR space = 98";

            //act
            var newQuery = new SelectParser().ReplaceSubqueryWithValue(query, subquery, "F-150", TypeEnum.String);


            //assert
            Assert.AreEqual(expected, newQuery);
        }
Exemple #4
0
        public void QueryHasWhereClause_No_False_Alarm()
        {
            //arrange
            string query = "select where, origin, space, where from someTable";

            SelectParser selectParser = new SelectParser();

            //act
            int idx = selectParser.IndexOfWhereClause(query, "someTable");

            //assert
            Assert.AreEqual(-1, idx);
        }
Exemple #5
0
        public void QueryHasWhereClause()
        {
            //arrange
            string query = "select name, origin, space, truck from someTable where origin > 2";

            SelectParser selectParser = new SelectParser();

            //act
            int idx = selectParser.IndexOfWhereClause(query, "sometable");

            //assert
            Assert.AreEqual(7, idx);
        }
Exemple #6
0
        public void GetTableName_Happy()
        {
            //arrange
            string query = "select name, origin, space, truck from someTable where origin > 2";

            SelectParser selectParser = new SelectParser();

            //act
            string tableName = selectParser.GetTableName(query);

            //assert
            Assert.AreEqual(tableName, "sometable");
        }
Exemple #7
0
        public void GetInnerMostSelectStatement()
        {
            //arrange
            string select = @"select * from somTable WHERE col IN (3,4,5) and colz = (select * from blag)";

            var parser = new SelectParser();

            //act

            string result = parser.GetInnerMostSelectStatement(select).Statement;

            Assert.AreEqual("select * from blag", result);
        }
Exemple #8
0
        public void GetColumns_Happy()
        {
            //arrange
            string query = "select name, origin, space, truck from someTable";

            SelectParser selectParser = new SelectParser();

            //act
            IList <string> columns = selectParser.GetColumns(query).Select(x => x.ColumnName).ToList();

            //assert
            Assert.AreEqual(columns[0], "name");
            Assert.AreEqual(columns[1], "origin");
            Assert.AreEqual(columns[2], "space");
            Assert.AreEqual(columns[3], "truck");
        }
Exemple #9
0
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="parserOptions">The parser options.</param>
        /// <returns>The IEntityParser.</returns>
        private static object Initialize <TEntity>(ParserOptions parserOptions)
        {
            var tokenSplitter = new TokenSplitter();

            var rangeParser  = new RangeParser();
            var equalsParser = new EqualsParser();

            var entityParser = new EntityParser <TEntity>();
            var typeSplitter = new TypeSplitter();

            var whereStatementParser = new WhereStatementParser <TEntity>(rangeParser, equalsParser, entityParser, typeSplitter);
            var whereParser          = new WhereParser(whereStatementParser, parserOptions);
            var orderByParser        = new OrderByParser <TEntity>(entityParser);
            var pageParser           = new PageParser();
            var selectParser         = new SelectParser <TEntity>(entityParser);

            return(new StatementParser(tokenSplitter, whereParser, pageParser, orderByParser, selectParser));
        }
Exemple #10
0
        public void ParsePredicates_Multiple_Predicates()
        {
            //arrange
            string query = @"select truck, origin, space
                            from someTable where origin > 8
                            AND truck = 'ford' 
                            OR space = 98";

            SelectParser selectParser = new SelectParser();

            //act
            List <string> predicates = selectParser.ParsePredicates(query).Predicates;

            //assert
            Assert.AreEqual("where origin > 8", predicates[0]);
            Assert.AreEqual("AND truck = 'ford'", predicates[1]);
            Assert.AreEqual("OR space = 98", predicates[2]);
        }
Exemple #11
0
        public static Executor GetInstance(string databaseName = Constants.FILE_NAME)
        {
            if (_instance == null)
            {
                lock (_lockObject)
                {
                    if (databaseName != null)
                    {
                        if (!File.Exists(databaseName))
                        {
                            using (File.Create(databaseName));
                        }
                    }

                    var updateParser    = new UpdateParser();
                    var stringParser    = new StringParser();
                    var reader          = new Reader();
                    var writer          = new Writer(reader);
                    var lockManager     = new LockManager(writer, reader);
                    var schemaFetcher   = new SchemaFetcher(reader);
                    var selectParser    = new SelectParser();
                    var insertParser    = new InsertParser(schemaFetcher);
                    var generalParser   = new GeneralParser();
                    var createParser    = new CreateParser();
                    var predicateParser = new PredicateParser();

                    var interpreter = new Interpreter(
                        selectParser,
                        insertParser,
                        updateParser,
                        schemaFetcher,
                        generalParser,
                        createParser,
                        stringParser,
                        lockManager,
                        reader,
                        predicateParser);

                    _instance = new Executor(interpreter);
                }
            }

            return(_instance);
        }
Exemple #12
0
        public void GetFirstMostInnerSelectStatement_Parses_With_Spaces()
        {
            //arrange
            SelectParser selectParser = new SelectParser();

            string query = @"select truck, origin, space
                            from someTable where origin > 8
                            AND truck = (   select truck from someTruckTable   ) 
                            OR space = 98";


            //act
            InnerStatement subquery = selectParser.GetFirstMostInnerParantheses(query);

            //assert
            Assert.AreEqual("   select truck from someTruckTable   ", subquery.Statement);
            Assert.AreEqual(130, subquery.StartIndexOfOpenParantheses);
            Assert.AreEqual(169, subquery.EndIndexOfCloseParantheses);
        }
Exemple #13
0
        private Dictionary <int, string> IndexToColumn(string query, TableDefinition tableDef)
        {
            Dictionary <int, string> selectIndexToColumnName = new Dictionary <int, string>();

            SelectParser selectParser = new SelectParser();

            List <SelectColumnDto> selectColumns = selectParser.GetColumns(query);

            if (selectColumns.First().ColumnName == "*")
            {
                return(tableDef.ColumnDefinitions.ToDictionary(x => (int)x.Index, x => x.ColumnName));
            }

            for (int i = 0; i < selectColumns.Count; i++)
            {
                selectIndexToColumnName[i] = selectColumns[i].ColumnName;
            }

            return(selectIndexToColumnName);
        }
        /// <summary>
        /// 查询一组记录
        /// </summary>
        /// <param name="table">数据表名称</param>
        /// <param name="filter">过滤条件</param>
        /// <param name="top">查询条数</param>
        /// <returns>返回数据列表</returns>
        public IEnumerable <T> Select <T>(string table, object filter, int top)
        {
            var filterEntities = JsonParser.ConvertEntities(filter);

            if (filterEntities.Count <= 0)
            {
                throw new Exception("未传入筛选条件,未防止错误,该执行已停止.");
            }
            var filterSql = new FilterParser().Parse(table, filterEntities[0]);

            if (string.IsNullOrEmpty(filterSql))
            {
                throw new Exception("未能解析出需要筛选sql语句,未防止错误,该执行已停止.");
            }
            var sql = new SelectParser().Parse(table, top);

            if (string.IsNullOrEmpty(sql))
            {
                throw new Exception("未能解析出需要执行的sql语句.");
            }
            return(this.Query <T>(sql + " WHERE " + filterSql));
        }
Exemple #15
0
 public Interpreter(SelectParser selectParser,
                    InsertParser insertParser,
                    UpdateParser updateParser,
                    SchemaFetcher schemaFetcher,
                    GeneralParser generalParser,
                    CreateParser createParser,
                    StringParser stringParser,
                    LockManager lockManager,
                    Reader reader,
                    PredicateParser predicateParser)
 {
     _selectParser    = selectParser;
     _insertParser    = insertParser;
     _updateParser    = updateParser;
     _schemaFetcher   = schemaFetcher;
     _generalParser   = generalParser;
     _createParser    = createParser;
     _stringParser    = stringParser;
     _lockManager     = lockManager;
     _reader          = reader;
     _predicateParser = predicateParser;
 }
        /// <summary>
        /// 查询单条记录
        /// </summary>
        /// <param name="table">数据表名称</param>
        /// <param name="filter">过滤条件</param>
        /// <returns>返回数据列表</returns>
        public T SelectSingle <T>(string table, object filter)
        {
            var filterEntities = JsonParser.ConvertEntities(filter);

            if (filterEntities.Count <= 0)
            {
                throw new Exception("未传入筛选条件,未防止错误,该执行已停止.");
            }
            var filterSql = new FilterParser().Parse(table, filterEntities[0]);

            if (string.IsNullOrEmpty(filterSql))
            {
                throw new Exception("未能解析出需要筛选sql语句,未防止错误,该执行已停止.");
            }
            var sql = new SelectParser().Parse(table);

            if (string.IsNullOrEmpty(sql))
            {
                throw new Exception("未能解析出需要执行的sql语句.");
            }
            var entity = this.QuerySingle <T>(sql + " WHERE " + filterSql);

            return(entity);
        }
Exemple #17
0
        async Task <object> getValue1(string template, IDictionary <string, object> variables, bool isVariable, object @object = null)
        {
            //枚举
            if (template.IndexOf("=>") > 0)
            {
                var segments = template.Split(new[] { "=>" }, 2, StringSplitOptions.RemoveEmptyEntries);

                var template_resource    = segments[0];
                var template_enumeration = segments[1];

                var includes = new IncludeParser(template_enumeration, templates).Parse();

                var value = await getValue2(template_resource, includes, variables, @object);

                var queryable = value is Resource
                    ? (value as Resource)?.Queryable
                    : (value as IEnumerable)?.AsQueryable();
                if (queryable == null)
                {
                    return(null);
                }

                var isArrayReturn = template_enumeration.EndsWith("[]");
                if (isArrayReturn)
                {
                    template_enumeration = template_enumeration.Substring(0, template_enumeration.Length - 2);
                }

                if (jsonqlLinqer != null)
                {
                    var selector = new SelectParser(templates).Parse(template_enumeration);
                    if (!string.IsNullOrWhiteSpace(selector))
                    {
                        queryable = jsonqlLinqer.Select(queryable, selector);
                    }
                }

                if (isArrayReturn)
                {
                    var list   = new List <object>();
                    var source = jsonqlLinqer == null?QueryableHelper.ToList(queryable) : jsonqlLinqer.ToList(queryable);

                    foreach (var item in source)
                    {
                        list.Add(await getValue1(template_enumeration, variables, false, item));
                    }
                    return(list);
                }
                else
                {
                    var first = jsonqlLinqer == null
                        ? QueryableHelper.FirstOrDefault(queryable)
                        : jsonqlLinqer.FirstOrDefault(queryable);

                    if (first == null)
                    {
                        return(null);
                    }

                    return(await getValue1(template_enumeration, variables, false, first));
                }
            }
            //取值
            else
            {
                var value = await getValue2(template, null, variables, @object);

                if (value is Resource)
                {
                    if (isVariable)
                    {
                        return(value);
                    }
                    else
                    {
                        var queryable = (value as Resource).Queryable;
                        return(jsonqlLinqer == null
                            ? QueryableHelper.ToList(queryable)
                            : jsonqlLinqer.ToList(queryable));
                    }
                }
                return(value);
            }
        }
 public SelectParserTests()
 {
     _selectParser = new SelectParser();
 }