Documentation

QueryBuilder extends CI_DB_query_builder
in package

AbstractYes

Extended Query Builder class.

Provides enhanced query building with additional methods like INSERT ON DUPLICATE KEY UPDATE and improved error handling.

Usage:

// Insert with duplicate key update
$id = $this->db
  ->set(['name' => 'John', 'email' => 'john@example.com'])
  ->insert_on_duplicate_update('users');

// Batch insert with duplicate key update
$affected = $this->db->insert_on_duplicate_update_batch('users', [
  ['name' => 'John', 'email' => 'john@example.com'],
  ['name' => 'Jane', 'email' => 'jane@example.com']
]);

Table of Contents

Methods

__construct()  : mixed
Initialize query builder.
insert()  : int
Insert a single row.
insert_batch()  : array<string|int, int>
Insert multiple rows.
insert_on_duplicate_update()  : int
Insert or update on duplicate key (MySQL specific).
insert_on_duplicate_update_batch()  : int
Batch insert or update on duplicate key (MySQL specific).
isset_qb_from()  : bool
Check if FROM table is set in query builder.
load_rdriver()  : string
Load the result driver class.
query()  : mixed
Execute a raw SQL query.
update()  : void
Update rows.
update_batch()  : int
Update multiple rows in batch.
_insert_on_duplicate_update()  : string
Generate INSERT ON DUPLICATE KEY UPDATE SQL statement.
_insert_on_duplicate_update_batch()  : string
Generate batch INSERT ON DUPLICATE KEY UPDATE SQL statement.

Methods

__construct()

Initialize query builder.

public __construct(array<string|int, mixed> $config) : mixed
Parameters
$config : array<string|int, mixed>

Database configuration array.

insert()

Insert a single row.

public insert([string $table = '' ][, array<string|int, mixed>|object|null $set = null ][, bool|null $escape = null ]) : int

Performs INSERT and returns the auto-increment ID.

Parameters
$table : string = ''

Table name.

$set : array<string|int, mixed>|object|null = null

Associative array of field/value pairs.

$escape : bool|null = null

Whether to escape values and identifiers.

Tags
throws
RuntimeException

On query failure.

Return values
int

Insert ID.

insert_batch()

Insert multiple rows.

public insert_batch(string $table[, array<string|int, mixed>|object|null $set = null ][, bool|null $escape = null ][, int $batchSize = 100 ]) : array<string|int, int>

Performs batch INSERT and returns array of insert IDs.

Parameters
$table : string

Table name.

$set : array<string|int, mixed>|object|null = null

Array of associative arrays with field/value pairs.

$escape : bool|null = null

Whether to escape values and identifiers.

$batchSize : int = 100

Number of rows to insert per query. Default is 100.

Tags
throws
RuntimeException

On query failure.

Return values
array<string|int, int>

Array of insert IDs.

insert_on_duplicate_update()

Insert or update on duplicate key (MySQL specific).

public insert_on_duplicate_update([string $table = '' ][, array<string|int, mixed>|object|null $set = null ][, bool|null $escape = null ]) : int

Performs INSERT and updates existing row if duplicate key is found.

Parameters
$table : string = ''

Table name. Uses qb_from if empty.

$set : array<string|int, mixed>|object|null = null

Associative array of field/value pairs.

$escape : bool|null = null

Whether to escape values and identifiers.

Tags
throws
RuntimeException

If no data or table specified.

Return values
int

Insert ID of the new or existing row.

insert_on_duplicate_update_batch()

Batch insert or update on duplicate key (MySQL specific).

public insert_on_duplicate_update_batch([string $table = '' ][, array<string|int, mixed>|object|null $set = null ][, bool|null $escape = null ][, int $batchSize = 100 ]) : int

Performs batch INSERT and updates existing rows if duplicate keys are found.

Parameters
$table : string = ''

Table name. Uses qb_from if empty.

$set : array<string|int, mixed>|object|null = null

Array of associative arrays with field/value pairs.

$escape : bool|null = null

Whether to escape values and identifiers.

$batchSize : int = 100

Number of rows to insert per query. Default is 100.

Tags
throws
RuntimeException

If no data or table specified.

Return values
int

Number of affected rows.

isset_qb_from()

Check if FROM table is set in query builder.

public isset_qb_from([int $index = 0 ]) : bool
Parameters
$index : int = 0

Index of the table in FROM clause. Default is 0.

Return values
bool

True if table is set at the given index.

load_rdriver()

Load the result driver class.

public load_rdriver() : string

Creates and returns the appropriate result driver class name for the current database driver.

Return values
string

Fully qualified result driver class name.

query()

Execute a raw SQL query.

public query(string $sql[, array<string|int, mixed>|false $binds = false ][, bool|null $returnObject = null ]) : mixed

Executes the given SQL statement with optional parameter binding.

Parameters
$sql : string

SQL statement to execute.

$binds : array<string|int, mixed>|false = false

Parameter bindings for prepared statements. Default is false.

$returnObject : bool|null = null

Return result object for SELECT queries.

Tags
throws
RuntimeException

On query failure.

Return values
mixed

True for write queries, result object for SELECT queries.

update()

Update rows.

public update([string $table = '' ][, array<string|int, mixed>|object|null $set = null ][, string|array<string|int, mixed>|null $where = null ][, int|null $limit = null ]) : void

Performs UPDATE query with optional WHERE and LIMIT clauses.

Parameters
$table : string = ''

Table name.

$set : array<string|int, mixed>|object|null = null

Associative array of field/value pairs.

$where : string|array<string|int, mixed>|null = null

WHERE clause conditions.

$limit : int|null = null

Maximum number of rows to update.

Tags
throws
RuntimeException

On query failure.

update_batch()

Update multiple rows in batch.

public update_batch(string $table[, array<string|int, mixed>|object|null $set = null ][, mixed $value = null ][, int $batchSize = 100 ]) : int

Performs batch UPDATE using a common index field.

Parameters
$table : string

Table name.

$set : array<string|int, mixed>|object|null = null

Array of associative arrays with field/value pairs.

$value : mixed = null
$batchSize : int = 100

Number of rows to update per query. Default is 100.

Tags
throws
RuntimeException

On query failure.

Return values
int

Number of affected rows.

_insert_on_duplicate_update()

Generate INSERT ON DUPLICATE KEY UPDATE SQL statement.

private _insert_on_duplicate_update(string $table, array<string|int, mixed> $keys, array<string|int, mixed> $values) : string
Parameters
$table : string

Table name.

$keys : array<string|int, mixed>

Column names.

$values : array<string|int, mixed>

Column values.

Return values
string

SQL query string.

_insert_on_duplicate_update_batch()

Generate batch INSERT ON DUPLICATE KEY UPDATE SQL statement.

private _insert_on_duplicate_update_batch(string $table, array<string|int, mixed> $keys, array<string|int, mixed> $values) : string
Parameters
$table : string

Table name.

$keys : array<string|int, mixed>

Column names.

$values : array<string|int, mixed>

Array of value sets.

Return values
string

SQL query string.


        
On this page

Search results