schemaRow["IsUnique"] = false; // ? tim
schemaRow["IsKey"] = false; // ? tim
schemaRow["BaseCatalogName"] = ""; // ? tim
+ schemaRow["BaseColumnName"] = PostgresLibrary.PQfname (pgResult, i);
schemaRow["BaseSchemaName"] = ""; // ? tim
schemaRow["BaseTableName"] = ""; // ? tim
schemaRow["IsUnique"] = false; // ? tim
schemaRow["IsKey"] = false; // ? tim
schemaRow["BaseCatalogName"] = ""; // ? tim
+ schemaRow["BaseColumnName"] = PostgresLibrary.PQfname (pgResult, i);
schemaRow["BaseSchemaName"] = ""; // ? tim
schemaRow["BaseTableName"] = ""; // ? tim
+2002-05-23 Tim Coleman <tim@timcoleman.com>
+ * System.Data.SqlClient/SqlCommand.cs: include
+ the BaseColumnName in the schema table. Was missed before.
+ * System.Data.Common/DbDataAdapter.cs: Use DataTable
+ mappings so that the DataSet and DataTable are more closely tied.
+ Get schema information from the DataTable using GetSchemaTable ()
+ Various other little fixes
+ * System.Data.Common/DataColumnMappingCollection.cs:
+ * System.Data.Common/DataTableMapping.cs:
+ * System.Data.Common/DataTableMappingCollection.cs: Some
+ implementation, enough to be used by DbDataAdapter.
+
2002-05-23 Daniel Morgan <danmorg@sc.rr.com>
* System.Data.SqlClient/SqlCommand.cs: set
//
// Author:
// Rodrigo Moya (rodrigo@ximian.com)
+// Tim Coleman (tim@timcoleman.com)
//
// (C) Ximian, Inc
+// (C) Copyright 2002 Tim Coleman
//
using System;
/// <summary>
/// Contains a collection of DataColumnMapping objects. This class cannot be inherited.
/// </summary>
- public sealed class DataColumnMappingCollection :
- MarshalByRefObject // IColumnMappingCollection, IList,
- // ICollection, IEnumerable
+ public sealed class DataColumnMappingCollection : MarshalByRefObject // , IColumnMappingCollection , IList, ICollection, IEnumerable
{
- private DataColumnMapping[] mappings = null;
- private int size = 0;
-
- public DataColumnMappingCollection () {
- }
+ #region Fields
- public int Add (object obj)
- {
- DataColumnMapping[] tmp = new DataColumnMapping[size + 1];
+ ArrayList list;
+ Hashtable sourceColumns;
+ Hashtable dataSetColumns;
- if (size > 0)
- Array.Copy (mappings, tmp, size);
- size++;
- mappings = tmp;
- mappings[size - 1] = (DataColumnMapping) obj;
+ #endregion
- return size;
+ #region Constructors
+
+ public DataColumnMappingCollection ()
+ {
+ list = new ArrayList ();
+ sourceColumns = new Hashtable ();
+ dataSetColumns = new Hashtable ();
}
- public void AddRange (DataColumnMapping[] values) {
- DataColumnMapping[] tmp = new DataColumnMapping[size + values.Length];
+ #endregion
+
+ #region Properties
+
+ public int Count {
+ get { return list.Count; }
+ }
- if (size > 0)
- Array.Copy (mappings, tmp, size);
- for (int i = 0; i < values.Length; i++) {
- tmp[i + size] = values[i];
+ public DataColumnMapping this[int index] {
+ get { return (DataColumnMapping)(list[index]); }
+ set {
+ DataColumnMapping mapping = (DataColumnMapping)(list[index]);
+ sourceColumns[mapping] = value;
+ dataSetColumns[mapping] = value;
+ list[index] = value;
}
-
- size += values.Length;
- mappings = tmp;
}
- public void Clear () {
- /* FIXME */
- for (int i = 0; i < size; i++)
- mappings[i] = null;
+ public DataColumnMapping this[string sourceColumn] {
+ get { return (DataColumnMapping)(sourceColumns[sourceColumn]); }
+ set { this[list.IndexOf (sourceColumns[sourceColumn])] = value; }
+ }
+
+ #endregion
+
+ #region Methods
- size = 0;
+ public int Add (object value)
+ {
+ if (!(value is DataColumnMapping))
+ throw new InvalidCastException ();
+
+ list.Add (value);
+ sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
+ dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
+ return list.IndexOf (value);
}
- public bool Contains (object obj) {
- for (int i = 0; i < size; i++) {
- if (obj.Equals (mappings[i]))
- return true;
- }
+ public DataColumnMapping Add (string sourceColumn, string dataSetColumn)
+ {
+ DataColumnMapping mapping = new DataColumnMapping (sourceColumn, dataSetColumn);
+ Add (mapping);
+ return mapping;
+ }
- return false;
+ public void AddRange (DataColumnMapping[] values)
+ {
+ foreach (DataColumnMapping mapping in values)
+ Add (mapping);
}
- public void CopyTo (Array array, int index) {
- Array.Copy (mappings, array, size);
+ public void Clear ()
+ {
+ list.Clear ();
}
- public DataColumnMapping GetByDataSetColumn (string value) {
- for (int i = 0; i < size; i++) {
- if (mappings[i].DataSetColumn == value)
- return mappings[i];
- }
+ public bool Contains (object value)
+ {
+ return (list.Contains (value));
+ }
- return null;
+ public bool Contains (string value)
+ {
+ return (sourceColumns.Contains (value));
}
- [MonoTODO]
- public static DataColumnMapping GetColumnMappingBySchemaAction (
- DataColumnMappingCollection columnMappings,
- string sourceColumn,
- MissingMappingAction mappingAction) {
- throw new NotImplementedException ();
+ public void CopyTo (Array array, int index)
+ {
+ ((DataColumn[])(list.ToArray())).CopyTo (array, index);
}
- public int IndexOf (object obj) {
- for (int i = 0; i < size; i++) {
- if (obj.Equals (mappings[i]))
- return i;
- }
+ public DataColumnMapping GetByDataSetColumn (string value)
+ {
+ return (DataColumnMapping)(dataSetColumns[value]);
+ }
+
+ public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
+ {
+ if (columnMappings.Contains (sourceColumn))
+ return columnMappings[sourceColumn];
- return -1;
+ if (mappingAction == MissingMappingAction.Ignore)
+ return null;
+
+ if (mappingAction == MissingMappingAction.Error)
+ throw new SystemException ();
+
+ return new DataColumnMapping (sourceColumn, sourceColumn);
}
- public int IndexOfDataSetColumn (string value) {
- for (int i = 0; i < size; i++) {
- if (mappings[i].DataSetColumn == value)
- return i;
- }
+ public IEnumerator GetEnumerator ()
+ {
+ return list.GetEnumerator ();
+ }
- return -1;
+/* FIXME
+ IColumnMapping IColumnMappingCollection.Add (string sourceColumnName, string dataSetColumnName)
+ {
+ return (IColumnMapping)(Add (sourceColumnName, dataSetColumnName));
}
- [MonoTODO]
- public void Insert (int index, object value) {
- throw new NotImplementedException ();
+ IColumnMapping IColumnMappingCollection.GetByDataSetColumn (string dataSetColumnName)
+ {
+ return (IColumnMapping)(GetByDataSetColumn (dataSetColumnName));
}
+*/
- [MonoTODO]
- public void Remove (object value) {
- throw new NotImplementedException ();
+ public int IndexOf (object value)
+ {
+ return list.IndexOf (value);
}
- [MonoTODO]
- public void RemoveAt (int index) {
- throw new NotImplementedException ();
+ public int IndexOf (string sourceColumn)
+ {
+ return list.IndexOf (sourceColumns[sourceColumn]);
}
-
- public int Count {
- get { return size; }
+
+ public int IndexOfDataSetColumn (string value)
+ {
+ return list.IndexOf (dataSetColumns[value]);
}
- public DataColumnMapping this[int index] {
- get {
- if (index < size)
- return mappings[index];
- return null;
- }
- set {
- if (index < size)
- mappings[index] = value;
- }
+ public void Insert (int index, object value)
+ {
+ list.Insert (index, value);
+ sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
+ dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
+ }
+
+ public void Remove (object value)
+ {
+ sourceColumns.Remove(((DataColumnMapping)value).SourceColumn);
+ dataSetColumns.Remove(((DataColumnMapping)value).DataSetColumn);
+ list.Remove (value);
}
+
+ public void RemoveAt (int index)
+ {
+ Remove (list[index]);
+ }
+
+ public void RemoveAt (string sourceColumn)
+ {
+ RemoveAt (list.IndexOf (sourceColumns[sourceColumn]));
+ }
+
+ #endregion
}
}
//
// Author:
// Rodrigo Moya (rodrigo@ximian.com)
+// Tim Coleman (tim@timcoleman.com)
//
// (C) Ximian, Inc
+// (C) Copyright 2002 Tim Coleman
//
using System.Data;
/// </summary>
public sealed class DataTableMapping : MarshalByRefObject // , ITableMapping, ICloneable
{
- [MonoTODO]
- public DataTableMapping() {
- throw new NotImplementedException ();
+ #region Fields
+
+ string sourceTable;
+ string dataSetTable;
+ DataColumnMappingCollection columnMappings;
+
+ #endregion
+
+ #region Constructors
+
+ public DataTableMapping ()
+ {
+ dataSetTable = String.Empty;
+ sourceTable = String.Empty;
+ columnMappings = new DataColumnMappingCollection ();
}
- [MonoTODO]
- public DataTableMapping (string a, string b) {
- throw new NotImplementedException ();
+ public DataTableMapping (string sourceTable, string dataSetTable)
+ : this ()
+ {
+ this.sourceTable = sourceTable;
+ this.dataSetTable = dataSetTable;
+ }
+
+ public DataTableMapping (string sourceTable, string dataSetTable, DataColumnMapping[] columnMappings)
+ : this (sourceTable, dataSetTable)
+ {
+ this.columnMappings.AddRange (columnMappings);
}
- [MonoTODO]
- public DataTableMapping(string a, string b, DataColumnMapping[] c) {
- throw new NotImplementedException ();
+ #endregion
+
+ #region Properties
+
+ public DataColumnMappingCollection ColumnMappings {
+ get { return columnMappings; }
}
- [MonoTODO]
- public DataColumnMapping GetColumnMappingBySchemaAction(
- string sourceColumn,
- MissingMappingAction mappingAction) {
- throw new NotImplementedException ();
+ public string DataSetTable {
+ get { return dataSetTable; }
+ set { dataSetTable = value; }
}
- [MonoTODO]
- public DataTable GetDataTableBySchemaAction(
- DataSet dataSet,
- MissingSchemaAction schemaAction) {
- throw new NotImplementedException ();
+ public string SourceTable {
+ get { return sourceTable; }
+ set { sourceTable = value; }
}
+
+ #endregion
- [MonoTODO]
- public DataColumnMappingCollection ColumnMappings {
- get { throw new NotImplementedException (); }
+ #region Methods
+
+ public DataColumnMapping GetColumnMappingBySchemaAction (string sourceColumn, MissingMappingAction mappingAction)
+ {
+ return DataColumnMappingCollection.GetColumnMappingBySchemaAction (columnMappings, sourceColumn, mappingAction);
}
[MonoTODO]
- public string DataSetTable {
- get { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
+ public DataTable GetDataTableBySchemaAction (DataSet dataSet, MissingSchemaAction schemaAction)
+ {
+ throw new NotImplementedException ();
}
- [MonoTODO]
- public string SourceTable {
- get { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
+ public override string ToString ()
+ {
+ return SourceTable;
}
+
+ #endregion
}
}
MarshalByRefObject, // ITableMappingCollection, IList,
IEnumerable //ICollection,
{
- private ArrayList mappingList;
- private ArrayList sourceTableList;
- private ArrayList dataSetTableList;
+
+ #region Fields
+
+ ArrayList mappings;
+ Hashtable sourceTables;
+ Hashtable dataSetTables;
+
+ #endregion
+
+ #region Constructors
public DataTableMappingCollection()
{
- sourceTableList = new ArrayList ();
- dataSetTableList = new ArrayList ();
- mappingList = new ArrayList ();
+ mappings = new ArrayList ();
+ sourceTables = new Hashtable ();
+ dataSetTables = new Hashtable ();
+ }
+
+ #endregion
+
+ #region Properties
+
+ public int Count
+ {
+ get { return mappings.Count; }
}
+ public DataTableMapping this[int index] {
+ get { return (DataTableMapping)(mappings[index]); }
+ set {
+ DataTableMapping mapping = (DataTableMapping)(mappings[index]);
+ sourceTables[mapping.SourceTable] = value;
+ dataSetTables[mapping.DataSetTable] = value;
+ mappings[index] = value;
+ }
+ }
+
+ [MonoTODO]
+ public DataTableMapping this[string sourceTable] {
+ get { return (DataTableMapping)(sourceTables[sourceTable]); }
+ set { this[mappings.IndexOf(sourceTables[sourceTable])] = value; }
+ }
+
+ #endregion
+
+ #region Methods
+
public int Add (object value)
{
if (!(value is System.Data.Common.DataTableMapping))
throw new SystemException ("The object passed in was not a DataTableMapping object.");
- string sourceTable = ((DataTableMapping)value).SourceTable;
- string dataSetTable = ((DataTableMapping)value).DataSetTable;
- mappingList.Add (value);
- dataSetTableList.Add (dataSetTable);
- return sourceTableList.Add (sourceTable);
+ sourceTables[((DataTableMapping)value).SourceTable] = value;
+ dataSetTables[((DataTableMapping)value).DataSetTable] = value;
+ return mappings.Add (value);
}
public DataTableMapping Add (string sourceTable, string dataSetTable)
{
- DataTableMapping dataTableMapping = new DataTableMapping (sourceTable, dataSetTable);
-
- mappingList.Add (dataTableMapping);
- sourceTableList.Add (sourceTable);
- dataSetTableList.Add (dataSetTable);
-
- return dataTableMapping ;
+ DataTableMapping mapping = new DataTableMapping (sourceTable, dataSetTable);
+ Add (mapping);
+ return mapping;
}
public void AddRange(DataTableMapping[] values)
public void Clear()
{
- sourceTableList.Clear ();
- dataSetTableList.Clear ();
- mappingList.Clear ();
+ sourceTables.Clear ();
+ dataSetTables.Clear ();
+ mappings.Clear ();
}
public bool Contains (object value)
{
- return mappingList.Contains (value);
+ return mappings.Contains (value);
}
public bool Contains (string value)
{
- return sourceTableList.Contains (value);
+ return sourceTables.Contains (value);
}
[MonoTODO]
public DataTableMapping GetByDataSetTable (string dataSetTable)
{
- return (DataTableMapping)mappingList[dataSetTableList.IndexOf(dataSetTable)];
+ return (DataTableMapping)(dataSetTables[dataSetTable]);
}
- [MonoTODO]
public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction)
{
- throw new NotImplementedException ();
+ if (tableMappings.Contains (sourceTable))
+ return tableMappings[sourceTable];
+ if (mappingAction == MissingMappingAction.Error)
+ throw new InvalidOperationException ();
+ if (mappingAction == MissingMappingAction.Ignore)
+ return null;
+ return new DataTableMapping (sourceTable, dataSetTable);
+ }
+
+ public IEnumerator GetEnumerator ()
+ {
+ return mappings.GetEnumerator ();
}
public int IndexOf (object value)
{
- return mappingList.IndexOf (value);
+ return mappings.IndexOf (value);
}
- public int IndexOf (string value)
+ public int IndexOf (string sourceTable)
{
- return sourceTableList.IndexOf (value);
+ return IndexOf (sourceTables[sourceTable]);
}
public int IndexOfDataSetTable (string dataSetTable)
{
- return dataSetTableList.IndexOf (dataSetTable);
+ return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
}
[MonoTODO]
throw new NotImplementedException ();
}
- [MonoTODO]
- public int Count
- {
- get { throw new NotImplementedException (); }
- }
- [MonoTODO]
- public DataTableMapping this[int i] {
- get { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
- }
- [MonoTODO]
- public DataTableMapping this[string s] {
- get { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
- }
-
- [MonoTODO]
- public IEnumerator GetEnumerator ()
- {
- throw new NotImplementedException ();
- }
+ #endregion
}
}
// Copyright (C) 2002 Tim Coleman
//
+using System.Collections;
using System.Data;
namespace System.Data.Common
protected virtual int Fill (DataSet dataSet, string srcTable, IDataReader dataReader, int startRecord, int maxRecords)
{
- DataTable table;
- int changeCount = 0;
- string tableName = srcTable;
- int i = 0;
-
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 table;
+ int readCount = 0;
+ int resultCount = 0;
+
+ string tableName = srcTable;
+ string baseColumnName;
+ string columnName;
+ ArrayList primaryKey;
+ bool resultsFound;
+ object[] itemArray;
+ DataTableMapping tableMapping;
+
+ DataRow row; // FIXME needed for incorrect operation below.
+
do
{
if (dataSet.Tables.Contains (tableName))
- {
- table = dataSet.Tables[tableName];
- }
- else // create a new table
- {
- table = new DataTable (tableName);
- for (int j = 0; j < dataReader.FieldCount; j += 1)
- {
- string baseColumnName = dataReader.GetName (j);
- string columnName = "";
-
- if (baseColumnName == "")
- baseColumnName = "Column";
- else
- columnName = baseColumnName;
-
- for (int k = 1; table.Columns.Contains (columnName) || columnName == ""; k += 1)
- columnName = String.Format ("{0}{1}", baseColumnName, k);
-
- table.Columns.Add (new DataColumn (columnName, dataReader.GetFieldType (j)));
- }
- dataSet.Tables.Add (table);
- }
+ table = dataSet.Tables[tableName];
+ else
+ table = new DataTable (tableName);
- DataRow row;
- object[] itemArray = new object[dataReader.FieldCount];
+ primaryKey = new ArrayList ();
- // limit results for first results only.
- if (srcTable == tableName)
- for (int k = 0; k < startRecord; k += 1)
- dataReader.Read ();
+ foreach (DataRow schemaRow in dataReader.GetSchemaTable ().Rows)
+ {
+ // generate a unique column name in the dataset table.
+ baseColumnName = (string)(schemaRow["BaseColumnName"]);
+ if (baseColumnName == "")
+ baseColumnName = "Column";
- while (dataReader.Read () && !(maxRecords > 0 && changeCount >= maxRecords && srcTable == tableName))
- {
- // need to check for existing rows to reconcile if we have key
- // information. skip this step for now
+ columnName = baseColumnName;
+
+ for (int i = 1; table.Columns.Contains (columnName); i += 1)
+ columnName = String.Format ("{0}{1}", baseColumnName, i);
+
+
+ tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, tableName, (string)(schemaRow["BaseTableName"]), MissingMappingAction);
- // append rows to the end of the current table.
+ // check to see if the column mapping exists
+ if (tableMapping.ColumnMappings.IndexOfDataSetColumn (baseColumnName) < 0)
+ {
+ if (MissingSchemaAction == MissingSchemaAction.Error)
+ throw new SystemException ();
+
+ table.Columns.Add (columnName, Type.GetType ((string)(schemaRow["DataType"])));
+ tableMapping.ColumnMappings.Add (columnName, baseColumnName);
+
+ }
+
+ if (!TableMappings.Contains (tableMapping))
+ TableMappings.Add (tableMapping);
+
+ if ((bool)(schemaRow["IsKey"]))
+ primaryKey.Add (table.Columns[columnName]);
+ }
+
+ if (MissingSchemaAction == MissingSchemaAction.AddWithKey && primaryKey.Count > 0)
+ table.PrimaryKey = (DataColumn[])(primaryKey.ToArray());
+
+
+ for (int k = 0; k < startRecord; k += 1)
+ dataReader.Read ();
+
+ resultsFound = false;
+
+ itemArray = new object[dataReader.FieldCount];
+
+ while (dataReader.Read () && !(maxRecords > 0 && readCount >= maxRecords))
+ {
dataReader.GetValues (itemArray);
row = table.Rows.Add (itemArray);
-
- if (AcceptChangesDuringFill)
+ if (AcceptChangesDuringFill)
row.AcceptChanges ();
- changeCount += 1;
+ /* FIXME
+
+ this is the way it should be done, but LoadDataRow has not been implemented yet.
+
+ table.BeginLoadData ();
+ table.LoadDataRow (itemArray, AcceptChangesDuringFill);
+ table.EndLoadData ();
+ */
+
+ readCount += 1;
+ resultsFound = true;
}
- i += 1;
- tableName = String.Format ("{0}{1}", srcTable, i);
+ if (resultsFound)
+ {
+ dataSet.Tables.Add (table);
+ tableName = String.Format ("{0}{1}", srcTable, ++resultCount);
+ }
+
+
+ startRecord = 0;
+ maxRecords = 0;
} while (dataReader.NextResult ());
dataReader.Close ();
- return changeCount;
+ return readCount;
}
protected virtual int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior)
schemaRow["IsUnique"] = false; // ? tim
schemaRow["IsKey"] = false; // ? tim
schemaRow["BaseCatalogName"] = ""; // ? tim
+ schemaRow["BaseColumnName"] = PostgresLibrary.PQfname (pgResult, i);
schemaRow["BaseSchemaName"] = ""; // ? tim
schemaRow["BaseTableName"] = ""; // ? tim