示例#1
0
        private string GenerateSqlScript(Action <ISqlDumper> dmpFunc)
        {
            var sw   = new StringWriter();
            var sqlo = new SqlOutputStream(_factory.CreateDialect(), sw, new SqlFormatProperties());
            var dmp  = _factory.CreateDumper(sqlo, new SqlFormatProperties());

            dmpFunc(dmp);
            return(sw.ToString());
        }
示例#2
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();
 }
示例#3
0
        public static string GenerateSql(IDatabaseFactory factory, Action <ISqlDumper> func)
        {
            var sw  = new StringWriter();
            var so  = new SqlOutputStream(factory.CreateDialect(), sw, new SqlFormatProperties());
            var dmp = factory.CreateDumper(so, new SqlFormatProperties());

            func(dmp);
            return(sw.ToString());
        }
示例#4
0
        public static string GenerateScript(this IDatabaseFactory factory, Action <ISqlDumper> script, SqlFormatProperties props)
        {
            StringWriter sw = new StringWriter();
            ISqlDumper   dmp;
            var          so = new SqlOutputStream(factory.CreateDialect(), sw, props);

            dmp = factory.CreateDumper(so, props);
            script(dmp);
            return(sw.ToString());
        }
示例#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 static string ToSql(this IDmlfNode node, IDatabaseFactory factory)
        {
            if (factory == null)
            {
                return("");
            }
            var sw  = new StringWriter();
            var dmp = factory.CreateDumper(new SqlOutputStream(factory.CreateDialect(), sw, SqlFormatProperties.Default), SqlFormatProperties.Default);

            node.GenSql(dmp);
            return(sw.ToString());
        }
示例#7
0
        public SqlScriptCompiler(IDatabaseFactory factory, DataSyncSqlModel datasync, IShellContext context, string procName)
        {
            _context = context;
            _procName = procName;
            _datasync = datasync;
            _factory = factory;

            _sw = new StringWriter();
            var so = new SqlOutputStream(factory.CreateDialect(), _sw, new SqlFormatProperties());
            so.OverrideCommandDelimiter(";");
            _dmp = factory.CreateDumper(so, new SqlFormatProperties());
        }
示例#8
0
        public SqlGeneratorModel(IConnectionProvider provider, TextWriter stream, GenerateSql options, DatabaseInfo database, ICancelableProcessCallback cancelable, Action <string> logger)
        {
            _cancelable  = cancelable;
            _provider    = provider;
            _logger      = logger;
            _factory     = _provider.Factory;
            _sqlo        = new SqlOutputStream(_factory.CreateDialect(), stream, new SqlFormatProperties());
            _dmp         = _factory.CreateDumper(_sqlo, new SqlFormatProperties());
            _options     = options;
            _database    = database;
            _dialectCaps = _factory.DialectCaps;

            FillWorkingSets();
        }
示例#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 static string GenerateSql(IDatabaseFactory factory, Action<ISqlDumper> func)
 {
     var sw = new StringWriter();
     var so = new SqlOutputStream(factory.CreateDialect(), sw, new SqlFormatProperties());
     var dmp = factory.CreateDumper(so, new SqlFormatProperties());
     func(dmp);
     return sw.ToString();
 }
示例#11
0
 public static string ToSql(this IDmlfNode node, IDatabaseFactory factory)
 {
     if (factory == null) return "";
     var sw = new StringWriter();
     var dmp = factory.CreateDumper(new SqlOutputStream(factory.CreateDialect(), sw, SqlFormatProperties.Default), SqlFormatProperties.Default);
     node.GenSql(dmp);
     return sw.ToString();
 }
示例#12
0
 public LiteralFormatterBase(IDatabaseFactory factory)
 {
     _factory = factory;
     _dialect = _factory.CreateDialect();
 }
示例#13
0
 public LiteralFormatterBase(IDatabaseFactory factory)
 {
     _factory = factory;
     _dialect = _factory.CreateDialect();
 }
示例#14
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());
        }
示例#15
0
        private void FreeExternalSources(DbConnection conn, IDatabaseFactory factory, IShellContext context)
        {
            if (!_externalSources.Any()) return;

            var sw = new StringWriter();
            var so = new ConnectionSqlOutputStream(conn, null, factory.CreateDialect());
            var dmp = factory.CreateDumper(so, new SqlFormatProperties());

            foreach (var exSource in _externalSources)
            {
                var tbl = new TableInfo(null) { FullName = exSource.ExternalDataName };
                dmp.DropTable(tbl, false);
            }
        }
示例#16
0
        private void FillExternalSources(DbConnection conn, IDatabaseFactory factory, IShellContext context)
        {
            if (!_externalSources.Any()) return;

            var sw = new StringWriter();
            var so = new ConnectionSqlOutputStream(conn, null, factory.CreateDialect());
            var dmp = factory.CreateDumper(so, new SqlFormatProperties());

            foreach (var exSource in _externalSources)
            {
                var tbl = new TableInfo(null) { FullName = exSource.ExternalDataName };
                foreach (var col in exSource.Dbsh.Columns)
                {
                    tbl.Columns.Add(new ColumnInfo(tbl)
                    {
                        Name = col.Name,
                        DataType = col.DataType ?? "nvarchar(500)",
                        CommonType = DbTypeBase.ParseType(col.DataType ?? "nvarchar(500)"),
                    });
                }
                dmp.CreateTable(tbl);

                var filterModel = FilterJoinSqlModel.Create(exSource, SourceGraphModel, factory);

                var copyTable = new DbShell.Core.CopyTable
                {
                    Source = filterModel?.DataSource ?? exSource.Dbsh.DataSource,
                    Target = new DbShell.Core.Table
                    {
                        Name = exSource.ExternalDataName.Name,
                        StructureOverride = tbl,
                    },
                    AllowBulkCopy = _model.AllowBulkCopy,
                };
                var runnable = (IRunnable)copyTable;
                runnable.Run(context);

                if (exSource.Dbsh.OnExternalFilledAssertion != null)
                {
                    string value = conn.ExecuteScalar(exSource.Dbsh.OnExternalFilledAssertion.Replace("${TABLE}", exSource.ExternalDataName.Name))?.ToString();
                    if (value != exSource.Dbsh.OnExternalFilledRequiredValue)
                    {
                        throw new Exception($"DBSH-00000 OnExternalFilledAssertion failed, source={exSource.SqlAlias}, required={exSource.Dbsh.OnExternalFilledRequiredValue}, actual={value}, query={exSource.Dbsh.OnExternalFilledAssertion}");
                    }
                }
            }
        }