GetColumnMappingBySchemaAction() приватный Метод

private GetColumnMappingBySchemaAction ( string sourceColumn, MissingMappingAction mappingAction ) : DataColumnMapping
sourceColumn string
mappingAction MissingMappingAction
Результат DataColumnMapping
Пример #1
0
 private DataColumn GetParameterDataColumn(String columnName, DataTableMapping mappings, DataRow row)
 {
     if (!ADP.IsEmpty(columnName))
     {
         DataColumnMapping columnMapping = mappings.GetColumnMappingBySchemaAction(columnName, this.missingMapping);
         if (null != columnMapping)
         {
             return(columnMapping.GetDataColumnBySchemaAction(row.Table, null, missingSchema));
         }
     }
     return(null);
 }
		private void UpdateParameterValues(
			IDbCommand command,
			StatementType statementType,
			DataRow row,
			DataTableMapping tableMapping)
		{
			foreach (DbParameter parameter in command.Parameters)
			{
				// Process only input parameters
				if ((parameter.Direction == ParameterDirection.Input || parameter.Direction == ParameterDirection.InputOutput) &&
					!String.IsNullOrEmpty(parameter.SourceColumn))
				{
					DataColumn column = null;

					/* Get the DataColumnMapping that matches the given
					 * column name
					 */
					DataColumnMapping columnMapping = tableMapping.GetColumnMappingBySchemaAction(
						parameter.SourceColumn,
						MissingMappingAction);

					if (columnMapping != null)
					{
						column = columnMapping.GetDataColumnBySchemaAction(row.Table, null, MissingSchemaAction);

						if (column != null)
						{
							DataRowVersion dataRowVersion = DataRowVersion.Default;

							if (statementType == StatementType.Insert)
							{
								dataRowVersion = DataRowVersion.Current;
							}
							else if (statementType == StatementType.Update)
							{
								dataRowVersion = parameter.SourceVersion;
							}
							else if (statementType == StatementType.Delete)
							{
								dataRowVersion = DataRowVersion.Original;
							}

							if (parameter.SourceColumnNullMapping)
							{
								parameter.Value = IsNull(row[column, dataRowVersion]) ? 1 : 0;
							}
							else
							{
								parameter.Value = row[column, dataRowVersion];
							}
						}
					}
				}
			}
		}
		/// <summary>
		/// Review .NET	Framework documentation.
		/// </summary>
		protected override int Update(DataRow[] dataRows, DataTableMapping tableMapping)
		{
			int updated = 0;
			IDbCommand command = null;
			StatementType statementType = StatementType.Insert;
			ICollection<IDbConnection> connections = new List<IDbConnection>();
			RowUpdatingEventArgs updatingArgs = null;
			Exception updateException = null;

			foreach (DataRow row in dataRows)
			{
				updateException = null;

				if (row.RowState == DataRowState.Detached ||
					row.RowState == DataRowState.Unchanged)
				{
					continue;
				}

				switch (row.RowState)
				{
					case DataRowState.Unchanged:
					case DataRowState.Detached:
						continue;

					case DataRowState.Added:
						command = InsertCommand;
						statementType = StatementType.Insert;
						break;

					case DataRowState.Modified:
						command = UpdateCommand;
						statementType = StatementType.Update;
						break;

					case DataRowState.Deleted:
						command = DeleteCommand;
						statementType = StatementType.Delete;
						break;
				}

				/* The order of	execution can be reviewed in the .NET 1.1 documentation
				 *
				 * 1. The values in	the	DataRow	are	moved to the parameter values.
				 * 2. The OnRowUpdating	event is raised.
				 * 3. The command executes.
				 * 4. If the command is	set	to FirstReturnedRecord,	then the first returned	result is placed in	the	DataRow.
				 * 5. If there are output parameters, they are placed in the DataRow.
				 * 6. The OnRowUpdated event is	raised.
				 * 7 AcceptChanges is called.
				 */

				try
				{
					updatingArgs = CreateRowUpdatingEvent(row, command, statementType, tableMapping);

					/* 1. Update Parameter values (It's	very similar to	what we
					 * are doing in	the	FbCommandBuilder class).
					 *
					 * Only	input parameters should	be updated.
					 */
					if (command != null && command.Parameters.Count > 0)
					{
						try
						{
							UpdateParameterValues(command, statementType, row, tableMapping);
						}
						catch (Exception ex)
						{
							updatingArgs.Errors = ex;
							updatingArgs.Status = UpdateStatus.ErrorsOccurred;
						}
					}

					// 2. Raise	RowUpdating	event
					OnRowUpdating(updatingArgs);

					if (updatingArgs.Status == UpdateStatus.SkipAllRemainingRows)
					{
						break;
					}
					else if (updatingArgs.Status == UpdateStatus.ErrorsOccurred)
					{
						if (updatingArgs.Errors == null)
						{
							throw new InvalidOperationException("RowUpdatingEvent: Errors occurred; no additional information is available.");
						}
						throw updatingArgs.Errors;
					}
					else if (updatingArgs.Status == UpdateStatus.SkipCurrentRow)
					{
						updated++;
						continue;
					}
					else if (updatingArgs.Status == UpdateStatus.Continue)
					{
						if (command != updatingArgs.Command)
						{
							command = updatingArgs.Command;
						}
						if (command == null)
						{
							/* Samples of exceptions thrown	by DbDataAdapter class
							 *
							 *	Update requires	a valid	InsertCommand when passed DataRow collection with new rows
							 *	Update requires	a valid	UpdateCommand when passed DataRow collection with modified rows.
							 *	Update requires	a valid	DeleteCommand when passed DataRow collection with deleted rows.
							 */
							throw new InvalidOperationException(CreateExceptionMessage(statementType));
						}

						// 3. Execute the command
						if (command.Connection.State == ConnectionState.Closed)
						{
							command.Connection.Open();
							// Track command connection
							connections.Add(command.Connection);
						}

						int rowsAffected = command.ExecuteNonQuery();
						if (rowsAffected == 0)
						{
							throw new DBConcurrencyException(new DBConcurrencyException().Message, null, new DataRow[] { row });
						}

						updated++;

						// http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=933212&SiteID=1
						if (statementType == StatementType.Insert)
						{
							row.AcceptChanges();
						}

						/* 4. If the command is	set	to FirstReturnedRecord,	then the
						 * first returned result is	placed in the DataRow.
						 *
						 * We have nothing to do in	this case as there are no
						 * support for batch commands.
						 */

						/* 5. Check	if we have output parameters and they should
						 * be updated.
						 *
						 * Only	output parameters should be	updated
						 */
						if (command.UpdatedRowSource == UpdateRowSource.OutputParameters ||
							command.UpdatedRowSource == UpdateRowSource.Both)
						{
							// Process output parameters
							foreach (IDataParameter parameter in command.Parameters)
							{
								if ((parameter.Direction == ParameterDirection.Output ||
									parameter.Direction == ParameterDirection.ReturnValue ||
									parameter.Direction == ParameterDirection.InputOutput) &&
									!String.IsNullOrEmpty(parameter.SourceColumn))
								{
									DataColumn column = null;

									DataColumnMapping columnMapping = tableMapping.GetColumnMappingBySchemaAction(
										parameter.SourceColumn,
										MissingMappingAction);

									if (columnMapping != null)
									{
										column = columnMapping.GetDataColumnBySchemaAction(
											row.Table,
											null,
											MissingSchemaAction);

										if (column != null)
										{
											row[column] = parameter.Value;
										}
									}
								}
							}
						}
					}
				}
				catch (Exception ex)
				{
					row.RowError = ex.Message;
					updateException = ex;
				}

				if (updatingArgs != null && updatingArgs.Status == UpdateStatus.Continue)
				{
					// 6. Raise	RowUpdated event
					RowUpdatedEventArgs updatedArgs = CreateRowUpdatedEvent(row, command, statementType, tableMapping);
					OnRowUpdated(updatedArgs);

					if (updatedArgs.Status == UpdateStatus.SkipAllRemainingRows)
					{
						break;
					}
					else if (updatedArgs.Status == UpdateStatus.ErrorsOccurred)
					{
						if (updatingArgs.Errors == null)
						{
							throw new InvalidOperationException("RowUpdatedEvent: Errors occurred; no additional information available.");
						}
						throw updatedArgs.Errors;
					}
					else if (updatedArgs.Status == UpdateStatus.SkipCurrentRow)
					{
					}
					else if (updatingArgs.Status == UpdateStatus.Continue)
					{
						// If the update result is an exception throw it
						if (!ContinueUpdateOnError && updateException != null)
						{
							CloseConnections(connections);
							throw updateException;
						}

						// 7. Call AcceptChanges
						if (AcceptChangesDuringUpdate && !row.HasErrors)
						{
							row.AcceptChanges();
						}
					}
				}
				else
				{
					// If the update result is an exception throw it
					if (!ContinueUpdateOnError && updateException != null)
					{
						CloseConnections(connections);
						throw updateException;
					}
				}
			}

			CloseConnections(connections);

			return updated;
		}
		/// <summary>
		/// Review .NET	Framework documentation.
		/// </summary>
		protected override int Update(DataRow[] dataRows, DataTableMapping tableMapping)
		{
			int						updated			= 0;
			IDbCommand				command			= null;
			StatementType			statementType	= StatementType.Insert;
			ArrayList				connections		= new ArrayList();
			RowUpdatingEventArgs	updatingArgs	= null;
			Exception				updateException = null;

			foreach (DataRow row in dataRows)
			{
				if (row.RowState == DataRowState.Detached ||
					row.RowState == DataRowState.Unchanged)
				{
					continue;
				}

				switch (row.RowState)
				{
					case DataRowState.Added:
						command = this.insertCommand;
						statementType = StatementType.Insert;
						break;

					case DataRowState.Modified:
						command = this.updateCommand;
						statementType = StatementType.Update;
						break;

					case DataRowState.Deleted:
						command = this.deleteCommand;
						statementType = StatementType.Delete;
						break;
				}

				/* The order of	execution can be reviewed in the .NET 1.1 documentation
					*
					* 1. The values in	the	DataRow	are	moved to the parameter values. 
					* 2. The OnRowUpdating	event is raised. 
					* 3. The command executes.	
					* 4. If the command is	set	to FirstReturnedRecord,	then the first returned	result is placed in	the	DataRow. 
					* 5. If there are output parameters, they are placed in the DataRow. 
					* 6. The OnRowUpdated event is	raised.	
					* 7 AcceptChanges is called. 
					*/

				try
				{
					/* 1. Update Parameter values (It's	very similar to	what we	
					 * are doing in	the	FbCommandBuilder class).
					 *
					 * Only	input parameters should	be updated.
					 */
					if (command != null && command.Parameters.Count > 0)
					{
						this.UpdateParameterValues(command, statementType, row, tableMapping);
					}

					// 2. Raise	RowUpdating	event
					updatingArgs = this.CreateRowUpdatingEvent(row, command, statementType, tableMapping);
					this.OnRowUpdating(updatingArgs);

					if (updatingArgs.Status == UpdateStatus.SkipAllRemainingRows)
					{
						break;
					}
					else if (updatingArgs.Status == UpdateStatus.ErrorsOccurred)
					{
						if (updatingArgs.Errors == null)
						{
							throw new InvalidOperationException("RowUpdatingEvent: Errors occurred; no additional is information available.");
						}
						throw updatingArgs.Errors;
					}
					else if (updatingArgs.Status == UpdateStatus.SkipCurrentRow)
					{
					}
					else if (updatingArgs.Status == UpdateStatus.Continue)
					{
						if (command != updatingArgs.Command)
						{
							command = updatingArgs.Command;
						}
						if (command == null)
						{
							/* Samples of exceptions thrown	by DbDataAdapter class
								*
								*	Update requires	a valid	InsertCommand when passed DataRow collection with new rows
								*	Update requires	a valid	UpdateCommand when passed DataRow collection with modified rows.
								*	Update requires	a valid	DeleteCommand when passed DataRow collection with deleted rows.
								*/
							string message = this.CreateExceptionMessage(statementType);
							throw new InvalidOperationException(message);
						}

						/* Validate that the command has a connection */
						if (command.Connection == null)
						{
							throw new InvalidOperationException("Update requires a command with a valid connection.");
						}

						// 3. Execute the command
						if (command.Connection.State == ConnectionState.Closed)
						{
							command.Connection.Open();
							// Track command connection
							connections.Add(command.Connection);
						}

						int rowsAffected = command.ExecuteNonQuery();
						if (rowsAffected == 0)
						{
							throw new DBConcurrencyException("An attempt to execute an INSERT, UPDATE, or DELETE statement resulted in zero records affected.");
						}

						updated++;

						/* 4. If the command is	set	to FirstReturnedRecord,	then the 
							* first returned result is	placed in the DataRow. 
							* 
							* We have nothing to do in	this case as there are no 
							* support for batch commands.
							*/

						/* 5. Check	if we have output parameters and they should 
							* be updated.
							*
							* Only	output paraneters should be	updated
							*/
						if (command.UpdatedRowSource == UpdateRowSource.OutputParameters ||
							command.UpdatedRowSource == UpdateRowSource.Both)
						{
							// Process output parameters
							foreach (IDataParameter parameter in command.Parameters)
							{
								if ((parameter.Direction == ParameterDirection.Output ||
									parameter.Direction == ParameterDirection.ReturnValue ||
									parameter.Direction == ParameterDirection.InputOutput) &&
									parameter.SourceColumn != null &&
									parameter.SourceColumn.Length > 0)
								{
									DataColumn column = null;

									DataColumnMapping columnMapping = tableMapping.GetColumnMappingBySchemaAction(
										parameter.SourceColumn,
										this.MissingMappingAction);

									if (columnMapping != null)
									{
										column = columnMapping.GetDataColumnBySchemaAction(
											row.Table,
											null,
											this.MissingSchemaAction);

										if (column != null)
										{
											row[column] = parameter.Value;
										}
									}
								}
							}
						}
					}
				}
				catch (Exception ex)
				{
					row.RowError	= ex.Message;
					updateException = ex;
				}

				if (updatingArgs.Status == UpdateStatus.Continue)
				{
					// 6. Raise	RowUpdated event
					RowUpdatedEventArgs	updatedArgs = this.CreateRowUpdatedEvent(row, command, statementType, tableMapping);
					this.OnRowUpdated(updatedArgs);

					if (updatedArgs.Status == UpdateStatus.SkipAllRemainingRows)
					{
						break;
					}
					else if (updatedArgs.Status == UpdateStatus.ErrorsOccurred)
					{
						if (updatingArgs.Errors == null)
						{
							throw new InvalidOperationException("RowUpdatedEvent: Errors occurred; no additional information available.");
						}
						throw updatedArgs.Errors;
					}
					else if (updatedArgs.Status == UpdateStatus.SkipCurrentRow)
					{
					}
					else if (updatingArgs.Status == UpdateStatus.Continue)
					{
						// If the update result is an exception throw it
						if (!this.ContinueUpdateOnError && updateException != null)
						{
							this.CloseConnections(connections);
							throw updateException;
						}

						// 7. Call AcceptChanges
						row.AcceptChanges();
					}
				}
				else
				{
					// If the update result is an exception throw it
					if (!this.ContinueUpdateOnError && updateException != null)
					{
						this.CloseConnections(connections);
						throw updateException;
					}
				}

				updateException = null;
			}

			this.CloseConnections(connections);

			return updated;
		}
Пример #5
0
        private void SetupSchemaWithoutKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
        {
            int[]  columnIndexMap  = null;
            bool[] chapterIndexMap = null;

            int mappingCount = 0;
            int count        = this.dataReader.FieldCount;

            GenerateFieldNames(count);

            DataColumnCollection columnCollection = null;

            for (int i = 0; i < count; ++i)
            {
                DataColumnMapping columnMapping = tableMapping.GetColumnMappingBySchemaAction(fieldNames[i], mappingAction);
                if (null == columnMapping)
                {
                    if (null == columnIndexMap)
                    {
                        columnIndexMap = CreateIndexMap(count, i);
                    }
                    columnIndexMap[i] = -1;
                    continue; // null means ignore (mapped to nothing)
                }

                bool ischapter = false;
                Type fieldType = this.dataReader.GetFieldType(i);
                if (typeof(IDataReader).IsAssignableFrom(fieldType))
                {
                    if (null == chapterIndexMap)
                    {
                        chapterIndexMap = new bool[count];
                    }
                    chapterIndexMap[i] = ischapter = true;

                    fieldType = typeof(Int32);
                }

                DataColumn dataColumn = columnMapping.GetDataColumnBySchemaAction(this.dataTable, fieldType, schemaAction);
                if (null == dataColumn)
                {
                    if (null == columnIndexMap)
                    {
                        columnIndexMap = CreateIndexMap(count, i);
                    }
                    columnIndexMap[i] = -1;
                    continue; // null means ignore (mapped to nothing)
                }

                if (null == dataColumn.Table)
                {
                    if (ischapter)
                    {
                        dataColumn.AllowDBNull   = false;
                        dataColumn.AutoIncrement = true;
                        dataColumn.ReadOnly      = true;
                    }
                    if (null == columnCollection)
                    {
                        columnCollection = dataTable.Columns;
                    }
                    columnCollection.Add(dataColumn);
                }
                else if (ischapter && !dataColumn.AutoIncrement)
                {
                    throw ADP.FillChapterAutoIncrement();
                }

                if (null != columnIndexMap)
                {
                    columnIndexMap[i] = dataColumn.Ordinal;
                }
                else if (i != dataColumn.Ordinal)
                {
                    columnIndexMap    = CreateIndexMap(count, i);
                    columnIndexMap[i] = dataColumn.Ordinal;
                }
                mappingCount++;
            }

            bool       addDataRelation = false;
            DataColumn chapterColumn   = null;

            if (null != chapterValue)   // add the extra column in the child table
            {
                DataColumnMapping columnMapping = tableMapping.GetColumnMappingBySchemaAction(tableMapping.SourceTable, mappingAction);
                if (null != columnMapping)
                {
                    Type fieldType = chapterValue.GetType();
                    chapterColumn = columnMapping.GetDataColumnBySchemaAction(this.dataTable, fieldType, schemaAction);
                    if (null != chapterColumn)
                    {
                        if (null == chapterColumn.Table)
                        {
                            if (null == columnCollection)
                            {
                                columnCollection = dataTable.Columns;
                            }
                            columnCollection.Add(chapterColumn);
                            addDataRelation = (null != parentChapterColumn);
                        }
                        mappingCount++;
                    }
                }
            }

            object[] dataValues = null;
            if (0 < mappingCount)
            {
                if ((null != this.dataSet) && (null == this.dataTable.DataSet))
                {
                    // Allowed to throw exception if DataTable is from wrong DataSet
                    this.dataSet.Tables.Add(this.dataTable);
                }
                if (gettingData)
                {
                    if (null == columnCollection)
                    {
                        columnCollection = dataTable.Columns;
                    }
                    _indexMap   = columnIndexMap;
                    _chapterMap = chapterIndexMap;
                    dataValues  = SetupMapping(count, columnCollection, chapterColumn, chapterValue);
                }
#if DEBUG
                else
                {
                    this.mappedMode = -1;
                }
#endif
            }
            else
            {
                this.dataTable = null;
            }

            if (addDataRelation)
            {
                AddRelation(parentChapterColumn, chapterColumn);
            }
            _readerDataValues = dataValues;
        }
		private DataColumn GetDataColumn(
			string columnName, DataTableMapping tableMapping, DataRow row)
		{
			DataColumn dataColumn = null;

			// Get the DataColumnMapping that matches
			// the given column	name
			DataColumnMapping columnMapping = tableMapping.GetColumnMappingBySchemaAction(
				columnName, this.dataAdapter.MissingMappingAction);

			if (columnMapping != null)
			{
				// Get the DataColumn for the given	column name
				dataColumn = columnMapping.GetDataColumnBySchemaAction(
					row.Table,
					null,
					this.dataAdapter.MissingSchemaAction);
			}

			return dataColumn;
		}