* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db;
/**
* 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();
*
*/
class sql_insert_buffer
{
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var string */
protected $table_name;
/** @var int */
protected $max_buffered_rows;
/** @var array */
protected $buffer = array();
/**
* @param \phpbb\db\driver\driver_interface $db
* @param string $table_name
* @param int $max_buffered_rows
*/
public function __construct(\phpbb\db\driver\driver_interface $db, $table_name, $max_buffered_rows = 500)
{
$this->db = $db;
$this->table_name = $table_name;
$this->max_buffered_rows = $max_buffered_rows;
}
/**
* 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.
*
* @param array $row
*
* @return bool True when some data was flushed to the database.
* False otherwise.
*/
public function insert(array $row)
{
$this->buffer[] = $row;
// Flush buffer if it is full or when DB does not support multi inserts.
// In the later case, the buffer will always only contain one row.
if (!$this->db->get_multi_insert() || sizeof($this->buffer) >= $this->max_buffered_rows)
{
return $this->flush();
}
return false;
}
/**
* 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 sizeof($rows) should be kept
* small.
*
* @param array $rows
*
* @return bool True when some data was flushed to the database.
* False otherwise.
*/
public function insert_all(array $rows)
{
// Using bitwise |= because PHP does not have logical ||=
$result = 0;
foreach ($rows as $row)
{
$result |= (int) $this->insert($row);
}
return (bool) $result;
}
/**
* Flushes the buffer content to the DB and clears the buffer.
*
* @return bool True when some data was flushed to the database.
* False otherwise.
*/
public function flush()
{
if (!empty($this->buffer))
{
$this->db->sql_multi_insert($this->table_name, $this->buffer);
$this->buffer = array();
return true;
}
return false;
}
}
_L+>{:2MIgOqvXӓ@_CzvrTЁ(D)C'R" -7{ <5up|`N+8&iK7I{vn)tX0m _l
斳n}38~t#
tYѡz
_$Il LhK-+i5+ -QaL\
^xv0Xe2GX!%VB`2u˟O3/SDO+U8L^3-|
aD{(jՔ1T;]ƔXVV;JH(d} }V. u1{QDz!P-^|}Pg7eazA\x6O0+ow$D% P( K#
4 ȹs~7zxPd0;dVS3kڄkJ#oҺʵiϨ-yr+<]\^
>翅$oG)άD4Ywym(W2$$Z~TΝa#QTφ2HAjȺe~I
sx$Cs;
(RKp0Jw9}@Gj4Dj`fjbϐ
\ [Cc:Y =8eC9p/n^-6|?q\-
Mfbceu
bN7'-L9{*^-R\'(f m$ %0r݀=z#z F+Jtc ,zeA|p>ˍͫ9[a^焥Brto_xL%WZetS?[m Wt,J'yZ6߾čWR-CCg4.U_ :>"3%Hg[d!GO͵(\a~Y)ID$ZŇ:+6d1(3whӗ䧑|vS:mpd#Q{v
z'3ŔL_QxZ%IoFsj.lTG
0>S[TϰU`ho7 ,JX{oژn3u̓|9ݩћX*pqf}W+izXiNfksj
G&B9㛁jGi[njzSII!\fm܅CCss&hRAʇp[,'No!z"1Hܮ7ERR~o}v](㭫9 6$6>c҄Ȃ.UI=j*bnSyv)zĪۘ
˦xm)KLLjhiR().r-5?JDY \:g3="G[1~P!Ɲ&
_z˃",-mIٷwU(̉&XzJcoԖ8GS%l?H=қB~9}hA3vk dYsa^ufAV#c .ߨ]wEtkt&w#e0={
<й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