[Mono-list] DbDataAdapter Fill When Schema Exists (DbDataAdapter.FillSchema)
Alan Tam
Tam@SiuLung.com
Tue, 25 Feb 2003 15:16:01 +0800
This is a multi-part message in MIME format.
------=_NextPart_000_0017_01C2DCE0.CEC0C4C0
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: 7bit
I've implemented DbDataAdapter to support filling when the schema already
exists.
According to MS implementation, if the column already exists, it is unmodified
(even if datatype is wrong). However, complications come when there are
multiple columns with the same name (I don't know whether it would happen at
all).
So this new implementation stores a new ArrayList of source column names.
Whenever a source column is scanned, calculate its correct name, adding a
trailing number if required, and add it to this ArrayList. Then find the
DataColumnMapping (do we need to calculate the DataTableMapping per row?!) to
map it to the corresponding dataset column name. Then find this name in the
datatable to see if we really need to add this new column.
Tim, can you look at it to see if it is implemented correctly.
Regards,
Alan
------=_NextPart_000_0017_01C2DCE0.CEC0C4C0
Content-Type: application/octet-stream;
name="DbDataAdapter.patch"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="DbDataAdapter.patch"
Index: System.Data.Common/DbDataAdapter.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Data/System.Data.Common/DbDataAdapter.cs,v=0A=
retrieving revision 1.22=0A=
diff -u -r1.22 DbDataAdapter.cs=0A=
--- System.Data.Common/DbDataAdapter.cs 24 Feb 2003 17:47:28 -0000 1.22=0A=
+++ System.Data.Common/DbDataAdapter.cs 25 Feb 2003 07:04:22 -0000=0A=
@@ -97,7 +102,6 @@=0A=
return Fill (dataSet, 0, 0, srcTable, SelectCommand, =
CommandBehavior.Default);=0A=
}=0A=
=0A=
- [MonoTODO ("Support filling after we have already filled.")]=0A=
protected virtual int Fill (DataTable dataTable, IDataReader =
dataReader) =0A=
{=0A=
int count =3D 0;=0A=
@@ -136,7 +140,6 @@=0A=
return this.Fill (dataSet, startRecord, maxRecords, srcTable, =
SelectCommand, CommandBehavior.Default);=0A=
}=0A=
=0A=
- [MonoTODO ("Support filling after we have already filled.")]=0A=
protected virtual int Fill (DataSet dataSet, string srcTable, =
IDataReader dataReader, int startRecord, int maxRecords) =0A=
{=0A=
if (startRecord < 0)=0A=
@@ -291,30 +294,31 @@=0A=
[MonoTODO ("Test")]=0A=
private void BuildSchema (IDataReader reader, DataTable table, =
SchemaType schemaType)=0A=
{=0A=
- string sourceColumnName;=0A=
- string sourceTableName;=0A=
- string dsColumnName;=0A=
-=0A=
- ArrayList primaryKey =3D new ArrayList (); =0A=
- table.Columns.Clear ();=0A=
+ ArrayList primaryKey =3D new ArrayList ();=0A=
+ ArrayList sourceColumns =3D new ArrayList ();=0A=
=0A=
foreach (DataRow schemaRow in reader.GetSchemaTable ().Rows) {=0A=
- // generate a unique column name in the dataset table.=0A=
+ string sourceTableName;=0A=
+ if (schemaRow ["BaseTableName"].Equals (DBNull.Value))=0A=
+ sourceTableName =3D table.TableName; =0A=
+ else=0A=
+ sourceTableName =3D (string) schemaRow ["BaseTableName"];=0A=
+=0A=
+ // generate a unique column name in the source table.=0A=
+ string sourceColumnName;=0A=
if (schemaRow ["BaseColumnName"].Equals (DBNull.Value))=0A=
sourceColumnName =3D DefaultSourceColumnName;=0A=
else =0A=
sourceColumnName =3D (string) schemaRow ["BaseColumnName"];=0A=
=0A=
- dsColumnName =3D sourceColumnName;=0A=
+ string realSourceColumnName =3D sourceColumnName;=0A=
=0A=
- for (int i =3D 1; table.Columns.Contains (dsColumnName); i +=3D 1) =0A=
- dsColumnName =3D String.Format ("{0}{1}", sourceColumnName, i);=0A=
-=0A=
- if (schemaRow ["BaseTableName"].Equals (DBNull.Value))=0A=
- sourceTableName =3D table.TableName; =0A=
- else=0A=
- sourceTableName =3D (string) schemaRow ["BaseTableName"];=0A=
+ for (int i =3D 1; sourceColumns.Contains (realSourceColumnName); i =
+=3D 1) =0A=
+ realSourceColumnName =3D String.Format ("{0}{1}", =
sourceColumnName, i);=0A=
+ sourceColumns.Add(realSourceColumnName);=0A=
=0A=
+ // generate DataSetColumnName from DataTableMapping, if any=0A=
+ string dsColumnName =3D realSourceColumnName;=0A=
DataTableMapping tableMapping =3D null;=0A=
if (schemaType =3D=3D SchemaType.Mapped)=0A=
tableMapping =3D =
DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, =
sourceTableName, table.TableName, MissingMappingAction.Ignore); =0A=
@@ -331,8 +335,8 @@=0A=
TableMappings.Add (tableMapping);=0A=
}=0A=
=0A=
- table.Columns.Add (dsColumnName, (Type) schemaRow ["DataType"]);=0A=
-=0A=
+ if (!table.Columns.Contains(dsColumnName))=0A=
+ table.Columns.Add (dsColumnName, (Type) schemaRow ["DataType"]);=0A=
=0A=
if (!schemaRow["IsKey"].Equals (DBNull.Value))=0A=
if ((bool) (schemaRow ["IsKey"]))=0A=
@@ -427,7 +431,9 @@=0A=
DataColumnMappingCollection columnMappings =3D =
tableMapping.ColumnMappings;=0A=
=0A=
foreach (IDataParameter parameter in command.Parameters) {=0A=
- string dsColumnName =3D columnMappings =
[parameter.SourceColumn].DataSetColumn;=0A=
+ string dsColumnName =3D parameter.SourceColumn;=0A=
+ DataColumnMapping mapping =3D columnMappings =
[parameter.SourceColumn];=0A=
+ if (mapping !=3D null) dsColumnName =3D mapping.DataSetColumn;=0A=
DataRowVersion rowVersion =3D DataRowVersion.Proposed;=0A=
=0A=
// Parameter version is ignored for non-update commands=0A=
------=_NextPart_000_0017_01C2DCE0.CEC0C4C0--