Esempio n. 1
0
 /**
  * Get the last insert ID from the driver after performing an insert on a table
  * with an auto-incrementing primary key, or null on failure.
  *
  * @return int|null
  */
 public function lastInsertId()
 {
     $table = $this->adapter->getLastInsertTableName();
     // No table name available, so let's bail
     if (!$table) {
         return null;
     }
     $meta = $this->adapter->getTableMetadata($table);
     if ($meta) {
         foreach ($meta['columns'] as $name => $columnMeta) {
             if ($columnMeta['PRIMARY'] && $columnMeta['IDENTITY']) {
                 $currval = $this->fetchOne('SELECT CURRVAL(PG_GET_SERIAL_SEQUENCE(?, ?))', [$table, $name]);
                 // Account for possibility of non-serial integer key with default of nextval(sequence)
                 if (null === $currval) {
                     $currval = $this->fetchOne('SELECT
                           CURRVAL(SUBSTRING(column_default FROM \'(?i)nextval\\\\(\'\'([^\'\']+)\'))
                         FROM information_schema.columns
                         WHERE table_name = ?
                         AND column_name = ?', [$table, $name]);
                 }
                 return $currval;
             }
         }
     }
     return null;
 }