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.
This commit is contained in:
Clemens Schwaighofer
2014-05-19 17:34:02 +09:00
parent 5e7359554f
commit 9e9770d3ef
3 changed files with 25 additions and 22 deletions

View File

@@ -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");
}

View File

@@ -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

View File

@@ -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