using System; using System.Data; using System.Data.Common; using System.Data.SqlClient; using SDA.DataProvider.Core; namespace SDA.DataProvider.MSSQLWrappers { internal class MSSQLDataAdapterWrapper : SDADataAdapterWrapper { internal SqlDataAdapter DataAdapter; internal MSSQLCommandWrapper InnerDeleteCommand; internal MSSQLCommandWrapper InnerInsertCommand; internal MSSQLCommandWrapper InnerSelectCommand; internal MSSQLCommandWrapper InnerUpdateCommand; static string SafeGetParametersText(SDACommandWrapper command) { string result = ""; for (int i = 0; i < command.Parameters.Count; i++) { result += command.Parameters[i].ParameterName + "="; if (command.Parameters[i].Value == null) { result += @"csharpnull, "; } else if (command.Parameters[i].Value == DBNull.Value) { result += @"DBNULL, "; } else { result += "[" + command.Parameters[i].Value + "], "; } } return result; } static string SafeGetCommandText(SDACommandWrapper command) { if (command != null) { return command.CommandText + "\n" + SafeGetParametersText(command); } return ""; } public MSSQLDataAdapterWrapper() { DataAdapter = new SqlDataAdapter(); } public override void Dispose() { DataAdapter.Dispose(); DataAdapter = null; InnerDeleteCommand = null; InnerInsertCommand = null; InnerSelectCommand = null; InnerUpdateCommand = null; } public override SDACommandWrapper DeleteCommand { get { return InnerDeleteCommand; } set { MSSQLCommandWrapper command = value as MSSQLCommandWrapper; DataAdapter.DeleteCommand = command != null ? command.Command : null; InnerDeleteCommand = command; } } public override SDACommandWrapper InsertCommand { get { return InnerInsertCommand; } set { MSSQLCommandWrapper command = value as MSSQLCommandWrapper; DataAdapter.InsertCommand = command?.Command; InnerInsertCommand = command; } } public override SDACommandWrapper SelectCommand { get { return InnerSelectCommand; } set { MSSQLCommandWrapper command = value as MSSQLCommandWrapper; DataAdapter.SelectCommand = command?.Command; InnerSelectCommand = command; } } public override SDACommandWrapper UpdateCommand { get { return InnerUpdateCommand; } set { MSSQLCommandWrapper command = value as MSSQLCommandWrapper; DataAdapter.UpdateCommand = command?.Command; InnerUpdateCommand = command; } } public override bool AcceptChangesDuringFill { get { return DataAdapter.AcceptChangesDuringFill; } set { DataAdapter.AcceptChangesDuringFill = value; } } public override bool ContinueUpdateOnError { get { return DataAdapter.ContinueUpdateOnError; } set { DataAdapter.ContinueUpdateOnError = value; } } public override MissingMappingAction MissingMappingAction { get { return DataAdapter.MissingMappingAction; } set { DataAdapter.MissingMappingAction = value; } } public override MissingSchemaAction MissingSchemaAction { get { return DataAdapter.MissingSchemaAction; } set { DataAdapter.MissingSchemaAction = value; } } public override DataTableMappingCollection TableMappings { get { return DataAdapter.TableMappings; } } public override int Fill(DataSet dataSet) { try { return DataAdapter.Fill(dataSet); } catch (SqlException exception) { throw MSSQLExceptionHelper.TranslateSqlException(exception, SafeGetCommandText(InnerSelectCommand)); } } public override int Fill(DataSet dts, int startRecord, int endRecord, string srcTable) { try { return DataAdapter.Fill(dts, startRecord, (endRecord - startRecord) + 1, srcTable); } catch (SqlException exception) { throw MSSQLExceptionHelper.TranslateSqlException(exception, SafeGetCommandText(InnerSelectCommand)); } } public override int Fill(DataTable dataTable) { try { return DataAdapter.Fill(dataTable); } catch (SqlException exception) { throw MSSQLExceptionHelper.TranslateSqlException(exception, SafeGetCommandText(InnerSelectCommand)); } } public override DataTable[] FillSchema(DataSet dataSet, SchemaType schemaType) { try { return DataAdapter.FillSchema(dataSet, schemaType); } catch (SqlException exception) { throw MSSQLExceptionHelper.TranslateSqlException(exception, SafeGetCommandText(InnerSelectCommand)); } } public override DataTable FillSchema(DataTable dataTable, SchemaType schemaType) { try { return DataAdapter.FillSchema(dataTable, schemaType); } catch (SqlException exception) { throw MSSQLExceptionHelper.TranslateSqlException(exception, SafeGetCommandText(InnerSelectCommand)); } } public override DataTable[] FillSchema(DataSet dataSet, SchemaType schemaType, string srcTable) { try { return DataAdapter.FillSchema(dataSet, schemaType, srcTable); } catch (SqlException exception) { throw MSSQLExceptionHelper.TranslateSqlException(exception, SafeGetCommandText(InnerSelectCommand)); } } public override int Update(DataRow[] dataRows) { return DataAdapter.Update(dataRows); } public override int Update(DataSet dataSet) { return DataAdapter.Update(dataSet); } public override int Update(DataTable dataTable) { return DataAdapter.Update(dataTable); } } }