CRM_Utils_SQL_Insert
in package
uses
CRM_Utils_SQL_EscapeStringTrait
Object-oriented SQL builder for INSERT queries.
Usage: $insert = CRM_Utils_SQL_Insert::into('mytable') ->row(array('col1' => '1', 'col2' => '2' )) ->row(array('col1' => '2b', 'col2' => '1b')); echo $insert->toSQL();
Note: In MySQL, numeric values may be escaped. Except for NULL values, it's reasonable for us to simply escape all values by default -- without any knowledge of the underlying schema.
Design principles:
- Portable
- No knowledge of the underlying SQL API (except for escaping -- CRM_Core_DAO::escapeString)
- No knowledge of the underlying data model
- Single file
- SQL clauses correspond to PHP functions ($select->where("foo_id=123"))
Table of Contents
Properties
- $allowLiterals : mixed
- $columns : array<string|int, mixed>
- Array<string> list of column names
- $rows : array<string|int, mixed>
- $table : string
- $verb : string
Methods
- __construct() : mixed
- Create a new SELECT query.
- allowLiterals() : mixed
- columns() : CRM_Utils_SQL_Insert
- Get columns.
- dao() : CRM_Utils_SQL_Insert
- Insert a record based on a DAO.
- escapeString() : string
- execute() : CRM_Core_DAO
- Execute the query.
- into() : CRM_Utils_SQL_Insert
- Create a new INSERT query.
- row() : CRM_Utils_SQL_Insert
- Get row.
- rows() : CRM_Utils_SQL_Insert
- Get rows.
- toSQL() : string
- Convert to SQL.
- usingReplace() : CRM_Utils_SQL_Insert
- Use REPLACE INTO instead of INSERT INTO.
Properties
$allowLiterals
protected
mixed
$allowLiterals
= \FALSE
$columns
Array<string> list of column names
private
array<string|int, mixed>
$columns
$rows
private
array<string|int, mixed>
$rows
$table
private
string
$table
$verb
private
string
$verb
Ex: 'INSERT INTO', 'REPLACE INTO'
Methods
__construct()
Create a new SELECT query.
public
__construct(string $table[, string $verb = 'INSERT INTO' ]) : mixed
Parameters
- $table : string
-
Table-name and optional alias.
- $verb : string = 'INSERT INTO'
-
Ex: 'INSERT INTO', 'REPLACE INTO'
allowLiterals()
public
allowLiterals([bool $allowLiterals = TRUE ]) : mixed
Parameters
- $allowLiterals : bool = TRUE
columns()
Get columns.
public
columns(array<string|int, mixed> $columns) : CRM_Utils_SQL_Insert
Parameters
- $columns : array<string|int, mixed>
Tags
Return values
CRM_Utils_SQL_Insertdao()
Insert a record based on a DAO.
public
static dao(CRM_Core_DAO $dao) : CRM_Utils_SQL_Insert
Parameters
- $dao : CRM_Core_DAO
Tags
Return values
CRM_Utils_SQL_InsertescapeString()
public
escapeString(string|null $value) : string
Parameters
- $value : string|null
Return values
string —SQL expression, e.g. "it's great" (with-quotes) or NULL (without-quotes)
execute()
Execute the query.
public
execute([bool $i18nRewrite = TRUE ]) : CRM_Core_DAO
Parameters
- $i18nRewrite : bool = TRUE
-
If the system has multilingual features, should the field/table names be rewritten?
Tags
Return values
CRM_Core_DAOinto()
Create a new INSERT query.
public
static into(string $table[, string $verb = 'INSERT INTO' ]) : CRM_Utils_SQL_Insert
Parameters
- $table : string
-
Table-name and optional alias.
- $verb : string = 'INSERT INTO'
-
Ex: 'INSERT INTO', 'REPLACE INTO'
Return values
CRM_Utils_SQL_Insertrow()
Get row.
public
row(array<string|int, mixed> $row) : CRM_Utils_SQL_Insert
Parameters
- $row : array<string|int, mixed>
Tags
Return values
CRM_Utils_SQL_Insertrows()
Get rows.
public
rows(array<string|int, mixed> $rows) : CRM_Utils_SQL_Insert
Parameters
- $rows : array<string|int, mixed>
Return values
CRM_Utils_SQL_InserttoSQL()
Convert to SQL.
public
toSQL() : string
Return values
string —SQL statement
usingReplace()
Use REPLACE INTO instead of INSERT INTO.
public
usingReplace([bool $asReplace = TRUE ]) : CRM_Utils_SQL_Insert
Parameters
- $asReplace : bool = TRUE