/************************************************************************************
*
* Umbraco Data Layer
* MIT Licensed work
* ©2008 Ruben Verborgh
*
***********************************************************************************/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using MSC = MySql.Data.MySqlClient;
namespace umbraco.DataLayer.SqlHelpers.MySql
{
///
/// Sql Helper for a MySQL 5.0 database.
///
public class MySqlHelper : SqlHelper
{
/// SQL parser that replaces the SQL-Server specific tokens by their MySQL equivalent.
private MySqlParser m_SqlParser = new MySqlParser();
/// Initializes a new instance of the class.
/// The connection string.
public MySqlHelper(string connectionString)
: base(connectionString)
{
m_Utility = new MySqlUtility(this);
}
///
/// Creates a new parameter for use with this specific implementation of ISqlHelper.
///
/// Name of the parameter.
/// Value of the parameter.
/// A new parameter of the correct type.
/// Abstract factory pattern
public override IParameter CreateParameter(string parameterName, object value)
{
return new MySqlParameter(parameterName, value);
}
/// Converts a the command before executing.
/// The command text.
/// The original command text.
protected override string ConvertCommand(string commandText)
{
return m_SqlParser.Parse(commandText);
}
/// Executes a command that returns a single value.
/// The command text.
/// The parameters.
/// The return value of the command.
protected override object ExecuteScalar(string commandText, MSC.MySqlParameter[] parameters)
{
return MSC.MySqlHelper.ExecuteScalar(ConnectionString, commandText, parameters);
}
/// Executes a command and returns the number of rows affected.
/// The command text.
/// The parameters.
///
/// The number of rows affected by the command.
///
protected override int ExecuteNonQuery(string commandText, MSC.MySqlParameter[] parameters)
{
return MSC.MySqlHelper.ExecuteNonQuery(ConnectionString, commandText, parameters);
}
/// Executes a command and returns a records reader containing the results.
/// The command text.
/// The parameters.
///
/// A data reader containing the results of the command.
///
protected override IRecordsReader ExecuteReader(string commandText, MSC.MySqlParameter[] parameters)
{
return new MySqlDataReader(MSC.MySqlHelper.ExecuteReader(ConnectionString, commandText, parameters));
}
/// Converts the scalar value to the given type.
/// Desired type of the value.
/// A scalar returned by ExecuteScalar.
/// The scalar, converted to type T.
protected override T ConvertScalar(object scalar)
{
if (scalar == null)
return default(T);
switch (typeof(T).FullName)
{
case "System.Boolean": return (T)(object)((1).Equals(scalar));
default: return base.ConvertScalar(scalar);
}
}
}
}