From 826d8851d4b10a6f604da942d48426ef1baba7a3 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 5 Sep 2017 07:02:09 +0000 Subject: [PATCH] dblayeroo: in isert, return the primary key if the provided value is not autoincrement field with valid value git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3920 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayeroo.php | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/dblayeroo.php b/dblayeroo.php index d0349d2..66f2341 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -2765,12 +2765,6 @@ class dblayeroo } $this->debugLog ("Entering createRequest ()"); $sql = $this->createRequest (); - if ($this->driver === "pgsql" && $this->command === "INSERT") - { - // PgSQL doesn't support the lastInsertId if there is no autoincrement - // field. Will return the primary key - $sql .= " RETURNING ".$this->sep.$this->primary.$this->sep; - } $this->debugLog ("Entering prepareRequest (XXX, ",false,")"); $st = $this->prepareRequest ($sql, false); $this->debugLog ("'",$this->getDisplayQuery (),"'"); @@ -2851,8 +2845,22 @@ class dblayeroo } return $result; case "INSERT": + // If the primary key is not autoincrement, return the provided value if + // exists + if (! in_array ("autoincrement", $this->fields[$this->primary]) && + key_exists ($this->primary, $this->setValues)) + return $this->setValues[$this->primary]; + + // If the primary key is autoincrement and the provided value is not null + // Return it + if (in_array ("autoincrement", $this->fields[$this->primary]) && + key_exists ($this->primary, $this->setValues) && + $this->setValues[$this->primary] !== null) + return $this->setValues[$this->primary]; + + // If the primary key is autoincrement and the provided value is null or + // not provided, return the autoincremented value // PostGres need the name of the column autoincrement to return something - // If there is no autoincrement column, do not request the lastInsertId $autoInc = null; if ($this->driver === "pgsql") { @@ -2866,13 +2874,6 @@ class dblayeroo } if ($autoInc !== null) $autoInc = $this->table."_${col}_seq"; - else - { - $this->debugLog ("INSERT: PgSQL INSERT without Autoincrement. ". - "Return the PK value defined in RETURN part"); - $result = $st->fetch (PDO::FETCH_ASSOC); - return $result[$this->primary]; - } } $this->debugLog ("INSERT: lastInsertId=", self::$instance[$this->dsn]->lastInsertId ($autoInc));