Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
sql_insert_buffer.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\db;
015
016 /**
017 * Collects rows for insert into a database until the buffer size is reached.
018 * Then flushes the buffer to the database and starts over again.
019 *
020 * Benefits over collecting a (possibly huge) insert array and then using
021 * $db->sql_multi_insert() include:
022 *
023 * - Going over max packet size of the database connection is usually prevented
024 * because the data is submitted in batches.
025 *
026 * - Reaching database connection timeout is usually prevented because
027 * submission of batches talks to the database every now and then.
028 *
029 * - Usage of less PHP memory because data no longer needed is discarded on
030 * buffer flush.
031 *
032 * Attention:
033 * Please note that users of this class have to call flush() to flush the
034 * remaining rows to the database after their batch insert operation is
035 * finished.
036 *
037 * Usage:
038 * <code>
039 * $buffer = new \phpbb\db\sql_insert_buffer($db, 'test_table', 1234);
040 *
041 * while (do_stuff())
042 * {
043 * $buffer->insert(array(
044 * 'column1' => 'value1',
045 * 'column2' => 'value2',
046 * ));
047 * }
048 *
049 * $buffer->flush();
050 * </code>
051 */
052 class sql_insert_buffer
053 {
054 /** @var \phpbb\db\driver\driver_interface */
055 protected $db;
056
057 /** @var string */
058 protected $table_name;
059
060 /** @var int */
061 protected $max_buffered_rows;
062
063 /** @var array */
064 protected $buffer = array();
065
066 /**
067 * @param \phpbb\db\driver\driver_interface $db
068 * @param string $table_name
069 * @param int $max_buffered_rows
070 */
071 public function __construct(\phpbb\db\driver\driver_interface $db, $table_name, $max_buffered_rows = 500)
072 {
073 $this->db = $db;
074 $this->table_name = $table_name;
075 $this->max_buffered_rows = $max_buffered_rows;
076 }
077
078 /**
079 * Inserts a single row into the buffer if multi insert is supported by the
080 * database (otherwise an insert query is sent immediately). Then flushes
081 * the buffer if the number of rows in the buffer is now greater than or
082 * equal to $max_buffered_rows.
083 *
084 * @param array $row
085 *
086 * @return bool True when some data was flushed to the database.
087 * False otherwise.
088 */
089 public function insert(array $row)
090 {
091 $this->buffer[] = $row;
092
093 // Flush buffer if it is full or when DB does not support multi inserts.
094 // In the later case, the buffer will always only contain one row.
095 if (!$this->db->get_multi_insert() || sizeof($this->buffer) >= $this->max_buffered_rows)
096 {
097 return $this->flush();
098 }
099
100 return false;
101 }
102
103 /**
104 * Inserts a row set, i.e. an array of rows, by calling insert().
105 *
106 * Please note that it is in most cases better to use insert() instead of
107 * first building a huge rowset. Or at least sizeof($rows) should be kept
108 * small.
109 *
110 * @param array $rows
111 *
112 * @return bool True when some data was flushed to the database.
113 * False otherwise.
114 */
115 public function insert_all(array $rows)
116 {
117 // Using bitwise |= because PHP does not have logical ||=
118 $result = 0;
119
120 foreach ($rows as $row)
121 {
122 $result |= (int) $this->insert($row);
123 }
124
125 return (bool) $result;
126 }
127
128 /**
129 * Flushes the buffer content to the DB and clears the buffer.
130 *
131 * @return bool True when some data was flushed to the database.
132 * False otherwise.
133 */
134 public function flush()
135 {
136 if (!empty($this->buffer))
137 {
138 $this->db->sql_multi_insert($this->table_name, $this->buffer);
139 $this->buffer = array();
140
141 return true;
142 }
143
144 return false;
145 }
146 }
147