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));