// Copyright (c) 2004-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation
//
// There are special exceptions to the terms and conditions of the GPL
// as it is applied to this software. View the full text of the
// exception in file EXCEPTIONS in the directory of this software
// distribution.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Data;
using System.Data.Common;
using System.ComponentModel;
using System.Collections.Generic;
namespace MySql.Data.MySqlClient
{
///
#if !CF
[System.Drawing.ToolboxBitmap( typeof(MySqlDataAdapter), "MySqlClient.resources.dataadapter.bmp")]
[System.ComponentModel.DesignerCategory("Code")]
[Designer("MySql.Data.MySqlClient.Design.MySqlDataAdapterDesigner,MySqlClient.Design")]
#endif
public sealed class MySqlDataAdapter : DbDataAdapter, IDbDataAdapter, IDataAdapter, ICloneable
{
private bool loadingDefaults;
private int updateBatchSize;
List commandBatch;
///
/// Occurs during Update before a command is executed against the data source. The attempt to update is made, so the event fires.
///
public event MySqlRowUpdatingEventHandler RowUpdating;
///
/// Occurs during Update after a command is executed against the data source. The attempt to update is made, so the event fires.
///
public event MySqlRowUpdatedEventHandler RowUpdated;
///
public MySqlDataAdapter()
{
loadingDefaults = true;
updateBatchSize = 1;
}
///
public MySqlDataAdapter(MySqlCommand selectCommand) : this()
{
SelectCommand = selectCommand;
}
///
public MySqlDataAdapter(string selectCommandText, MySqlConnection connection) : this()
{
SelectCommand = new MySqlCommand(selectCommandText, connection);
}
///
public MySqlDataAdapter(string selectCommandText, string selectConnString) : this()
{
SelectCommand = new MySqlCommand(selectCommandText,
new MySqlConnection(selectConnString) );
}
#region Properties
///
#if !CF
[Description("Used during Update for deleted rows in Dataset.")]
#endif
public new MySqlCommand DeleteCommand
{
get { return (MySqlCommand)base.DeleteCommand; }
set { base.DeleteCommand = value; }
}
///
#if !CF
[Description("Used during Update for new rows in Dataset.")]
#endif
public new MySqlCommand InsertCommand
{
get { return (MySqlCommand)base.InsertCommand; }
set { base.InsertCommand = value; }
}
///
#if !CF
[Description("Used during Fill/FillSchema")]
[Category("Fill")]
#endif
public new MySqlCommand SelectCommand
{
get { return (MySqlCommand)base.SelectCommand; }
set { base.SelectCommand = value; }
}
///
#if !CF
[Description("Used during Update for modified rows in Dataset.")]
#endif
public new MySqlCommand UpdateCommand
{
get { return (MySqlCommand)base.UpdateCommand; }
set { base.UpdateCommand = value; }
}
internal bool LoadDefaults
{
get { return loadingDefaults; }
set { loadingDefaults = value; }
}
#endregion
#region Batching Support
public override int UpdateBatchSize
{
get { return updateBatchSize; }
set { updateBatchSize = value; }
}
protected override void InitializeBatching()
{
commandBatch = new List();
}
protected override int AddToBatch(IDbCommand command)
{
// the first time each command is asked to be batched, we ask
// that command to prepare its batchable command text. We only want
// to do this one time for each command
MySqlCommand commandToBatch = (MySqlCommand)command;
if (commandToBatch.BatchableCommandText == null)
commandToBatch.GetCommandTextForBatching();
IDbCommand cloneCommand = (IDbCommand)((ICloneable)command).Clone();
commandBatch.Add(cloneCommand);
return commandBatch.Count - 1;
}
protected override int ExecuteBatch()
{
int recordsAffected = 0;
int index = 0;
while (index < commandBatch.Count)
{
MySqlCommand cmd = (MySqlCommand)commandBatch[index++];
for (int index2 = index; index2 < commandBatch.Count; index2++,index++)
{
MySqlCommand cmd2 = (MySqlCommand)commandBatch[index2];
if (cmd2.BatchableCommandText == null ||
cmd2.CommandText != cmd.CommandText) break;
cmd.AddToBatch(cmd2);
}
recordsAffected += cmd.ExecuteNonQuery();
}
return recordsAffected;
}
protected override void ClearBatch()
{
if (commandBatch.Count > 0)
{
MySqlCommand cmd = (MySqlCommand)commandBatch[0];
if (cmd.Batch != null)
cmd.Batch.Clear();
}
commandBatch.Clear();
}
protected override void TerminateBatching()
{
ClearBatch();
commandBatch = null;
}
protected override IDataParameter GetBatchedParameter(int commandIdentifier, int parameterIndex)
{
return (IDataParameter)commandBatch[commandIdentifier].Parameters[parameterIndex];
}
#endregion
///
/// Overridden. See .
///
///
///
///
///
///
override protected RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
{
return new MySqlRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
}
///
/// Overridden. See .
///
///
///
///
///
///
override protected RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
{
return new MySqlRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
}
///
/// Overridden. Raises the RowUpdating event.
///
/// A MySqlRowUpdatingEventArgs that contains the event data.
override protected void OnRowUpdating(RowUpdatingEventArgs value)
{
if (RowUpdating != null)
RowUpdating(this, (value as MySqlRowUpdatingEventArgs));
}
///
/// Overridden. Raises the RowUpdated event.
///
/// A MySqlRowUpdatedEventArgs that contains the event data.
override protected void OnRowUpdated(RowUpdatedEventArgs value)
{
if (RowUpdated != null)
RowUpdated(this, (value as MySqlRowUpdatedEventArgs));
}
}
///
/// Represents the method that will handle the event of a .
///
public delegate void MySqlRowUpdatingEventHandler(object sender, MySqlRowUpdatingEventArgs e);
///
/// Represents the method that will handle the event of a .
///
public delegate void MySqlRowUpdatedEventHandler(object sender, MySqlRowUpdatedEventArgs e);
///
/// Provides data for the RowUpdating event. This class cannot be inherited.
///
public sealed class MySqlRowUpdatingEventArgs : RowUpdatingEventArgs
{
///
/// Initializes a new instance of the MySqlRowUpdatingEventArgs class.
///
/// The to
/// .
/// The to execute during .
/// One of the values that specifies the type of query executed.
/// The sent through an .
public MySqlRowUpdatingEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
: base(row, command, statementType, tableMapping)
{
}
///
/// Gets or sets the MySqlCommand to execute when performing the Update.
///
new public MySqlCommand Command
{
get { return (MySqlCommand)base.Command; }
set { base.Command = value; }
}
}
///
/// Provides data for the RowUpdated event. This class cannot be inherited.
///
public sealed class MySqlRowUpdatedEventArgs : RowUpdatedEventArgs
{
///
/// Initializes a new instance of the MySqlRowUpdatedEventArgs class.
///
/// The sent through an .
/// The executed when is called.
/// One of the values that specifies the type of query executed.
/// The sent through an .
public MySqlRowUpdatedEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
: base(row, command, statementType, tableMapping)
{
}
///
/// Gets or sets the MySqlCommand executed when Update is called.
///
new public MySqlCommand Command
{
get { return (MySqlCommand)base.Command; }
}
}
}