Пример #1
0
        public string BuildBulkInsertSql(List <PoolEntry <object[]> > rows)
        {
            const string rowSeparator = ",\r\n";

            ArgsChecker.CheckForNull <ArgumentNullException>(rows, () => ArgsChecker.EArgs(nameof(rows)));
            ArgsChecker.CheckForNull <InvalidOperationException>(ColumnNames, () => ArgsChecker.EArgs("ColumnNames can't be null"));
            if (TableName == string.Empty)
            {
                throw new InvalidOperationException("TableName can't be blank");
            }
            if (LiteralParamBinding && ColumnNameToMetadataIndexMap == null)
            {
                throw new InvalidOperationException("ColumnNameToMetadataIndexMap can't be null");
            }

            var stringBuilder = new StringBuilder($"INSERT INTO {TableName}\r\n(");

            foreach (var columnName in ColumnNames)
            {
                stringBuilder.Append($"\"{columnName}\",");
            }
            stringBuilder.Remove(stringBuilder.Length - 1, 1);
            stringBuilder.Append(") VALUES\r\n");
            for (var i = 0; i < rows.Count; i++)
            {
                stringBuilder.Append("(");
                if (!LiteralParamBinding)
                {
                    var columnIndex = 0;
                    foreach (var dummy in ColumnNames)
                    {
                        stringBuilder.Append($"{ParamIndicator}P{columnIndex++}_{i},");
                    }
                }
                else
                {
                    foreach (var column in ColumnNameToMetadataIndexMap)
                    {
                        stringBuilder.Append(_getNativeValueAsSqlString(column.Value >= 0 ? rows[i].Value[column.Value] : DBNull.Value) + ",");
                    }
                }

                stringBuilder.Remove(stringBuilder.Length - 1, 1);
                stringBuilder.Append($"){rowSeparator}");
                if (SingleParamSetInsertStatement)
                {
                    break;
                }
            }
            stringBuilder.Remove(stringBuilder.Length - rowSeparator.Length, rowSeparator.Length);

            return(stringBuilder.ToString());
        }
        public override void Prepare()
        {
            ArgsChecker.CheckForNull <NullReferenceException>(ColumnMetadatas, () => ArgsChecker.EArgs(nameof(ColumnMetadatas)));

            base.Prepare();

            var regExPattern    = "^";
            var currentColumnId = 0;

            while (currentColumnId++ < ColumnMetadatas.Count)
            {
                regExPattern += $"(.*){Delimiter}";
            }
            regExPattern  = regExPattern.Remove(regExPattern.Length - Delimiter.Length, Delimiter.Length);
            regExPattern += "$";
            RegexParser   = new Regex(regExPattern, RegexOptions.Compiled | RegexOptions.CultureInvariant);

            _prepared = true;
        }
        public override void Prepare()
        {
            ArgsChecker.CheckForNull <NullReferenceException>(ColumnMetadatas, () => ArgsChecker.EArgs(nameof(ColumnMetadatas)));

            base.Prepare();

            var regExPattern    = "^";
            var prevPosition    = 0;
            var prevColumnSize  = 0;
            var currentColumnId = 0;

            foreach (var columnMeta in ColumnMetadatas)
            {
                if (columnMeta.ColumnSize == null)
                {
                    throw new NullReferenceException($"{nameof(columnMeta.ColumnSize)}[{currentColumnId}]");
                }

                var currentPosition = columnMeta.StartPosition ?? prevPosition + prevColumnSize;
                if (currentPosition < prevPosition + prevColumnSize)
                {
                    throw new DataPipelineException("Field position can't be < than previous field position + previous column size");
                }
                if (currentPosition > prevPosition + prevColumnSize)
                {
                    regExPattern += $".{{{currentPosition - prevPosition - prevColumnSize}}}";
                }
                regExPattern  += $"(.{{{columnMeta.ColumnSize}}})";
                prevPosition   = currentPosition;
                prevColumnSize = (int)columnMeta.ColumnSize;
                currentColumnId++;
            }

            regExPattern += "$";
            RegexParser   = new Regex(regExPattern, RegexOptions.Compiled | RegexOptions.CultureInvariant);

            _prepared = true;
        }
Пример #4
0
        public string BuildBulkSql(
            string sqlCommandText,
            List <PoolEntry <object[]> > rows,
            bool paramsAsList)
        {
            ArgsChecker.CheckForNull <ArgumentNullException>(rows, () => ArgsChecker.EArgs(nameof(rows)));
            ArgsChecker.CheckForNull <InvalidOperationException>(ColumnNames, () => ArgsChecker.EArgs("ColumnNames can't be null"));
            if (LiteralParamBinding && ColumnNameToMetadataIndexMap == null)
            {
                throw new InvalidOperationException("ColumnNameToMetadataIndexMap can't be null");
            }

            var stringBuilder = new StringBuilder(!paramsAsList ? "SELECT " : "");

            var columnNumber = 0;

            if (!paramsAsList)
            {
                if (!LiteralParamBinding)
                {
                    foreach (var columnName in ColumnNames)
                    {
                        stringBuilder.Append($"{ParamIndicator}P{columnNumber++}_0 \"{columnName}\",");
                    }
                }
                else
                {
                    foreach (var column in ColumnNameToMetadataIndexMap)
                    {
                        stringBuilder.Append($"{_getNativeValueAsSqlString(column.Value >= 0 ? rows[0].Value[column.Value] : DBNull.Value)} \"{column.Key}\",");
                    }
                }

                stringBuilder.Remove(stringBuilder.Length - 1, 1);
                stringBuilder.Append($" {InternalSelectSuffix} ");
            }

            for (var i = paramsAsList ? 0 : 1; i < rows.Count; i++)
            {
                stringBuilder.Append(!paramsAsList ? "\r\nUNION ALL\r\nSELECT " : "");
                if (!LiteralParamBinding)
                {
                    columnNumber = 0;
                    foreach (var dummy in ColumnNames)
                    {
                        stringBuilder.Append($"{ParamIndicator}P{columnNumber++}_{i},");
                    }
                }
                else
                {
                    foreach (var column in ColumnNameToMetadataIndexMap)
                    {
                        stringBuilder.Append(_getNativeValueAsSqlString(column.Value >= 0 ? rows[i].Value[column.Value] : DBNull.Value) + ",");
                    }
                }

                if (!paramsAsList || i == rows.Count - 1)
                {
                    stringBuilder.Remove(stringBuilder.Length - 1, 1);
                }
                if (!paramsAsList)
                {
                    stringBuilder.Append($" {InternalSelectSuffix} ");
                }
            }

            var newSqlCommandText = Regex.Replace(sqlCommandText,
                                                  @"(\/\*\<DATA\>\*\/.*?\/\*\<\/DATA\>\*\/)|@@@Parameters|@@@Params",
                                                  stringBuilder.ToString(),
                                                  RegexOptions.Compiled | RegexOptions.Singleline);

            return(newSqlCommandText);
        }