QueryBuilder
public class QueryBuilder
Note: Changing the QueryBuilder should only be needed for adding support for a new database plugin.
QueryBuilder is used in code dealing with variances between the various database engines. As
different databases have different query syntax, sometimes changes need to be
made when generating the actual SQL statement to run. Additional changes should be made by updating the QueryBuilder substitutions array. Every query component
then builds its string representation using that array.
Usage Example:
In this example, a QueryBuilder for PostgreSQL is initialized.
Parameters are set for PostgreSQL and string keywords for SQL queries are substituted into the queryBuilder.
The queryBuilder is then used to create a string description of the Table class instance called todotable.
let queryBuilder = QueryBuilder(withDeleteRequiresUsing: true, withUpdateRequiresFrom: true, createAutoIncrement: createAutoIncrement)
queryBuilder.updateSubstitutions([QueryBuilder.QuerySubstitutionNames.ucase : "UPPER",
QueryBuilder.QuerySubstitutionNames.lcase : "LOWER",
QueryBuilder.QuerySubstitutionNames.len : "LENGTH",
QueryBuilder.QuerySubstitutionNames.numberedParameter : "$",
QueryBuilder.QuerySubstitutionNames.namedParameter : "",
QueryBuilder.QuerySubstitutionNames.double : "double precision"
])
let description = try todotable.build(queryBuilder: queryBuilder)
-
An array of substitutions to be made in query string representation.
Declaration
Swift
public var substitutions: [String] -
Enum defining the cases and their index for the substitutions array used by
QueryBuilderto account for variances between the various database engines.Usage Example:
In this example, a
QueryBuilderfor PostgreSQL is initialized. Parameters are set for PostgreSQL and strings keywords for SQL queries are substituted into the queryBuilder. TheQuerySubstitutionNamesenum is used to refer to substitutions by name instead of their position in the “substitutions” array.
See morelet queryBuilder = QueryBuilder(withDeleteRequiresUsing: true, withUpdateRequiresFrom: true, createAutoIncrement: createAutoIncrement) queryBuilder.updateSubstitutions([QueryBuilder.QuerySubstitutionNames.ucase : "UPPER", QueryBuilder.QuerySubstitutionNames.lcase : "LOWER", QueryBuilder.QuerySubstitutionNames.len : "LENGTH", QueryBuilder.QuerySubstitutionNames.numberedParameter : "$", QueryBuilder.QuerySubstitutionNames.namedParameter : "", QueryBuilder.QuerySubstitutionNames.double : "double precision" ])Declaration
Swift
public enum QuerySubstitutionNames : Int
-
An indication whether the parameters should be numbered (e.g., ‘$1, $2’), or just marked with the numbered parameter marker (e.g., ‘?’).
Declaration
Swift
public let addNumbersToParameters: Bool -
The starting index for numbered parameters.
Declaration
Swift
public let firstParameterIndex: Int -
An indication whether ANY on subqueries is supported.
Declaration
Swift
public let anyOnSubquerySupported: Bool -
An indication whether a
DELETEquery should use theUSINGclause for tables inWITHclause.Declaration
Swift
public let withDeleteRequiresUsing: Bool -
An indication whether an
UPDATEquery should use theFROMclause for tables inWITHclause.Declaration
Swift
public let withUpdateRequiresFrom: Bool -
An implementer of the ColumnCreator protocol that provides methods to build a string representation of a column.
Declaration
Swift
public let columnBuilder: ColumnCreator -
An indication whether the drop index syntax requires the
ON table.nameclause.Declaration
Swift
public let dropIndexRequiresOnTableName: Bool -
DateFormatter to convert between date and string instances.
Declaration
Swift
public let dateFormatter: DateFormatter?
-
init(addNumbersToParameters:firstParameterIndex: anyOnSubquerySupported: withDeleteRequiresUsing: withUpdateRequiresFrom: columnBuilder: dropIndexRequiresOnTableName: dateFormatter: ) Initialize an instance of QueryBuilder.
Usage Example:
In this example, a
QueryBuilderfor PostgreSQL is initialized. Parameters not defined are set to default values.let queryBuilder = QueryBuilder(withDeleteRequiresUsing: true, withUpdateRequiresFrom: true, createAutoIncrement: createAutoIncrement)Declaration
Swift
public init(addNumbersToParameters: Bool = true, firstParameterIndex: Int = 1, anyOnSubquerySupported: Bool = true, withDeleteRequiresUsing: Bool = false, withUpdateRequiresFrom: Bool = false, columnBuilder: ColumnCreator, dropIndexRequiresOnTableName: Bool = false, dateFormatter: DateFormatter? = nil)Parameters
addNumbersToParametersAn indication whether query parameters should be numbered.
firstParameterIndexThe starting index for numbered parameters.
anyOnSubquerySupportedAn indication whether ANY on subqueries is supported.
withDeleteRequiresUsingAn indication whether a
DELETEquery should useUSINGclause for tables inWITHclause.withUpdateRequiresFromAn indication whether an
UPDATEquery should useFROMclause for tables inWITHclause.createAutoIncrementA function to create an autoincrement expression for the column, based on the column type.
dropIndexRequiresOnTableNameAn indication whether the drop index syntax requires
ON table.nameclause.dateFormatterDateFormatter to convert between date and string instances.
-
Update substitutions array of a
QueryBuilderinstance.Usage Example:
In this example, a
QueryBuilderfor PostgreSQL is initialized. The default substitutions are updated for a PostgreSQL database.let queryBuilder = QueryBuilder(withDeleteRequiresUsing: true, withUpdateRequiresFrom: true, createAutoIncrement: createAutoIncrement) queryBuilder.updateSubstitutions([QueryBuilder.QuerySubstitutionNames.ucase : "UPPER", QueryBuilder.QuerySubstitutionNames.lcase : "LOWER", QueryBuilder.QuerySubstitutionNames.len : "LENGTH", QueryBuilder.QuerySubstitutionNames.numberedParameter : "$", QueryBuilder.QuerySubstitutionNames.namedParameter : "", QueryBuilder.QuerySubstitutionNames.double : "double precision" ])Declaration
Swift
public func updateSubstitutions(_ newSubstitutions: [QuerySubstitutionNames : String])Parameters
newSubstitutionsA Dictionary containing the entries to update.
View on GitHub
QueryBuilder Class Reference