예제 #1
0
        public override string ToStr()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("update ").Append(TableClause.ToStr());
            if (Set.Count > 0)
            {
                sb.Append(" SET ");
                for (int i = 0; i < Set.Count; i++)
                {
                    var sc = Set[i];
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    sb.Append(sc.ToStr());
                }
            }
            if (Where != null)
            {
                sb.Append(" where ").Append(Where.ToStr());
            }
            AddReturningToStr(sb);
            return(sb.ToString());
        }
예제 #2
0
        public override string ToSql(ExpressionSqlBuilder builder)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("update ");
            if (builder.DbType == DriverType.PostgreSQL)
            {
                sb.Append(TableClause.ToSql(builder));
                writeSET(sb, builder);
            }
            if (builder.DbType == DriverType.SqlServer)
            {
                if (string.IsNullOrEmpty(TableClause.Alias))
                {
                    sb.Append(TableClause.ToSql(builder));
                    writeSET(sb, builder);
                }
                else
                {
                    sb.Append(builder.EncodeTable(TableClause.Alias) + " ");
                    writeSET(sb, builder);
                    sb.Append(" from ").Append(TableClause.ToSql(builder)).Append(" ");
                }
            }

            AddReturningToSql1(builder, sb);
            if (Where != null)
            {
                sb.Append(" where ").Append(Where.ToSql(builder));
            }
            AddReturningToSql2(builder, sb);
            return(sb.ToString());
        }
예제 #3
0
        public static TableClause CreateByTable(string[] logicalName, ITableDesc table)
        {
            TableClause st = new TableClause();

            st.Table = table;
            return(st);
        }
예제 #4
0
        public static TableClause CreateBySelect(ITableDesc select)
        {
            TableClause st = new TableClause();

            st.Table = select;
            return(st);
        }
예제 #5
0
        public TableClause[] GetTables()
        {
            var r = new TableClause[1] {
                TableClause
            };

            return(r);
        }
예제 #6
0
        public override string ToSql(ExpressionSqlBuilder builder)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("insert into ").Append(TableClause.ToSql(builder));
            if (DefaultValues)
            {
                sb.Append(" DEFAULT VALUES");
                AddReturningToSql1(builder, sb);
            }
            else
            {
                if (ColumnOfValues.Count > 0)
                {
                    sb.Append(" ( ");
                    for (int i = 0; i < ColumnOfValues.Count; i++)
                    {
                        var sc = ColumnOfValues[i];
                        if (i > 0)
                        {
                            sb.Append(", ");
                        }

                        if (!(sc is FieldExpr))
                        {
                            throw new Exception("В перечне колонок INSERT INTO () должны быть простые имена колонок");
                        }
                        FieldExpr fe = sc as FieldExpr;
                        sb.Append(fe.ToSqlShort(builder));
                    }
                    sb.Append(")");
                }
                AddReturningToSql1(builder, sb);
                if (Values != null && Values.Count > 0)
                {
                    sb.Append(" values");
                    for (int i = 0; i < Values.Count; i++)
                    {
                        var sc = Values[i];
                        if (i > 0)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(sc.ToSql(builder));
                    }
                }
                else
                {
                    sb.Append(" ").Append(Select.ToSql(builder));
                }
            }
            AddReturningToSql2(builder, sb);
            return(sb.ToString());
        }
예제 #7
0
        public override string ToStr()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("delete from ").Append(TableClause.ToStr());
            if (Where != null)
            {
                sb.Append(" where ").Append(Where.ToStr());
            }
            AddReturningToStr(sb);
            return(sb.ToString());
        }
예제 #8
0
        public override string ToStr()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("insert into ").Append(TableClause.ToStr());
            if (DefaultValues)
            {
                sb.Append(" DEFAULT VALUES");
            }
            else
            {
                if (ColumnOfValues.Count > 0)
                {
                    sb.Append(" ( ");
                    for (int i = 0; i < ColumnOfValues.Count; i++)
                    {
                        var sc = ColumnOfValues[i];
                        if (i > 0)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(sc.ToStr());
                    }
                    sb.Append(")");
                }
                if (Values != null && Values.Count > 0)
                {
                    sb.Append(" values");
                    for (int i = 0; i < Values.Count; i++)
                    {
                        var sc = Values[i];
                        //sb.Append("(");
                        if (i > 0)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(sc.ToStr());
                        //sb.Append(")");
                    }
                }
                else
                {
                    sb.Append(" ").Append(Select.ToStr());
                }
            }
            AddReturningToStr(sb);
            return(sb.ToString());
        }
예제 #9
0
        public override IExplore Expolore(DelegateExpessionExplorer del)
        {
            if (TableClause != null)
            {
                TableClause = (TableClause)TableClause.Expolore(del);
            }
            List <ColumnClause> Returning2 = new List <ColumnClause>();

            ReturningColumns.ForEach(a =>
            {
                ColumnClause ss = (ColumnClause)a.Expolore(del);
                if (ss != null)
                {
                    Returning2.Add(ss);
                }
            });
            ReturningColumns.Replace(Returning2);
            return(base.Expolore(del));
        }
예제 #10
0
        public override string ToSql(ExpressionSqlBuilder builder)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("delete ");
            if (builder.DbType == DriverType.SqlServer)
            {
                if (!string.IsNullOrEmpty(TableClause.Alias))
                {
                    sb.Append(builder.EncodeTable(TableClause.Alias)).Append(" ");
                }
            }
            sb.Append("from ");
            sb.Append(TableClause.ToSql(builder)).Append(" ");
            AddReturningToSql1(builder, sb);
            if (Where != null)
            {
                sb.Append(" where ").Append(Where.ToSql(builder));
            }
            AddReturningToSql2(builder, sb);
            return(sb.ToString());
        }
예제 #11
0
        public override void ParseInside(ExpressionParser parser)
        {
            var collection = parser.Collection;
            var lex        = collection.CurrentLexem();

            if (lex.LexemText.ToLower() != "insert")
            {
                throw new Exception("Not INSERT statment");
            }
            lex = collection.GotoNextMust();
            if (lex.LexemText.ToLower() != "into")
            {
                throw new Exception("INTO keyword is not found");
            }
            lex = collection.GotoNextMust();
            string[] tablename = CommonParserFunc.ReadTableName(collection);
            TableClause = TableClause.CreateByTable(tablename, collection.TableGetter.GetTableByName(tablename, collection.TableGetterUseCache));
            lex         = collection.GotoNextMust();
            if (lex.IsSkobraOpen())
            {
                while (true)
                {
                    lex = collection.GotoNextMust(); //пропускаем SET или ','
                    //lex = collection.CurrentLexem();

                    var col = CommonParserFunc.ReadColumn(collection);
                    ColumnOfValues.Add(col);
                    lex = collection.GotoNextMust();
                    if (lex == null)
                    {
                        break;
                    }
                    if (lex.LexemType == LexType.Zpt)
                    {
                        continue;
                    }
                    if (lex.IsSkobraClose())
                    {
                        break;
                    }
                    collection.Error("Unknow lexem", collection.CurrentLexem());
                }
                //пропускаем ')'
                lex = collection.GotoNextMust();
            }
            else
            {
                if (lex.LexemType == LexType.Command && lex.LexemText.ToLower() == "default")
                {
                    lex = collection.GotoNextMust();
                    if (lex.LexemType == LexType.Command && lex.LexemText.ToLower() == "values")
                    {
                        DefaultValues = true;
                        return;
                    }
                    else
                    {
                        collection.Error("Expected keyword VALUES", lex);
                    }
                }
            }

            if (lex == null)
            {
                return;
            }
            if (lex.LexemText.ToLower() == "values")
            {
                lex = collection.GotoNextMust();
                if (!lex.IsSkobraOpen())
                {
                    collection.Error("'(' not found", lex);
                }
                while (true)
                {
                    SubExpression sub = new SubExpression();
                    sub.MultiValues = true;
                    while (true)
                    {
                        lex = collection.GotoNextMust(); //пропускаем SET или ','
                        //lex = collection.CurrentLexem();

                        ExpressionParser e = new ExpressionParser(collection);
                        e.Parse();
                        sub.AddChild(e.Single());
                        lex = collection.CurrentLexem();
                        if (lex == null)
                        {
                            break;
                        }
                        if (lex.LexemType == LexType.Zpt)
                        {
                            continue;
                        }
                        if (lex.IsSkobraClose())
                        {
                            break;
                        }
                        collection.Error("Unknow lexem", collection.CurrentLexem());
                    }
                    Values.Add(sub);
                    lex = collection.GotoNext();
                    if (lex != null && lex.LexemType == LexType.Zpt)
                    {
                        lex = collection.GotoNextMust();
                        if (!lex.IsSkobraOpen())
                        {
                            collection.ErrorWaitKeyWord("(", lex);
                        }
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            if (lex.LexemText.ToLower() == "select" || lex.IsSkobraOpen())
            {
                ExpressionParser e = new ExpressionParser(collection);
                e.Parse();
                var expr = e.Single();
                var sel  = ParserUtils.FindSelect(expr);
                if (sel == null)
                {
                    throw new Exception("Values in INSERT not found");
                }
                Select = sel;
            }
            ParseReturining(parser);
        }
예제 #12
0
        private TableClause Read_table_reference(LexemCollection collection, bool isFirst)
        {
            var lex = collection.CurrentLexem();

            if (lex == null)
            {
                return(null);
            }
            //+left join
            //+left outer join
            //+inner join
            //+cross join
            //+join
            //+right join
            //+right outer join
            // full join
            JoinType jt = JoinType.Cross;

            if (!isFirst)
            {
                string s1 = collection.CurrentLexem().LexemText.ToLower();
                string s2 = null;
                string s3 = null;
                if (joinStrings.Contains(s1))
                {
                    lex = collection.GotoNext();
                    if (lex != null && joinStrings.Contains(lex.LexemText.ToLower()))
                    {
                        s2  = lex.LexemText.ToLower();
                        lex = collection.GotoNext();
                        if (lex != null && joinStrings.Contains(lex.LexemText.ToLower()))
                        {
                            s3  = lex.LexemText.ToLower();
                            lex = collection.GotoNext();
                        }
                    }
                }

                if (!joinStrings.Contains(s1))// случай ","
                {
                    jt = JoinType.Cross;
                }
                else
                if ((s3 == null && s2 == null && s1 == "join") ||
                    (s3 == null && s1 == "cross" && s2 == "join"))
                {
                    jt = JoinType.Cross;
                }
                else if ((s3 == null && s1 == "left" && s2 == "join") ||
                         (s1 == "left" && s2 == "outer" && s3 == "join"))
                {
                    jt = JoinType.Left;
                }
                else if ((s3 == null && s1 == "right" && s2 == "join") ||
                         (s1 == "right" && s2 == "outer" && s3 == "join"))
                {
                    jt = JoinType.Left;
                }
                else if (s3 == null && s1 == "inner" && s2 == "join")
                {
                    jt = JoinType.Inner;
                }
                else if (s3 == null && s1 == "full" && s2 == "join")
                {
                    jt = JoinType.Full;
                }
                else
                {
                    collection.Error("Unknow JOIN clause", collection.CurrentLexem());
                }
            }
            TableClause st = null;

            if (lex == null)
            {
                collection.Error("Error in table clause", collection.GetPrev());
            }
            if (!lex.IsSkobraOpen())
            {
                string[] tableName = CommonParserFunc.ReadTableName(collection);
                var      v         = collection.TableGetter.GetTableByName(tableName, collection.TableGetterUseCache);
                st  = TableClause.CreateByTable(tableName, v);
                lex = collection.GotoNext();
            }
            else
            {
                lex = collection.GotoNext();
                if (lex == null)
                {
                    collection.Error("Expression is not found", collection.GetLast());
                }
                //подзапрос
                var idx = collection.IndexLexem;
                ExpressionParser tonode = new ExpressionParser(collection);
                tonode.Parse();
                if (tonode.Results.Count != 1)
                {
                    collection.Error("не верное число параметров", collection.Get(idx));
                }
                lex = collection.CurrentLexem();
                if (lex == null || !lex.IsSkobraClose())
                {
                    collection.Error("Expression is not closed", collection.CurrentLexem());
                }
                lex = collection.GotoNext();

                //ITableDesc subselect = CommonUtils.FindParentSelect(tonode.Single()); Это штука работает не правильно
                ITableDesc subselect = (ITableDesc)tonode.Single();
                if (subselect == null)
                {
                    collection.Error("Subselect not found", collection.Get(idx));
                }
                st = TableClause.CreateBySelect(subselect);
            }
            st.Join = jt;
            if (lex == null)
            {
                return(st);
            }
            if (lex.LexemText.ToLower() == "as")
            {
                collection.GotoNext();
                if (lex == null)
                {
                    collection.Error("Alias not found", collection.GetPrev());
                }
                st.Alias = CommonParserFunc.ReadAlias(collection);
                if (st.Alias == null)
                {
                    collection.Error("Alias not found", collection.GetPrev());
                }
                lex = collection.GotoNext();
            }
            else
            {
                if (lex.LexemType == LexType.Text)
                {
                    st.Alias = CommonParserFunc.ReadAlias(collection);
                    if (st.Alias == null)
                    {
                        collection.Error("Alias not found", collection.GetPrev());
                    }
                    lex = collection.GotoNext();
                }
                else
                if (lex.LexemType == LexType.Command &&
                    !nextStrings.Contains(lex.LexemText.ToLower()) &&
                    !joinStrings.Contains(lex.LexemText.ToLower()) &&
                    lex.LexemText.ToLower() != "on")
                {
                    st.Alias = CommonParserFunc.ReadAlias(collection);
                    if (st.Alias == null)
                    {
                        collection.Error("Alias not found", collection.GetPrev());
                    }
                    lex = collection.GotoNext();
                }
            }
            lex = collection.CurrentLexem();
            if (lex == null)
            {
                return(st);
            }
            if (lex.LexemType == LexType.Command && lex.LexemText.ToLower() == "on")
            {
                lex = collection.GotoNext();
                var idx = collection.IndexLexem;
                ExpressionParser tonode = new ExpressionParser(collection);
                tonode.Parse();
                if (tonode.Results.Count != 1)
                {
                    collection.Error("не верное число параметров", collection.Get(idx));
                }
                st.OnExpression = tonode.Single();
            }
            return(st);
        }