#region Copyright
// Advanced.Data.Provider.AdpCommand
//
// Copyright (C) 2004 Astrein Engenharia de Manuteno S/A
// Copyright (C) 2004 Everaldo Canuto <everaldo_canuto@yahoo.com.br>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#endregion
using System;
using System.Data;
namespace Advanced.Data.Provider{
/// <summary>
/// Represents a Transact-SQL statement or stored procedure to execute in a database server.
/// </summary>
public class AdpCommand : IAdpCommand
{
#region Fields
internal IDbCommand command;
internal AdpConnection connection;
internal AdpTransaction transaction;
private string commandText;
#endregion
#region Constructors and destructors
/// <summary>
/// Initializes a new instance of the <see cref='AdpCommand'/> class.
/// </summary>
public AdpCommand(): base()
{
// nothing.
}
/// <summary>
/// Initializes a new instance of the <see cref='AdpCommand'/> class with the text of the query and a string connection.
/// </summary>
/// <param name="cmdText">A Transact-SQL statement or stored procedure name to execute.</param>
/// <param name="connectionString">A connection string to create a <see cref='AdpConnection'/> object.</param>
public AdpCommand(string cmdText, string connectionString): base()
{
this.Connection = new AdpConnection(connectionString);
this.CommandText = cmdText;
}
/// <summary>
/// Initializes a new instance of the <see cref='AdpCommand'/> class with the text of the query and a string connection.
/// </summary>
/// <param name="cmdText">A Transact-SQL statement or stored procedure name to execute.</param>
/// <param name="connectionString">A connection string to create a <see cref='AdpConnection'/> object.</param>
/// <param name="cmdType">Gets or sets a value indicating how the CommandText property is to be interpreted.</param>
public AdpCommand(string cmdText, string connectionString, CommandType cmdType): base()
{
this.Connection = new AdpConnection(connectionString);
this.CommandText = cmdText;
this.CommandType = cmdType;
}
/// <summary>
/// Initializes a new instance of the <see cref='AdpCommand'/> class with the text of the query and a <see cref='AdpConnection'/>.
/// </summary>
/// <param name="cmdText">A Transact-SQL statement or stored procedure name to execute.</param>
/// <param name="connection">The <see cref='AdpConnection'/> used by this instance of the <see cref='AdpCommand'/>.</param>
public AdpCommand(string cmdText, AdpConnection connection): base()
{
this.Connection = connection;
this.CommandText = cmdText;
}
/// <summary>
/// Initializes a new instance of the <see cref='AdpCommand'/> class with the text of the query,
/// a <see cref='AdpConnection'/> and a <see cref='AdpTransaction'/>.
/// </summary>
/// <param name="cmdText">A Transact-SQL statement or stored procedure name to execute.</param>
/// <param name="connection">The <see cref='AdpConnection'/> used by this instance of the <see cref='AdpCommand'/>.</param>
/// <param name="transaction">The <see cref='AdpTransaction'/> within which the <see cref='AdpCommand'/> executes.</param>
public AdpCommand(string cmdText, AdpConnection connection, AdpTransaction transaction)
{
this.connection = connection;
this.transaction = transaction;
this.CommandText = cmdText;
}
/// <summary>
/// Releases the resources used by the <see cref='AdpCommand'/>.
/// </summary>
public void Dispose()
{
}
#endregion
#region Public methods
/// <summary>
/// Attempts to cancel the execution of a <see cref='AdpCommand'/>.
/// </summary>
public void Cancel()
{
command.Cancel();
}
/// <summary>
/// Creates a prepared version of the command on an instance of database server.
/// </summary>
public void Prepare()
{
command.Prepare();
}
#endregion
#region Execute
/// <summary>
/// Prepare SQL statement or stored procedure to execute in a database server.
/// </summary>
private void PrepareExecute()
{
// If closed then open connection.
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
/*if (connection.Provider.providerParamPrefix != "@")
{
cmdstr = commandText.Replace(connection.Provider.providerParamPrefix);
}
else
{
cmdstr = commandText;
}*/
}
/// <summary>
/// Executes a Transact-SQL statement defined in CommandText property of <see cref='AdpCommand'/> and returns
/// a <see cref='DataSet'/> object.
/// </summary>
/// <returns>A <see cref='DataSet'/> object.</returns>
public DataSet ExecuteDataSet()
{
PrepareExecute();
AdpDataAdapter adp = new AdpDataAdapter(this);
DataSet result = new DataSet();
adp.Fill(result);
return result;
}
/// <summary>
/// Executes a Transact-SQL statement defined in CommandText property of <see cref='AdpCommand'/> and returns
/// a <see cref='DataSet'/> object.
/// </summary>
/// <param name="table">The table name.</param>
/// <returns>A <see cref='DataSet'/> object.</returns>
public DataSet ExecuteDataSet(string table)
{
PrepareExecute();
AdpDataAdapter adp = new AdpDataAdapter(this);
DataSet result = new DataSet();
adp.Fill(result, table);
return result;
}
/// <summary>
/// Executes a Transact-SQL statement defined in CommandText property of <see cref='AdpCommand'/> and returns
/// a <see cref='DataTable'/> object.
/// </summary>
/// <returns>A <see cref='DataTable'/> object.</returns>
public DataTable ExecuteDataTable()
{
DataSet ds = ExecuteDataSet();
return ds.Tables[0];
}
/// <summary>
/// Executes a Transact-SQL statement defined in CommandText property of <see cref='AdpCommand'/> and returns
/// a <see cref='DataTable'/> object.
/// </summary>
/// <param name="table">The table name.</param>
/// <returns>A <see cref='DataTable'/> object.</returns>
public DataTable ExecuteDataTable(string table)
{
DataSet ds = ExecuteDataSet(table);
return ds.Tables[table];
}
/// <summary>
/// Executes a Transact-SQL statement against the connection and returns the number of rows affected.
/// </summary>
/// <returns>The number os rows affected.</returns>
public int ExecuteNonQuery()
{
PrepareExecute();
return command.ExecuteNonQuery();
}
/// <summary>
/// Overload the implemented interface <see cref='IDataReader'/>.
/// </summary>
/// <returns>A <see cref='AdpDataReader'/> object.</returns>
IDataReader System.Data.IDbCommand.ExecuteReader(System.Data.CommandBehavior behavior)
{
PrepareExecute();
AdpDataReader result = new AdpDataReader();
result.dataReader = command.ExecuteReader(behavior);
return result;
}
/// <summary>
/// Overload the implemented interface <see cref='IDataReader'/>.
/// </summary>
/// <returns>A <see cref='AdpDataReader'/> object.</returns>
IDataReader System.Data.IDbCommand.ExecuteReader()
{
return ((IDbCommand)this).ExecuteReader(CommandBehavior.Default);
}
/// <summary>
/// Sends the CommandText to the Connection and builds a <see cref='AdpDataReader'/> using one of the <see cref='CommandBehavior'/> values.
/// </summary>
/// <param name="behavior">One of the <see cref='CommandBehavior'/> values.</param>
/// <returns>A <see cref='AdpDataReader'/> object.</returns>
public AdpDataReader ExecuteReader(System.Data.CommandBehavior behavior)
{
return (AdpDataReader)((IDbCommand)this).ExecuteReader(behavior);
}
/// <summary>
/// Sends the CommandText to the connection and builds a <see cref='AdpDataReader'/>.
/// </summary>
/// <returns>A AdpDataReader object.</returns>
public AdpDataReader ExecuteReader()
{
return (AdpDataReader)((IDbCommand)this).ExecuteReader();
}
/// <summary>
/// Executes the query, and returns the first column of the first row in the result set returned by the query.
/// Extra columns or rows are ignored.
/// </summary>
/// <returns>The first column of the first row in the result set returned by the query. Extra columns or rows are ignored.</returns>
public object ExecuteScalar()
{
PrepareExecute();
return command.ExecuteScalar();
}
#endregion
#region CreateParameter
/// <summary>
/// Overload the implemented interface <see cref='IDbDataParameter'/>.
/// </summary>
IDbDataParameter System.Data.IDbCommand.CreateParameter()
{
AdpParameter param = new AdpParameter();
param.dataParameter = command.CreateParameter();
if (param.ParameterName == null)
{
int i = Parameters.Count + 1;
param.ParameterName = "param" + i.ToString();
}
Parameters.Add(param);
return param;
}
/// <summary>
/// Creates a new instance of a <see cref='AdpParameter'/> object.
/// </summary>
/// <returns>A <see cref='AdpParameter'/> object.</returns>
public AdpParameter CreateParameter()
{
AdpParameter param = (AdpParameter)((IDbCommand)this).CreateParameter();
//param.paramPrefix = this.Provider.ParamPrefix
//connection.Provider.providerParamPrefix;
return param;
}
/// <summary>
/// Creates a new instance of a <see cref='AdpParameter'/> object.
/// </summary>
/// <param name="parameterName">The name of the <see cref='AdpParameter'/>.</param>
/// <param name="value">The value of the <see cref='AdpParameter'/>.</param>
/// <returns>A <see cref='AdpParameter'/> object.</returns>
public AdpParameter CreateParameter(string parameterName, object value)
{
AdpParameter param = CreateParameter();
param.ParameterName = parameterName;
param.Value = value;
return param;
}
/// <summary>
/// Creates a new instance of a <see cref='AdpParameter'/> object.
/// </summary>
/// <param name="parameterName">The name of the <see cref='AdpParameter'/>.</param>
/// <param name="dbType">The data type of the <see cref='AdpParameter'/>.</param>
/// <returns>A <see cref='AdpParameter'/> object.</returns>
public AdpParameter CreateParameter(string parameterName, DbType dbType)
{
AdpParameter param = CreateParameter();
param.ParameterName = parameterName;
param.DbType = dbType;
return param;
}
/// <summary>
/// Creates a new instance of a <see cref='AdpParameter'/> object.
/// </summary>
/// <param name="parameterName">The name of the <see cref='AdpParameter'/>.</param>
/// <param name="dbType">The data type of the <see cref='AdpParameter'/>.</param>
/// <param name="size">The maximum size, in bytes, of the data within the column.</param>
/// <returns>A <see cref='AdpParameter'/> object.</returns>
public AdpParameter CreateParameter(string parameterName, DbType dbType, int size)
{
AdpParameter param = CreateParameter(parameterName, dbType);
param.Size = size;
return param;
}
/// <summary>
/// Creates a new instance of a <see cref='AdpParameter'/> object.
/// </summary>
/// <param name="parameterName">The name of the <see cref='AdpParameter'/>.</param>
/// <param name="dbType">The data type of the <see cref='AdpParameter'/>.</param>
/// <param name="size">The maximum size, in bytes, of the data within the column.</param>
/// <param name="sourceColumn">The name of the source column that is mapped to the <see cref='DataSet'/> and used for loading or returning the Value.</param>
/// <returns>A AdpParameter object.</returns>
public AdpParameter CreateParameter(string parameterName, DbType dbType, int size, string sourceColumn)
{
AdpParameter param = CreateParameter(parameterName, dbType, size);
param.SourceColumn = sourceColumn;
return param;
}
/// <summary>
/// Creates a new instance of a <see cref='AdpParameter'/> object.
/// </summary>
/// <param name="parameterName">The name of the <see cref='AdpParameter'/>.</param>
/// <param name="dbType">The data type of the <see cref='AdpParameter'/>.</param>
/// <param name="size">The maximum size, in bytes, of the data within the column.</param>
/// <param name="direction">Indicating whether the parameter is input-only, output-only, bidirectional, or a stored procedure return value parameter.</param>
/// <param name="precision">The maximum number of digits used to represent the Value property.</param>
/// <param name="scale">The number of decimal places to which Value is resolved.</param>
/// <param name="sourceColumn">The name of the source column that is mapped to the <see cref='DataSet'/> and used for loading or returning the Value.</param>
/// <param name="sourceVersion">The <see cref='DataRowVersion'/> to use when loading Value.</param>
/// <param name="value">The value of the <see cref='AdpParameter'/>.</param>
/// <returns>A <see cref='AdpParameter'/> object.</returns>
public AdpParameter CreateParameter(string parameterName, DbType dbType, int size, ParameterDirection direction, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
{
AdpParameter param = CreateParameter(parameterName, dbType, size, sourceColumn);
param.Direction = direction;
param.Precision = precision;
param.Scale = scale;
param.SourceVersion = sourceVersion;
param.Value = value;
return param;
}
#endregion
#region Public properties
/// <summary>
/// Overload the implemented interface <see cref='IDbConnection'/>.
/// </summary>
IDbConnection System.Data.IDbCommand.Connection
{
get
{
return connection;
}
set
{
connection=(AdpConnection)value;
}
}
/// <summary>
/// Gets or sets the <see cref='AdpConnection'/> used by this instance of the <see cref='AdpCommand'/>.
/// </summary>
public AdpConnection Connection
{
get
{
return connection;
}
set
{
connection = value;
command = connection.connection.CreateCommand();
}
}
/// <summary>
/// Gets or sets the Transact-SQL statement or stored procedure to execute at the data source.
/// </summary>
public string CommandText
{
get
{
return commandText;
}
set
{
commandText = value;
command.CommandText = commandText;
}
}
/// <summary>
/// Gets or sets the wait time before terminating the attempt to execute a command and generating an error.
/// </summary>
public int CommandTimeout
{
get
{
return command.CommandTimeout;
}
set
{
command.CommandTimeout=value;
}
}
/// <summary>
/// Gets or sets a value indicating how the CommandText property is to be interpreted.
/// </summary>
public System.Data.CommandType CommandType
{
get
{
return command.CommandType;
}
set
{
command.CommandType = value;
}
}
/// <summary>
/// Overload the implemented interface <see cref='IDataParameterCollection'/>.
/// </summary>
IDataParameterCollection System.Data.IDbCommand.Parameters
{
get
{
AdpParameterCollection param = new AdpParameterCollection();
param._Collection = command.Parameters;
return param;
}
}
/// <summary>
/// Gets the <see cref='AdpParameterCollection'/>.
/// </summary>
public AdpParameterCollection Parameters
{
get
{
return (AdpParameterCollection)((IDbCommand)this).Parameters;
}
}
/// <summary>
/// Gets the <see cref='AdpProvider'/> used by this instance of the <see cref='AdpCommand'/>.
/// </summary>
public AdpProvider Provider
{
get
{
return connection.Provider;
}
}
/// <summary>
/// Overload the implemented interface <see cref='IDbTransaction'/>.
/// </summary>
IDbTransaction System.Data.IDbCommand.Transaction
{
get
{
return transaction;
}
set
{
transaction=(AdpTransaction)value;
}
}
/// <summary>
/// Gets or sets the <see cref='AdpTransaction'/> within which the <see cref='AdpCommand'/> executes.
/// </summary>
public AdpTransaction Transaction
{
get
{
return transaction;
}
set
{
transaction=value;
command.Transaction=transaction.transaction;
}
}
/// <summary>
/// Gets or sets how command results are applied to the DataRow when used by the Update method of the <see cref='AdpDataAdapter'/>.
/// </summary>
public System.Data.UpdateRowSource UpdatedRowSource
{
get
{
return command.UpdatedRowSource;
}
set
{
command.UpdatedRowSource=value;
}
}
#endregion
}
}
|