Exemple #1
0
        private string MinTableName(ExpressionSqlBuilder builder)
        {
            string      res;
            TableClause tc1 = this.TableClause;

            if (!(tc1.Table is TableDesc))
            {
                throw new Exception("Can not find alias table " + tc1.Table.ToStr());
            }
            ITableSource item = this.FindParentTableSource();
            var          arr  = item.GetTables();

            if (arr.Length == 1 && arr[0] == tc1)
            {
                return("");
            }
            while (item != null)
            {
                res = DoMinTableName(builder, item, tc1);
                if (res != null)
                {
                    return(res);
                }
                var t = item.FindParentTableSource();
                if (t == item)
                {
                    throw new Exception("internal parser error (132)");
                }
                item = t;
            }
            throw new Exception("Table clause not found in expression");
        }
Exemple #2
0
        private static string DoMinTableName(ExpressionSqlBuilder builder, ITableSource parent, TableClause tc1)
        {
            TableDesc td1           = (TableDesc)tc1.Table;
            var       arr           = parent.GetTables();
            bool      find          = false;
            bool      findDubName   = false;
            bool      findDubSchema = false;

            foreach (var tc in arr)
            {
                if (tc == tc1)
                {
                    find = true;
                }
                else
                if (string.IsNullOrEmpty(tc.Alias) &&
                    (tc.Table is TableDesc)
                    )
                {
                    TableDesc td = (TableDesc)tc.Table;
                    if (td.PhysicalTableName.ToLower() == td1.PhysicalTableName.ToLower())
                    {
                        findDubName = true;
                        if (td.PhysicalSchema.ToLower() == td1.PhysicalSchema.ToLower())
                        {
                            findDubSchema = true;
                        }
                    }
                }
            }
            if (!find)
            {
                return(null);
            }
            if (!findDubName)
            {
                return(builder.EncodeTable(td1.PhysicalTableName));
            }
            if (!findDubSchema)
            {
                return(builder.EncodeTable(td1.PhysicalSchema) + "." + builder.EncodeTable(td1.PhysicalTableName));
            }
            throw new Exception("Not unique table " + builder.EncodeTable(td1.PhysicalSchema) + "." + builder.EncodeTable(td1.PhysicalTableName));
        }
Exemple #3
0
        private void FindAndMakeField()
        {
            FieldExpr    f          = this;
            string       tableAlias = f.TableAlias;
            string       fieldAlias = f.FieldName;
            string       schema     = f.Schema;
            bool         ok         = false;
            ITableSource tsource    = CommonUtils.FindParentTableSource(this);

            while (tsource != null)
            {
                var tables = tsource.GetTables();
                foreach (var st in tables)
                {
                    bool okTable = false;
                    if (!string.IsNullOrEmpty(tableAlias))
                    {
                        if (st.CompareWithColumn(new string[2] {
                            schema, tableAlias
                        }))
                        {
                            okTable = true;
                        }
                    }
                    else
                    {
                        okTable = true;
                    }
                    if (okTable)
                    {
                        var c = st.Table.ByName(fieldAlias);
                        if (c != null)
                        {
                            if (ok)
                            {
                                throw new Exception("Невозможно определить однозначно колонку");
                            }
                            f.Bind(st, fieldAlias);
                            ok = true;
                        }
                    }
                }
                if (ok)
                {
                    break;
                }
                if (string.IsNullOrEmpty(tableAlias) && tsource is SelectExpresion)
                {
                    SelectExpresion select = tsource as SelectExpresion;
                    foreach (var c in select.TableColumns)
                    {
                        if (StringComparer.InvariantCultureIgnoreCase.Compare(c.Name, fieldAlias) == 0)
                        {
                            if (ok)
                            {
                                throw new Exception("Невозможно определить однозначно колонку");
                            }
                            ok = true;
                            f.Bind(select, fieldAlias);
                        }
                    }
                }
                if (ok)
                {
                    break;
                }

                tsource = CommonUtils.FindParentTableSource(tsource);
            }
            if (!ok)
            {
                throw new Exception("Column \"" + fieldAlias + "\" is not found");
            }
        }