public static function castResult($result, array $a, Stub $stub, $isNested) { $a['num rows'] = pg_num_rows($result); $a['status'] = pg_result_status($result); if (isset(self::$resultStatus[$a['status']])) { $a['status'] = new ConstStub(self::$resultStatus[$a['status']], $a['status']); } $a['command-completion tag'] = pg_result_status($result, PGSQL_STATUS_STRING); if (-1 === $a['num rows']) { foreach (self::$diagCodes as $k => $v) { $a['error'][$k] = pg_result_error_field($result, $v); } } $a['affected rows'] = pg_affected_rows($result); $a['last OID'] = pg_last_oid($result); $fields = pg_num_fields($result); for ($i = 0; $i < $fields; ++$i) { $field = array('name' => pg_field_name($result, $i), 'table' => sprintf('%s (OID: %s)', pg_field_table($result, $i), pg_field_table($result, $i, true)), 'type' => sprintf('%s (OID: %s)', pg_field_type($result, $i), pg_field_type_oid($result, $i)), 'nullable' => (bool) pg_field_is_null($result, $i), 'storage' => pg_field_size($result, $i) . ' bytes', 'display' => pg_field_prtlen($result, $i) . ' chars'); if (' (OID: )' === $field['table']) { $field['table'] = null; } if ('-1 bytes' === $field['storage']) { $field['storage'] = 'variable size'; } elseif ('1 bytes' === $field['storage']) { $field['storage'] = '1 byte'; } if ('1 chars' === $field['display']) { $field['display'] = '1 char'; } $a['fields'][] = new EnumStub($field); } return $a; }
/** * Structure of our fields (type, length and null) * * @param resource $resource * @return array */ public function field_structures($resource) { $result = []; if ($resource) { for ($i = 0; $i < pg_num_fields($resource); $i++) { $name = pg_field_name($resource, $i); $result[$name]['type'] = pg_field_type($resource, $i); $result[$name]['null'] = pg_field_is_null($resource, $i); $result[$name]['length'] = pg_field_size($resource, $i); } } return $result; }
function handle_piece_free($type, $from, $data, $res) { global $connection; $from->need_level("member"); $piece_index = $data['piece_index']; $piece_id = _find_piece_id($from, $piece_index); $admin_mode = $data['admin_mode'] && $from->has_level('admin'); $result = pg_query($connection, 'SELECT owner FROM pieces WHERE id = ' . $piece_id); if (pg_num_rows($result) == 0) { throw new Exception("This slice doesn't exist."); } if (pg_field_is_null($result, 0, 0)) { throw new Exception("This slice isn't owned by you."); } $owner_id = pg_fetch_result($result, 0, 0); if ($owner_id !== $from->user_id() && !$admin_mode) { throw new Exception("This is not your slice"); } $owner_nick = $from->nick(); if ($owner_id !== $from->user_id()) { $result = pg_query($connection, 'SELECT nick FROM users WHERE id = ' . $owner_id); $owner_nick = pg_fetch_result($result, 0, "nick"); } // // Actually free // $result = pg_query($connection, 'UPDATE pieces SET owner = NULL WHERE id = ' . $piece_id); //TODO: format piece info hash update_kml($from->pieid); $pinfo = array("piece_index" => $piece_index, "owner" => ""); $res->to_pie($from, array('piece_owner', $pinfo)); _update_user_reserved($res, $from, $owner_id, $owner_nick); $res->to_pie($from, info_msg("%s has freed slice #%s.", $from->nick(), $piece_index)); _add_piece_info_log($res, $from, $piece_index, $piece_id, "Slice has been freed"); _update_pie_timestamp($from, $connection); }
public function execute($sql) { // hide errors $ini_err = ini_get('display_errors'); ini_set('display_errors', 0); $res = false; $this->res_errMsg = null; // --- process sql if ($this->dbType == 'postgres') { $this->res_data = pg_query($this->dbConn, $sql); if (!$this->res_data) { $this->res_errMsg = pg_last_error($this->dbConn); } else { $this->res_errMsg = pg_result_error($this->res_data); $this->res_affectedRows = pg_affected_rows($this->res_data); $this->res_rowCount = pg_num_rows($this->res_data); $this->res_fieldCount = pg_num_fields($this->res_data); $res = new dbRecordSet($this->dbType, $this->res_data, $this->res_rowCount, $this->res_fieldCount); // -- parse field names for ($i = 0; $i < $this->res_fieldCount; $i++) { $this->res_fields[$i] = pg_field_name($this->res_data, $i); $this->res_fieldsInfo[$i] = array(); $this->res_fieldsInfo[$i]['type'] = pg_field_type($this->res_data, $i); $this->res_fieldsInfo[$i]['len'] = pg_field_size($this->res_data, $i); $this->res_fieldsInfo[$i]['is_null'] = pg_field_is_null($this->res_data, $i); $this->res_fieldsInfo[$i]['prt_len'] = pg_field_prtlen($this->res_data, $i); } } // log error if ($this->res_errMsg != '') { // put here code to log error } } // --- mysql if ($this->dbType == 'mysql') { $this->res_data = mysql_query($sql, $this->dbConn); if (!$this->res_data) { $this->res_errMsg = mysql_error($this->dbConn); } else { @($this->res_errMsg = mysql_error($this->res_data)); @($this->res_affectedRows = mysql_affected_rows($this->res_data)); @($this->res_rowCount = mysql_num_rows($this->res_data)); @($this->res_fieldCount = mysql_num_fields($this->res_data)); @($res = new dbRecordSet($this->dbType, $this->res_data, $this->res_rowCount, $this->res_fieldCount)); // -- parse field names for ($i = 0; $i < $this->res_fieldCount; $i++) { $this->res_fields[$i] = mysql_field_name($this->res_data, $i); $this->res_fieldsInfo[$i] = array(); $this->res_fieldsInfo[$i]['type'] = mysql_field_type($this->res_data, $i); $this->res_fieldsInfo[$i]['len'] = mysql_field_len($this->res_data, $i); $this->res_fieldsInfo[$i]['flags'] = mysql_field_flags($this->res_data, $i); } } // log error if ($this->res_errMsg != '') { // put here code to log error } } $this->res_sql = $sql; // show debug info if on if ($this->debug == true) { print "<pre>" . $sql . "<hr>"; if ($this->res_errMsg != '') { print "<span style='color: red'>" . $this->res_errMsg . "</span><hr>"; } print "</pre>"; } // restore errors ini_set('display_errors', $ini_err); return $res; }
public function execute($sql) { global $sys_dbPrefix; global $ses_userid; // hide errors $ini_err = ini_get('display_errors'); ini_set('display_errors', 0); $res = false; // --- process sql if ($this->dbType == 'postgres') { $this->res_data = pg_query($this->dbConn, $sql); if (!$this->res_data) { $this->res_errMsg = pg_last_error($this->dbConn); } else { $this->res_errMsg = pg_result_error($this->res_data); $this->res_affectedRows = pg_affected_rows($this->res_data); $this->res_rowCount = pg_num_rows($this->res_data); $this->res_fieldCount = pg_num_fields($this->res_data); $res = new phpRecordSet($this->dbType, $this->res_data, $this->res_rowCount, $this->res_fieldCount); // -- parse field names for ($i = 0; $i < $this->res_fieldCount; $i++) { $this->res_fields[$i] = pg_field_name($this->res_data, $i); $this->res_fieldsInfo[$i] = array(); $this->res_fieldsInfo[$i]['type'] = pg_field_type($this->res_data, $i); $this->res_fieldsInfo[$i]['len'] = pg_field_size($this->res_data, $i); $this->res_fieldsInfo[$i]['is_null'] = pg_field_is_null($this->res_data, $i); $this->res_fieldsInfo[$i]['prt_len'] = pg_field_prtlen($this->res_data, $i); } } // log error if ($this->res_errMsg != '') { $userid = $ses_userid != null ? $ses_userid : 'null'; $ssql = "INSERT INTO " . $sys_dbPrefix . "log_error(domain, url, userid, sql, error)\n\t\t\t\t\t\t VALUES('" . $_SERVER["HTTP_HOST"] . "', '" . pg_escape_string($_SERVER["REQUEST_URI"]) . "', {$userid}, \n\t\t\t\t\t\t\t'" . pg_escape_string($sql) . "', '" . pg_escape_string($this->res_errMsg) . "');"; pg_query($this->dbConn, $ssql); } } // --- mysql if ($this->dbType == 'mysql') { $this->res_data = mysql_query($sql, $this->dbConn); if (!$this->res_data) { $this->res_errMsg = mysql_error($this->dbConn); } else { $this->res_errMsg = mysql_error($this->res_data); $this->res_affectedRows = mysql_affected_rows($this->res_data); $this->res_rowCount = mysql_num_rows($this->res_data); $this->res_fieldCount = mysql_num_fields($this->res_data); $res = new phpRecordSet($this->dbType, $this->res_data, $this->res_rowCount, $this->res_fieldCount); // -- parse field names for ($i = 0; $i < $this->res_fieldCount; $i++) { $this->res_fields[$i] = mysql_field_name($this->res_data, $i); $this->res_fieldsInfo[$i] = array(); $this->res_fieldsInfo[$i]['type'] = mysql_field_type($this->res_data, $i); $this->res_fieldsInfo[$i]['len'] = mysql_field_len($this->res_data, $i); $this->res_fieldsInfo[$i]['flags'] = mysql_field_flags($this->res_data, $i); } } // log error if ($this->res_errMsg != '') { $userid = $ses_userid != null ? $ses_userid : 'null'; $ssql = "INSERT INTO " . $sys_dbPrefix . "log_error(domain, url, userid, sql, error)\n\t\t\t\t\t\t VALUES('" . $_SERVER["HTTP_HOST"] . "', '" . mysql_escape_string($_SERVER["REQUEST_URI"]) . "', {$userid}, \n\t\t\t\t\t\t\t'" . mysql_escape_string($sql) . "', '" . mysql_escape_string($this->res_errMsg) . "');"; mysql_query($this->dbConn, $ssql); } } // show debug info if on if ($this->debug == true) { print "<pre>" . $sql . "<hr>"; if ($this->res_errMsg != '') { print "<span style='color: red'>" . $this->res_errMsg . "</span><hr>"; } print "</pre>"; } // restore errors ini_set('display_errors', $ini_err); return $res; }
} for ($i = 0; $i < $rows; $i++) { pg_fetch_array($result, $i, PGSQL_NUM); } for ($i = 0; $i < $rows; $i++) { pg_fetch_object($result); } for ($i = 0; $i < $rows; $i++) { pg_fetch_row($result, $i); } for ($i = 0; $i < $rows; $i++) { pg_fetch_result($result, $i, 0); } pg_result_error($result); pg_num_rows(pg_execute($db, "php_test", array(100))); pg_num_fields(pg_execute($db, "php_test", array(100))); pg_field_name($result, 0); pg_field_num($result, $field_name); pg_field_size($result, 0); pg_field_type($result, 0); pg_field_prtlen($result, 0); pg_field_is_null($result, 0); $result = pg_prepare($db, "php_test2", "INSERT INTO " . $table_name . " VALUES (\$1, \$2);"); pg_result_error($result); pg_free_result($result); $result = pg_execute($db, "php_test2", array(9999, "A'BC")); pg_last_oid($result); pg_free_result($result); } pg_close($db); echo "OK";
public function fetch_all($index = null) { if ($this->error) { return null; } if (!$this->rs) { $this->select()->read()->ok(); } $rows = array(); while ($this->rs && ($row = pg_fetch_array($this->rs, count($rows), PGSQL_NUM)) && $row) { $j = count($rows); $res = array(); $pri = array(); foreach ($row as $i => $value) { $type = pg_field_type($this->rs, $i); $field = pg_field_name($this->rs, $i); $res[$field] = $value; if (pg_field_is_null($this->rs, $j, $i)) { $res[$field] = NULL; } else { if (preg_match('/^(int|bigint|smallint)/', $type)) { $res[$field] = intval($value); } else { if (preg_match('/^(float|double)/', $type)) { $res[$field] = floatval($value); } } } } $rows[$index ? $res[$index] : $j] = $res; } foreach ($rows as $i => $row) { $rows[$i] = dict($row); } return dict($rows); }
function pg_fetch_field($result, $file_number) { $pg_to_php = array('bit' => 'bit', 'boolean' => 'bool', 'box' => 'box', 'character' => 'bpchar', 'char' => 'bpchar', 'bytea' => 'bytea', 'cidr' => 'cidr', 'circle' => 'circle', 'date' => 'date', 'daterange' => 'daterange', 'real' => 'float4', 'double precision' => 'float8', 'inet' => 'inet', 'smallint' => 'int2', 'smallserial' => 'int2', 'integer' => 'int4', 'serial' => 'int4', 'int4range' => 'int4range', 'bigint' => 'int8', 'bigserial' => 'int8', 'int8range' => 'int8range', 'interval' => 'interval', 'json' => 'json', 'lseg' => 'lseg', 'macaddr' => 'macaddr', 'money' => 'money', 'decimal' => 'numeric', 'numeric' => 'numeric', 'numrange' => 'numrange', 'path' => 'path', 'point' => 'point', 'polygon' => 'polygon', 'text' => 'text', 'time' => 'time', 'time without time zone' => 'time', 'timestamp' => 'timestamp', 'timestamp without time zone' => 'timestamp', 'timestamp with time zone' => 'timestamptz', 'time with time zone' => 'timetz', 'tsquery' => 'tsquery', 'tsrange' => 'tsrange', 'tstzrange' => 'tstzrange', 'tsvector' => 'tsvector', 'uuid' => 'uuid', 'bit varying' => 'varbit', 'character varying' => 'varchar', 'varchar' => 'varchar', 'xml' => 'xml'); $arr['name'] = pg_field_name($result, $field_number); $arr['table'] = pg_field_table($result, $field_number); $arr['max_length'] = pg_field_size($result, $field_number); $arr['not_null'] = @pg_field_is_null($result, $field_number, $arr['name']); $arr['primary_key'] = -1; $arr['unique_key '] = -1; $arr['multiple_key'] = -1; $arr['numeric'] = -1; $arr['blob'] = -1; $arr['type'] = $pg_to_php[pg_field_type($result, $file_number)]; $arr['unsigned'] = -1; $arr['zerofill'] = -1; return (object) $arr; }
/** * Determine whether the value of a query result located in given row and * field is a null. * * @param int $rownum number of the row where the data can be found * @param int $colnum field number where the data can be found * @return mixed true or false on success, a MDB2 error on failure * @access public */ function resultIsNull($rownum, $colnum) { if ($this->mdb->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL) { return parent::resultIsNull($rownum, $colnum); } $value = pg_field_is_null($this->result, $rownum, $colnum); if (!$value) { if (is_null($this->result)) { return $this->mdb->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 'fetch: resultset has already been freed'); } } return (bool) $value; }
/** * Get the column information * @param integer * @return object */ protected function fetch_field($intOffset) { $objData = new stdClass(); $objData->name = @pg_field_name($this->resResult, $intOffset); $objData->max_length = @pg_field_size($this->resResult, $intOffset); $objData->not_null = @pg_field_is_null($this->resResult, $intOffset); $objData->type = @pg_field_type($this->resResult, $intOffset); return $objData; }
/** * Чтение 1 записи, возврат объекта * @param null $i * @return object */ public function row($i = null) { //Если значение больше, чем строк в запросе, выходим if ($i >= pg_num_rows($this->result)) { return false; } //Результат возврата $return = pg_fetch_object($this->result, $i); if ($return == false) { return $return; } //Прооходим по колонкам, собираем массив foreach ($return as $column => $value) { //Получаем номер столбца $num = pg_field_num($this->result, $column); //Если значение нулевое, идём дальше if (pg_field_is_null($this->result, $i, $num)) { $return->{$column} = null; continue; } //В зависимости от типа присваиваем значение switch (pg_field_type($this->result, $num)) { case "bool": $return->{$column} = $value == "t" ? true : false; break; case "int4": case "int8": $return->{$column} = (int) $value; break; } } return $return; }
public static function arrayToPhp($string, $pg_data_type) { // If the string passed is Null, we cant to preserve its value. if ($string === null) { return Null; } // Ensure string starts with "{". Otherwise, PostgreSQL will freak // out. if ($string[0] !== '{') { $string = '{' . $string; } // Likewise, ensure string ends with "}". if (substr($string, -1) !== '}') { $string .= '}'; } // Ensure the data-type passed ends with "[]". This informs // PostgreSQL the data-type passed is to be parsed as an array. if (substr($pg_data_type, -2) !== '[]') { $pg_data_type .= '[]'; } // TODO: Shouldn't $string be encapsulated in pg_escape_string()? $grab_array_values = pg_query("SELECT UNNEST('" . pg_escape_string($string) . "'::" . $pg_data_type . ") AS value"); $array_values = array(); $pos = 0; while ($array_value = pg_fetch_assoc($grab_array_values)) { // Account for Null values. if (pg_field_is_null($grab_array_values, $pos, 'value')) { $array_values[] = Null; } else { $array_values[] = $array_value['value']; } $pos++; } return $array_values; }
/** * Get column information * @param int * @return array */ protected function fetch_field($intOffset) { $arrData['name'] = @pg_field_name($this->resResult, $intOffset); $arrData['max_length'] = @pg_field_size($this->resResult, $intOffset); $arrData['not_null'] = @pg_field_is_null($this->resResult, $intOffset); $arrData['type'] = @pg_field_type($this->resResult, $intOffset); return $arrData; }