19 namespace OPiQuotations;
21 require_once
'OPiQuotations/log.inc';
22 require_once
'OPiQuotations/OPiQuotation.inc';
43 public function __construct($host, $user, $password, $db_name) {
45 assert(
'is_string($host)');
46 assert(
'is_string($user)');
47 assert(
'is_string($password)');
48 assert(
'is_string($db_name)');
51 \mysqli_report(MYSQLI_REPORT_OFF);
53 assert(\mysqli_report(MYSQLI_REPORT_ALL^MYSQLI_REPORT_STRICT^MYSQLI_REPORT_INDEX) ===
true);
56 $this->connect = new \mysqli($host, $user, $password, $db_name);
58 if ( $this->connect->connect_error ) {
59 to_log(
'mysqli connect error '.\mysqli_connect_errno().
' : '.\mysqli_connect_error().
'
60 '.@print_r($this->connect,
true));
61 $this->connect = null;
62 } elseif ( !$this->connect->set_charset(
'utf8') ) {
63 to_log(
'mysqli::set_charset(\'utf8\') failed!
64 Server:'.$this->connect->server_info.
'
65 Client:'.$this->connect->client_info.
'
66 '.@print_r($this->connect,
true));
67 $this->connect->close();
68 $this->connect = null;
78 if ( !empty($this->connect) ) {
79 $this->connect->close();
94 return !empty($this->connect);
109 assert(
'is_string($s)');
110 assert(
'$this->is_connected()');
113 return $this->connect->real_escape_string($s);
128 assert(
'is_string($table)');
129 assert(
'in_array($table, array(\'author\', \'nation\', \'subject\' or \'work\'))');
134 if (!$this->is_connected()) {
135 to_log(
'Db.list_to_assoc(\''.print_r($table,
true).
'\') impossible because is NOT connected!
');
140 $id_names = array('author
' => 'nation_author_id
',
141 'nation
' => 'nation_author_id
');
142 $id_name = (isset($id_names[$table])
145 $is_maxim = ($table === 'nation
'
146 ? ' AND `q`.`is_maxim`=1
'
147 : ($table === 'author
' || $table === 'work
'
148 ? ' AND `q`.`is_maxim`=0
'
151 $query = 'SELECT `t`.`
id`, `t`.`name`, (SELECT COUNT(*) FROM `quotation` AS `q` WHERE `t`.`
id`=`q`.`
'.$id_name.'`
'.$is_maxim.') AS `nb`
152 FROM `
'.$table.'` AS `t`
153 ORDER BY `name` COLLATE utf8_unicode_ci;
';
155 $result = $this->connect->query($query);
156 if ( $result !== false ) {
157 while ($row = $result->fetch_assoc()) {
158 settype($row['id'], 'int');
159 settype($row['nb
'], 'int');
160 if ($row['nb
'] > 0) {
161 $quots[$row['id']] = array($row['name
'], $row['nb
']);
165 $result->free_result();
171 Server:
'.$this->connect->server_info.'
172 Client:
'.$this->connect->client_info.'
173 '.@print_r($this->connect, true));
192 public function nb($is_maxim=null) {
194 assert('($is_maxim === null) || is_bool($is_maxim)
');
195 assert('$this->is_connected()
');
198 if (!$this->is_connected()) {
199 to_log('Db.
nb(\
''.print_r($is_maxim,
true).
'\') impossible because is NOT connected!
');
204 $query = 'SELECT COUNT(*) AS `nb`
205 FROM `quotation` AS `q`';
206 if ($is_maxim !== null) {
208 WHERE `q`.`is_maxim`='.($is_maxim
214 $result = $this->connect->query($query);
215 if ( $result !==
false ) {
216 $row = $result->fetch_assoc();
218 return (
int)$row[
'nb'];
224 Server:'.$this->connect->server_info.
'
225 Client:'.$this->connect->client_info.
'
226 '.@print_r($this->connect,
true));
247 assert(
'is_string($query)');
248 assert(
'$this->is_connected()');
251 if (!$this->is_connected()) {
252 to_log(
'Db.query_insert(\''.print_r($query,
true).
'\') impossible because is NOT connected!
');
257 $r = $this->connect->query($query);
263 Server:
'.$this->db->connect->server_info.'
264 Client:
'.$this->db->connect->client_info.'
265 '.@print_r($this->db->connect, true));
287 public function query_quotations($where, $order='
') {
289 assert('is_string($where)
');
290 assert('is_string($order)
');
291 assert('$this->is_connected()
');
296 if (!$this->is_connected()) {
297 to_log('Db.
query_quotations(\
''.print_r($where,
true).
'\', \
''.print_r($order,
true).
'\') impossible because is NOT connected!
');
302 $query = array('SELECT `
id`, `text`, `translation`, `is_maxim`, `is_marked`, `subject`, `nation`, `author`, `work`
',
303 'FROM `vw_quotation`
');
304 $where = (string)$where;
308 $order = (string)$order;
316 $result = $this->connect->query($query);
317 if ( $result !== false ) {
318 while ($row = $result->fetch_assoc()) {
319 settype($row['id'], 'int');
320 settype($row['is_maxim
'], 'bool');
321 settype($row['is_marked
'], 'bool');
323 $quots[] = new OPiQuotation($row['id'], $row['text
'], $row['is_maxim
'], $row['is_marked
'],
325 $row['subject
'], ($row['is_maxim
']
327 : $row['author
']), $row['work
']);
330 $result->free_result();
336 Server:
'.$this->connect->server_info.'
337 Client:
'.$this->connect->client_info.'
338 '.@print_r($this->connect, true));