private InsertStep CreateExecutionStep(TableIdentifier tiSource, TableIdentifier tiDestination, TableMetadata table, object[] destinationRow, int level) { var step = new InsertStep { StepId = _nextStepId++, SourceTable = tiSource, DestinationTable = tiDestination, TableMetadata = table, Depth = level }; //Renseignement des variables à générer foreach (var col in table.ColumnsDefinition) { var colName = col.Name; var valueToGenerate = ((col.IsPrimary && !col.IsAutoIncrement) || col.IsUniqueKey) && !col.IsForeignKey; var pkToGenerate = col.IsPrimary && col.IsAutoIncrement; if (valueToGenerate | pkToGenerate) { var sqlVar = new SqlVariable(_nextVariableId++); step.Variables.Add(sqlVar); var pos = table.ColumnsDefinition.IndexOf(c => c.Name == colName); destinationRow[pos] = sqlVar; } } step.Datarow = destinationRow; AddInsertStep(step); return(step); }
private InsertStep GetInsertStep(InsertStatementGroup value) { var step = new InsertStep(); step.Columns = _statement.ColumnNames; step.Values = value.Values; step.TableName = _statement.Tables.First(); step.DatabaseName = _statement.DatabaseName; return(step); }
private void AddInsertStep(InsertStep step) { var connId = step.DestinationTable.ServerId; if (!_executionPlanByServer.ContainsKey(connId)) { _executionPlanByServer.Add(connId, new ExecutionPlan()); } _executionPlanByServer[connId].InsertSteps.Add(step); //Recopie dans le plan d'exécution pour la performance foreach (var sqlVar in step.Variables) { _executionPlanByServer[connId].Variables.Add(sqlVar); } }
internal void GemerateInsertStatment(Metadatas metadata, InsertStep step, IDbCommand cmd, StringBuilder sql, IDbTransaction transaction, ref int nbParams) { var tableMetadata = metadata.GetTable(step.DestinationTable); if (tableMetadata.ColumnsDefinition.Count != step.Datarow.Length) { throw new Exception("The step doesn't correspond to schema!"); } var insertWriter = SqlWriter.GetInsertWriter() .AppendColumns(step.DestinationTable, tableMetadata.ColumnsDefinition); var sbPostInsert = new StringBuilder(); //Valeurs des colonnes for (var i = 0; i < tableMetadata.ColumnsDefinition.Count; i++) { var col = tableMetadata.ColumnsDefinition[i]; var sqlVar = step.Datarow[i] as SqlVariable; //Variable à générer if (((col.IsPrimary && !col.IsAutoIncrement) || col.IsUniqueKey) && !col.IsForeignKey) { if (sqlVar == null) { throw new NullReferenceException(); } sqlVar.Value = DataBuilder.BuildDataColumn(this, transaction, step.DestinationTable.ServerId, step.DestinationTable.Database, step.DestinationTable.Schema, step.TableMetadata, col); insertWriter.AppendValue(sqlVar.Value); } //Post insert variable (auto generated primary key) else if (col.IsPrimary && col.IsAutoIncrement) { if (sqlVar == null) { throw new NullReferenceException(); } if (step.Depth == 0 || sqlVar.QueryValue) { sbPostInsert.Append(SqlWriter.SelectLastIdentity(sqlVar.Id, tableMetadata.Name, col.Name)); } } //Normal variables else { //On fait référence à une variable if (sqlVar != null) { //Si variable auto-généré if (sqlVar.Value == null) { insertWriter.AppendVariable(sqlVar.Id.ToString()); } //Sinon variable déjà générée else { insertWriter.AppendValue(sqlVar.Value); } } //C'est une valeur brute else { var sqlVarName = SqlWriter.NamedParamPrefix + tableMetadata.ColumnsDefinition[i].Name.FormatSqlParam() + step.StepId; var p = cmd.CreateParameter(); p.ParameterName = sqlVarName; if (col.IsDataColumnBuildable()) { p.Value = DataBuilder.BuildDataColumn(this, transaction, step.DestinationTable.ServerId, step.DestinationTable.Database, step.DestinationTable.Schema, step.TableMetadata, col); } else { p.Value = step.Datarow[i]; } p.DbType = col.DbType; cmd.Parameters.Add(p); insertWriter.Append(sqlVarName).Append(","); nbParams++; } } } insertWriter.Complete(); sql.Append(insertWriter.ToStringBuilder()) .Append(sbPostInsert); }