示例#1
0
        private ICdlReader CreateTableReader(TableInfo table, out DbCommand cmd)
        {
            string sql = GenerateSqlScript(dmp =>
            {
                dmp.Put("^select ");
                bool was = false;
                foreach (var col in table.Columns)
                {
                    if (was)
                    {
                        dmp.Put(",");
                    }
                    dmp.ColumnReadableValue(col);
                    was = true;
                }
                dmp.Put(" ^from %f", table.FullName);
            });
            var dda  = _factory.CreateDataAdapter();
            var conn = _provider.Connect();

            cmd = conn.CreateCommand();
            cmd.CommandTimeout = 3600;
            cmd.CommandText    = sql;
            _cancelable?.AddCancelMethod(cmd, cmd.Cancel);
            var reader = cmd.ExecuteReader();
            var result = dda.AdaptReader(reader, command: cmd);

            result.Disposing += () =>
            {
                reader.Dispose();
                conn.Dispose();
            };
            return(result);
        }
示例#2
0
 public DataSetModel(DatabaseInfo targetDatabase, IShellContext context, IDatabaseFactory factory)
 {
     _targetDatabase = targetDatabase;
     _context = context;
     _factory = factory;
     _dda = _factory.CreateDataAdapter();
 }
示例#3
0
        //public void SaveToXml(XmlElement xml)
        //{
        //    foreach (var elem in Inserts)
        //    {
        //        elem.SaveToXml(xml.AddChild("Insert"));
        //    }
        //    foreach (var elem in Updates)
        //    {
        //        elem.SaveToXml(xml.AddChild("Update"));
        //    }
        //    foreach (var elem in Deletes)
        //    {
        //        elem.SaveToXml(xml.AddChild("Delete"));
        //    }
        //}

        //private void DumpTarget(ISqlDumper dmp, ChangeSetItem item)
        //{
        //    string linkedInfoStr = item.LinkedInfo != null ? item.LinkedInfo.ToString() : "";
        //    dmp.Put("%s%f", linkedInfoStr, item.TargetTable);
        //}

        //private void DumpWhere(ISqlDumper dmp, ChangeSetItem item, List<ChangeSetCondition> conditions, DatabaseInfo db)
        //{
        //    dmp.Put("^ where ");
        //    bool wasCond = false;
        //    foreach(var cond in conditions)
        //    {
        //        if (wasCond) dmp.Put(" ^and ");
        //        wasCond = true;
        //        DumpCondition(dmp, item, cond, db);
        //    }
        //}

        public DmlfBatch GetCommands(DatabaseInfo db, IDatabaseFactory factory)
        {
            var disableFks = new HashSet<Tuple<NameWithSchema, string>>();
            var dda = factory.CreateDataAdapter();
            var converter = new CdlValueConvertor(new DataFormatSettings());

            foreach (var upd in Updates)
            {
                if (upd.DisableReferencedForeignKeys || upd.UpdateReferences || DisableReferencedForeignKeys || UpdateReferences)
                {
                    var table = db.FindTable(upd.TargetTable);
                    if (table == null) continue;
                    foreach (var fk in table.GetReferences())
                    {
                        disableFks.Add(Tuple.Create(fk.OwnerTable.FullName, fk.ConstraintName));
                    }
                }
            }

            var res = new DmlfBatch();

            foreach (var fk in disableFks) res.DisableConstraint(fk.Item1, fk.Item2, true);

            foreach (var ins in Inserts)
            {
                ins.GetCommands(res, db, dda, converter);
            }

            foreach (var upd in Updates)
            {
                upd.GetInsertCommands(res, db, this, dda, converter);
            }

            foreach (var upd in Updates)
            {
                upd.GetCommands(res, db, this, dda, converter);
            }

            foreach (var upd in Updates)
            {
                upd.GetDeleteCommands(res, db, this);
            }

            foreach (var del in Deletes)
            {
                del.GetCommands(res, db, this);
            }

            foreach (var fk in disableFks) res.DisableConstraint(fk.Item1, fk.Item2, false);

            res.Commands.ForEach(x =>
                {
                    var cmd = x as DmlfCommandBase;
                    if (cmd != null) cmd.SimplifyFromAliases();
                });

            return res;
        }
示例#4
0
 public SqlDumper(ISqlOutputStream stream, IDatabaseFactory factory, SqlFormatProperties props)
 {
     m_stream = stream;
     m_props = props;
     m_factory = factory;
     m_DDA = m_factory.CreateDataAdapter();
     m_formatterState.DDA = m_DDA;
     m_dialect = m_factory.CreateDialect();
 }
示例#5
0
 public SqlDumper(ISqlOutputStream stream, IDatabaseFactory factory, SqlFormatProperties props)
 {
     _stream             = stream;
     _props              = props;
     _factory            = factory;
     _DDA                = _factory.CreateDataAdapter();
     _formatterState.DDA = _DDA;
     _dialect            = _factory.CreateDialect();
     _dumperCaps         = _factory.DumperCaps;
     _dialectCaps        = _factory.DialectCaps;
 }
示例#6
0
 public RecordToDbAdapter(TargetColumnMap columnMap, IDatabaseFactory targetFactory, DataFormatSettings formatSettings)
 {
     _columnMap = columnMap;
     _dda = targetFactory.CreateDataAdapter();
     _outputConv = new CdlValueConvertor(formatSettings);
 }
示例#7
0
        public static string Format(IDatabaseFactory factory, SqlFormatProperties props, SqlFormatterState state, string format, params object[] args)
        {
            IDialectDataAdapter dda = null;
            if (state != null) dda = state.DDA;
            if (dda == null) dda = factory.CreateDataAdapter();
            var dialect = factory.CreateDialect();

            int argindex = 0;
            StringBuilder sb = new StringBuilder();
            int i = 0;
            while (i < format.Length)
            {
                char c = format[i];
                switch (c)
                {
                    case '^': // SQL keyword
                        {
                            i++;
                            DumpSeparatorIfNeeded(sb, props, state);
                            while (i < format.Length && (Char.IsLetter(format, i) || format[i] == '_'))
                            {
                                sb.Append(GetCasedChar(format[i], props.SqlCommandCase));
                                i++;
                            }
                            DataDumped(state);
                        }
                        break;
                    case '&': // indentation & spacing
                        {
                            i++;
                            c = format[i];
                            i++;
                            char level = '0';
                            if (c == '1' || c == '2' || c == '3' || c == '5')
                            {
                                level = c;
                                c = format[i];
                                i++;
                            }
                            if (level != '0')
                            {
                                // indentation levels
                                if (props.IndentationLevel == SqlIndentationLevel.Original || props.IndentationLevel == SqlIndentationLevel.SingleLine)
                                {
                                    if (c == 'n' || c == 's')
                                    {
                                        if (state != null)
                                        {
                                            state.SeparatorNeeded = true;
                                        }
                                        else
                                        {
                                            sb.Append(" ");
                                        }
                                    }
                                    // when original indentation is used, don't use our separators
                                    break;
                                }
                                bool valid = (props.IndentationLevel == SqlIndentationLevel.Compact && (level == '2' || level == '5'))
                                    || (props.IndentationLevel == SqlIndentationLevel.Large && (level == '3' || level == '5'));
                                if (!valid)
                                {
                                    break; // mark is not for this indentation level
                                }
                            }
                            switch (c)
                            {
                                case '&':
                                    sb.Append("&");
                                    break;
                                case 'n':
                                    if (state == null) DumpEoln(sb, props, state);
                                    else state.LineFeedNeeded = true;
                                    break;
                                case '>':
                                    if (state != null) state.IndentLevel++;
                                    break;
                                case '<':
                                    if (state != null) state.IndentLevel--;
                                    break;
                                case 's':
                                    if (state != null) state.SeparatorNeeded = true;
                                    else sb.Append(" ");
                                    break;
                                case 'r':
                                    DumpSeparatorIfNeeded(sb, props, state);
                                    break;
                                case 'd':
                                    DataDumped(state);
                                    break;
                                default:
                                    throw new InternalError("DBSH-00042 Unknown & formatting instruction:" + c);
                            }
                        }
                        break;
                    case '%': // format parameter
                        {
                            i++;
                            c = format[i];

                            if (c == '%')
                            {
                                sb.Append('%');
                                i++;
                            }
                            else if (c == ',' || c == ';') // comma separated list
                            {
                                i++;
                                bool lining = c == ';';
                                c = format[i];
                                bool ok = false;
                                if (args[argindex] is IEnumerable) ok = true;
                                if (args[argindex] is ICdlRecord && c == 'v') ok = true;
                                if (!ok) throw new InternalError("DBSH-00043 List must be of type Enumerable");

                                bool was = false;
                                if (args[argindex] is IEnumerable)
                                {
                                    if (lining)
                                    {
                                        state.IndentLevel++;
                                        DumpEoln(sb, props, state);
                                    }
                                    foreach (object item in (IEnumerable)args[argindex])
                                    {
                                        if (was)
                                        {
                                            if (lining)
                                            {
                                                DumpEoln(sb, props, state);
                                                sb.Append(",");
                                            }
                                            else
                                            {
                                                sb.Append(", ");
                                            }
                                        }
                                        WriteFormattedValue(dialect, props, sb, item, c, state, dda);
                                        was = true;
                                    }
                                    if (lining)
                                    {
                                        state.IndentLevel--;
                                    }
                                }
                                else
                                {
                                    var rec = (ICdlRecord)args[argindex];
                                    if (lining)
                                    {
                                        state.IndentLevel++;
                                        DumpEoln(sb, props, state);
                                    }
                                    for (int x = 0; x < rec.FieldCount; x++)
                                    {
                                        if (lining)
                                        {
                                            DumpEoln(sb, props, state);
                                            sb.Append(",");
                                        }
                                        else
                                        {
                                            sb.Append(", ");
                                        }
                                        rec.ReadValue(x);
                                        sb.Append(GetSqlLiteral(props, dda, state, rec));
                                        was = true;
                                    }
                                    if (lining)
                                    {
                                        state.IndentLevel--;
                                    }
                                }

                                argindex++;
                                i++;
                            }
                            else if (c == ':')
                            {
                                object orig = args[argindex];
                                argindex++;
                                i++;
                                c = format[i];
                                object arg = args[argindex];
                                argindex++;
                                i++;
                                WriteFormattedValue(dialect, props, sb, arg, c, state, dda);
                            }
                            else
                            {
                                WriteFormattedValue(dialect, props, sb, args[argindex], c, state, dda);
                                argindex++;
                                i++;
                            }
                        }
                        break;
                    default:
                        {
                            if (Char.IsWhiteSpace(c))
                            {
                                if (state != null) state.SeparatorNeeded = false;
                            }
                            else
                            {
                                DumpSeparatorIfNeeded(sb, props, state);
                            }
                            sb.Append(c);
                            i++;
                        }
                        break;
                }
            }
            return sb.ToString();
        }
示例#8
0
        //public void SaveToXml(XmlElement xml)
        //{
        //    foreach (var elem in Inserts)
        //    {
        //        elem.SaveToXml(xml.AddChild("Insert"));
        //    }
        //    foreach (var elem in Updates)
        //    {
        //        elem.SaveToXml(xml.AddChild("Update"));
        //    }
        //    foreach (var elem in Deletes)
        //    {
        //        elem.SaveToXml(xml.AddChild("Delete"));
        //    }
        //}

        //private void DumpTarget(ISqlDumper dmp, ChangeSetItem item)
        //{
        //    string linkedInfoStr = item.LinkedInfo != null ? item.LinkedInfo.ToString() : "";
        //    dmp.Put("%s%f", linkedInfoStr, item.TargetTable);
        //}

        //private void DumpWhere(ISqlDumper dmp, ChangeSetItem item, List<ChangeSetCondition> conditions, DatabaseInfo db)
        //{
        //    dmp.Put("^ where ");
        //    bool wasCond = false;
        //    foreach(var cond in conditions)
        //    {
        //        if (wasCond) dmp.Put(" ^and ");
        //        wasCond = true;
        //        DumpCondition(dmp, item, cond, db);
        //    }
        //}

        public DmlfBatch GetCommands(DatabaseInfo db, IDatabaseFactory factory)
        {
            var disableFks = new HashSet <Tuple <NameWithSchema, string> >();
            var dda        = factory.CreateDataAdapter();
            var converter  = new CdlValueConvertor(new DataFormatSettings());

            foreach (var upd in Updates)
            {
                if (upd.DisableReferencedForeignKeys || upd.UpdateReferences || DisableReferencedForeignKeys || UpdateReferences)
                {
                    var table = db.FindTable(upd.TargetTable);
                    if (table == null)
                    {
                        continue;
                    }
                    foreach (var fk in table.GetReferences())
                    {
                        disableFks.Add(Tuple.Create(fk.OwnerTable.FullName, fk.ConstraintName));
                    }
                }
            }

            var res = new DmlfBatch();

            foreach (var fk in disableFks)
            {
                res.DisableConstraint(fk.Item1, fk.Item2, true);
            }

            foreach (var ins in Inserts)
            {
                ins.GetCommands(res, db, dda, converter);
            }

            foreach (var upd in Updates)
            {
                upd.GetInsertCommands(res, db, this, dda, converter);
            }

            foreach (var upd in Updates)
            {
                upd.GetCommands(res, db, this, dda, converter);
            }

            foreach (var upd in Updates)
            {
                upd.GetDeleteCommands(res, db, this);
            }

            foreach (var del in Deletes)
            {
                del.GetCommands(res, db, this);
            }

            foreach (var fk in disableFks)
            {
                res.DisableConstraint(fk.Item1, fk.Item2, false);
            }

            res.Commands.ForEach(x =>
            {
                var cmd = x as DmlfCommandBase;
                if (cmd != null)
                {
                    cmd.SimplifyFromAliases();
                }
            });

            return(res);
        }
示例#9
0
        public static string Format(IDatabaseFactory factory, SqlFormatProperties props, SqlFormatterState state, string format, params object[] args)
        {
            IDialectDataAdapter dda = null;

            if (state != null)
            {
                dda = state.DDA;
            }
            if (dda == null)
            {
                dda = factory.CreateDataAdapter();
            }
            var dialect = factory.CreateDialect();

            int           argindex = 0;
            StringBuilder sb       = new StringBuilder();
            int           i        = 0;

            while (i < format.Length)
            {
                char c = format[i];
                switch (c)
                {
                case '^':     // SQL keyword
                {
                    i++;
                    DumpSeparatorIfNeeded(sb, props, state);
                    while (i < format.Length && (Char.IsLetter(format, i) || format[i] == '_'))
                    {
                        sb.Append(GetCasedChar(format[i], props.SqlCommandCase));
                        i++;
                    }
                    DataDumped(state);
                }
                break;

                case '&':     // indentation & spacing
                {
                    i++;
                    c = format[i];
                    i++;
                    char level = '0';
                    if (c == '1' || c == '2' || c == '3' || c == '5')
                    {
                        level = c;
                        c     = format[i];
                        i++;
                    }
                    if (level != '0')
                    {
                        // indentation levels
                        if (props.IndentationLevel == SqlIndentationLevel.Original || props.IndentationLevel == SqlIndentationLevel.SingleLine)
                        {
                            if (c == 'n' || c == 's')
                            {
                                if (state != null)
                                {
                                    state.SeparatorNeeded = true;
                                }
                                else
                                {
                                    sb.Append(" ");
                                }
                            }
                            // when original indentation is used, don't use our separators
                            break;
                        }
                        bool valid = (props.IndentationLevel == SqlIndentationLevel.Compact && (level == '2' || level == '5')) ||
                                     (props.IndentationLevel == SqlIndentationLevel.Large && (level == '3' || level == '5'));
                        if (!valid)
                        {
                            break;         // mark is not for this indentation level
                        }
                    }
                    switch (c)
                    {
                    case '&':
                        sb.Append("&");
                        break;

                    case 'n':
                        if (state == null)
                        {
                            DumpEoln(sb, props, state);
                        }
                        else
                        {
                            state.LineFeedNeeded = true;
                        }
                        break;

                    case '>':
                        if (state != null)
                        {
                            state.IndentLevel++;
                        }
                        break;

                    case '<':
                        if (state != null)
                        {
                            state.IndentLevel--;
                        }
                        break;

                    case 's':
                        if (state != null)
                        {
                            state.SeparatorNeeded = true;
                        }
                        else
                        {
                            sb.Append(" ");
                        }
                        break;

                    case 'r':
                        DumpSeparatorIfNeeded(sb, props, state);
                        break;

                    case 'd':
                        DataDumped(state);
                        break;

                    default:
                        throw new InternalError("DBSH-00042 Unknown & formatting instruction:" + c);
                    }
                }
                break;

                case '%':     // format parameter
                {
                    i++;
                    c = format[i];

                    if (c == '%')
                    {
                        sb.Append('%');
                        i++;
                    }
                    else if (c == ',' || c == ';')         // comma separated list
                    {
                        i++;
                        bool lining = c == ';';
                        c = format[i];
                        bool ok = false;
                        if (args[argindex] is IEnumerable)
                        {
                            ok = true;
                        }
                        if (args[argindex] is ICdlRecord && c == 'v')
                        {
                            ok = true;
                        }
                        if (!ok)
                        {
                            throw new InternalError("DBSH-00043 List must be of type Enumerable");
                        }

                        bool was = false;
                        if (args[argindex] is IEnumerable)
                        {
                            if (lining)
                            {
                                state.IndentLevel++;
                                DumpEoln(sb, props, state);
                            }
                            foreach (object item in (IEnumerable)args[argindex])
                            {
                                if (was)
                                {
                                    if (lining)
                                    {
                                        DumpEoln(sb, props, state);
                                        sb.Append(",");
                                    }
                                    else
                                    {
                                        sb.Append(", ");
                                    }
                                }
                                WriteFormattedValue(dialect, props, sb, item, c, state, dda);
                                was = true;
                            }
                            if (lining)
                            {
                                state.IndentLevel--;
                            }
                        }
                        else
                        {
                            var rec = (ICdlRecord)args[argindex];
                            if (lining)
                            {
                                state.IndentLevel++;
                                DumpEoln(sb, props, state);
                            }
                            for (int x = 0; x < rec.FieldCount; x++)
                            {
                                if (lining)
                                {
                                    DumpEoln(sb, props, state);
                                    sb.Append(",");
                                }
                                else
                                {
                                    sb.Append(", ");
                                }
                                rec.ReadValue(x);
                                sb.Append(GetSqlLiteral(props, dda, state, rec));
                                was = true;
                            }
                            if (lining)
                            {
                                state.IndentLevel--;
                            }
                        }

                        argindex++;
                        i++;
                    }
                    else if (c == ':')
                    {
                        object orig = args[argindex];
                        argindex++;
                        i++;
                        c = format[i];
                        object arg = args[argindex];
                        argindex++;
                        i++;
                        WriteFormattedValue(dialect, props, sb, arg, c, state, dda);
                    }
                    else
                    {
                        WriteFormattedValue(dialect, props, sb, args[argindex], c, state, dda);
                        argindex++;
                        i++;
                    }
                }
                break;

                default:
                {
                    if (Char.IsWhiteSpace(c))
                    {
                        if (state != null)
                        {
                            state.SeparatorNeeded = false;
                        }
                    }
                    else
                    {
                        DumpSeparatorIfNeeded(sb, props, state);
                    }
                    sb.Append(c);
                    i++;
                }
                break;
                }
            }
            return(sb.ToString());
        }
示例#10
0
 public RecordToDbAdapter(TargetColumnMap columnMap, IDatabaseFactory targetFactory, DataFormatSettings formatSettings)
 {
     _columnMap  = columnMap;
     _dda        = targetFactory.CreateDataAdapter();
     _outputConv = new CdlValueConvertor(formatSettings);
 }