Example #1
0
 //
 public override DbCommandInfo BuildSqlInsertCommand(EntityCommand entityCommand)
 {
     const string SqlInsertTemplate = @"
     INSERT INTO {0}
       ({1})
       VALUES
     ({2});
     {3} {4}
     ";
       const string SqlGetIdentityTemplate = "\r\nSET {0} = SCOPE_IDENTITY();";
       const string SqlGetRowVersionTemplate = "\r\nSET {0} = @@DBTS;";
       var table = DbModel.LookupDbObject<DbTableInfo>(entityCommand.TargetEntityInfo, throwNotFound: true);
       var idClause = string.Empty;
       var rvClause = string.Empty;
       var listColumns = new List<DbColumnInfo>();
       var listValues = new StringList();
       var cmdName = ModelConfig.NamingPolicy.ConstructDbCommandName(entityCommand, table.TableName, "Insert");
       var dbCmdInfo = CreateDbCommandInfo(entityCommand, cmdName, table, DbExecutionType.NonQuery, null);
       foreach (var prm in dbCmdInfo.Parameters) {
     var col = prm.SourceColumn;
     if (col.Flags.IsSet(DbColumnFlags.Identity))
       idClause = string.Format(SqlGetIdentityTemplate, prm.Name);
     if (col.Member.Flags.IsSet(EntityMemberFlags.RowVersion))
       rvClause = string.Format(SqlGetRowVersionTemplate, prm.Name);
     if (!col.Flags.IsSet(DbColumnFlags.NoInsert)) {
       listColumns.Add(col);
       listValues.Add(prm.Name);
     }
       }
       //build SQL
       var strColumns = listColumns.GetSqlNameList();
       var strValues = string.Join(", ", listValues);
       dbCmdInfo.Sql = string.Format(SqlInsertTemplate, table.FullName, strColumns, strValues, idClause, rvClause);
       return dbCmdInfo;
 }
Example #2
0
 public virtual DbCommandInfo BuildSqlInsertCommand(EntityCommand entityCommand)
 {
     const string SqlInsertTemplate = "INSERT INTO {0} \r\n  ({1}) \r\n  VALUES \r\n    ({2});";
       var table = DbModel.LookupDbObject<DbTableInfo>(entityCommand.TargetEntityInfo, throwNotFound: true);
       var idClause = string.Empty;
       var listColumns = new List<DbColumnInfo>();
       var listValues = new StringList();
       var cmdName = ModelConfig.NamingPolicy.ConstructDbCommandName(entityCommand, table.TableName, "Insert");
       var dbCmdInfo = CreateDbCommandInfo(entityCommand, cmdName, table, DbExecutionType.NonQuery, null);
       foreach (var prm in dbCmdInfo.Parameters) {
     var col = prm.SourceColumn;
     if (!col.Flags.IsSet(DbColumnFlags.NoInsert)) {
       listColumns.Add(col);
       listValues.Add(prm.Name);
     }
       }
       //build SQL
       var strColumns = listColumns.GetSqlNameList();
       var strValues = string.Join(", ", listValues);
       dbCmdInfo.Sql = string.Format(SqlInsertTemplate, table.FullName, strColumns, strValues) + idClause;
       return dbCmdInfo;
 }
Example #3
0
 //
 public override DbCommandInfo BuildSqlInsertCommand(EntityCommand entityCommand)
 {
     const string SqlInsertTemplate = @"
     INSERT INTO {0}
       ({1})
       VALUES
     ({2});
     {3}
     ";
       bool useStoredProc = this.DbModel.Config.Options.IsSet(DbOptions.UseStoredProcs);
       var table = DbModel.LookupDbObject<DbTableInfo>(entityCommand.TargetEntityInfo, throwNotFound: true);
       var idClause = string.Empty;
       var listColumns = new List<DbColumnInfo>();
       var listValues = new StringList();
       var cmdName = ModelConfig.NamingPolicy.ConstructDbCommandName(entityCommand, table.TableName, "Insert");
       var dbCmdInfo = CreateDbCommandInfo(entityCommand, cmdName, table, DbExecutionType.NonQuery, null);
       foreach (var prm in dbCmdInfo.Parameters) {
     var col = prm.SourceColumn;
     if (!col.Flags.IsSet(DbColumnFlags.NoInsert)) {
       listColumns.Add(col);
       listValues.Add(prm.Name);
     }
     // identity
     if (col.Flags.IsSet(DbColumnFlags.Identity)) {
       if (useStoredProc)
     //append to stored proc
     idClause = string.Format("SET {0} = LAST_INSERT_ID();", prm.Name);
       else
     dbCmdInfo.PostUpdateActions.Add((conn, cmd, rec) => {
       var idCmd = conn.DbConnection.CreateCommand();
       idCmd.CommandText = "Select LAST_INSERT_ID();";
       idCmd.Transaction = conn.DbTransaction;
       var id = conn.Database.ExecuteDbCommand(idCmd, conn, DbExecutionType.Scalar); //it is decimal
       var intId = Convert.ChangeType(id, prm.SourceColumn.Member.DataType);
       rec.SetValueDirect(prm.SourceColumn.Member, intId);
     });
     }//if identity
       }
      // this.ModelConfig.Options.IsSet(DbOptions.us)
       //build SQL
       var strColumns = listColumns.GetSqlNameList();
       var strValues = string.Join(", ", listValues);
       dbCmdInfo.Sql = string.Format(SqlInsertTemplate, table.FullName, strColumns, strValues, idClause);
       return dbCmdInfo;
 }