/** * 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; }
/** * Load the metadata for the options table. * * @throws \Dewdrop\Exception * @return array */ protected function loadTableMetadata() { if (!$this->tableName) { throw new DewdropException('Table name must be set prior to loading metadata.'); } return $this->dbAdapter->getTableMetadata($this->tableName); }
/** * Get an array representing the columns in the root table's primary * key. The returned array's keys will be the column names and the values * will be the metadata for that column as returned by * \Dewdrop\Db\Adapter::describeTable(). * * @return array */ protected function getEntityPrimaryKey() { if (!$this->entityPrimaryKey) { $meta = $this->db->getTableMetadata($this->tableName); $pkey = array(); foreach ($meta['columns'] as $name => $column) { if ($column['PRIMARY']) { $pkey[$name] = $column; } } $this->entityPrimaryKey = $pkey; } return $this->entityPrimaryKey; }
/** * Get the values for any foreign keys in the DB table that reference the user * object's table. Allows the filter to be applied on a per-user basis. * * @return array */ protected function getUserReferenceValues() { $user = $this->getUser(); if (!$user) { return []; } $out = []; $userTable = $user->getTable(); $metadata = $this->dbAdapter->getTableMetadata($this->dbTableName); foreach ($metadata['references'] as $foreignKey => $reference) { if ($reference['table'] === $userTable->getTableName()) { $out[$foreignKey] = $user->get($reference['column']); } } return $out; }
/** * Load this table's metadata from the file generated by the db-metadata * CLI command. The metadata currently has two sections: * * - titles: Default singular and plural titles for the model. * - columns: The columns in the table with types, constraints, etc. * * You can retrieve the entirety of the metadata information by providing * null values to both arguments. You can retrieve an entire section of * the metdata by only specifying the first argument. Or, you can specify * values for both arguments to retrieve a specific member of a specific * section. * * For example, to get metadata only for the "name" column, you would call: * * <pre> * $this->getMetadata('columns', 'name'); * </pre> * * @param string $section * @param string $index * @return array */ public function getMetadata($section = null, $index = null) { if (!$this->metadata) { $this->metadata = $this->db->getTableMetadata($this->tableName); } if ($section && $index) { if (isset($this->metadata[$section][$index])) { return $this->metadata[$section][$index]; } else { return false; } } elseif ($section) { if (isset($this->metadata[$section])) { return $this->metadata[$section]; } else { return false; } } else { return $this->metadata; } }
/** * Determine a reasonable value to use as a title for a foreign key * reference. We'll look for a name or title in the referenced table. If * neither is present, we just grab the first column, which is probably the * ID itself. * * However, you can call setReferenceTitleColumn() to supply a different * column to use or an Expr object. * * @param $localColumn * @param array $reference * @param string $alias * @return string|Expr */ private function findReferenceTitleColumn($localColumn, array $reference, $alias) { if (array_key_exists($localColumn, $this->referenceTitleColumns)) { $value = $this->referenceTitleColumns[$localColumn]; if (is_callable($value)) { $value = call_user_func($value, $alias); if (!is_string($value) && !$value instanceof Expr) { throw new Exception('Title column callbacks should return a string or Expr.'); } } return $value; } $metadata = $this->db->getTableMetadata($reference['table']); $columns = array_keys($metadata['columns']); if (in_array('name', $columns)) { return 'name'; } elseif (in_array('title', $columns)) { return 'title'; } else { return array_shift($columns); } }