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);
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->connect->server_info.' 264 Client:
'.$this->connect->client_info.' 265 '.@print_r($this->connect, true)); 295 public function query_quotations($where='
', $order='', $limit=null, $offset=null) { 297 assert('is_string($where)
'); 298 assert('is_string($order)
'); 299 assert('($limit === null) || (is_int($limit) && ($limit >= 0))
'); 300 assert('($offset === null) || (is_int($offset) && ($offset >= 0))
'); 301 assert('$this->is_connected()
'); 306 if (!$this->is_connected()) { 307 to_log('Db.
query_quotations(\
''.print_r($where,
true).
'\', \
''.print_r($order,
true).
'\') impossible because is NOT connected!
'); 314 $query = array('SELECT `
id`, `text`, `text_lang`, `translation`, `is_maxim`, `is_marked`, `subject`, `nation`, `author`, `work`,
', 315 ' `selection_label`, `is_misattributed`
', 316 'FROM `vw_quotation_selection`
'); 318 $where = (string)$where; 325 $query[] = 'GROUP BY `
id`
'; 327 $order = (string)$order; 334 if ($limit !== null) { 335 $limit = max(0, (int)$limit); 337 if ($offset === null) { 338 $query[] = 'LIMIT
'.$limit; 341 $offset = max(0, (int)$offset); 342 $query[] = 'LIMIT
'.$limit.' OFFSET
'.$offset; 352 $result = $this->connect->query($query); 353 if ($result !== false) { 354 // Fill associative table 355 while ($row = $result->fetch_assoc()) { 356 settype($row['id'], 'int'); 357 settype($row['is_maxim
'], 'bool'); 358 settype($row['is_marked
'], 'bool'); 359 settype($row['is_misattributed
'], 'bool'); 364 assert(!isset($quots[$id])); 367 $quots[$id] = new OPiQuotation($id, $row['text
'], 378 $row['is_misattributed
']); 381 $result->free_result(); 390 Server:
'.$this->connect->server_info.' 391 Client:
'.$this->connect->client_info.' 392 '.@print_r($this->connect, true)); 399 if (!empty($quots)) { 400 // Get selections for all these quotations 401 $query = 'SELECT `
id`, `selection_label`, `selection_datetime_utc`, `selection_url`
402 FROM `vw_quotation_selection`
403 WHERE `selection_label` IS NOT NULL AND `
id` IN (
'.implode(',
', array_keys($quots)).')
404 ORDER BY `selection_datetime_utc`';
406 $result = $this->connect->query($query);
408 while ($row =
$result->fetch_assoc()) {
409 settype($row[
'id'],
'int');
412 ($row[
'selection_datetime_utc'] === null
414 :
new \DateTime($row[
'selection_datetime_utc'])),
415 $row[
'selection_url']);
416 $quots[$row[
'id']]->selections_add(
$selection);
425 to_log(
'MySQL Db.query_quotations: get selections 428 Server:'.$this->connect->server_info.
' 429 Client:'.$this->connect->client_info.
' 430 '.@print_r($this->connect,
true));
470 assert(
'is_string($where)');
473 if (!$this->is_connected()) {
474 to_log(
'Db.query_quotations(\''.print_r($where,
true).
'\', \
''.print_r($order,
true).
'\') impossible because is NOT connected!
'); 481 $query = array('SELECT DISTINCT `
id`
', 482 'FROM `vw_quotation_selection`
'); 484 $where = (string)$where; 496 $result = $this->connect->query($query); 497 if ($result !== false) { 498 $nb = $result->num_rows; 504 Server:
'.$this->connect->server_info.' 505 Client:
'.$this->connect->client_info.' 506 '.@print_r($this->connect, true)); 524 public function to_string($x) {
525 return '\
''.$this->escape((
string)$x).
'\'';
542 : $this->to_string(
''.$x));
to_log($message)
Append $message in LOG_FILE.
query_quotations_nb($where='')
Return the number of all OPiQuotation that match with the SQL $where condition.
query_quotations($where='', $order='', $limit=null, $offset=null)
Return an array with all OPiQuotation that match with the SQL $where condition in order specified by ...
__destruct()
Close the connection to the database.
nb($is_maxim=null)
Return the numbers of quotations and/or maxims.
list_to_assoc($table)
Return a associative table id => array(name, number of use) of elements of the table.
query_insert($query)
Execute the MySQL query INSERT.
__construct($host, $user, $password, $db_name)
Open the connection to the database $db_name.
is_connected()
If connection is ok then return true else return false.
to_string_or_NULL($x)
If $x === null then return 'NULL' else return to_string($x).
escape($s)
Return the string with its special characters escaped for use in a SQL statement. ...
$connect
Connection to the MySQL database.
if( $search !==null &&$search !=='')