class CRM_Utils_SQL_Insert

Dear God Why Do I Have To Write This (Dumb SQL Builder)

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"))

Methods

into(string $table)

Create a new INSERT query.

dao( $dao)

Insert a record based on a DAO.

__construct(string $table)

Create a new SELECT query.

columns(array $columns)

Get columns.

rows(array $rows)

Get rows.

row(array $row)

Get row.

usingReplace(bool $asReplace = TRUE)

Use REPLACE INTO instead of INSERT INTO.

string
escapeString(string|NULL $value)

Escape string.

string
toSQL()

Convert to SQL.

Details

at line 49
static CRM_Utils_SQL_Insert into(string $table)

Create a new INSERT query.

Parameters

string $table Table-name and optional alias.

Return Value

CRM_Utils_SQL_Insert

at line 60
static CRM_Utils_SQL_Insert dao( $dao)

Insert a record based on a DAO.

Parameters

$dao

Return Value

CRM_Utils_SQL_Insert

Exceptions

CRM_Core_Exception

at line 81
__construct(string $table)

Create a new SELECT query.

Parameters

string $table Table-name and optional alias.

at line 94
CRM_Utils_SQL_Insert columns(array $columns)

Get columns.

Parameters

array $columns

Return Value

CRM_Utils_SQL_Insert

Exceptions

CRM_Core_Exception

at line 109
CRM_Utils_SQL_Insert rows(array $rows)

Get rows.

Parameters

array $rows

Return Value

CRM_Utils_SQL_Insert

at line 124
CRM_Utils_SQL_Insert row(array $row)

Get row.

Parameters

array $row

Return Value

CRM_Utils_SQL_Insert

Exceptions

CRM_Core_Exception

at line 151
CRM_Utils_SQL_Insert usingReplace(bool $asReplace = TRUE)

Use REPLACE INTO instead of INSERT INTO.

Parameters

bool $asReplace

Return Value

CRM_Utils_SQL_Insert

at line 164
protected string escapeString(string|NULL $value)

Escape string.

Parameters

string|NULL $value

Return Value

string SQL expression, e.g. "it\'s great" (with-quotes) or NULL (without-quotes)

at line 174
string toSQL()

Convert to SQL.

Return Value

string SQL statement