public static DBCommand Build(DBTable table, string procName, DBCommandTypes type, IEnumerable <DBColumn> columns = null) { if (table.PrimaryKey == null && (type == DBCommandTypes.Delete || type == DBCommandTypes.Update)) { return(null); } if (columns == null) { columns = table.Columns; } string commandText = procName; string prefix = table.Schema.System.ParameterPrefix; CommandType ctype = CommandType.StoredProcedure; if (string.IsNullOrEmpty(procName)) { ctype = CommandType.Text; commandText = table.Schema.System.FormatCommand(table, type, null, columns); } var command = new DBCommand(commandText, ctype); if (type == DBCommandTypes.Delete) { command.Parameters.Add(new DBCommandParameter(table.PrimaryKey, prefix) { Direction = ParameterDirection.Input }); } else { DBCommandParameter paramId = null; if (table.PrimaryKey != null) { paramId = new DBCommandParameter(table.PrimaryKey, prefix) { Direction = ParameterDirection.Input }; if (type == DBCommandTypes.Insert || type == DBCommandTypes.InsertSequence) { command.Parameters.Add(paramId); } //else if (type == DBCommandTypes.InsertSequence) //{ //command.Parameters.Add(paramId); //paramId.Direction = ParameterDirection.InputOutput; //} } foreach (var column in columns) { if (column.ColumnType == DBColumnTypes.Default && column != table.PrimaryKey) { var prm = new DBCommandParameter(column, prefix); if (ctype == CommandType.Text) { prm.Direction = ParameterDirection.Input; } command.Parameters.Add(prm); } } if (paramId != null && type == DBCommandTypes.Update) { command.Parameters.Add(paramId); } } return(command); }
public List <T> Load(IDbCommand command, DBLoadParam param = DBLoadParam.None, DBTransaction baseTransaction = null) { var list = new List <T>(); var transaction = baseTransaction ?? new DBTransaction(Connection, null, true); try { transaction.AddCommand(command); if ((param & DBLoadParam.Reference) == DBLoadParam.Reference) { LoadReferenceBlock(command, transaction); } if ((param & DBLoadParam.Referencing) == DBLoadParam.Referencing) { LoadReferencingBlock(command, transaction); } if (transaction.Canceled) { return(list); } var whereInd = command.CommandText.IndexOf("where ", StringComparison.OrdinalIgnoreCase); var arg = new DBLoadProgressEventArgs(transaction.View, 0, 0, null); if (transaction.View != null && transaction.View.Table == this && transaction.View.IsStatic) { transaction.View.Clear(); } if ((transaction.ReaderParam & DBLoadParam.GetCount) == DBLoadParam.GetCount) { string w = whereInd == -1 ? string.Empty : command.CommandText.Substring(whereInd); var val = transaction.ExecuteQuery(transaction.AddCommand(DBCommand.CloneCommand(command, BuildQuery(w, "a", null, "count(*)"))), DBExecuteType.Scalar); arg.TotalCount = val is Exception ? -1 : int.Parse(val.ToString()); if (arg.TotalCount < 0 || arg.TotalCount == 0) { return(list); } if (items.Capacity < arg.TotalCount) { items.Capacity = arg.TotalCount; } //arg.TotalCount = Rows._items.Capacity; } //var buffer = new List<T>(arg.TotalCount == 0 ? 1 : arg.TotalCount); if (transaction.Canceled) { return(list); } using (transaction.Reader = transaction.ExecuteQuery(command, DBExecuteType.Reader) as IDataReader) { CheckColumns(transaction); while (!transaction.Canceled && transaction.Reader.Read()) { T row = null; lock (Lock) { row = LoadFromReader(transaction); if (!row.Attached && (transaction.ReaderParam & DBLoadParam.NoAttach) != DBLoadParam.NoAttach) { Add(row); } } if (arg.TotalCount > 0) { arg.Current++; arg.CurrentRow = row; RaiseLoadProgress(arg); } list.Add(row); if (transaction.View?.Table == this && transaction.View.IsStatic) { transaction.View.Add(row); } } } transaction.Reader = null; //Check IsSynchronized if (transaction.View?.Table == this) { transaction.View.IsSynchronized = true; } if (whereInd < 0) { IsSynchronized = true; } //return buffer; } finally { transaction.Reader = null; RaiseLoadCompleate(transaction); if (baseTransaction == null) { transaction.Dispose(); } } return(list); }