using System;
using System.Collections.Generic;
using System.Text;
using ScrewTurn.Wiki.Plugins.SqlCommon;
using System.Data.Common;
using System.Data.SqlClient;
namespace ScrewTurn.Wiki.Plugins.SqlServer {
///
/// Implements a command builder for SQL Server.
///
public class SqlServerCommandBuilder : ICommandBuilder {
///
/// Gets the table and column name prefix.
///
public string ObjectNamePrefix {
get { return "["; }
}
///
/// Gets the table and column name suffix.
///
public string ObjectNameSuffix {
get { return "]"; }
}
///
/// Gets the parameter name prefix.
///
public string ParameterNamePrefix {
get { return "@"; }
}
///
/// Gets the parameter name suffix.
///
public string ParameterNameSuffix {
get { return ""; }
}
///
/// Gets the parameter name placeholder.
///
public string ParameterPlaceholder {
get { throw new NotImplementedException(); }
}
///
/// Gets a value indicating whether to use named parameters. If false,
/// parameter placeholders will be equal to .
///
public bool UseNamedParameters {
get { return true; }
}
///
/// Gets the string to use in order to separate queries in a batch.
///
public string BatchQuerySeparator {
get { return "; "; }
}
///
/// Gets a new database connection, open.
///
/// The connection string.
/// The connection.
public DbConnection GetConnection(string connString) {
DbConnection cn = new SqlConnection(connString);
cn.Open();
return cn;
}
///
/// Gets a properly built database command, with the underlying connection already open.
///
/// The connection string.
/// The prepared query.
/// The parameters, if any.
/// The command.
public DbCommand GetCommand(string connString, string preparedQuery, List parameters) {
return GetCommand(GetConnection(connString), preparedQuery, parameters);
}
///
/// Gets a properly built database command, re-using an open connection.
///
/// The open connection to use.
/// The prepared query.
/// The parameters, if any.
/// The command.
public DbCommand GetCommand(DbConnection connection, string preparedQuery, List parameters) {
DbCommand cmd = connection.CreateCommand();
cmd.CommandText = preparedQuery;
foreach(Parameter param in parameters) {
cmd.Parameters.Add(new SqlParameter("@" + param.Name, param.Value));
}
return cmd;
}
///
/// Gets a properly built database command, re-using an open connection and a begun transaction.
///
/// The transaction.
/// The prepared query.
/// The parameters, if any.
/// The command.
public DbCommand GetCommand(DbTransaction transaction, string preparedQuery, List parameters) {
DbCommand cmd = transaction.Connection.CreateCommand();
cmd.Transaction = transaction;
cmd.CommandText = preparedQuery;
foreach(Parameter param in parameters) {
cmd.Parameters.Add(new SqlParameter("@" + param.Name, param.Value));
}
return cmd;
}
}
}