using System;
using System.Collections.Generic;
using System.Text;
namespace ScrewTurn.Wiki.Plugins.SqlCommon {
///
/// A tool for building queries.
///
public class QueryBuilder {
private ICommandBuilder builder;
///
/// Initializes a new instance of the class.
///
/// The command builder.
public QueryBuilder(ICommandBuilder builder) {
this.builder = builder;
}
///
/// Initializes a new instance of the class.
///
/// The command builder.
/// The new instance.
public static QueryBuilder NewQuery(ICommandBuilder builder) {
return new QueryBuilder(builder);
}
///
/// Builds a SELECT query.
///
/// The table.
/// The columns to select.
/// The SELECT query.
public string SelectFrom(string table, string[] columns) {
StringBuilder sb = new StringBuilder(100);
sb.Append("select ");
for(int i = 0; i < columns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(columns[i]);
sb.Append(builder.ObjectNameSuffix);
if(i != columns.Length - 1) sb.Append(",");
sb.Append(" ");
}
sb.Append("from ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
return sb.ToString();
}
///
/// Builds a SELECT query.
///
/// The table.
/// The SELECT query.
public string SelectFrom(string table) {
StringBuilder sb = new StringBuilder(100);
sb.Append("select * from ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
return sb.ToString();
}
///
/// Builds a SELECT query with a JOIN clause.
///
/// The min table.
/// The joined table.
/// The main table column to join.
/// The joined table column to join.
/// The JOIN type.
/// The main table columns to select.
/// The joined table columns to select.
/// The SELECT query (returned columns are named like [Table_Column]).
public string SelectFrom(string table, string joinedTable, string tableColumn, string joinedTableColumn, Join join,
string[] tableColumns, string[] joinedTableColumns) {
return SelectFrom(table, joinedTable, new string[] { tableColumn }, new string[] { joinedTableColumn }, join,
tableColumns, joinedTableColumns);
}
///
/// Builds a SELECT query with a JOIN clause.
///
/// The min table.
/// The joined table.
/// The main table columns to join.
/// The joined table columns to join.
/// The JOIN type.
/// The main table columns to select.
/// The joined table columns to select.
/// The SELECT query (returned columns are named like [Table_Column]).
public string SelectFrom(string table, string joinedTable, string[] joinTableColumns, string[] joinJoinedTableColumns, Join join,
string[] tableColumns, string[] joinedTableColumns) {
StringBuilder sb = new StringBuilder(200);
sb.Append("select ");
for(int i = 0; i < tableColumns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(tableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" as ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append("_");
sb.Append(tableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(", ");
}
for(int i = 0; i < joinedTableColumns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" as ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTable);
sb.Append("_");
sb.Append(joinedTableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
if(i != joinedTableColumns.Length - 1) sb.Append(", ");
}
sb.Append(" from ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" ");
sb.Append(JoinToString(join));
sb.Append(" ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" on ");
for(int i = 0; i < joinTableColumns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinTableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" = ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinJoinedTableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
if(i != joinTableColumns.Length - 1) sb.Append(" and ");
}
return sb.ToString();
}
///
/// Builds a SELECT query with a JOIN clause.
///
/// The main table.
/// The joined table.
/// The main table column to join.
/// The joined table column to join.
/// The JOIN type.
/// The SELECT query.
public string SelectFrom(string table, string joinedTable, string tableColumn, string joinedTableColumn, Join join) {
StringBuilder sb = new StringBuilder(100);
sb.Append("select * from ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" ");
sb.Append(JoinToString(join));
sb.Append(" ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" on ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(tableColumn);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" = ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTableColumn);
sb.Append(builder.ObjectNameSuffix);
return sb.ToString();
}
///
/// Builds a SELECT query with two JOIN clauses (both on the main table).
///
/// The main table.
/// The joined table.
/// The main table column to join.
/// The joined table column to join.
/// The join.
/// The main table columns to select.
/// The joined table columns to select.
/// The other joined table.
/// The other joined table column to join.
/// The join.
/// The other joined table columns to select.
/// The SELECT query (returned columns are named like [Table_Column]).
public string SelectFrom(string table, string joinedTable, string tableColumn, string joinedTableColumn, Join join,
string[] tableColumns, string[] joinedTableColumns,
string otherJoinedTable, string otherJoinedTableColumn, Join otherJoin, string[] otherJoinedTableColumns) {
StringBuilder sb = new StringBuilder(200);
sb.Append("select ");
for(int i = 0; i < tableColumns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(tableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" as ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append("_");
sb.Append(tableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(", ");
}
for(int i = 0; i < joinedTableColumns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" as ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTable);
sb.Append("_");
sb.Append(joinedTableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(", ");
}
for(int i = 0; i < otherJoinedTableColumns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(otherJoinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(otherJoinedTableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" as ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(otherJoinedTable);
sb.Append("_");
sb.Append(otherJoinedTableColumns[i]);
sb.Append(builder.ObjectNameSuffix);
if(i != otherJoinedTableColumns.Length - 1) sb.Append(", ");
}
sb.Append(" from ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" ");
sb.Append(JoinToString(join));
sb.Append(" ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" on ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(tableColumn);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" = ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(joinedTableColumn);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" ");
sb.Append(JoinToString(otherJoin));
sb.Append(" ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(otherJoinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" on ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(tableColumn);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" = ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(otherJoinedTable);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
sb.Append(builder.ObjectNamePrefix);
sb.Append(otherJoinedTableColumn);
sb.Append(builder.ObjectNameSuffix);
return sb.ToString();
}
///
/// Converts a Join type to its string representation.
///
/// The join type.
/// The string representation.
private static string JoinToString(Join join) {
switch(join) {
case Join.Join:
return "join";
case Join.InnerJoin:
return "inner join";
case Join.LeftJoin:
return "left join";
case Join.RightJoin:
return "right join";
default:
throw new NotSupportedException();
}
}
///
/// Builds a SELECT COUNT(*) query.
///
/// The table.
/// The SELECT query.
public string SelectCountFrom(string table) {
StringBuilder sb = new StringBuilder(100);
sb.Append("select count(*) from ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
return sb.ToString();
}
///
/// Builds a WHERE clause condition.
///
/// The table the column belongs to, or null.
/// The column.
/// The operator.
/// The parameter name.
/// The condition.
private string BuildWhereClause(string table, string column, WhereOperator op, string parameter) {
StringBuilder sb = new StringBuilder(80);
if(table != null) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
}
sb.Append(builder.ObjectNamePrefix);
sb.Append(column);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" ");
sb.Append(WhereOperatorToString(op));
if(op != WhereOperator.IsNull && op != WhereOperator.IsNotNull) {
sb.Append(" ");
if(builder.UseNamedParameters) {
sb.Append(builder.ParameterNamePrefix);
sb.Append(parameter);
sb.Append(builder.ParameterNameSuffix);
}
else sb.Append(builder.ParameterPlaceholder);
}
return sb.ToString();
}
///
/// Applies a WHERE clause to a query.
///
/// The query.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// The resulting query.
public string Where(string query, string column, WhereOperator op, string parameter) {
return Where(query, null, column, op, parameter, false, false);
}
///
/// Applies a WHERE clause to a query.
///
/// The query.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// A value indicating whether to open a bracket after the WHERE.
/// A value indicating whether to close a bracket after the clause.
/// The resulting query.
public string Where(string query, string column, WhereOperator op, string parameter, bool openBracket, bool closeBracket) {
return Where(query, null, column, op, parameter, openBracket, closeBracket);
}
///
/// Applies a WHERE clause to a query.
///
/// The query.
/// The table the column belongs to.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// The resulting query.
public string Where(string query, string table, string column, WhereOperator op, string parameter) {
return Where(query, table, column, op, parameter, false, false);
}
///
/// Applies a WHERE clause to a query.
///
/// The query.
/// The table the column belongs to.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// A value indicating whether to open a bracket after the WHERE.
/// A value indicating whether to close a bracket after the clause.
/// The resulting query.
public string Where(string query, string table, string column, WhereOperator op, string parameter, bool openBracket, bool closeBracket) {
StringBuilder sb = new StringBuilder(150);
sb.Append(query);
sb.Append(" where ");
if(openBracket) sb.Append("(");
sb.Append(BuildWhereClause(table, column, op, parameter));
if(closeBracket) sb.Append(")");
return sb.ToString();
}
///
/// Builds a WHERE clause with IN operator.
///
/// The table the column belongs to, or null.
/// The column subject of the WHERE clause.
/// The names of the parameters in the IN set.
/// The resulting clause.
private string BuildWhereInClause(string table, string column, string[] parameters) {
StringBuilder sb = new StringBuilder(100);
if(!string.IsNullOrEmpty(table)) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
}
sb.Append(builder.ObjectNamePrefix);
sb.Append(column);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" in (");
for(int i = 0; i < parameters.Length; i++) {
if(builder.UseNamedParameters) {
sb.Append(builder.ParameterNamePrefix);
sb.Append(parameters[i]);
sb.Append(builder.ParameterNameSuffix);
}
else {
sb.Append(builder.ParameterPlaceholder);
}
if(i != parameters.Length - 1) sb.Append(", ");
}
sb.Append(")");
return sb.ToString();
}
///
/// Applies a WHERE clause with IN operator to a query.
///
/// The query.
/// The column subject of the WHERE clause.
/// The names of the parameters in the IN set.
/// The resulting query.
public string WhereIn(string query, string column, string[] parameters) {
return WhereIn(query, null, column, parameters);
}
///
/// Applies a WHERE clause with IN operator to a query.
///
/// The query.
/// The table the column belongs to.
/// The column subject of the WHERE clause.
/// The names of the parameters in the IN set.
/// The resulting query.
public string WhereIn(string query, string table, string column, string[] parameters) {
StringBuilder sb = new StringBuilder(200);
sb.Append(query);
sb.Append(" where ");
sb.Append(BuildWhereInClause(table, column, parameters));
return sb.ToString();
}
///
/// Applies a WHERE NOT IN (subQuery) clause to a query.
///
/// The query.
/// The table the column belongs to.
/// The column.
/// The subQuery.
/// The resulting query.
public string WhereNotInSubquery(string query, string table, string column, string subQuery) {
StringBuilder sb = new StringBuilder(200);
sb.Append(query);
sb.Append(" where ");
if(table != null) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(".");
}
sb.Append(builder.ObjectNamePrefix);
sb.Append(column);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" not in (");
sb.Append(subQuery);
sb.Append(")");
return sb.ToString();
}
///
/// Adds another WHERE clause to a query.
///
/// The query.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// The resulting query.
public string AndWhere(string query, string column, WhereOperator op, string parameter) {
return AndWhere(query, column, op, parameter, false, false);
}
///
/// Adds another WHERE clause to a query.
///
/// The query.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// A value indicating whether to open a bracket after the AND.
/// A value indicating whether to close a bracket after the clause.
/// The resulting query.
public string AndWhere(string query, string column, WhereOperator op, string parameter, bool openBracket, bool closeBracket) {
return AndWhere(query, null, column, op, parameter, openBracket, closeBracket);
}
///
/// Adds another WHERE clause to a query.
///
/// The query.
/// The table the column belongs to.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// The resulting query.
public string AndWhere(string query, string table, string column, WhereOperator op, string parameter) {
return AndWhere(query, table, column, op, parameter, false, false);
}
///
/// Adds another WHERE clause to a query.
///
/// The query.
/// The table the column belongs to.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// A value indicating whether to open a bracket after the AND.
/// A value indicating whether to close a bracket after the clause.
/// The resulting query.
public string AndWhere(string query, string table, string column, WhereOperator op, string parameter, bool openBracket, bool closeBracket) {
StringBuilder sb = new StringBuilder(200);
sb.Append(query);
sb.Append(" and ");
if(openBracket) sb.Append("(");
sb.Append(BuildWhereClause(table, column, op, parameter));
if(closeBracket) sb.Append(")");
return sb.ToString();
}
///
/// Adds another WHERE clause to a query.
///
/// The query.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// The resulting query.
public string OrWhere(string query, string column, WhereOperator op, string parameter) {
return OrWhere(query, column, op, parameter, false, false);
}
///
/// Adds another WHERE clause to a query.
///
/// The query.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// A value indicating whether to open a bracket after the OR.
/// A value indicating whether to close a bracket after the clause.
/// The resulting query.
public string OrWhere(string query, string column, WhereOperator op, string parameter, bool openBracket, bool closeBracket) {
return OrWhere(query, null, column, op, parameter, openBracket, closeBracket);
}
///
/// Adds another WHERE clause to a query.
///
/// The query.
/// The table the column belongs to.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// The resulting query.
public string OrWhere(string query, string table, string column, WhereOperator op, string parameter) {
return OrWhere(query, table, column, op, parameter, false, false);
}
///
/// Adds another WHERE clause to a query.
///
/// The query.
/// The table the column belongs to.
/// The column subject of the WHERE clause.
/// The operator.
/// The name of the parameter for the WHERE clause.
/// A value indicating whether to open a bracket after the AND.
/// A value indicating whether to close a bracket after the clause.
/// The resulting query.
public string OrWhere(string query, string table, string column, WhereOperator op, string parameter, bool openBracket, bool closeBracket) {
StringBuilder sb = new StringBuilder(200);
sb.Append(query);
sb.Append(" or ");
if(openBracket) sb.Append("(");
sb.Append(BuildWhereClause(table, column, op, parameter));
if(closeBracket) sb.Append(")");
return sb.ToString();
}
///
/// Converts a WHERE operator to its corresponding string.
///
/// The operator.
/// The string.
private static string WhereOperatorToString(WhereOperator op) {
switch(op) {
case WhereOperator.Like:
return "like";
case WhereOperator.Equals:
return "=";
case WhereOperator.NotEquals:
return "<>";
case WhereOperator.GreaterThan:
return ">";
case WhereOperator.LessThan:
return "<";
case WhereOperator.GreaterThanOrEqualTo:
return ">=";
case WhereOperator.LessThanOrEqualTo:
return "<=";
case WhereOperator.IsNull:
return "is null";
case WhereOperator.IsNotNull:
return "is not null";
default:
throw new NotSupportedException();
}
}
///
/// Applies an ORDER BY clause to a query.
///
/// The query.
/// The columns to order by.
/// The ordering directions for each column.
/// The resulting query.
public string OrderBy(string query, string[] columns, Ordering[] orderings) {
StringBuilder sb = new StringBuilder(200);
sb.Append(query);
sb.Append(" order by ");
for(int i = 0; i < columns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(columns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" ");
sb.Append(OrderingToString(orderings[i]));
if(i != columns.Length - 1) sb.Append(", ");
}
return sb.ToString();
}
///
/// Converts an ordering to string.
///
/// The ordering.
/// The string.
private static string OrderingToString(Ordering ordering) {
switch(ordering) {
case Ordering.Asc:
return "asc";
case Ordering.Desc:
return "desc";
default:
throw new NotSupportedException();
}
}
///
/// Applies a GROUP BY clause to a query.
///
/// The query.
/// The columns to group by.
/// The resulting query.
public string GroupBy(string query, string[] columns) {
StringBuilder sb = new StringBuilder(200);
sb.Append(query);
sb.Append(" group by ");
for(int i = 0; i < columns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(columns[i]);
sb.Append(builder.ObjectNameSuffix);
if(i != columns.Length - 1) sb.Append(", ");
}
return sb.ToString();
}
///
/// Builds an INSERT INTO query.
///
/// The destination table.
/// The columns names.
/// The parameters names.
/// The INSERT INTO query.
public string InsertInto(string table, string[] columns, string[] parameters) {
StringBuilder sb = new StringBuilder(200);
sb.Append("insert into ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" (");
for(int i = 0; i < columns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(columns[i]);
sb.Append(builder.ObjectNameSuffix);
if(i != columns.Length - 1) sb.Append(", ");
}
sb.Append(") values (");
for(int i = 0; i < parameters.Length; i++) {
if(builder.UseNamedParameters) {
sb.Append(builder.ParameterNamePrefix);
sb.Append(parameters[i]);
sb.Append(builder.ParameterNameSuffix);
}
else {
sb.Append(builder.ParameterPlaceholder);
}
if(i != parameters.Length - 1) sb.Append(", ");
}
sb.Append(")");
return sb.ToString();
}
///
/// Builds an UPDATE query.
///
/// The table.
/// The columns to update.
/// The parameters.
/// The UPDATE query, without any WHERE clause.
public string Update(string table, string[] columns, string[] parameters) {
StringBuilder sb = new StringBuilder(100);
sb.Append("update ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" set ");
for(int i = 0; i < columns.Length; i++) {
sb.Append(builder.ObjectNamePrefix);
sb.Append(columns[i]);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" = ");
if(builder.UseNamedParameters) {
sb.Append(builder.ParameterNamePrefix);
sb.Append(parameters[i]);
sb.Append(builder.ParameterNameSuffix);
}
else {
sb.Append(builder.ParameterPlaceholder);
}
if(i != columns.Length - 1) sb.Append(", ");
}
return sb.ToString();
}
///
/// Builds an UPDATE query that increments the numerical value of a column by one.
///
/// The table.
/// The column to update.
/// The increment or decrement value.
/// The UPDATE query, without any WHERE clause.
public string UpdateIncrement(string table, string column, int increment) {
StringBuilder sb = new StringBuilder(100);
sb.Append("update ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" set ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(column);
sb.Append(builder.ObjectNameSuffix);
sb.Append(" = ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(column);
sb.Append(builder.ObjectNameSuffix);
if(increment > 0) sb.Append(" + ");
else sb.Append(" - ");
sb.Append(Math.Abs(increment).ToString());
return sb.ToString();
}
///
/// Builds a DELETE FROM query.
///
/// The table.
/// The DELETE FROM query, without any WHERE clause.
public string DeleteFrom(string table) {
StringBuilder sb = new StringBuilder(100);
sb.Append("delete from ");
sb.Append(builder.ObjectNamePrefix);
sb.Append(table);
sb.Append(builder.ObjectNameSuffix);
return sb.ToString();
}
///
/// Appends a query to an existing query for batch execution.
///
/// The query.
/// The second query.
/// The resulting query.
public string AppendForBatch(string query, string secondQuery) {
return query + builder.BatchQuerySeparator + secondQuery;
}
}
///
/// Lists WHERE operators.
///
public enum WhereOperator {
///
/// LIKE.
///
Like,
///
/// =.
///
Equals,
///
/// <>.
///
NotEquals,
///
/// >.
///
GreaterThan,
///
/// <
///
LessThan,
///
/// >=.
///
GreaterThanOrEqualTo,
///
/// <=.
///
LessThanOrEqualTo,
///
/// IS NULL.
///
IsNull,
///
/// IS NOT NULL.
///
IsNotNull
}
///
/// List JOIN types.
///
public enum Join {
///
/// JOIN.
///
Join,
///
/// INNER JOIN.
///
InnerJoin,
///
/// LEFT JOIN.
///
LeftJoin,
///
/// RIGHT JOIN.
///
RightJoin
}
///
/// Lists ordering directions.
///
public enum Ordering {
///
/// Ascending.
///
Asc,
///
/// Descending.
///
Desc
}
}