// Rodrigo Moya (rodrigo@ximian.com)
// Tim Coleman (tim@timcoleman.com)
// Sureshkumar T <tsureshkumar@novell.com>
+// Veerapuram Varadhan <vvaradhan@novell.com>
//
// (C) Ximian, Inc
// Copyright (C) Tim Coleman, 2002-2003
//
//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004, 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
using System.Collections;
using System.ComponentModel;
using System.Data;
+using System.Reflection;
using System.Runtime.InteropServices;
-namespace System.Data.Common {
-#if NET_2_0
+namespace System.Data.Common
+{
public abstract class DbDataAdapter : DataAdapter, IDbDataAdapter, IDataAdapter, ICloneable
-#else
- public abstract class DbDataAdapter : DataAdapter, ICloneable
-#endif
{
#region Fields
public const string DefaultSourceTableName = "Table";
const string DefaultSourceColumnName = "Column";
+ CommandBehavior _behavior = CommandBehavior.Default;
-#if NET_2_0
IDbCommand _selectCommand;
- internal IDbCommand _updateCommand;
- internal IDbCommand _deleteCommand;
- internal IDbCommand _insertCommand;
-#endif
+ IDbCommand _updateCommand;
+ IDbCommand _deleteCommand;
+ IDbCommand _insertCommand;
#endregion // Fields
#region Constructors
- protected DbDataAdapter()
+ protected DbDataAdapter ()
{
}
- protected DbDataAdapter(DbDataAdapter adapter) : base(adapter)
+ protected DbDataAdapter (DbDataAdapter adapter) : base (adapter)
{
}
#region Properties
-#if NET_2_0
protected internal CommandBehavior FillCommandBehavior {
- get { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
+ get { return _behavior; }
+ set { _behavior = value; }
}
IDbCommand IDbDataAdapter.SelectCommand {
- get { return _selectCommand; }
- set { _selectCommand = value; }
+ get { return ((DbDataAdapter)this).SelectCommand; }
+ set { ((DbDataAdapter)this).SelectCommand = (DbCommand)value; }
}
IDbCommand IDbDataAdapter.UpdateCommand{
- get { return _updateCommand; }
- set { _updateCommand = value; }
+ get { return ((DbDataAdapter)this).UpdateCommand; }
+ set { ((DbDataAdapter)this).UpdateCommand = (DbCommand)value; }
}
-
+
IDbCommand IDbDataAdapter.DeleteCommand{
- get { return _deleteCommand; }
- set { _deleteCommand = value; }
+ get { return ((DbDataAdapter)this).DeleteCommand; }
+ set { ((DbDataAdapter)this).DeleteCommand = (DbCommand)value; }
}
IDbCommand IDbDataAdapter.InsertCommand{
- get { return _insertCommand; }
- set { _insertCommand = value; }
+ get { return ((DbDataAdapter)this).InsertCommand; }
+ set { ((DbDataAdapter)this).InsertCommand = (DbCommand)value; }
}
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public DbCommand SelectCommand {
- get { return (DbCommand) ((IDbDataAdapter) this).SelectCommand; }
- set { ((IDbDataAdapter) this).SelectCommand = value; }
+ get {
+ return (DbCommand) _selectCommand;
+ //return (DbCommand) ((IDbDataAdapter)this).SelectCommand;
+ }
+ set {
+ if (_selectCommand != value) {
+ _selectCommand = value;
+ ((IDbDataAdapter)this).SelectCommand = value;
+ }
+ }
}
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public DbCommand DeleteCommand {
- get { return (DbCommand) ((IDbDataAdapter) this).DeleteCommand; }
- set { ((IDbDataAdapter) this).DeleteCommand = value; }
+ get {
+ return (DbCommand) _deleteCommand;
+ //return (DbCommand) ((IDbDataAdapter)this).DeleteCommand;
+ }
+ set {
+ if (_deleteCommand != value) {
+ _deleteCommand = value;
+ ((IDbDataAdapter)this).DeleteCommand = value;
+ }
+ }
}
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public DbCommand InsertCommand {
- get { return (DbCommand) ((IDbDataAdapter) this).InsertCommand; }
- set { ((IDbDataAdapter) this).InsertCommand = value; }
+ get {
+ return (DbCommand) _insertCommand;
+ //return (DbCommand) ((IDbDataAdapter)this).InsertCommand;
+ }
+ set {
+ if (_insertCommand != value) {
+ _insertCommand = value;
+ ((IDbDataAdapter)this).InsertCommand = value;
+ }
+ }
}
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public DbCommand UpdateCommand {
- get { return (DbCommand) ((IDbDataAdapter) this).UpdateCommand; }
- set { ((IDbDataAdapter) this).UpdateCommand = value; }
+ get {
+ return (DbCommand) _updateCommand;
+ //return (DbCommand) ((IDbDataAdapter)this).DeleteCommand;
+ }
+ set {
+ if (_updateCommand != value) {
+ _updateCommand = value;
+ ((IDbDataAdapter)this).UpdateCommand = value;
+ }
+ }
}
- [MonoTODO]
[DefaultValue (1)]
public virtual int UpdateBatchSize {
get { return 1; }
- set { throw new NotSupportedException (); }
- }
-#else
- IDbCommand SelectCommand {
- get { return ((IDbDataAdapter) this).SelectCommand; }
- }
-
- IDbCommand UpdateCommand {
- get { return ((IDbDataAdapter) this).UpdateCommand; }
- }
-
- IDbCommand DeleteCommand {
- get { return ((IDbDataAdapter) this).DeleteCommand; }
+ set {
+ if (value != 1)
+ throw new NotSupportedException ();
+ }
}
- IDbCommand InsertCommand {
- get { return ((IDbDataAdapter) this).InsertCommand; }
- }
-#endif
-
#endregion // Properties
- #region Events
-
-#if ONLY_1_0 || ONLY_1_1
-
- [DataCategory ("Fill")]
- [DataSysDescription ("Event triggered when a recoverable error occurs during Fill.")]
- public event FillErrorEventHandler FillError;
-
-#endif
- #endregion // Events
-
#region Methods
-#if NET_2_0
protected virtual RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
StatementType statementType,
DataTableMapping tableMapping)
return new RowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
}
- [MonoTODO]
protected virtual void OnRowUpdated (RowUpdatedEventArgs value)
{
- throw new NotImplementedException ();
+ if (Events ["RowUpdated"] != null) {
+ Delegate [] rowUpdatedList = Events ["RowUpdated"].GetInvocationList ();
+ foreach (Delegate rowUpdated in rowUpdatedList) {
+ MethodInfo rowUpdatedMethod = rowUpdated.Method;
+ rowUpdatedMethod.Invoke (value, null);
+ }
+ }
}
- [MonoTODO]
protected virtual void OnRowUpdating (RowUpdatingEventArgs value)
{
- throw new NotImplementedException ();
- }
-#else
- protected abstract RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
- StatementType statementType,
- DataTableMapping tableMapping);
-
- protected abstract RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command,
- StatementType statementType,
- DataTableMapping tableMapping);
-
- protected abstract void OnRowUpdated (RowUpdatedEventArgs value);
- protected abstract void OnRowUpdating (RowUpdatingEventArgs value);
-#endif
-
-
- private FillErrorEventArgs CreateFillErrorEvent (DataTable dataTable, object[] values, Exception e)
- {
- FillErrorEventArgs args = new FillErrorEventArgs (dataTable, values);
- args.Errors = e;
- args.Continue = false;
- return args;
+ if (Events ["RowUpdating"] != null) {
+ Delegate [] rowUpdatingList = Events ["RowUpdating"].GetInvocationList ();
+ foreach (Delegate rowUpdating in rowUpdatingList) {
+ MethodInfo rowUpdatingMethod = rowUpdating.Method;
+ rowUpdatingMethod.Invoke (value, null);
+ }
+ }
}
protected override void Dispose (bool disposing)
public override int Fill (DataSet dataSet)
{
- return Fill (dataSet, 0, 0, DefaultSourceTableName, ((IDbDataAdapter) this).SelectCommand, CommandBehavior.Default);
+ return Fill (dataSet, 0, 0, DefaultSourceTableName, ((IDbDataAdapter) this).SelectCommand, _behavior);
}
- public int Fill (DataTable dataTable)
+ public int Fill (DataTable dataTable)
{
if (dataTable == null)
throw new ArgumentNullException ("DataTable");
- return Fill (dataTable, ((IDbDataAdapter) this).SelectCommand, CommandBehavior.Default);
+ return Fill (dataTable, ((IDbDataAdapter) this).SelectCommand, _behavior);
}
- public int Fill (DataSet dataSet, string srcTable)
+ public int Fill (DataSet dataSet, string srcTable)
{
- return Fill (dataSet, 0, 0, srcTable, ((IDbDataAdapter) this).SelectCommand, CommandBehavior.Default);
+ return Fill (dataSet, 0, 0, srcTable, ((IDbDataAdapter) this).SelectCommand, _behavior);
}
-#if NET_2_0
- [MonoTODO ("This needs to be moved to DataAdapter.For now, just override")]
- protected override
-#else
- protected virtual
-#endif
- int Fill (DataTable dataTable, IDataReader dataReader)
- {
- if (dataReader.FieldCount == 0) {
- dataReader.Close ();
- return 0;
- }
-
- int count = 0;
- try {
- string tableName = SetupSchema (SchemaType.Mapped, dataTable.TableName);
- if (tableName != null) {
- dataTable.TableName = tableName;
- FillTable (dataTable, dataReader, 0, 0, ref count);
- }
- } finally {
- dataReader.Close ();
- }
-
- return count;
- }
-
- protected virtual int Fill (DataTable dataTable, IDbCommand command, CommandBehavior behavior)
+ protected virtual int Fill (DataTable dataTable, IDbCommand command, CommandBehavior behavior)
{
CommandBehavior commandBehavior = behavior;
// first see that the connection is not close.
- if (command.Connection.State == ConnectionState.Closed)
- {
+ if (command.Connection.State == ConnectionState.Closed) {
command.Connection.Open ();
commandBehavior |= CommandBehavior.CloseConnection;
}
return Fill (dataTable, command.ExecuteReader (commandBehavior));
}
- public int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable)
+ public int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable)
{
- return this.Fill (dataSet, startRecord, maxRecords, srcTable, ((IDbDataAdapter) this).SelectCommand, CommandBehavior.Default);
+ return this.Fill (dataSet, startRecord, maxRecords, srcTable, ((IDbDataAdapter) this).SelectCommand, _behavior);
}
-#if NET_2_0
[MonoTODO]
- public int Fill (int startRecord, int maxRecords, DataTable[] dataTables)
+ public int Fill (int startRecord, int maxRecords, params DataTable[] dataTables)
{
throw new NotImplementedException ();
}
{
throw new NotImplementedException ();
}
-#endif
-
-
-#if NET_2_0
- [MonoTODO ("This needs to be moved to DataAdapter.For now, just override")]
- protected override
-#else
- protected virtual
-#endif
- int Fill (DataSet dataSet, string srcTable, IDataReader dataReader, int startRecord, int maxRecords)
- {
- if (dataSet == null)
- throw new ArgumentNullException ("DataSet");
-
- if (startRecord < 0)
- throw new ArgumentException ("The startRecord parameter was less than 0.");
- if (maxRecords < 0)
- throw new ArgumentException ("The maxRecords parameter was less than 0.");
-
- DataTable dataTable = null;
- int resultIndex = 0;
- int count = 0;
-
- try {
- string tableName = srcTable;
- do {
- // Non-resultset queries like insert, delete or update aren't processed.
- if (dataReader.FieldCount != -1)
- {
- tableName = SetupSchema (SchemaType.Mapped, tableName);
- if (tableName != null) {
-
- // check if the table exists in the dataset
- if (dataSet.Tables.Contains (tableName))
- // get the table from the dataset
- dataTable = dataSet.Tables [tableName];
- else {
- // Do not create schema if MissingSchemAction is set to Ignore
- if (this.MissingSchemaAction == MissingSchemaAction.Ignore)
- continue;
- dataTable = dataSet.Tables.Add (tableName);
- }
-
- if (!FillTable (dataTable, dataReader, startRecord, maxRecords, ref count)) {
- continue;
- }
-
- tableName = String.Format ("{0}{1}", srcTable, ++resultIndex);
-
- startRecord = 0;
- maxRecords = 0;
- }
- }
- } while (dataReader.NextResult ());
- }
- finally {
- dataReader.Close ();
- }
- return count;
- }
-
- protected virtual int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior)
+ protected virtual int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior)
{
+ if (command.Connection == null)
+ throw new InvalidOperationException ("Connection state is closed");
+
if (MissingSchemaAction == MissingSchemaAction.AddWithKey)
behavior |= CommandBehavior.KeyInfo;
CommandBehavior commandBehavior = behavior;
command.Connection.Open ();
commandBehavior |= CommandBehavior.CloseConnection;
}
- return Fill (dataSet, srcTable, command.ExecuteReader (commandBehavior), startRecord, maxRecords);
- }
-
- private bool FillTable (DataTable dataTable, IDataReader dataReader, int startRecord, int maxRecords, ref int counter)
- {
- if (dataReader.FieldCount == 0)
- return false;
-
- int counterStart = counter;
-
- int[] mapping = BuildSchema (dataReader, dataTable, SchemaType.Mapped);
-
- int[] sortedMapping = new int[mapping.Length];
- int length = sortedMapping.Length;
- for(int i=0; i < sortedMapping.Length; i++) {
- if (mapping[i] >= 0)
- sortedMapping[mapping[i]] = i;
- else
- sortedMapping[--length] = i;
- }
-
- for (int i = 0; i < startRecord; i++) {
- dataReader.Read ();
- }
-
- dataTable.BeginLoadData ();
- while (dataReader.Read () && (maxRecords == 0 || (counter - counterStart) < maxRecords)) {
- try {
- dataTable.LoadDataRow (dataReader, sortedMapping, length, AcceptChangesDuringFill);
- counter++;
- }
- catch (Exception e) {
- object[] readerArray = new object[dataReader.FieldCount];
- object[] tableArray = new object[mapping.Length];
- // we get the values from the datareader
- dataReader.GetValues (readerArray);
- // copy from datareader columns to table columns according to given mapping
- for (int i = 0; i < mapping.Length; i++) {
- if (mapping[i] >= 0) {
- tableArray[i] = readerArray[mapping[i]];
- }
- }
- FillErrorEventArgs args = CreateFillErrorEvent (dataTable, tableArray, e);
- OnFillError (args);
-
- // if args.Continue is not set to true or if a handler is not set, rethrow the error..
- if(!args.Continue)
- throw e;
- }
- }
- dataTable.EndLoadData ();
- return true;
+ return Fill (dataSet, srcTable, command.ExecuteReader (commandBehavior),
+ startRecord, maxRecords);
}
-#if NET_2_0
/// <summary>
- /// Fills the given datatable using values from reader. if a value
- /// for a column is null, that will be filled with default value.
+ /// Fills the given datatable using values from reader. if a value
+ /// for a column is null, that will be filled with default value.
/// </summary>
/// <returns>No. of rows affected </returns>
internal static int FillFromReader (DataTable table,
IDataReader reader,
- int start,
+ int start,
int length,
int [] mapping,
LoadOption loadOption
)
- {
- if (reader.FieldCount == 0)
+ {
+ if (reader.FieldCount == 0)
return 0 ;
- for (int i = 0; i < start; i++)
- reader.Read ();
-
- int counter = 0;
- object [] values = new object [mapping.Length];
- while (reader.Read () &&
- (length == 0 || counter < length)) {
-
- for (int i = 0 ; i < mapping.Length; i++)
- values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
-
- table.BeginLoadData ();
- table.LoadDataRow (values, loadOption);
- table.EndLoadData ();
- counter++;
- }
- return counter;
- }
+ for (int i = 0; i < start; i++)
+ reader.Read ();
+
+ int counter = 0;
+ object [] values = new object [mapping.Length];
+ while (reader.Read () && (length == 0 || counter < length)) {
+ for (int i = 0 ; i < mapping.Length; i++)
+ values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
+ table.BeginLoadData ();
+ table.LoadDataRow (values, loadOption);
+ table.EndLoadData ();
+ counter++;
+ }
+ return counter;
+ }
internal static int FillFromReader (DataTable table,
IDataReader reader,
- int start,
+ int start,
int length,
int [] mapping,
LoadOption loadOption,
FillErrorEventHandler errorHandler)
- {
- if (reader.FieldCount == 0)
+ {
+ if (reader.FieldCount == 0)
return 0 ;
- for (int i = 0; i < start; i++)
- reader.Read ();
-
- int counter = 0;
- object [] values = new object [mapping.Length];
- while (reader.Read () &&
- (length == 0 || counter < length)) {
-
- for (int i = 0 ; i < mapping.Length; i++)
- values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
-
- table.BeginLoadData ();
+ for (int i = 0; i < start; i++)
+ reader.Read ();
+
+ int counter = 0;
+ object [] values = new object [mapping.Length];
+ while (reader.Read () && (length == 0 || counter < length)) {
+ for (int i = 0 ; i < mapping.Length; i++)
+ values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
+ table.BeginLoadData ();
try {
table.LoadDataRow (values, loadOption);
} catch (Exception e) {
if(!args.Continue)
throw e;
}
- table.EndLoadData ();
- counter++;
- }
- return counter;
- }
-
-#endif // NET_2_0
+ table.EndLoadData ();
+ counter++;
+ }
+ return counter;
+ }
- public override DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType)
+ public override DataTable [] FillSchema (DataSet dataSet, SchemaType schemaType)
{
- return FillSchema (dataSet, schemaType, ((IDbDataAdapter) this).SelectCommand, DefaultSourceTableName, CommandBehavior.Default);
+ return FillSchema (dataSet, schemaType, ((IDbDataAdapter) this).SelectCommand, DefaultSourceTableName, _behavior);
}
- public DataTable FillSchema (DataTable dataTable, SchemaType schemaType)
+ public DataTable FillSchema (DataTable dataTable, SchemaType schemaType)
{
- return FillSchema (dataTable, schemaType, ((IDbDataAdapter) this).SelectCommand, CommandBehavior.Default);
+ return FillSchema (dataTable, schemaType, ((IDbDataAdapter) this).SelectCommand, _behavior);
}
- public DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, string srcTable)
+ public DataTable [] FillSchema (DataSet dataSet, SchemaType schemaType, string srcTable)
{
- return FillSchema (dataSet, schemaType, ((IDbDataAdapter) this).SelectCommand, srcTable, CommandBehavior.Default);
+ return FillSchema (dataSet, schemaType, ((IDbDataAdapter) this).SelectCommand, srcTable, _behavior);
}
- protected virtual DataTable FillSchema (DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior)
+ protected virtual DataTable FillSchema (DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior)
{
if (dataTable == null)
throw new ArgumentNullException ("DataTable");
}
IDataReader reader = command.ExecuteReader (behavior);
- try
- {
+ try {
string tableName = SetupSchema (schemaType, dataTable.TableName);
- if (tableName != null)
- {
+ if (tableName != null) {
// FillSchema should add the KeyInfo unless MissingSchemaAction
// is set to Ignore or Error.
MissingSchemaAction schemaAction = MissingSchemaAction;
BuildSchema (reader, dataTable, schemaType, schemaAction,
MissingMappingAction, TableMappings);
}
- }
- finally
- {
+ } finally {
reader.Close ();
}
return dataTable;
}
- protected virtual DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior)
+ protected virtual DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior)
{
if (dataSet == null)
throw new ArgumentNullException ("DataSet");
string tableName = srcTable;
int index = 0;
DataTable table;
- try
- {
+ try {
// FillSchema should add the KeyInfo unless MissingSchemaAction
// is set to Ignore or Error.
MissingSchemaAction schemaAction = MissingSchemaAction;
do {
tableName = SetupSchema (schemaType, tableName);
- if (tableName != null)
- {
+ if (tableName != null) {
if (dataSet.Tables.Contains (tableName))
- table = dataSet.Tables [tableName];
- else
- {
+ table = dataSet.Tables [tableName];
+ else {
// Do not create schema if MissingSchemAction is set to Ignore
if (this.MissingSchemaAction == MissingSchemaAction.Ignore)
continue;
tableName = String.Format ("{0}{1}", srcTable, ++index);
}
}while (reader.NextResult ());
- }
- finally
- {
+ } finally {
reader.Close ();
}
- return (DataTable[]) output.ToArray (typeof (DataTable));
- }
-
- private string SetupSchema (SchemaType schemaType, string sourceTableName)
- {
- DataTableMapping tableMapping = null;
-
- if (schemaType == SchemaType.Mapped)
- {
- tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, sourceTableName, sourceTableName, MissingMappingAction);
- if (tableMapping != null)
- return tableMapping.DataSetTable;
- return null;
- }
- else
- return sourceTableName;
+ return (DataTable []) output.ToArray (typeof (DataTable));
}
[EditorBrowsable (EditorBrowsableState.Advanced)]
- public override IDataParameter[] GetFillParameters ()
+ public override IDataParameter[] GetFillParameters ()
{
- IDataParameter[] parameters = new IDataParameter [SelectCommand.Parameters.Count];
- SelectCommand.Parameters.CopyTo (parameters, 0);
+ IDbCommand selectCmd = ((IDbDataAdapter) this).SelectCommand;
+ IDataParameter[] parameters = new IDataParameter [selectCmd.Parameters.Count];
+ selectCmd.Parameters.CopyTo (parameters, 0);
return parameters;
}
- // this method builds the schema for a given datatable. it returns a int array with
- // "array[ordinal of datatable column] == index of source column in data reader".
- // each column in the datatable has a mapping to a specific column in the datareader,
- // the int array represents this match.
- private int[] BuildSchema (IDataReader reader, DataTable table, SchemaType schemaType)
- {
- return BuildSchema (reader, table, schemaType, MissingSchemaAction,
- MissingMappingAction, TableMappings);
- }
-
- /// <summary>
- /// Creates or Modifies the schema of the given DataTable based on the schema of
- /// the reader and the arguments passed.
- /// </summary>
- internal static int[] BuildSchema (IDataReader reader,
- DataTable table,
- SchemaType schemaType,
- MissingSchemaAction missingSchAction,
- MissingMappingAction missingMapAction,
- DataTableMappingCollection dtMapping
- )
- {
- int readerIndex = 0;
- // FIXME : this fails if query has fewer columns than a table
- int[] mapping = new int[table.Columns.Count]; // mapping the reader indexes to the datatable indexes
-
- for(int i=0; i < mapping.Length; i++) {
- mapping[i] = -1;
- }
-
- ArrayList primaryKey = new ArrayList ();
- ArrayList sourceColumns = new ArrayList ();
- bool createPrimaryKey = true;
-
- DataTable schemaTable = reader.GetSchemaTable ();
-
- DataColumn ColumnNameCol = schemaTable.Columns["ColumnName"];
- DataColumn DataTypeCol = schemaTable.Columns["DataType"];
- DataColumn IsAutoIncrementCol = schemaTable.Columns["IsAutoIncrement"];
- DataColumn AllowDBNullCol = schemaTable.Columns["AllowDBNull"];
- DataColumn IsReadOnlyCol = schemaTable.Columns["IsReadOnly"];
- DataColumn IsKeyCol = schemaTable.Columns["IsKey"];
- DataColumn IsUniqueCol = schemaTable.Columns["IsUnique"];
- DataColumn ColumnSizeCol = schemaTable.Columns["ColumnSize"];
-
- foreach (DataRow schemaRow in schemaTable.Rows) {
- // generate a unique column name in the source table.
- string sourceColumnName;
- string realSourceColumnName ;
- if (ColumnNameCol == null || schemaRow.IsNull(ColumnNameCol) || (string)schemaRow [ColumnNameCol] == String.Empty) {
- sourceColumnName = DefaultSourceColumnName;
- realSourceColumnName = DefaultSourceColumnName + "1";
- }
- else {
- sourceColumnName = (string) schemaRow [ColumnNameCol];
- realSourceColumnName = sourceColumnName;
- }
-
- for (int i = 1; sourceColumns.Contains (realSourceColumnName); i += 1)
- realSourceColumnName = String.Format ("{0}{1}", sourceColumnName, i);
- sourceColumns.Add(realSourceColumnName);
-
- // generate DataSetColumnName from DataTableMapping, if any
- string dsColumnName = realSourceColumnName;
- DataTableMapping tableMapping = null;
-
- //FIXME : The sourcetable name shud get passed as a parameter..
- int index = dtMapping.IndexOfDataSetTable (table.TableName);
- string srcTable = (index != -1 ? dtMapping[index].SourceTable : table.TableName);
- tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (dtMapping, srcTable, table.TableName, missingMapAction);
- if (tableMapping != null)
- {
- table.TableName = tableMapping.DataSetTable;
- // check to see if the column mapping exists
- DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, realSourceColumnName, missingMapAction);
- if (columnMapping != null)
- {
- Type columnType = (Type)schemaRow[DataTypeCol];
- DataColumn col =
- columnMapping.GetDataColumnBySchemaAction(
- table ,
- columnType,
- missingSchAction);
-
- if (col != null)
- {
- // if the column is not in the table - add it.
- if (table.Columns.IndexOf(col) == -1)
- {
- if (missingSchAction == MissingSchemaAction.Add
- || missingSchAction == MissingSchemaAction.AddWithKey)
- table.Columns.Add(col);
-
- int[] tmp = new int[mapping.Length + 1];
- Array.Copy(mapping,0,tmp,0,col.Ordinal);
- Array.Copy(mapping,col.Ordinal,tmp,col.Ordinal + 1,mapping.Length - col.Ordinal);
- mapping = tmp;
- }
-
-
- if (missingSchAction == MissingSchemaAction.AddWithKey) {
-
- object value = (AllowDBNullCol != null) ? schemaRow[AllowDBNullCol] : null;
- bool allowDBNull = value is bool ? (bool)value : true;
-
- value = (IsKeyCol != null) ? schemaRow[IsKeyCol] : null;
- bool isKey = value is bool ? (bool)value : false;
-
- value = (IsAutoIncrementCol != null) ? schemaRow[IsAutoIncrementCol] : null;
- bool isAutoIncrement = value is bool ? (bool)value : false;
-
- value = (IsReadOnlyCol != null) ? schemaRow[IsReadOnlyCol] : null;
- bool isReadOnly = value is bool ? (bool)value : false;
-
- value = (IsUniqueCol != null) ? schemaRow[IsUniqueCol] : null;
- bool isUnique = value is bool ? (bool)value : false;
-
- col.AllowDBNull = allowDBNull;
- // fill woth key info
- if (isAutoIncrement && DataColumn.CanAutoIncrement(columnType)) {
- col.AutoIncrement = true;
- if (!allowDBNull)
- col.AllowDBNull = false;
- }
-
- if (columnType == DbTypes.TypeOfString) {
- col.MaxLength = (ColumnSizeCol != null) ? (int)schemaRow[ColumnSizeCol] : 0;
- }
-
- if (isReadOnly)
- col.ReadOnly = true;
-
- if (!allowDBNull && (!isReadOnly || isKey))
- col.AllowDBNull = false;
- if (isUnique && !isKey && !columnType.IsArray) {
- col.Unique = true;
- if (!allowDBNull)
- col.AllowDBNull = false;
- }
-
- // This might not be set by all DataProviders
- bool isHidden = false;
- if (schemaTable.Columns.Contains ("IsHidden")) {
- value = schemaRow["IsHidden"];
- isHidden = ((value is bool) ? (bool)value : false);
- }
-
- if (isKey && !isHidden) {
- primaryKey.Add (col);
- if (allowDBNull)
- createPrimaryKey = false;
- }
- }
- // add the ordinal of the column as a key and the index of the column in the datareader as a value.
- mapping[col.Ordinal] = readerIndex++;
- }
- }
- }
- }
- if (primaryKey.Count > 0) {
- DataColumn[] colKey = (DataColumn[])(primaryKey.ToArray(typeof (DataColumn)));
- if (createPrimaryKey)
- table.PrimaryKey = colKey;
- else {
- UniqueConstraint uConstraint = new UniqueConstraint(colKey);
- for (int i = 0; i < table.Constraints.Count; i++) {
- if (table.Constraints[i].Equals(uConstraint)) {
- uConstraint = null;
- break;
- }
- }
-
- if (uConstraint != null)
- table.Constraints.Add(uConstraint);
- }
- }
- return mapping;
- }
-
[MonoTODO]
+ [Obsolete ("use 'protected DbDataAdapter(DbDataAdapter)' ctor")]
object ICloneable.Clone ()
{
throw new NotImplementedException ();
}
- public int Update (DataRow[] dataRows)
+ public int Update (DataRow [] dataRows)
{
if (dataRows == null)
throw new ArgumentNullException("dataRows");
if (dataRows.Length == 0)
return 0;
- if (dataRows[0] == null)
+ if (dataRows [0] == null)
throw new ArgumentException("dataRows[0].");
- DataTable table = dataRows[0].Table;
+ DataTable table = dataRows [0].Table;
if (table == null)
throw new ArgumentException("table is null reference.");
// all rows must be in the same table
- for (int i = 0; i < dataRows.Length; i++)
- {
- if (dataRows[i] == null)
- throw new ArgumentException("dataRows[" + i + "].");
- if (dataRows[i].Table != table)
+ for (int i = 0; i < dataRows.Length; i++) {
+ if (dataRows [i] == null)
+ throw new ArgumentException ("dataRows[" + i + "].");
+ if (dataRows [i].Table != table)
throw new ArgumentException(
" DataRow["
+ i
// get table mapping for this rows
DataTableMapping tableMapping = TableMappings.GetByDataSetTable(table.TableName);
- if (tableMapping == null)
- {
+ if (tableMapping == null) {
tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(
TableMappings,
table.TableName,
}
}
- DataRow[] copy = table.NewRowArray(dataRows.Length);
- Array.Copy(dataRows, 0, copy, 0, dataRows.Length);
- return Update(copy, tableMapping);
+ DataRow[] copy = table.NewRowArray (dataRows.Length);
+ Array.Copy (dataRows, 0, copy, 0, dataRows.Length);
+ return Update (copy, tableMapping);
}
- public override int Update (DataSet dataSet)
+ public override int Update (DataSet dataSet)
{
return Update (dataSet, DefaultSourceTableName);
}
- public int Update (DataTable dataTable)
+ public int Update (DataTable dataTable)
{
/*
int index = TableMappings.IndexOfDataSetTable (dataTable.TableName);
return Update (dataTable, TableMappings [index]);
*/
DataTableMapping tableMapping = TableMappings.GetByDataSetTable (dataTable.TableName);
- if (tableMapping == null)
- {
+ if (tableMapping == null) {
tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (
TableMappings,
dataTable.TableName,
private int Update (DataTable dataTable, DataTableMapping tableMapping)
{
- DataRow[] rows = dataTable.NewRowArray(dataTable.Rows.Count);
+ DataRow [] rows = dataTable.NewRowArray(dataTable.Rows.Count);
dataTable.Rows.CopyTo (rows, 0);
return Update (rows, tableMapping);
}
- protected virtual int Update (DataRow[] dataRows, DataTableMapping tableMapping)
+ protected virtual int Update (DataRow [] dataRows, DataTableMapping tableMapping)
{
int updateCount = 0;
foreach (DataRow row in dataRows) {
}
RowUpdatingEventArgs argsUpdating = CreateRowUpdatingEvent (row, command, statementType, tableMapping);
- row.RowError = null;
- OnRowUpdating(argsUpdating);
- switch(argsUpdating.Status) {
+ row.RowError = String.Empty;
+ OnRowUpdating (argsUpdating);
+ switch (argsUpdating.Status) {
case UpdateStatus.Continue :
//continue in update operation
break;
case UpdateStatus.ErrorsOccurred :
- if (argsUpdating.Errors == null) {
+ if (argsUpdating.Errors == null)
argsUpdating.Errors = ExceptionHelper.RowUpdatedError();
- }
row.RowError += argsUpdating.Errors.Message;
- if (!ContinueUpdateOnError) {
+ if (!ContinueUpdateOnError)
throw argsUpdating.Errors;
- }
continue;
case UpdateStatus.SkipAllRemainingRows :
return updateCount;
updateCount++;
continue;
default :
- throw ExceptionHelper.InvalidUpdateStatus(argsUpdating.Status);
+ throw ExceptionHelper.InvalidUpdateStatus (argsUpdating.Status);
}
- command = argsUpdating.Command;
+ command = argsUpdating.Command;
try {
if (command != null) {
DataColumnMappingCollection columnMappings = tableMapping.ColumnMappings;
- IDataParameter nullCheckParam = null;
foreach (IDataParameter parameter in command.Parameters) {
- if ((parameter.Direction & ParameterDirection.Input) != 0) {
- string dsColumnName = parameter.SourceColumn;
- if (columnMappings.Contains(parameter.SourceColumn))
- dsColumnName = columnMappings [parameter.SourceColumn].DataSetColumn;
- if (dsColumnName == null || dsColumnName.Length <= 0) {
- nullCheckParam = parameter;
- continue;
- }
+ if ((parameter.Direction & ParameterDirection.Input) == 0)
+ continue;
- DataRowVersion rowVersion = parameter.SourceVersion;
- // Parameter version is ignored for non-update commands
- if (statementType == StatementType.Delete)
- rowVersion = DataRowVersion.Original;
+ DataRowVersion rowVersion = parameter.SourceVersion;
+ // Parameter version is ignored for non-update commands
+ if (statementType == StatementType.Delete)
+ rowVersion = DataRowVersion.Original;
+ string dsColumnName = parameter.SourceColumn;
+ if (columnMappings.Contains(dsColumnName)) {
+ dsColumnName = columnMappings [dsColumnName].DataSetColumn;
parameter.Value = row [dsColumnName, rowVersion];
- if (nullCheckParam != null && (parameter.Value != null
- && parameter.Value != DBNull.Value)) {
+ } else {
+ parameter.Value = null;
+ }
+
+ DbParameter nullCheckParam = parameter as DbParameter;
+
+ if (nullCheckParam != null && nullCheckParam.SourceColumnNullMapping) {
+ if (parameter.Value != null && parameter.Value != DBNull.Value)
nullCheckParam.Value = 0;
- nullCheckParam = null;
- }
+ else
+ nullCheckParam.Value = 1;
+ nullCheckParam = null;
}
}
}
- }
- catch (Exception e) {
+ } catch (Exception e) {
argsUpdating.Errors = e;
argsUpdating.Status = UpdateStatus.ErrorsOccurred;
}
-
IDataReader reader = null;
- try {
- if (command == null) {
- throw ExceptionHelper.UpdateRequiresCommand(commandName);
- }
+ try {
+ if (command == null)
+ throw ExceptionHelper.UpdateRequiresCommand (commandName);
CommandBehavior commandBehavior = CommandBehavior.Default;
if (command.Connection.State == ConnectionState.Closed) {
row [dstColumnName] = reader [columnName];
} finally {
dstColumn.ReadOnly = readOnlyState;
- }
+ }
}
}
}
// if the execute does not effect any rows we throw an exception.
if (tmp == 0)
throw new DBConcurrencyException("Concurrency violation: the " +
- commandName +"Command affected 0 records.");
+ commandName +"Command affected 0 records.", null,
+ new DataRow [] { row });
updateCount += tmp;
-
+
if (command.UpdatedRowSource == UpdateRowSource.Both ||
command.UpdatedRowSource == UpdateRowSource.OutputParameters) {
// Update output parameters to row values
} finally {
dstColumn.ReadOnly = readOnlyState;
}
-
}
}
-
- RowUpdatedEventArgs updatedArgs = CreateRowUpdatedEvent(row, command, statementType, tableMapping);
- OnRowUpdated(updatedArgs);
- switch(updatedArgs.Status) {
+
+ RowUpdatedEventArgs updatedArgs = CreateRowUpdatedEvent (row, command, statementType, tableMapping);
+ OnRowUpdated (updatedArgs);
+ switch (updatedArgs.Status) {
case UpdateStatus.Continue:
break;
case UpdateStatus.ErrorsOccurred:
- if (updatedArgs.Errors == null) {
+ if (updatedArgs.Errors == null)
updatedArgs.Errors = ExceptionHelper.RowUpdatedError();
- }
row.RowError += updatedArgs.Errors.Message;
- if (!ContinueUpdateOnError) {
+ if (!ContinueUpdateOnError)
throw updatedArgs.Errors;
- }
break;
case UpdateStatus.SkipCurrentRow:
continue;
case UpdateStatus.SkipAllRemainingRows:
return updateCount;
}
-#if NET_2_0
if (!AcceptChangesDuringUpdate)
continue;
-#endif
row.AcceptChanges ();
- } catch(Exception e) {
+ } catch (Exception e) {
row.RowError = e.Message;
- if (!ContinueUpdateOnError) {
+ if (!ContinueUpdateOnError)
throw e;
- }
} finally {
- if (reader != null && ! reader.IsClosed) {
+ if (reader != null && ! reader.IsClosed)
reader.Close ();
- }
- }
- }
+ }
+ }
return updateCount;
}
- public int Update (DataSet dataSet, string sourceTable)
+ public int Update (DataSet dataSet, string srcTable)
{
MissingMappingAction mappingAction = MissingMappingAction;
if (mappingAction == MissingMappingAction.Ignore)
mappingAction = MissingMappingAction.Error;
- DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, sourceTable, sourceTable, mappingAction);
+ DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, srcTable, srcTable, mappingAction);
- DataTable dataTable = dataSet.Tables[tableMapping.DataSetTable];
+ DataTable dataTable = dataSet.Tables [tableMapping.DataSetTable];
if (dataTable == null)
throw new ArgumentException (String.Format ("Missing table {0}",
- sourceTable));
+ srcTable));
+
+ /** Copied from another Update function **/
+ if (tableMapping != null) {
+ foreach (DataColumn col in dataTable.Columns) {
+ if (tableMapping.ColumnMappings.IndexOf (col.ColumnName) >= 0)
+ continue;
+ DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction (tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction);
+ if (columnMapping == null)
+ columnMapping = new DataColumnMapping (col.ColumnName, col.ColumnName);
+ tableMapping.ColumnMappings.Add (columnMapping);
+ }
+ } else {
+ ArrayList cmc = new ArrayList ();
+ foreach (DataColumn col in dataTable.Columns)
+ cmc.Add (new DataColumnMapping (col.ColumnName, col.ColumnName));
+ tableMapping =
+ new DataTableMapping (
+ dataTable.TableName,
+ dataTable.TableName,
+ cmc.ToArray (typeof (DataColumnMapping)) as DataColumnMapping []);
+ }
+ /**end insert from another update**/
return Update (dataTable, tableMapping);
}
-
-#if NET_2_0
+
// All the batch methods, should be implemented, if supported,
- // by individual providers
+ // by individual providers
- protected virtual int AddToBatch (IDbCommand cmd)
+ protected virtual int AddToBatch (IDbCommand command)
{
- throw new NotSupportedException ();
+ throw CreateMethodNotSupportedException ();
}
protected virtual void ClearBatch ()
{
- throw new NotSupportedException ();
+ throw CreateMethodNotSupportedException ();
}
protected virtual int ExecuteBatch ()
{
- throw new NotSupportedException ();
+ throw CreateMethodNotSupportedException ();
}
- protected virtual IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIdentifer)
+ protected virtual IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
{
- throw new NotSupportedException ();
+ throw CreateMethodNotSupportedException ();
+ }
+
+ protected virtual bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
+ {
+ recordsAffected = 1;
+ error = null;
+ return true;
}
protected virtual void InitializeBatching ()
{
- throw new NotSupportedException ();
+ throw CreateMethodNotSupportedException ();
}
protected virtual void TerminateBatching ()
{
- throw new NotSupportedException ();
+ throw CreateMethodNotSupportedException ();
}
-#endif
-#if ONLY_1_0 || ONLY_1_1
- protected virtual void OnFillError (FillErrorEventArgs value)
+ Exception CreateMethodNotSupportedException ()
{
- if (FillError != null)
- FillError (this, value);
+ return new NotSupportedException ("Method is not supported.");
}
-#endif
-
#endregion // Methods
}
}