internal ExecuteBlockInsertSql GetInsertSql(FbInsertSqlContext insertContext, InsertColumn[] insertValues, bool resultRequired, bool skipCommandParameters)
        {
            List<string> columnsList = new List<string>(insertValues.Length);
            List<string> valuesList = new List<string>(insertValues.Length);
            List<string> parametersList = new List<string>(insertValues.Length);
            int parametersSize = 0;

            for (int i = 0; i < insertValues.Length; i++)
            {
                var currentValue = insertValues[i];
                columnsList.Add(currentValue.Column.QuotedName);

                if (skipCommandParameters && CanSkipCommandParameterFor(currentValue))
                {
                    valuesList.Add(currentValue.ValueToSql());
                }
                else
                {
                    if (currentValue.ParameterName == null)
                        currentValue.ParameterName = GetNextParameterName(insertContext);

                    valuesList.Add(":" + currentValue.ParameterName);
                    parametersList.Add(String.Format("{0} {1}=@{0}", currentValue.ParameterName,
                        currentValue.ValueParameterToSql()));
                    parametersSize += currentValue.Column.Size + ParameterSizeInBytes;
                }
            }

            string insertSql;
            if (resultRequired)
            {
                insertSql = String.Format("INSERT INTO {0} ({1}) VALUES({2}) RETURNING {3} into {4};suspend;",
                    insertContext.TableName, String.Join(",", columnsList), String.Join(",", valuesList), insertContext.ReturnsColumnSql, insertContext.ReturnsVariablesSql);
            }
            else
            {
                insertSql = String.Format("INSERT INTO {0} ({1}) VALUES({2});", insertContext.TableName, String.Join(",", columnsList), String.Join(",", valuesList));
            }

            return new ExecuteBlockInsertSql(String.Join(",", parametersList), insertSql, parametersSize);
        }
Пример #2
0
        private IDictionary<string, object> CreateAndExecuteInsertCommand(IDbConnection connection, Table table, InsertColumn[] insertColumns, bool resultRequired, IDbTransaction transaction = null)
        {
            using (var command = connection.CreateCommand())
            {
                command.Transaction = transaction;
                command.CommandText = GetInsertSql(table, insertColumns, resultRequired);

                var columnParameters = new Dictionary<string,FbParameter>(insertColumns.Length);

                foreach (var insertColumn in insertColumns)
                {
                    columnParameters[insertColumn.Name] = new FbParameter
                    {
                        ParameterName = insertColumn.ParameterName,
                        Value = insertColumn.Value,
                        Direction = resultRequired ? ParameterDirection.InputOutput : ParameterDirection.Input
                    };

                    command.Parameters.Add(columnParameters[insertColumn.Name]);
                }

                return ExecuteInsertCommand(command, columnParameters, resultRequired);
            }
        }
Пример #3
0
 private string GetInsertSql(Table table, InsertColumn[] insertData, bool resultRequired)
 {
     string columnsSql = String.Join(",", insertData.Select(s => s.Column.QuotedName));
     string valuesSql = String.Join(",", insertData.Select(c => c.ParameterName));
     
     if (resultRequired) return string.Format("INSERT INTO {0} ({1}) VALUES({2}) RETURNING {1};", table.QualifiedName, columnsSql, valuesSql);
     else return string.Format("INSERT INTO {0} ({1}) VALUES({2});", table.QualifiedName, columnsSql, valuesSql);
 }
 private bool CanSkipCommandParameterFor(InsertColumn insertColumn)
 {
     return(insertColumn.Value == null || !insertColumn.Value.GetType().IsArray);
 }
 internal ExecuteBlockInsertSql GetInsertSql(FbInsertSqlContext insertContext, InsertColumn[] insertValues, bool resultRequired)
 {
     return GetInsertSql(insertContext, insertValues, resultRequired, insertContext.SkipCommandParameters);
 }
 private bool CanSkipCommandParameterFor(InsertColumn insertColumn)
 {
     return insertColumn.Value == null || !insertColumn.Value.GetType().IsArray;
 }