Пример #1
0
        public static string InsertInto(this ISqlDialect d, IDataDescriptor descriptor, string table, string schema,
                                        List <string> columns, bool returnKeys)
        {
            return(StringBuilderPool.Scoped(sb =>
            {
                if (!d.BeforeInsert(descriptor, sb))
                {
                    return;
                }

                sb.Append("INSERT INTO ");
                sb.AppendTable(d, table, schema).Append(" (");

                if (!d.BeforeInsertColumns(descriptor, sb, columns))
                {
                    return;
                }

                for (var i = 0; i < columns.Count; i++)
                {
                    var column = columns[i];
                    sb.AppendName(d, column);
                    if (i < columns.Count - 1)
                    {
                        sb.Append(", ");
                    }
                }

                sb.Append(") ");

                if (returnKeys &&
                    d.TryFetchInsertedKey(FetchInsertedKeyLocation.BeforeValues, out var fetchBeforeValues))
                {
                    sb.Append(fetchBeforeValues).Append(" ");
                }

                sb.Append("VALUES (");

                for (var i = 0; i < columns.Count; i++)
                {
                    var column = columns[i];
                    sb.AppendParameter(d, column);
                    if (i < columns.Count - 1)
                    {
                        sb.Append(",");
                    }
                }

                sb.Append(")");

                if (returnKeys &&
                    d.TryFetchInsertedKey(FetchInsertedKeyLocation.AfterStatement, out var fetchAfterStatement))
                {
                    sb.Append("; ").Append(fetchAfterStatement);
                }
            }));
        }