Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
 public function lastId()
 {
     $re = $this->connectionId ? @pg_last_oid($this->connectionId) : false;
     if (!$re) {
         throw new Exception($this->errorInfo());
     }
     return $re;
 }
Ejemplo n.º 3
0
 public static function create($entry, $body, $author, $author_ip)
 {
     if (!$entry->id || !trim($body)) {
         return null;
     }
     $result = pg_query("insert into comments (entry_id, body, author, author_ip) values ('" . pesc($entry->id) . "', '" . pesc($body) . "', '" . pesc($author) . "', '" . pesc($author_ip) . "')");
     if (!($id = pg_last_oid($result))) {
         return null;
     }
     return self::from_row(pg_query("select * from comments where id = '" . pesc($id) . "'"));
 }
Ejemplo n.º 4
0
 public function save()
 {
     if ($this->id == 0) {
         $this->id = pg_last_oid(pg_query("insert into cars () values ()"));
         self::$_rows[$this->id] = array();
     }
     self::$_rows[$this->id]['id'] = $this->id;
     self::$_rows[$this->id]['price'] = $this->price;
     self::$_rows[$this->id]['make'] = $this->make;
     self::$_rows[$this->id]['model'] = $this->model;
     self::$_rows[$this->id]['year'] = $this->year;
     self::$_rows[$this->id]['mileage'] = $this->mileage;
     self::$_rows[$this->id]['vin'] = $this->vin;
     self::$_rows[$this->id]['uri'] = $this->uri;
     self::$_rows[$this->id]['dealer'] = $this->dealer;
     $updates = array();
     foreach (self::$_rows[$this->id] as $col => $val) {
         $updates[] = pesc($col) . '=' . pesc($val);
     }
     pg_query('update cars set ' . implode(', ', $updates) . ' where id=\'' . pesc($this->id) . '\'');
     $this->loadVariables();
 }
Ejemplo n.º 5
0
/**
 * Returns the latest INSERT_ID of an SQL INSERT INTO command, for auto-increment columns
 *
 * @access public
 * @param  string   Name of the table to get a INSERT ID for
 * @param  string   Name of the column to get a INSERT ID for
 * @return int      Value of the auto-increment column
 */
function serendipity_db_insert_id($table = '', $id = '')
{
    global $serendipity;
    if (empty($table) || empty($id)) {
        // BC - will/should never be called with empty parameters!
        return pg_last_oid($serendipity['dbLastResult']);
    } else {
        $query = "SELECT currval('{$serendipity['dbPrefix']}{$table}_{$id}_seq'::text) AS {$id}";
        $res = pg_query($serendipity['dbConn'], $query);
        if (pg_num_rows($res)) {
            $insert_id = pg_fetch_array($res, 0, PGSQL_ASSOC);
            return $insert_id[$id];
        } else {
            return pg_last_oid($serendipity['dbLastResult']);
            // BC - should not happen!
        }
    }
}
Ejemplo n.º 6
0
 function get_insert_id($query)
 {
     $this->last_oid = pg_last_oid($this->result);
     if (empty($this->last_oid)) {
         return '';
     }
     // try to find table name
     eregi("insert *into *([^ ]+).*", $query, $regs);
     //print_r($regs);
     $table_name = $regs[1];
     $query_for_id = "SELECT * FROM {$table_name} WHERE oid='{$this->last_oid}'";
     //echo $query_for_id."<br>";
     $result_for_id = pg_query($this->dbh, $query_for_id);
     if (pg_num_rows($result_for_id)) {
         $id = pg_fetch_array($result_for_id, 0, PGSQL_NUM);
         //print_r($id);
         return $id[0];
     }
 }
Ejemplo n.º 7
0
 public function getInsertID()
 {
     return pg_last_oid($this->result[$name]);
 }
 /**
  * Insert ID
  *
  * @access	public
  * @return	integer
  */
 function insert_id()
 {
     return pg_last_oid($this->result_id);
 }
Ejemplo n.º 9
0
 function last_insert_id(&$result, $pkfield, $table)
 {
     // returns the id of the most recently modified record
     trigger_before('last_insert_id', $this, $this);
     global $prefix;
     $oid = @pg_last_oid($result);
     if (!$oid) {
         trigger_error(@pg_last_error($this->conn), E_USER_ERROR);
     }
     $sql = "SELECT " . $pkfield . " FROM " . $prefix . $table . " WHERE oid = " . $oid;
     $res = $this->get_result($sql);
     if (!$res) {
         trigger_error("error in last_insert_id in postgresql.php" . @pg_last_error($this->conn), E_USER_ERROR);
     } else {
         return $this->result_value($res, 0, $pkfield);
     }
 }
Ejemplo n.º 10
0
 function _performQuery($queryMain)
 {
     $this->_lastQuery = $queryMain;
     $isInsert = preg_match('/^\\s* INSERT \\s+/six', $queryMain[0]);
     //
     // Note that in case of INSERT query we CANNOT work with prepare...execute
     // cache, because RULEs do not work after pg_execute(). This is a very strange
     // bug... To reproduce:
     //   $DB->query("CREATE TABLE test(id SERIAL, str VARCHAR(10)) WITH OIDS");
     //   $DB->query("CREATE RULE test_r AS ON INSERT TO test DO (SELECT 111 AS id)");
     //   print_r($DB->query("INSERT INTO test(str) VALUES ('test')"));
     // In case INSERT + pg_execute() it returns new row OID (numeric) instead
     // of result of RULE query. Strange, very strange...
     //
     if ($this->DbSimple_Postgresql_USE_NATIVE_PHOLDERS && !$isInsert) {
         // Use native placeholders only if PG supports them.
         $this->_expandPlaceholders($queryMain, true);
         $hash = md5($queryMain[0]);
         if (!isset($this->prepareCache[$hash])) {
             $prepared = @pg_prepare($this->link, $hash, $queryMain[0]);
             if ($prepared === false) {
                 return $this->_setDbError($queryMain[0]);
             } else {
                 $this->prepareCache[$hash] = true;
             }
         } else {
             // Prepare cache hit!
         }
         $result = pg_execute($this->link, $hash, array_slice($queryMain, 1));
     } else {
         // No support for native placeholders on INSERT query.
         $this->_expandPlaceholders($queryMain, false);
         $result = @pg_query($this->link, $queryMain[0]);
     }
     if ($result === false) {
         return $this->_setDbError($queryMain);
     }
     if (!pg_num_fields($result)) {
         if ($isInsert) {
             // INSERT queries return generated OID (if table is WITH OIDs).
             //
             // Please note that unfortunately we cannot use lastval() PostgreSQL
             // stored function because it generates fatal error if INSERT query
             // does not contain sequence-based field at all. This error terminates
             // the current transaction, and we cannot continue to work nor know
             // if table contains sequence-updateable field or not.
             //
             // To use auto-increment functionality you must invoke
             //   $insertedId = $DB->query("SELECT lastval()")
             // manually where it is really needed.
             //
             return @pg_last_oid($result);
         }
         // Non-SELECT queries return number of affected rows, SELECT - resource.
         return @pg_affected_rows($result);
     }
     return $result;
 }
Ejemplo n.º 11
0
 public function insert_id()
 {
     return pg_last_oid($this->connection);
 }
Ejemplo n.º 12
0
 public function lastInsertId()
 {
     return $this->_result ? pg_last_oid($this->_result) : 0;
 }
 function ultimo_insertado($enlace = "")
 {
     return pg_last_oid($enlace);
 }
Ejemplo n.º 14
0
 function LastOID()
 {
     if (version_compare(phpversion(), "4.2.0", "ge") > 0) {
         $this->oid = pg_last_oid($this->result);
     }
     return $this->oid;
 }
Ejemplo n.º 15
0
/**
 * Executes a query on a Vmoodle database. Query must return no results,
 * so it may be an INSERT or an UPDATE or a DELETE.
 * @param object $vmoodle The Vmoodle object.
 * @param string $sql The SQL request.
 * @param handle $cnx The connection to the Vmoodle database.
 * @return boolean true if the request is well-executed, false otherwise.
 */
function vmoodle_execute_query(&$vmoodle, $sql, $cnx)
{
    // If database is MySQL typed.
    if ($vmoodle->vdbtype == 'mysql') {
        if (!($res = mysql_query($sql, $cnx))) {
            echo "vmoodle_execute_query() : " . mysql_error($cnx) . "<br/>";
            return false;
        }
        if ($newid = mysql_insert_id($cnx)) {
            $res = $newid;
            // get the last insert id in case of an INSERT
        }
    } elseif ($vmoodle->vdbtype == 'postgres') {
        if (!($res = pg_query($cnx, $sql))) {
            echo "vmoodle_execute_query() : " . pg_last_error($cnx) . "<br/>";
            return false;
        }
        if ($newid = pg_last_oid($res)) {
            $res = $newid;
            // Get the last insert id in case of an INSERT.
        }
    } else {
        echo "vmoodle_execute_query() : Database not supported<br/>";
        return false;
    }
    return $res;
}
Ejemplo n.º 16
0
 /** NOT REALLY SUPPORTED, returned value is not last inserted id
  *	Returns pg_last_oid function
  *       	this->lastInsertId( void ):String
  * @Return	String		OID returned from Postgre
  */
 function lastInsertId()
 {
     $result = 0;
     if (!is_null($this->__result)) {
         $result = pg_last_oid($this->__result);
     }
     return $result;
 }
 function last_oid($result)
 {
     if ($oid = pg_last_oid($result)) {
         return $oid;
     } else {
         return false;
     }
 }
Ejemplo n.º 18
0
 public function insertID()
 {
     return !$this->query ? false : (int) pg_last_oid($this->query);
 }
Ejemplo n.º 19
0
 /**
  * Insert ID
  *
  * @return	int
  */
 public function insertID()
 {
     $v = pg_version($this->connID);
     // 'server' key is only available since PostgreSQL 7.4
     $v = isset($v['server']) ? $v['server'] : 0;
     $table = func_num_args() > 0 ? func_get_arg(0) : null;
     $column = func_num_args() > 1 ? func_get_arg(1) : null;
     if ($table === null && $v >= '8.1') {
         $sql = 'SELECT LASTVAL() AS ins_id';
     } elseif ($table !== null) {
         if ($column !== null && $v >= '8.0') {
             $sql = "SELECT pg_get_serial_sequence('{$table}', '{$column}') AS seq";
             $query = $this->query($sql);
             $query = $query->row();
             $seq = $query->seq;
         } else {
             // seq_name passed in table parameter
             $seq = $table;
         }
         $sql = "SELECT CURRVAL('{$seq}') AS ins_id";
     } else {
         return pg_last_oid($this->resultID);
     }
     $query = $this->query($sql);
     $query = $query->getRow();
     return (int) $query->ins_id;
 }
 /**
  * Get the last auto-generated ID from the RecordSet.
  *
  * @return int Returns the last auto-generated ID from the RecordSet.
  */
 function LastId()
 {
     return pg_last_oid($this->result);
 }
Ejemplo n.º 21
0
 /**
  * 获取最后插入的ID
  *
  * @return int
  */
 protected function _insert_id()
 {
     $connection = $this->connection();
     $v = pg_version($connection);
     $v = isset($v['server']) ? $v['server'] : 0;
     // 'server' key is only available since PosgreSQL 7.4
     $table = func_num_args() > 0 ? func_get_arg(0) : null;
     $column = func_num_args() > 1 ? func_get_arg(1) : null;
     if ($table === null && $v >= '8.1') {
         $sql = 'SELECT LASTVAL() AS ins_id';
     } elseif ($table !== null) {
         if ($column !== null && $v >= '8.0') {
             $sql = 'SELECT pg_get_serial_sequence(\'' . $table . "', '" . $column . "') AS seq";
             $query = pg_query($sql);
             $query = pg_fetch_array($query);
             $seq = $query['seq'];
         } else {
             // seq_name passed in table parameter
             $seq = $table;
         }
         $sql = 'SELECT CURRVAL(\'' . $seq . "') AS ins_id";
     } else {
         return pg_last_oid($this->result_id);
     }
     $query = $this->query($sql);
     $query = pg_fetch_array($query);
     return (int) $query['ins_id'];
 }
Ejemplo n.º 22
0
 /**
  * Insert ID
  *
  * @access	public
  * @return	integer
  */
 function insert_id()
 {
     $v = $this->_version();
     $v = $v['server'];
     $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
     $column = func_num_args() > 1 ? func_get_arg(1) : NULL;
     if ($table == NULL && $v >= '8.1') {
         $sql = 'SELECT LASTVAL() as ins_id';
     } elseif ($table != NULL && $column != NULL && $v >= '8.0') {
         $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
         $query = $this->query($sql);
         $row = $query->row();
         $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
     } elseif ($table != NULL) {
         // seq_name passed in table parameter
         $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
     } else {
         return pg_last_oid($this->result_id);
     }
     $query = $this->query($sql);
     $row = $query->row();
     return $row->ins_id;
 }
Ejemplo n.º 23
0
 public function insertID()
 {
     if (empty($this->query)) {
         return false;
     }
     return pg_last_oid($this->query);
 }
Ejemplo n.º 24
0
 function insert_id($table, $column)
 {
     $oid = pg_last_oid($this->last_result);
     /* 
     Shouldn't be need, but incase they don't have oid support ( not sure why they wouldn't )
     They can fall back to the less acurrate method, totally not recommended for active sites
     
     if ($result = $this->query("SELECT last_value FROM pg_get_serial_sequence($table, $column)"))
     {
     	$return = $this->fetch_row_assoc($result);
     	$this->free_result($result);
     	
     	$return['last_value'];
     }
     */
     if ($oid === false || !($result = $this->query("SELECT {$column} FROM {$table} WHERE oid = {$oid}"))) {
         return false;
     }
     $return = $this->fetch_row_assoc($result);
     $this->free_result($result);
     return $return[$column];
 }
Ejemplo n.º 25
0
 /**
  * Executes a sql statement.
  *
  * @param string $key Cache key
  * @param int $expire Expiration time in seconds
  * @return object Query results object
  * @throws Exception When database is not defined
  */
 public function execute($key = null, $expire = 0)
 {
     if (!$this->db) {
         throw new Exception('Database is not defined.');
     }
     if ($key !== null) {
         $result = $this->fetch($key);
         if ($this->is_cached) {
             return $result;
         }
     }
     $result = null;
     $this->is_cached = false;
     $this->num_rows = 0;
     $this->affected_rows = 0;
     $this->insert_id = -1;
     $this->last_query = $this->sql;
     if ($this->stats_enabled) {
         if (empty($this->stats)) {
             $this->stats = array('queries' => array());
         }
         $this->query_time = microtime(true);
     }
     if (!empty($this->sql)) {
         $error = null;
         switch ($this->db_type) {
             case 'pdo':
                 try {
                     $result = $this->db->prepare($this->sql);
                     if (!$result) {
                         $error = $this->db->errorInfo();
                     } else {
                         $result->execute();
                         $this->num_rows = $result->rowCount();
                         $this->affected_rows = $result->rowCount();
                         $this->insert_id = $this->db->lastInsertId();
                     }
                 } catch (PDOException $ex) {
                     $error = $ex->getMessage();
                 }
                 break;
             case 'mysqli':
                 $result = $this->db->query($this->sql);
                 if (!$result) {
                     $error = $this->db->error;
                 } else {
                     if (is_object($result)) {
                         $this->num_rows = $result->num_rows;
                     } else {
                         $this->affected_rows = $this->db->affected_rows;
                     }
                     $this->insert_id = $this->db->insert_id;
                 }
                 break;
             case 'mysql':
                 $result = mysql_query($this->sql, $this->db);
                 if (!$result) {
                     $error = mysql_error();
                 } else {
                     if (!is_bool($result)) {
                         $this->num_rows = mysql_num_rows($result);
                     } else {
                         $this->affected_rows = mysql_affected_rows($this->db);
                     }
                     $this->insert_id = mysql_insert_id($this->db);
                 }
                 break;
             case 'pgsql':
                 $result = pg_query($this->db, $this->sql);
                 if (!$result) {
                     $error = pg_last_error($this->db);
                 } else {
                     $this->num_rows = pg_num_rows($result);
                     $this->affected_rows = pg_affected_rows($result);
                     $this->insert_id = pg_last_oid($result);
                 }
                 break;
             case 'sqlite':
                 $result = sqlite_query($this->db, $this->sql, SQLITE_ASSOC, $error);
                 if ($result !== false) {
                     $this->num_rows = sqlite_num_rows($result);
                     $this->affected_rows = sqlite_changes($this->db);
                     $this->insert_id = sqlite_last_insert_rowid($this->db);
                 }
                 break;
             case 'sqlite3':
                 $result = $this->db->query($this->sql);
                 if ($result === false) {
                     $error = $this->db->lastErrorMsg();
                 } else {
                     $this->num_rows = 0;
                     $this->affected_rows = $result ? $this->db->changes() : 0;
                     $this->insert_id = $this->db->lastInsertRowId();
                 }
                 break;
         }
         if ($error !== null) {
             if ($this->show_sql) {
                 $error .= "\nSQL: " . $this->sql;
             }
             throw new Exception('Database error: ' . $error);
         }
     }
     if ($this->stats_enabled) {
         $time = microtime(true) - $this->query_time;
         $this->stats['queries'][] = array('query' => $this->sql, 'time' => $time, 'rows' => (int) $this->num_rows, 'changes' => (int) $this->affected_rows);
     }
     return $result;
 }
Ejemplo n.º 26
0
 /**
  * {@inheritdoc}
  */
 public function lastInsertId()
 {
     return @pg_last_oid($this->connection);
 }
Ejemplo n.º 27
0
 function insert_ID()
 {
     return @pg_last_oid($this->zp_db_resource);
 }
Ejemplo n.º 28
0
 public static function query($query, $QT = NULL)
 {
     if (!self::$link) {
         self::connect();
     }
     // filter the query, if filters are available
     // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
     $query = str_replace(iPHP_DB_PREFIX_TAG, iPHP_DB_PREFIX, $query);
     $query = str_replace('`', '', $query);
     // initialise return
     $return_val = 0;
     self::flush();
     // Log how the function was called
     self::$func_call = __CLASS__ . "::query(\"{$query}\")";
     // Keep track of the last query for debug..
     self::$last_query = $query;
     // Perform the query via std pgsql_query function..
     if (SAVEQUERIES) {
         self::timer_start();
     }
     self::$result = pg_query(self::$link, $query);
     self::$num_queries++;
     if (SAVEQUERIES) {
         self::$queries[] = array($query, self::timer_stop());
     }
     // If there is an error then take note of it..
     if (self::$last_error = pg_result_error(self::$result)) {
         self::print_error();
         return false;
     }
     $QH = strtoupper(substr($query, 0, strpos($query, ' ')));
     if (in_array($QH, array('INSERT', 'DELETE', 'UPDATE', 'REPLACE', 'SET', 'CREATE', 'DROP', 'ALTER'))) {
         $rows_affected = pg_affected_rows(self::$result);
         // Take note of the insert_id
         if (in_array($QH, array("INSERT", "REPLACE"))) {
             self::$insert_id = pg_last_oid(self::$result);
         }
         // Return number of rows affected
         $return_val = $rows_affected;
     } else {
         if ($QT == "field") {
             $i = 0;
             while ($i < pg_num_fields(self::$result)) {
                 self::$col_info[$i] = pg_field_name(self::$result);
                 $i++;
             }
         } else {
             $num_rows = 0;
             while ($row = pg_fetch_object(self::$result)) {
                 self::$last_result[$num_rows] = $row;
                 $num_rows++;
             }
             // Log number of rows the query returned
             self::$num_rows = $num_rows;
             // Return number of rows selected
             $return_val = $num_rows;
         }
         pg_free_result(self::$result);
     }
     return $return_val;
 }
Ejemplo n.º 29
0
 /**
  * Insert ID
  *
  * @return	string
  */
 public function insert_id()
 {
     $v = pg_version($this->conn_id);
     $v = isset($v['server']) ? $v['server'] : 0;
     // 'server' key is only available since PosgreSQL 7.4
     $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
     $column = func_num_args() > 1 ? func_get_arg(1) : NULL;
     if ($table === NULL && $v >= '8.1') {
         $sql = 'SELECT LASTVAL() AS ins_id';
     } elseif ($table !== NULL) {
         if ($column !== NULL && $v >= '8.0') {
             $sql = 'SELECT pg_get_serial_sequence(\'' . $table . "', '" . $column . "') AS seq";
             $query = $this->query($sql);
             $query = $query->row();
             $seq = $query->seq;
         } else {
             // seq_name passed in table parameter
             $seq = $table;
         }
         $sql = 'SELECT CURRVAL(\'' . $seq . "') AS ins_id";
     } else {
         return pg_last_oid($this->result_id);
     }
     $query = $this->query($sql);
     $query = $query->row();
     return (int) $query->ins_id;
 }
Ejemplo n.º 30
0
    }
    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";