From 9e9770d3ef8e49a7c3c55815a4552aebdcc72455 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 19 May 2014 17:34:02 +0900 Subject: [PATCH] Bug fix to be able to ignore auto returning pk for inserts Some tables do not have primary keys, so an override code is needed to avoid getting auto returning set there. If db exec is called 'NULL' (or the async version), then no RETURNING is added or checked. --- www/admin/edit_base.inc | 2 +- www/admin/edit_order.php | 1 - www/libs/Class.DB.IO.inc | 44 ++++++++++++++++++++++------------------ 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/www/admin/edit_base.inc b/www/admin/edit_base.inc index 4bf90f9f..7ddf4ade 100644 --- a/www/admin/edit_base.inc +++ b/www/admin/edit_base.inc @@ -253,7 +253,7 @@ for ($i = 0; $i < count($output); $i ++) { $t_q = "('".$form->db_escape_string($output[$i])."')"; - $form->db_exec($q.$t_q); + $form->db_exec($q.$t_q, 'NULL'); } $elements[] = $form->form_create_element("filename"); } diff --git a/www/admin/edit_order.php b/www/admin/edit_order.php index f4913b38..0988a335 100644 --- a/www/admin/edit_order.php +++ b/www/admin/edit_order.php @@ -29,7 +29,6 @@ // set session name define('SET_SESSION_NAME', EDIT_SESSION_NAME); require(LIBS."Class.Login.inc"); - require(LIBS."Class.DB.IO.inc"); require(LIBS.'Class.Smarty.Extend.inc'); // default lang diff --git a/www/libs/Class.DB.IO.inc b/www/libs/Class.DB.IO.inc index 9b728572..53764bfc 100644 --- a/www/libs/Class.DB.IO.inc +++ b/www/libs/Class.DB.IO.inc @@ -600,7 +600,7 @@ } // METHOD _db_prepare_exec - // PARAMS query, primary key + // PARAMS query, primary key [if set to NULL no returning will be added] // RETURN md5 OR boolean false on error // DESC sub function for db_exec and db_exec_async // * checks query is set @@ -643,29 +643,32 @@ // if we do have an insert, check if there is no RETURNING pk_id, add it if I can get the PK id if ($this->_check_query_for_insert($this->query, true)) { - if (!$pk_name) - { - // TODO: get primary key from table name - list($schema, $table) = $this->_db_return_table($this->query); - if (!$this->pk_name_table[$table]) - { - $this->pk_name_table[$table] = $this->db_functions->_db_primary_key($table, $schema); - } - $pk_name = $this->pk_name_table[$table]; - } $this->pk_name = $pk_name; - if (!preg_match("/ returning /i", $this->query) && $this->pk_name) + if ($pk_name != 'NULL') { - $this->query .= " RETURNING ".$this->pk_name; - $this->returning_id = true; - } - elseif (preg_match("/ returning (.*)/i", $this->query, $matches) && $this->pk_name) - { - if (!preg_match("/$this->pk_name/", $matches[1])) + if (!$pk_name) { - $this->query .= " , ".$this->pk_name; + // TODO: get primary key from table name + list($schema, $table) = $this->_db_return_table($this->query); + if (!$this->pk_name_table[$table]) + { + $this->pk_name_table[$table] = $this->db_functions->_db_primary_key($table, $schema); + } + $pk_name = $this->pk_name_table[$table]; + } + if (!preg_match("/ returning /i", $this->query) && $this->pk_name) + { + $this->query .= " RETURNING ".$this->pk_name; $this->returning_id = true; } + elseif (preg_match("/ returning (.*)/i", $this->query, $matches) && $this->pk_name) + { + if (!preg_match("/$this->pk_name/", $matches[1])) + { + $this->query .= " , ".$this->pk_name; + $this->returning_id = true; + } + } } } // for DEBUG, only on first time ;) @@ -731,7 +734,7 @@ // if not select do here // count affected rows $this->num_rows = $this->db_functions->_db_affected_rows($this->cursor); - if ($this->_check_query_for_insert($this->query, true)) + if ($this->_check_query_for_insert($this->query, true) && $this->pk_name != 'NULL') { // set insert_id if (!$this->returning_id) @@ -1047,6 +1050,7 @@ // (if this was not set, method will quit with a 0 (failure) // pk_name -> optional primary key name, for insert id return if the pk name is very different // if pk name is table name and _id, pk_name is not needed to be set + // if NULL is given here, no RETURNING will be auto added // RETURN cursor for this query // DESC executes the query and returns & sets the internal cursor // fruthermore this functions also sets varios other vars