Esempio n. 1
0
        public void AddColumn(Keyword k, DbColumn c, bool includeAlias)
        {
            int    insertPos = -1;
            string sql       = "";

            if (insertPos == -1)
            {
                if (k.tokenType != TokenType.Select && k.startOffset == 0)
                {
                    sql = " " + k.shortName.ToLower();
                    if (k.tokenType == TokenType.Group || k.tokenType == TokenType.Order)
                    {
                        sql += " by";
                    }
                    sql      += " ";
                    insertPos = k.GetInsertOffset();
                }
                else
                {
                    insertPos = k.rightExtent;
                }
            }

            string alias = AddTable(c.table, includeAlias);

            sql += T.AppendTo(alias, c.name, ".");
            if (k.tokenType == TokenType.Where)
            {
                sql = sql + "=" + QObject.GetZeroSqlValueForType(c.dataType);
                if (k.columns.Count > 0)
                {
                    sql = " and " + sql;
                }
            }
            else
            {
                if (k.columns.Count > 0)
                {
                    sql = ", " + sql;
                    // insertPos--;
                }
            }
            while (Char.IsWhiteSpace(Query.rootQuery.expression[insertPos - 1]))
            {
                insertPos--;
            }
            inserts.Add(insertPos, sql);
        }
Esempio n. 2
0
        public string AddTable(DbTable t, bool includeAlias)
        {
            string alias = "";

            if (includeAlias)
            {
                alias = t.GetAlias(true);
            }
            int insertPos = 0;

            if (query.from == null)
            {
                Keyword k = Keyword.CreateKeyword(0, "from");
                k.parentQuery = query;
                insertPos     = k.GetInsertOffset();
                inserts.Add(insertPos, ("from " + t.name + " " + alias).Trim());
            }
            else
            {
                if (query.from.tables.GetTableByName(t.name) == null)
                {
                    insertPos = query.from.rightExtent;
                    while (Char.IsWhiteSpace(Query.rootQuery.expression[insertPos - 1]))
                    {
                        insertPos--;
                    }
                    string bestJoin = null;
                    foreach (Table table in query.from.tables.tokens)
                    {
                        string join = table.dbTable.RenderJoin(t.name, includeAlias);
                        if (join != "" && (bestJoin == null || bestJoin.CountOccurrances('.') > join.CountOccurrances('.')))
                        {
                            bestJoin = join;
                        }
                    }
                    if (bestJoin != null)
                    {
                        inserts.Add(insertPos, bestJoin);
                    }
                }
            }

            return(alias);
        }