diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index 824e85a5e4d..73915025c6c 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -459,4 +459,19 @@ abstract class DoliDB implements Database return false; } + + /** + * 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; + } } diff --git a/htdocs/core/db/mysqli.class.php b/htdocs/core/db/mysqli.class.php index 27083e25e8a..72d713da892 100644 --- a/htdocs/core/db/mysqli.class.php +++ b/htdocs/core/db/mysqli.class.php @@ -1294,6 +1294,28 @@ class DoliDBMysqli extends DoliDB return $result; } + + /** + * Prepare a SQL statement for execution + * + * @param string $sql SQL query to prepare + * @return false|mysqli_stmt + */ + public function prepare($sql) + { + if (!$this->connected) { + $this->lasterror = 'Not connected to database'; + return false; + } + $stmt = $this->db->prepare($sql); + if ($stmt === false) { + $this->lasterror = $this->db->error; + $this->lastqueryerror = $sql; + return false; + } + + return $stmt; + } } if (class_exists('mysqli')) { diff --git a/htdocs/core/db/pgsql.class.php b/htdocs/core/db/pgsql.class.php index 017d7e63061..d2f3f6ee8c8 100644 --- a/htdocs/core/db/pgsql.class.php +++ b/htdocs/core/db/pgsql.class.php @@ -1512,4 +1512,23 @@ class DoliDBPgsql extends DoliDB return array(); } + + /** + * Prepare a SQL statement for execution (PostgreSQL prepared statement) + * + * @param string $sql The SQL query to prepare + * @return string|false The name of the prepared statement on success, or false on failure + */ + public function prepare($sql) + { + $stmtname = uniqid('dolipgstmt_'); // Generate a unique identifier for the statement + + $result = pg_prepare($this->db, $stmtname, $sql); + if (!$result) { + $this->lasterror = pg_last_error($this->db); + return false; + } + + return $stmtname; // We just return the name of the prepared statement + } }