/** * Get table metadata (fields description) * \return description */ function meta($table) { $res = pg_meta_data($this->conn, $table); if (!$res) { throw new Exception(DB_ERR . 'Meta: ' . pg_last_error($this->conn)); } return $res; }
function last_insert_id($table) { # This assumes that the very first column in the table is a sequence $cols = pg_meta_data($this->link, $table); $colnames = array_keys($cols); $col = $colnames[0]; if (strpos(@$cols[$col]['type'], 'int') === 0) { $sql = "select currval('{$table}_{$col}_seq') as lastinsertid"; $rs = pg_query($this->link, $sql); $last_id = pg_fetch_result($rs, 0); return $last_id; } }
function get_meta_data($table) { $meta = pg_meta_data($this->db, $table); return is_array($meta)?$meta:NULL; }
public function MetaData($tableName) { return pg_meta_data($this->connection, $tableName); }
function getMetaData($table) { $this->connect("PG"); preg_match("/^[\\w'-]*\\./", $table, $matches); $_schema = $matches[0]; preg_match("/[\\w'-]*\$/", $table, $matches); $_table = $matches[0]; if (!$_schema) { $_schema = $this->postgisschema; } if (version_compare(PHP_VERSION, '5.3.0') >= 0) { // If running 5.3 then use schema.table $arr = pg_meta_data($this->db, str_replace(".", "", $_schema) . "." . $_table); } else { // if running below 5.3 then set SEARCH_PATH and use just table without schema $this->execQuery("SET SEARCH_PATH TO " . str_replace(".", "", $_schema), "PG"); $arr = pg_meta_data($this->db, $_table); } $this->close(); return $arr; }
<td>Field length</td> <td>Field NOT NULL?</td> </tr> <?php // attempt a connection $dbh = pg_connect("host=localhost port=5432 dbname=geoviz user=postgres password=user"); if (!$dbh) { die("Error in connection: " . pg_last_error()); } // get table metadata $meta = pg_meta_data($dbh, "bodenluft_4326"); foreach ($meta as $key => $value) { echo "<tr>"; echo "<td>{$key}</td>"; echo "<td>" . $value['type'] . "</td>"; echo "<td>" . $value['len'] . "</td>"; echo "<td>" . ($value['not null'] == 1 ? 'true' : 'false') . "</td>"; echo "</tr>"; } // close connection pg_close($dbh); ?> </table> </body>
function getMetaData($table) { $__conn = $this->connect(); if ($__conn) { $arr = pg_meta_data($__conn, $table); return $arr; } }
<?php error_reporting(E_ALL); include 'config.inc'; $db = pg_connect($conn_str); $meta = pg_meta_data($db, $table_name); var_dump($meta);
function getMetaData($table, $temp = false) { $this->connect("PG"); preg_match("/^[\\w'-]*\\./", $table, $matches); $_schema = $matches[0]; preg_match("/[\\w'-]*\$/", $table, $matches); $_table = $matches[0]; if (!$_schema) { $_schema = $this->postgisschema; } if (!$temp) { $arr = pg_meta_data($this->db, str_replace(".", "", $_schema) . "." . $_table); } else { $arr = pg_meta_data($this->db, $_table); } $this->close(); return $arr; }
/** * Returns types of columns in given result set. * @param resource $resultSet * @param string $table * @return array */ public function getColumnTypes($resultSet, $table) { if ($table === null) { return array(); } $cols = pg_meta_data($this->resource, $table); foreach ($cols as $key => $value) { $cols[$key] = preg_replace('~[^a-z]~i', '', $value['type']); } return $cols; }
public function desc($table_name) { return pg_meta_data($this->db, $table_name); }
function additions($table, $additions) { if ($table != $this->tableName) { $conn = $this->connection(); $relation = $this->makeSchema(pg_meta_data($conn, $table), $table); foreach ($additions as $addition => $name) { foreach ($relation as $field => $attrs) { $tablePath = $table . "." . $addition; if ($field == $tablePath) { $this->tableSchema[$tablePath] = $attrs; $this->additionsNames[$tablePath] = $name; } } } } else { foreach ($additions as $addition => $name) { $tablePath = $addition; $this->tableSchema[$tablePath] = array('type' => 'numeric'); $this->additionsNames[$tablePath] = $name; } } }
$output .= generateClassObjectCreator($tableName, $className, $fields); // Done $output .= generateClassFetchData($tableName, $className, $fields); // Done $output .= generateClassGetID($className, $fields); // DONE foreach ($fields as $fieldName => $field) { $output .= generateClassGetField($className, $fieldName, $field); // Done } $output .= generateClassDelete($tableName, $fields); $output .= generateClassUpdate($tableName, $className, $fields); // Done $output .= generateClassBottom($className); // Done return $output; } global $conn; if ($argc < 2) { print "Usage: " . $argv[0] . " <table_name>\n"; exit - 1; } $tableName = $argv[1]; $meta = pg_meta_data($conn, $tableName); if (!is_array($meta)) { print "Error: table {$tableName} not found\n"; exit - 1; } print generateHeader($tableName); print generate_GETOBJECT($tableName); print generateClass($tableName, $meta);
<?php include 'config.inc'; $conn = pg_connect($conn_str); pg_query('CREATE SCHEMA phptests'); pg_query('CREATE TABLE phptests.foo (id INT, id2 INT)'); pg_query('CREATE TABLE foo (id INT, id3 INT)'); var_dump(pg_meta_data($conn, 'foo')); var_dump(pg_meta_data($conn, 'phptests.foo')); pg_query('DROP TABLE foo'); pg_query('DROP TABLE phptests.foo'); pg_query('DROP SCHEMA phptests');
/** * Load the schema for this table */ function db_schema() { // Loadup from the cache $cache_file = '/tmp/ar_schema-' . $this->db_connection . '-' . $this->tablename . '.cache'; if (file_exists($cache_file)) { $a = unserialize(file_get_contents($cache_file)); $this->primary_key_name = $a['primary_key_name']; $this->schema = $a['schema']; if (!($t = @filectime($cache_file))) { if ($t > time() - 3600) { @unlink($cache_file); } } return true; } if ($meta = pg_meta_data($this->db, $this->tablename)) { while (list($key, $val) = each($meta)) { $this->schema[$key] = array(); $this->schema[$key]['type'] = $val['type']; $this->schema[$key]['default'] = null; $this->schema[$key]['length'] = $val['len']; } } // Set default primary_key name if (!$this->primary_key_name) { $this->primary_key_name = 'id'; } // Write to the cache $cache = array('schema' => $this->schema, 'primary_key_name' => $this->primary_key_name); file_put_contents($cache_file, serialize($cache), LOCK_EX); return true; }
/** * @throws ObjectNotFoundException * @return DBTable **/ public function getTableInfo($table) { static $types = ['time' => DataType::TIME, 'date' => DataType::DATE, 'timestamp' => DataType::TIMESTAMP, 'timestamptz' => DataType::TIMESTAMPTZ, 'timestamp with time zone' => DataType::TIMESTAMPTZ, 'bool' => DataType::BOOLEAN, 'int2' => DataType::SMALLINT, 'int4' => DataType::INTEGER, 'int8' => DataType::BIGINT, 'numeric' => DataType::NUMERIC, 'float4' => DataType::REAL, 'float8' => DataType::DOUBLE, 'varchar' => DataType::VARCHAR, 'bpchar' => DataType::CHAR, 'text' => DataType::TEXT, 'bytea' => DataType::BINARY, 'ip4' => DataType::IP, 'inet' => DataType::IP, 'ip4r' => DataType::IP_RANGE, 'hstore' => DataType::HSTORE, 'uuid' => DataType::UUID, 'json' => DataType::JSON, 'jsonb' => DataType::JSONB, 'tsvector' => null, 'ltree' => null]; try { $res = pg_meta_data($this->link, $table); } catch (BaseException $e) { throw new ObjectNotFoundException("unknown table '{$table}'"); } $table = new DBTable($table); foreach ($res as $name => $info) { Assert::isTrue(array_key_exists($info['type'], $types), 'unknown type "' . $types[$info['type']] . '" found in column "' . $name . '"'); if (empty($types[$info['type']])) { continue; } $column = new DBColumn(DataType::create($types[$info['type']])->setNull(!$info['not null']), $name); $table->addColumn($column); } return $table; }
function meta_data($sTable) { $meta = pg_meta_data($this->conn, $sTable); if (is_array($meta)) { return $meta; } else { return "0"; } }
/** * Check if a table exists * * @param string $tableName The table to check * @access public * @return boolean */ public function existsTable($tableName) { $exists = pg_meta_data(self::$_link, $tableName); if ($exists === false) { return false; } else { return true; } }
static function table_change_column($table, $field_name, $ftype) { $type = ""; if (SETUP_DB_TYPE == "mysqli") { $sql = sprintf("show columns from %s", $table); if (($dbcolumns = sql_fetch($sql)) === false) { return false; } foreach ($dbcolumns as $column) { if ($column["Field"] == $field_name) { $type = str_replace(",0", "", $column["Type"]); } } if ($type != str_replace(" default 0", "", $ftype)) { if (!sql_query(sprintf("ALTER TABLE %s MODIFY %s %s", $table, $field_name, $ftype))) { return false; } } } else { if (SETUP_DB_TYPE == "pgsql") { if (($dbcolumns = pg_meta_data(sys::$db, $table)) === false) { return false; } foreach ($dbcolumns as $key => $column) { if ($key == $field_name) { $type = $column["type"]; } } if (strpos($type, "default 0")) { $addon = sprintf(", ALTER %s set default 0", $field_name); } else { $addon = ""; } $ftype = str_replace(" default 0", "", $ftype); if ($type != $ftype) { if (!sql_query(sprintf("ALTER TABLE %s ALTER %s TYPE %s", $table, $field_name, $ftype) . $addon)) { return false; } } } } // sqlite: no change return true; }