cvs: php4 /pear/DB pgsql.php
| From: | Thomas V.V.Cox | Date: | Fri, 03 Aug 2001 18:29:39 +0000 |
| Subject: | cvs: php4 /pear/DB pgsql.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-416@lists.php.net to get a copy of this message | ||
cox Fri Aug 3 14:29:39 2001 EDT
Modified files:
/php4/pear/DB pgsql.php
Log:
added tableInfo() (thanks to Francis Romera <francis@vulcanonet.com>)
Index: php4/pear/DB/pgsql.php diff -u php4/pear/DB/pgsql.php:1.44 php4/pear/DB/pgsql.php:1.45 --- php4/pear/DB/pgsql.php:1.44 Thu Aug 2 18:27:40 2001 +++ php4/pear/DB/pgsql.php Fri Aug 3 14:29:39 2001 @@ -294,7 +294,7 @@ /** * Free the internal resources associated with $result. * - * @param $result PostgreSQL result identifier or DB statement identifier + * @param $result int PostgreSQL result identifier or DB statement identifier * * @return bool TRUE on success, FALSE if $result is invalid */ @@ -344,7 +344,7 @@ /** * Get the number of columns in a result set. * - * @param $result PostgreSQL result identifier + * @param $result resource PostgreSQL result identifier * * @return int the number of columns per row in $result */ @@ -363,7 +363,7 @@ /** * Get the number of rows in a result set. * - * @param $result PostgreSQL result identifier + * @param $result resource PostgreSQL result identifier * * @return int the number of rows in $result */ @@ -449,7 +449,7 @@ * Gets the number of rows affected by the last query. * if the last query was a select, returns 0. * - * @return number of rows affected by the last query or DB_ERROR + * @return int number of rows affected by the last query or DB_ERROR */ function affectedRows() { @@ -503,7 +503,7 @@ * Create the sequence * * @param string $seq_name the name of the sequence - * @return DB error + * @return mixed DB_OK on success or DB error on error * @access public */ function createSequence($seq_name) @@ -522,7 +522,7 @@ * Drop a sequence * * @param string $seq_name the name of the sequence - * @return DB error + * @return mixed DB_OK on success or DB error on error * @access public */ function dropSequence($seq_name) @@ -546,10 +546,182 @@ } // }}} + // {{{ _pgFieldFlags() + + /** + * Flags of a Field + * + * @param int $resource PostgreSQL result identifier + * @param int $num_field the field number + * + * @return string The flags of the field ("not_null", "default_xx", "primary_key", + * "unique" and "multiple_key" are supported) + * @access private + */ + function _pgFieldFlags($resource, $num_field, $table_name) + { + $field_name = @pg_fieldname($resource, $num_field); + + $result = pg_exec($this->connection, "SELECT f.attnotnull, f.atthasdef + FROM pg_attribute f, pg_class tab, pg_type typ + WHERE tab.relname = typ.typname + AND typ.typrelid = f.attrelid + AND f.attname = '$field_name' + AND tab.relname = '$table_name'"); + $row = pg_fetch_row($result, 0); + $flags = ($row[0] == 't') ? 'not_null ' : ''; + + if ($row[1] == 't') { + $result = pg_exec($this->connection, "SELECT a.adsrc + FROM pg_attribute f, pg_class tab, pg_type typ, pg_attrdef a + WHERE tab.relname = typ.typname AND typ.typrelid = f.attrelid + AND f.attrelid = a.adrelid AND f.attname = '$field_name' + AND tab.relname = '$table_name'"); + $row = pg_fetch_row($result, 0); + $num = str_replace('\'', '', $row[0]); + + $flags .= "default_$num "; + } + + $result = pg_exec($this->connection, "SELECT i.indisunique, i.indisprimary, i.indkey + FROM pg_attribute f, pg_class tab, pg_type typ, pg_index i + WHERE tab.relname = typ.typname + AND typ.typrelid = f.attrelid + AND f.attrelid = i.indrelid + AND f.attname = '$field_name' + AND tab.relname = '$table_name'"); + $count = pg_numrows($result); + + for ($i = 0; $i < $count ; $i++) { + $row = pg_fetch_row($result, $i); + $keys = explode(" ", $row[2]); + + if (in_array($num_field + 1, $keys)) { + $flags .= ($row[0] == 't') ? 'unique ' : ''; + $flags .= ($row[1] == 't') ? 'primary ' : ''; + if (count($keys) > 1) + $flags .= 'multiple_key '; + } + } + + return trim($flags); + } + + // }}} + // {{{ tableInfo() + + /** + * Returns information about a table or a result set + * + * NOTE: doesn't support table name and flags if called from a db_result + * + * @param mixed $resource PostgreSQL result identifier or table name + * @param int $mode A valid tableInfo mode (DB_TABLEINFO_ORDERTABLE or + * DB_TABLEINFO_ORDER) + * + * @return array An array with all the information + */ + function tableInfo($result, $mode = null) + { + $count = 0; + $id = 0; + $res = array(); + + /* + * depending on $mode, metadata returns the following values: + * + * - mode is false (default): + * $result[]: + * [0]["table"] table name + * [0]["name"] field name + * [0]["type"] field type + * [0]["len"] field length + * [0]["flags"] field flags + * + * - mode is DB_TABLEINFO_ORDER + * $result[]: + * ["num_fields"] number of metadata records + * [0]["table"] table name + * [0]["name"] field name + * [0]["type"] field type + * [0]["len"] field length + * [0]["flags"] field flags + * ["order"][field name] index of field named "field name" + * The last one is used, if you have a field name, but no index. + * Test: if (isset($result['meta']['myfield'])) { ... + * + * - mode is DB_TABLEINFO_ORDERTABLE + * the same as above. but additionally + * ["ordertable"][table name][field name] index of field + * named "field name" + * + * this is, because if you have fields from different + * tables with the same field name * they override each + * other with DB_TABLEINFO_ORDER + * + * you can combine DB_TABLEINFO_ORDER and + * DB_TABLEINFO_ORDERTABLE with DB_TABLEINFO_ORDER | + * DB_TABLEINFO_ORDERTABLE * or with DB_TABLEINFO_FULL + */ + + // if $result is a string, then we want information about a + // table without a resultset + + if (is_string($result)) { + $id = pg_exec($this->connection,"SELECT * FROM $result"); + if (empty($id)) { + return $this->pgsqlRaiseError(); + } + } else { // else we want information about a resultset + $id = $result; + if (empty($id)) { + return $this->pgsqlRaiseError(); + } + } + + $count = @pg_numfields($id); + + // made this IF due to performance (one if is faster than $count if's) + if (empty($mode)) { + + for ($i=0; $i<$count; $i++) { + $res[$i]['table'] = (is_string($result)) ? $result : ''; + $res[$i]['name'] = @pg_fieldname ($id, $i); + $res[$i]['type'] = @pg_fieldtype ($id, $i); + $res[$i]['len'] = @pg_fieldsize ($id, $i); + $res[$i]['flags'] = (is_string($result)) ? $this->_pgFieldflags($id, $i, $result) : ''; + } + + } else { // full + $res["num_fields"]= $count; + + for ($i=0; $i<$count; $i++) { + $res[$i]['table'] = (is_string($result)) ? $result : ''; + $res[$i]['name'] = @pg_fieldname ($id, $i); + $res[$i]['type'] = @pg_fieldtype ($id, $i); + $res[$i]['len'] = @pg_fieldsize ($id, $i); + $res[$i]['flags'] = (is_string($result)) ? $this->_pgFieldFlags($id, $i, $result) : ''; + if ($mode & DB_TABLEINFO_ORDER) { + $res['order'][$res[$i]['name']] = $i; + } + if ($mode & DB_TABLEINFO_ORDERTABLE) { + $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; + } + } + } + + // free the result only if we were called on a table + if (is_resource($id)) { + @pg_freeresult($id); + } + return $res; + } + + // }}} } // Local variables: // tab-width: 4 // c-basic-offset: 4 // End: -?> +?> \ No newline at end of file
Index: php4/pear/DB/pgsql.php diff -u php4/pear/DB/pgsql.php:1.44 php4/pear/DB/pgsql.php:1.45 --- php4/pear/DB/pgsql.php:1.44 Thu Aug 2 18:27:40 2001 +++ php4/pear/DB/pgsql.php Fri Aug 3 14:29:39 2001 @@ -294,7 +294,7 @@ /** * Free the internal resources associated with $result. * - * @param $result PostgreSQL result identifier or DB statement identifier + * @param $result int PostgreSQL result identifier or DB statement identifier * * @return bool TRUE on success, FALSE if $result is invalid */ @@ -344,7 +344,7 @@ /** * Get the number of columns in a result set. * - * @param $result PostgreSQL result identifier + * @param $result resource PostgreSQL result identifier * * @return int the number of columns per row in $result */ @@ -363,7 +363,7 @@ /** * Get the number of rows in a result set. * - * @param $result PostgreSQL result identifier + * @param $result resource PostgreSQL result identifier * * @return int the number of rows in $result */ @@ -449,7 +449,7 @@ * Gets the number of rows affected by the last query. * if the last query was a select, returns 0. * - * @return number of rows affected by the last query or DB_ERROR + * @return int number of rows affected by the last query or DB_ERROR */ function affectedRows() { @@ -503,7 +503,7 @@ * Create the sequence * * @param string $seq_name the name of the sequence - * @return DB error + * @return mixed DB_OK on success or DB error on error * @access public */ function createSequence($seq_name) @@ -522,7 +522,7 @@ * Drop a sequence * * @param string $seq_name the name of the sequence - * @return DB error + * @return mixed DB_OK on success or DB error on error * @access public */ function dropSequence($seq_name) @@ -546,10 +546,182 @@ } // }}} + // {{{ _pgFieldFlags() + + /** + * Flags of a Field + * + * @param int $resource PostgreSQL result identifier + * @param int $num_field the field number + * + * @return string The flags of the field ("not_null", "default_xx", "primary_key", + * "unique" and "multiple_key" are supported) + * @access private + */ + function _pgFieldFlags($resource, $num_field, $table_name) + { + $field_name = @pg_fieldname($resource, $num_field); + + $result = pg_exec($this->connection, "SELECT f.attnotnull, f.atthasdef + FROM pg_attribute f, pg_class tab, pg_type typ + WHERE tab.relname = typ.typname + AND typ.typrelid = f.attrelid + AND f.attname = '$field_name' + AND tab.relname = '$table_name'"); + $row = pg_fetch_row($result, 0); + $flags = ($row[0] == 't') ? 'not_null ' : ''; + + if ($row[1] == 't') { + $result = pg_exec($this->connection, "SELECT a.adsrc + FROM pg_attribute f, pg_class tab, pg_type typ, pg_attrdef a + WHERE tab.relname = typ.typname AND typ.typrelid = f.attrelid + AND f.attrelid = a.adrelid AND f.attname = '$field_name' + AND tab.relname = '$table_name'"); + $row = pg_fetch_row($result, 0); + $num = str_replace('\'', '', $row[0]); + + $flags .= "default_$num "; + } + + $result = pg_exec($this->connection, "SELECT i.indisunique, i.indisprimary, i.indkey + FROM pg_attribute f, pg_class tab, pg_type typ, pg_index i + WHERE tab.relname = typ.typname + AND typ.typrelid = f.attrelid + AND f.attrelid = i.indrelid + AND f.attname = '$field_name' + AND tab.relname = '$table_name'"); + $count = pg_numrows($result); + + for ($i = 0; $i < $count ; $i++) { + $row = pg_fetch_row($result, $i); + $keys = explode(" ", $row[2]); + + if (in_array($num_field + 1, $keys)) { + $flags .= ($row[0] == 't') ? 'unique ' : ''; + $flags .= ($row[1] == 't') ? 'primary ' : ''; + if (count($keys) > 1) + $flags .= 'multiple_key '; + } + } + + return trim($flags); + } + + // }}} + // {{{ tableInfo() + + /** + * Returns information about a table or a result set + * + * NOTE: doesn't support table name and flags if called from a db_result + * + * @param mixed $resource PostgreSQL result identifier or table name + * @param int $mode A valid tableInfo mode (DB_TABLEINFO_ORDERTABLE or + * DB_TABLEINFO_ORDER) + * + * @return array An array with all the information + */ + function tableInfo($result, $mode = null) + { + $count = 0; + $id = 0; + $res = array(); + + /* + * depending on $mode, metadata returns the following values: + * + * - mode is false (default): + * $result[]: + * [0]["table"] table name + * [0]["name"] field name + * [0]["type"] field type + * [0]["len"] field length + * [0]["flags"] field flags + * + * - mode is DB_TABLEINFO_ORDER + * $result[]: + * ["num_fields"] number of metadata records + * [0]["table"] table name + * [0]["name"] field name + * [0]["type"] field type + * [0]["len"] field length + * [0]["flags"] field flags + * ["order"][field name] index of field named "field name" + * The last one is used, if you have a field name, but no index. + * Test: if (isset($result['meta']['myfield'])) { ... + * + * - mode is DB_TABLEINFO_ORDERTABLE + * the same as above. but additionally + * ["ordertable"][table name][field name] index of field + * named "field name" + * + * this is, because if you have fields from different + * tables with the same field name * they override each + * other with DB_TABLEINFO_ORDER + * + * you can combine DB_TABLEINFO_ORDER and + * DB_TABLEINFO_ORDERTABLE with DB_TABLEINFO_ORDER | + * DB_TABLEINFO_ORDERTABLE * or with DB_TABLEINFO_FULL + */ + + // if $result is a string, then we want information about a + // table without a resultset + + if (is_string($result)) { + $id = pg_exec($this->connection,"SELECT * FROM $result"); + if (empty($id)) { + return $this->pgsqlRaiseError(); + } + } else { // else we want information about a resultset + $id = $result; + if (empty($id)) { + return $this->pgsqlRaiseError(); + } + } + + $count = @pg_numfields($id); + + // made this IF due to performance (one if is faster than $count if's) + if (empty($mode)) { + + for ($i=0; $i<$count; $i++) { + $res[$i]['table'] = (is_string($result)) ? $result : ''; + $res[$i]['name'] = @pg_fieldname ($id, $i); + $res[$i]['type'] = @pg_fieldtype ($id, $i); + $res[$i]['len'] = @pg_fieldsize ($id, $i); + $res[$i]['flags'] = (is_string($result)) ? $this->_pgFieldflags($id, $i, $result) : ''; + } + + } else { // full + $res["num_fields"]= $count; + + for ($i=0; $i<$count; $i++) { + $res[$i]['table'] = (is_string($result)) ? $result : ''; + $res[$i]['name'] = @pg_fieldname ($id, $i); + $res[$i]['type'] = @pg_fieldtype ($id, $i); + $res[$i]['len'] = @pg_fieldsize ($id, $i); + $res[$i]['flags'] = (is_string($result)) ? $this->_pgFieldFlags($id, $i, $result) : ''; + if ($mode & DB_TABLEINFO_ORDER) { + $res['order'][$res[$i]['name']] = $i; + } + if ($mode & DB_TABLEINFO_ORDERTABLE) { + $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; + } + } + } + + // free the result only if we were called on a table + if (is_resource($id)) { + @pg_freeresult($id); + } + return $res; + } + + // }}} } // Local variables: // tab-width: 4 // c-basic-offset: 4 // End: -?> +?> \ No newline at end of file