class sql_insert_buffer

Collects rows for insert into a database until the buffer size is reached.

Then flushes the buffer to the database and starts over again.

Benefits over collecting a (possibly huge) insert array and then using $db->sql_multi_insert() include:

  • Going over max packet size of the database connection is usually prevented because the data is submitted in batches.

  • Reaching database connection timeout is usually prevented because submission of batches talks to the database every now and then.

  • Usage of less PHP memory because data no longer needed is discarded on buffer flush.

Attention: Please note that users of this class have to call flush() to flush the remaining rows to the database after their batch insert operation is finished.

Usage:

$buffer = new \phpbb\db\sql_insert_buffer($db, 'test_table', 1234);

while (do_stuff())
{
    $buffer->insert(array(
        'column1' => 'value1',
        'column2' => 'value2',
    ));
}

$buffer->flush();

Properties

protected driver_interface $db
protected string $table_name
protected int $max_buffered_rows
protected array $buffer

Methods

__construct(driver_interface $db, string $table_name, int $max_buffered_rows = 500)

No description

bool
insert(array $row)

Inserts a single row into the buffer if multi insert is supported by the database (otherwise an insert query is sent immediately). Then flushes the buffer if the number of rows in the buffer is now greater than or equal to $max_buffered_rows.

bool
insert_all(array $rows)

Inserts a row set, i.e. an array of rows, by calling insert().

bool
flush()

Flushes the buffer content to the DB and clears the buffer.

Details

at line 71
__construct(driver_interface $db, string $table_name, int $max_buffered_rows = 500)

No description

Parameters

driver_interface $db
string $table_name
int $max_buffered_rows

at line 89
bool insert(array $row)

Inserts a single row into the buffer if multi insert is supported by the database (otherwise an insert query is sent immediately). Then flushes the buffer if the number of rows in the buffer is now greater than or equal to $max_buffered_rows.

Parameters

array $row

Return Value

bool

True when some data was flushed to the database. False otherwise.

at line 115
bool insert_all(array $rows)

Inserts a row set, i.e. an array of rows, by calling insert().

Please note that it is in most cases better to use insert() instead of first building a huge rowset. Or at least count($rows) should be kept small.

Parameters

array $rows

Return Value

bool

True when some data was flushed to the database. False otherwise.

at line 134
bool flush()

Flushes the buffer content to the DB and clears the buffer.

Return Value

bool

True when some data was flushed to the database. False otherwise.