dolibarr/htdocs/core/db/DoliDB.class.php

507 lines
15 KiB
PHP
Raw Permalink Normal View History

2013-09-10 10:29:55 +00:00
<?php
/*
2015-05-12 17:01:01 +00:00
* Copyright (C) 2013-2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
2015-03-10 13:09:29 +00:00
* Copyright (C) 2014-2015 Laurent Destailleur <eldy@users.sourceforge.net>
Qual: Fix phan notices (#37837) * Qual: Ignore PhanUndeclaredProperty for specific lines * Fix: Update payment mode reference in PDF generation # Fix: Update payment mode reference in PDF generation The payment mode reference in the PDF generation was incorrectly using `type_payment_code`, an unknown property in Salary. It has been updated to use `type_code` as used elsewhere for the same purpose. * Qual: Typing of several DDLUpdateField field_desc items made optional # Qual: Typing of several DDLUpdateField field_desc items made optional Not all fields are required for updateing the DB typing, and ExtraFields was not providing all of them. * Qual: Update typing for phan notices # Qual: Update typing for phan notices Fix typing mismatches * Qual: Fix extrafields typing in objectline_create.tpl.php # Qual: Fix extrafields typing in objectline_create.tpl.php - Update typing for phan * Qual: Fix typing for Phan & re-use encrypted password when requested # Qual: Fix typing for Phan & re-use encrypted password when requested - Added a cast to string for the crypted password to hint phan that it returns the string version (not the array) - Added a check to ensure the password is not already crypted before generating a new password. - Updated the type hints for parameters `$changelater`, `$notrigger`, `$nosyncmember`, and `$passwordalreadycrypted` to include the range of possible values. - Updated the return type hint for the `setPassword` method to include the range of possible error values. * Qual: Fix file argument to showPreview() calls # Qual: Fix file argument to showPreview() calls Fix argument type mismatches by adjusting '$file' arguments in showPreview calls. * Qual: Update login_c type to string # Qual: Update login_c type to string login_c is username from db. * Qual: Update type hints in treeview.lib.php # Qual: Update type hints in treeview.lib.php Update type hints for static analysis. * Qual: Add null check for objecttmp in selectForFormsList # Qual: Add null check for objecttmp in selectForFormsList Add null check for objecttmp before calling selectForFormsList to ensure static analysis * Qual: Fix phan through null check on object argument # Qual: Fix phan through null check on object argument - Do null check to avoid null parameter (fix phan); - Set more specific type for argument. * Qual: Change type hint of $remise_percent parameter to float # Qual: Change type hint of $remise_percent parameter to float DB type is float, so correct arg type to fix phan notice. * Qual: Ignore some phan notices The descriptions for the linkedObjectsFullLoaded and oldcopy properties have been updated to avoid modification by code formatting tools * Qual: Update phan baseline * Qual: Cast remise_percent to float to match typing requirements * Qual: Cast label to string to help static analysis * Qual: Update date_cancel property type and assignment To fix phan notices: - The date_cancel property type was changed from 'int|string' to 'int|""' to match return type from jdate(). - Additionally, the assignment of date_cancel was updated to use dol_now() instead of $this->db->idate(dol_now()). * Qual: Provide the expected user_id instead of the User object Changed the user fetch logic from using `user_author` to `user_creation_id` * Qual: Correct bool argument from 0 to false ($fill) * Qual/Fix: Correct date converted to dol_print_date * Qual: Fix argument type for addline() * Qual: Update Phan baseline.txt
2026-04-13 10:50:23 +00:00
* Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
2013-09-10 10:29:55 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 19:55:30 +00:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2013-09-10 10:29:55 +00:00
*/
/**
2014-09-27 14:00:11 +00:00
* \file htdocs/core/db/DoliDB.class.php
* \brief Class file to manage Dolibarr database access
2013-09-10 10:29:55 +00:00
*/
require_once DOL_DOCUMENT_ROOT.'/core/db/Database.interface.php';
2014-02-21 11:38:43 +00:00
2024-04-28 15:10:51 +00:00
2013-09-10 10:29:55 +00:00
/**
* Class to manage Dolibarr database access
*/
2014-02-21 11:38:43 +00:00
abstract class DoliDB implements Database
2013-09-10 10:29:55 +00:00
{
/** Force subclass to implement VERSIONMIN - required DB version */
const VERSIONMIN = self::VERSIONMIN;
/** Force subclass to implement LABEL - description of DB type */
const LABEL = self::LABEL;
2024-04-27 16:08:13 +00:00
/** @var false|resource|mysqli|mysqliDoli|SQLite3|PgSql\Connection|DoliDB Database handler */
2015-05-12 17:01:01 +00:00
public $db;
/** @var string Database type */
public $type;
/** @var string Charset used to force charset when creating database */
public $forcecharset = 'utf8';
2015-05-12 17:01:01 +00:00
/** @var string Collate used to force collate when creating database */
public $forcecollate = 'utf8_unicode_ci';
2023-01-03 17:54:11 +00:00
2015-05-12 17:01:01 +00:00
/** @var resource Resultset of last query */
2024-07-09 17:29:35 +00:00
private $_results; // @phpstan-ignore-line
2023-01-03 17:54:11 +00:00
2015-05-12 17:01:01 +00:00
/** @var bool true if connected, else false */
public $connected;
/** @var bool true if database selected, else false */
public $database_selected;
/** @var string Selected database name */
public $database_name;
/** @var string Database username */
public $database_user;
/** @var string Database host */
public $database_host;
/** @var int Database port */
public $database_port;
/** @var int >=1 if a transaction is opened, 0 otherwise */
public $transaction_opened;
/** @var string Last successful query */
public $lastquery;
2015-06-06 12:34:57 +00:00
/** @var string Last failed query */
2015-05-12 17:01:01 +00:00
public $lastqueryerror;
/** @var string Last error message */
public $lasterror;
/** @var string Last error number. For example: 'DB_ERROR_RECORD_ALREADY_EXISTS', '12345', ... */
2015-05-12 17:01:01 +00:00
public $lasterrno;
2013-09-10 10:29:55 +00:00
2022-01-18 11:24:19 +00:00
/** @var string If we need to set a prefix specific to the database so it can be reused (when defined instead of MAIN_DB_PREFIX) to forge requests */
2022-01-18 11:16:13 +00:00
public $prefix_db;
2015-05-12 17:01:01 +00:00
/** @var bool Status */
public $ok;
/** @var string */
public $error;
2014-02-06 19:40:01 +00:00
2022-01-20 17:50:18 +00:00
/**
2023-01-04 16:36:31 +00:00
* Return the DB prefix found into prefix_db (if it was set manually by doing $dbhandler->prefix_db=...).
2023-01-03 17:54:11 +00:00
* Otherwise return MAIN_DB_PREFIX (common use).
2022-01-20 17:50:18 +00:00
*
* @return string The DB prefix
*/
public function prefix()
{
return (empty($this->prefix_db) ? MAIN_DB_PREFIX : $this->prefix_db);
}
2014-03-15 04:54:13 +00:00
/**
* Format a SQL IF
*
* @param string $test Test string (example: 'cd.statut=0', 'field IS NULL')
* @param string $resok resultat si test equal
* @param string $resko resultat si test non equal
2014-03-15 04:54:13 +00:00
* @return string SQL string
*/
public function ifsql($test, $resok, $resko)
2014-03-15 04:54:13 +00:00
{
//return 'IF('.$test.','.$resok.','.$resko.')'; // Not sql standard
return '(CASE WHEN '.$test.' THEN '.$resok.' ELSE '.$resko.' END)';
2014-03-15 04:54:13 +00:00
}
2024-03-29 13:52:27 +00:00
/**
* Return SQL string to aggregate using the Standard Deviation of population
*
* @param string $nameoffield Name of field
* @return string SQL string
*/
2024-04-09 23:38:36 +00:00
public function stddevpop($nameoffield)
2024-03-29 13:52:27 +00:00
{
return 'STDDEV_POP('.$nameoffield.')';
}
/**
* Return SQL string to force an index
*
* @param string $nameofindex Name of index
* @param int $mode 0=Use, 1=Force
* @return string SQL string
*/
public function hintindex($nameofindex, $mode = 1)
{
return '';
}
2022-11-10 16:19:12 +00:00
/**
* Format a SQL REGEXP
*
* @param string $subject Field name to test
2022-11-10 16:19:12 +00:00
* @param string $pattern SQL pattern to match
* @param int $sqlstring 0=the string being tested is a hard coded string, 1=the string is a field
* @return string SQL string
*/
2023-12-15 10:02:50 +00:00
public function regexpsql($subject, $pattern, $sqlstring = 0)
{
2022-11-10 16:19:12 +00:00
if ($sqlstring) {
return "(". $subject ." REGEXP '" . $this->escape($pattern) . "')";
2022-11-10 16:19:12 +00:00
}
return "('". $this->escape($subject) ."' REGEXP '" . $this->escape($pattern) . "')";
}
2014-03-15 04:54:13 +00:00
2014-03-15 05:04:16 +00:00
/**
* Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.
* Function to use to build INSERT, UPDATE or WHERE predica
*
Qual: Fix phan notices (#37837) * Qual: Ignore PhanUndeclaredProperty for specific lines * Fix: Update payment mode reference in PDF generation # Fix: Update payment mode reference in PDF generation The payment mode reference in the PDF generation was incorrectly using `type_payment_code`, an unknown property in Salary. It has been updated to use `type_code` as used elsewhere for the same purpose. * Qual: Typing of several DDLUpdateField field_desc items made optional # Qual: Typing of several DDLUpdateField field_desc items made optional Not all fields are required for updateing the DB typing, and ExtraFields was not providing all of them. * Qual: Update typing for phan notices # Qual: Update typing for phan notices Fix typing mismatches * Qual: Fix extrafields typing in objectline_create.tpl.php # Qual: Fix extrafields typing in objectline_create.tpl.php - Update typing for phan * Qual: Fix typing for Phan & re-use encrypted password when requested # Qual: Fix typing for Phan & re-use encrypted password when requested - Added a cast to string for the crypted password to hint phan that it returns the string version (not the array) - Added a check to ensure the password is not already crypted before generating a new password. - Updated the type hints for parameters `$changelater`, `$notrigger`, `$nosyncmember`, and `$passwordalreadycrypted` to include the range of possible values. - Updated the return type hint for the `setPassword` method to include the range of possible error values. * Qual: Fix file argument to showPreview() calls # Qual: Fix file argument to showPreview() calls Fix argument type mismatches by adjusting '$file' arguments in showPreview calls. * Qual: Update login_c type to string # Qual: Update login_c type to string login_c is username from db. * Qual: Update type hints in treeview.lib.php # Qual: Update type hints in treeview.lib.php Update type hints for static analysis. * Qual: Add null check for objecttmp in selectForFormsList # Qual: Add null check for objecttmp in selectForFormsList Add null check for objecttmp before calling selectForFormsList to ensure static analysis * Qual: Fix phan through null check on object argument # Qual: Fix phan through null check on object argument - Do null check to avoid null parameter (fix phan); - Set more specific type for argument. * Qual: Change type hint of $remise_percent parameter to float # Qual: Change type hint of $remise_percent parameter to float DB type is float, so correct arg type to fix phan notice. * Qual: Ignore some phan notices The descriptions for the linkedObjectsFullLoaded and oldcopy properties have been updated to avoid modification by code formatting tools * Qual: Update phan baseline * Qual: Cast remise_percent to float to match typing requirements * Qual: Cast label to string to help static analysis * Qual: Update date_cancel property type and assignment To fix phan notices: - The date_cancel property type was changed from 'int|string' to 'int|""' to match return type from jdate(). - Additionally, the assignment of date_cancel was updated to use dol_now() instead of $this->db->idate(dol_now()). * Qual: Provide the expected user_id instead of the User object Changed the user fetch logic from using `user_author` to `user_creation_id` * Qual: Correct bool argument from 0 to false ($fill) * Qual/Fix: Correct date converted to dol_print_date * Qual: Fix argument type for addline() * Qual: Update Phan baseline.txt
2026-04-13 10:50:23 +00:00
* @param int|'' $param Date TMS to convert
* @param 'gmt'|'tzserver' $gm 'gmt'=Input information are GMT values, 'tzserver'=Local to server TZ
* @return string Date in a string YYYY-MM-DD HH:MM:SS
2014-03-15 05:04:16 +00:00
*/
public function idate($param, $gm = 'tzserver')
2014-03-15 05:04:16 +00:00
{
2022-04-05 11:54:09 +00:00
// TODO $param should be gmt, so we should have default $gm to 'gmt' instead of default 'tzserver'
return dol_print_date($param, "%Y-%m-%d %H:%M:%S", $gm);
2014-03-15 05:04:16 +00:00
}
2014-03-15 05:07:46 +00:00
/**
* Return last error code
*
* @return string lasterrno
*/
public function lasterrno()
2014-03-15 05:07:46 +00:00
{
return $this->lasterrno;
}
2020-09-18 15:13:01 +00:00
/**
* Sanitize a string for SQL forging
*
2025-01-03 18:50:38 +00:00
* @param string $stringtosanitize String to sanitize
* @param int $allowsimplequote 1=Allow simple quotes in string around val separated by "," but only when string is used as a list of SQL string "'aa', 'bb', 'cc', ..."). Can be used for IN ...
* 2=Allow all simple quotes. If you use this value, the return MUST be escaped to forge SQL strings.
2023-12-15 10:02:50 +00:00
* @param int $allowsequals 1=Allow equals sign
* @param int $allowsspace 1=Allow space char
2024-05-06 16:25:06 +00:00
* @param int $allowschars 1=Allow a-z chars
2020-09-18 15:13:01 +00:00
* @return string String escaped
*/
2024-05-06 16:25:06 +00:00
public function sanitize($stringtosanitize, $allowsimplequote = 0, $allowsequals = 0, $allowsspace = 0, $allowschars = 1)
2020-09-18 15:13:01 +00:00
{
$result = preg_replace('/[^0-9_\-\.,'.($allowschars ? '\p{L}' : '').($allowsequals ? '=' : '').($allowsimplequote ? "\'" : '').($allowsspace ? ' ' : '').']/ui', '', $stringtosanitize);
//$result = preg_replace('/[^0-9_\-\.,'.($allowschars ? 'a-z' : '').($allowsequals ? '=' : '').($allowsimplequote ? "\'" : '').($allowsspace ? ' ' : '').']/i', '', $stringtosanitize);
if ($allowsimplequote == 1) {
// Remove all quotes that are inside a string and not around
$tmpchars = explode(',', $result);
$newstringarray = array();
foreach ($tmpchars as $tmpchar) {
$reg = array();
if (preg_match('/^\'(.*)\'$/', $tmpchar, $reg)) {
$newstringarray[] = "'".str_replace("'", "", $reg[1])."'";
} else {
$newstringarray[] = str_replace("'", "", $tmpchar);
}
}
2025-03-06 10:34:27 +00:00
$result = implode(',', $newstringarray);
}
return $result;
2020-09-18 15:13:01 +00:00
}
2014-03-15 05:17:12 +00:00
/**
* Start transaction
*
2022-10-06 15:09:10 +00:00
* @param string $textinlog Add a small text into log. '' by default.
* @return int 1 if transaction successfully opened or already opened, 0 if error
2014-03-15 05:17:12 +00:00
*/
public function begin($textinlog = '')
2014-03-15 05:17:12 +00:00
{
2021-02-23 21:03:23 +00:00
if (!$this->transaction_opened) {
$ret = $this->query("BEGIN");
2021-02-23 21:03:23 +00:00
if ($ret) {
2014-03-15 05:17:12 +00:00
$this->transaction_opened++;
dol_syslog("BEGIN Transaction".($textinlog ? ' '.$textinlog : ''), LOG_DEBUG);
dol_syslog('', 0, 1);
return 1;
} else {
return 0;
2014-03-15 05:17:12 +00:00
}
2020-05-21 13:05:19 +00:00
} else {
2014-03-15 05:17:12 +00:00
$this->transaction_opened++;
dol_syslog('', 0, 1);
2014-03-15 05:17:12 +00:00
return 1;
}
}
/**
* Validate a database transaction
*
* @param string $log Add more log to default log line
* @return int 1 if validation is OK or transaction level no started, 0 if ERROR
*/
public function commit($log = '')
{
dol_syslog('', 0, -1);
2021-02-23 21:03:23 +00:00
if ($this->transaction_opened <= 1) {
$ret = $this->query("COMMIT");
2021-02-23 21:03:23 +00:00
if ($ret) {
$this->transaction_opened = 0;
dol_syslog("COMMIT Transaction".($log ? ' '.$log : ''), LOG_DEBUG);
2014-10-04 23:22:17 +00:00
return 1;
2020-05-21 13:05:19 +00:00
} else {
2014-10-04 23:22:17 +00:00
return 0;
}
2020-05-21 13:05:19 +00:00
} else {
$this->transaction_opened--;
return 1;
}
}
/**
2019-06-22 17:15:15 +00:00
* Cancel a transaction and go back to initial data values
*
2014-11-15 14:19:37 +00:00
* @param string $log Add more log to default log line
* @return resource|int 1 if cancellation is ok or transaction not open, 0 if error
*/
public function rollback($log = '')
{
dol_syslog('', 0, -1);
2021-02-23 21:03:23 +00:00
if ($this->transaction_opened <= 1) {
$ret = $this->query("ROLLBACK");
$this->transaction_opened = 0;
dol_syslog("ROLLBACK Transaction".($log ? ' '.$log : ''), LOG_DEBUG);
return $ret;
2020-05-21 13:05:19 +00:00
} else {
$this->transaction_opened--;
return 1;
}
}
/**
* Define limits and offset of request
*
* @param int $limit Maximum number of lines returned (-1=conf->liste_limit, 0=no limit)
* @param int $offset Numero of line from where starting fetch
* @return string String with SQL syntax to add a limit and offset
*/
public function plimit($limit = 0, $offset = 0)
{
global $conf;
2021-02-23 21:03:23 +00:00
if (empty($limit)) {
return "";
}
if ($limit < 0) {
$limit = $conf->liste_limit;
}
if ($offset > 0) {
2021-08-27 20:42:04 +00:00
return " LIMIT ".((int) $offset).",".((int) $limit)." ";
2021-02-23 21:03:23 +00:00
} else {
2021-08-27 20:42:04 +00:00
return " LIMIT ".((int) $limit)." ";
2021-02-23 21:03:23 +00:00
}
}
2014-03-15 05:40:13 +00:00
/**
* Return version of database server into an array
*
2024-03-20 21:48:53 +00:00
* @return string[] Version array
2014-03-15 05:40:13 +00:00
*/
public function getVersionArray()
2014-03-15 05:40:13 +00:00
{
return preg_split("/[\.,-]/", $this->getVersion());
2014-03-15 05:40:13 +00:00
}
2014-03-15 05:48:39 +00:00
/**
* Return last request executed with query()
*
* @return string Last query
*/
public function lastquery()
2014-03-15 05:48:39 +00:00
{
return $this->lastquery;
}
2014-02-06 19:40:01 +00:00
/**
* Define sort criteria of request
*
2025-09-28 13:20:46 +00:00
* @param string $sortfield List of sort fields, separated by comma. Example: 't1.fielda,t2.fieldb' or 't1.fielda,concat(a,b,c) as abc,t2.fieldb'
* @param string $sortorder Sort order, separated by comma. Example: 'ASC,DESC'. Note: If the quantity for sortorder values is lower than sortfield, we used the last value for missing values.
2017-11-25 15:21:44 +00:00
* @return string String to provide syntax of a sort sql string
2014-02-06 19:40:01 +00:00
*/
2023-06-10 08:47:36 +00:00
public function order($sortfield = '', $sortorder = '')
2014-02-06 19:40:01 +00:00
{
2021-02-23 21:03:23 +00:00
if (!empty($sortfield)) {
$oldsortorder = '';
$return = '';
2025-09-28 13:20:46 +00:00
// If text is "field1, f(a,b,c) as xxx, field2", we must convert string into 'field1,xxx,field2'
$sortfield = preg_replace('/[a-z_]+\([^\)]*\) as ([\w]+)/i', '\1', $sortfield);
$fields = explode(',', $sortfield);
$orders = (!empty($sortorder) ? explode(',', $sortorder) : array());
$i = 0;
2025-09-28 13:20:46 +00:00
2020-10-29 17:44:15 +00:00
foreach ($fields as $val) {
2025-09-28 13:20:46 +00:00
// Sanitized fieldname
$fieldname = preg_replace('/[^0-9a-z_\.]/i', '', $val);
if (!$fieldname) {
continue;
}
2021-02-23 21:03:23 +00:00
if (!$return) {
$return .= ' ORDER BY ';
} else {
$return .= ', ';
}
2014-02-06 19:40:01 +00:00
2025-09-28 13:20:46 +00:00
// Add field
$return .= $fieldname;
2020-10-30 04:45:36 +00:00
$tmpsortorder = (empty($orders[$i]) ? '' : trim($orders[$i]));
// Only ASC and DESC values are valid SQL
if (strtoupper($tmpsortorder) === 'ASC') {
$oldsortorder = 'ASC';
$return .= ' ASC';
} elseif (strtoupper($tmpsortorder) === 'DESC') {
$oldsortorder = 'DESC';
$return .= ' DESC';
} else {
$return .= ' '.($oldsortorder ? $oldsortorder : 'ASC');
2015-05-12 17:01:01 +00:00
}
$i++;
2014-02-06 19:40:01 +00:00
}
return $return;
2020-05-21 13:05:19 +00:00
} else {
2014-02-06 19:40:01 +00:00
return '';
}
}
2014-03-15 04:47:17 +00:00
2014-03-15 06:11:45 +00:00
/**
* Return last error label
*
2014-10-04 23:22:17 +00:00
* @return string Last error
2014-03-15 06:11:45 +00:00
*/
public function lasterror()
2014-03-15 06:11:45 +00:00
{
return $this->lasterror;
}
2014-03-15 04:47:17 +00:00
/**
* Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
* 19700101020000 -> 3600 with server TZ = +1 and $gm='tzserver'
* 19700101020000 -> 7200 whatever is server TZ if $gm='gmt'
2014-03-15 04:47:17 +00:00
*
2026-02-07 23:11:01 +00:00
* @param ?string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
2026-02-08 13:51:11 +00:00
* @param bool|int|string $gm 'gmt'=Input information are GMT values, 'tzserver'=Local to server TZ
* @return int|'' Date TMS or ''
2014-03-15 04:47:17 +00:00
*/
public function jdate($string, $gm = 'tzserver')
2014-03-15 04:47:17 +00:00
{
2021-01-03 14:10:33 +00:00
// TODO $string should be converted into a GMT timestamp, so param gm should be set to true by default instead of false
2026-02-07 23:11:01 +00:00
$string = (string) $string;
2026-02-08 11:58:22 +00:00
if ($string == '' || $string == '0' || $string == "0000-00-00 00:00:00") {
2021-02-23 21:03:23 +00:00
return '';
}
$string = preg_replace('/([^0-9])/i', '', $string);
$tmp = $string.'000000';
$date = dol_mktime((int) substr($tmp, 8, 2), (int) substr($tmp, 10, 2), (int) substr($tmp, 12, 2), (int) substr($tmp, 4, 2), (int) substr($tmp, 6, 2), (int) substr($tmp, 0, 4), $gm);
2026-02-07 23:11:01 +00:00
2014-03-15 04:47:17 +00:00
return $date;
}
2014-03-15 06:20:00 +00:00
/**
* Return last query in error
*
* @return string lastqueryerror
*/
public function lastqueryerror()
2014-03-15 06:20:00 +00:00
{
return $this->lastqueryerror;
}
2020-04-18 11:20:08 +00:00
/**
2020-04-18 11:44:13 +00:00
* Return first result from query as object
* Note : This method executes a given SQL query and retrieves the first row of results as an object. It should only be used with SELECT queries
* Don't add LIMIT to your query, it will be added by this method
2021-11-01 02:30:22 +00:00
*
* @param string $sql The sql query string
* @return bool|int|object False on failure, 0 on empty, object on success
2020-04-18 11:20:08 +00:00
*/
public function getRow($sql)
{
2021-11-01 02:30:22 +00:00
$sql .= ' LIMIT 1';
2020-04-18 11:20:08 +00:00
2024-03-11 15:27:46 +00:00
$resql = $this->query($sql);
if ($resql) {
$obj = $this->fetch_object($resql);
2021-11-05 11:42:42 +00:00
if ($obj) {
2024-03-11 15:27:46 +00:00
$this->free($resql);
2021-11-04 11:10:19 +00:00
return $obj;
2021-11-05 11:42:42 +00:00
} else {
return 0;
2021-11-04 11:10:19 +00:00
}
2020-04-18 11:20:08 +00:00
}
return false;
}
2020-04-18 11:44:13 +00:00
/**
2024-03-11 15:26:17 +00:00
* Return all results from query as an array of objects. Using this is a bad practice and is discouraged.
2024-03-11 15:30:33 +00:00
* Note : It should only be used with SELECT queries and with a limit. If you are not able to defined/know what can be the limit, it
* just means this function is not what you need. Do not use it.
2021-11-01 02:30:22 +00:00
*
2024-03-11 15:26:17 +00:00
* @param string $sql The sql query string. Must end with "... LIMIT x"
* @return false|Object[] Result
2020-04-18 11:44:13 +00:00
*/
2024-03-11 15:26:17 +00:00
public function getRows($sql)
2020-04-18 11:44:13 +00:00
{
if (!preg_match('/LIMIT \d+(?:(?:,\ *\d*)|(?:\ +OFFSET\ +\d*))?\ *;?$/', $sql)) {
trigger_error(__CLASS__ .'::'.__FUNCTION__.'() query must have a LIMIT clause', E_USER_ERROR);
2024-03-06 16:24:23 +00:00
}
2024-03-11 15:27:46 +00:00
$resql = $this->query($sql);
if ($resql) {
2020-04-18 11:44:13 +00:00
$results = array();
2024-03-11 15:27:46 +00:00
if ($this->num_rows($resql) > 0) {
while ($obj = $this->fetch_object($resql)) {
2020-04-18 11:44:13 +00:00
$results[] = $obj;
}
}
2024-03-11 15:27:46 +00:00
$this->free($resql);
2020-04-18 11:44:13 +00:00
return $results;
}
return false;
}
2025-12-21 15:04:12 +00:00
/**
* Get the last ID of an auto-increment field of a table
*
* @param string $table Name of table
* @return int Next ID or -1 if error
*/
public function getNextAutoIncrementId($table)
{
$this->lasterror = 'getNextAutoIncrementId() not implemented for this driver. Failed to get next ID for table '.$table;
return -1;
}
/**
* Prepare a SQL statement for execution
*
* This method must be implemented by subclasses.
*
* @param string $sql SQL query to prepare
* @return mixed Driver-specific prepared statement object or false on failure
*/
public function prepare($sql)
{
$this->lasterror = 'prepare() not implemented for this driver. Failed to prepare '.$sql;
return false;
}
2013-09-10 10:29:55 +00:00
}