/** * This function returns the last insert id. * * @access public * @override * @param string $table the table to be queried * @param string $column the column representing the table's id * @return integer the last insert id * @throws Throwable_SQL_Exception indicates that the query failed * * @see http://www.php.net/manual/en/pdo.lastinsertid.php */ public function get_last_insert_id($table = NULL, $column = 'id') { if (!$this->is_connected()) { throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: Unable to find connection.'); } try { if (is_string($table)) { $sql = $this->sql; $precompiler = DB_SQL::precompiler($this->data_source); $table = $precompiler->prepare_identifier($table); $column = $precompiler->prepare_identifier($column); $alias = $precompiler->prepare_alias('id'); $id = (int) $this->query("SELECT MAX({$column}) AS {$alias} FROM {$table};")->get('id', 0); $this->sql = $sql; return $id; } return (int) $this->query('SELECT LASTVAL() AS "id";')->get('id', 0); } catch (Exception $ex) { throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: :reason', array(':reason' => $ex->getMessage())); } }
/** * This function returns the last insert id. * * @access public * @override * @param string $table the table to be queried * @param string $column the column representing the table's id * @return integer the last insert id * @throws Throwable_SQL_Exception indicates that the query failed */ public function get_last_insert_id($table = NULL, $column = 'id') { if (!$this->is_connected()) { throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: Unable to find connection.'); } if (is_string($table)) { $sql = $this->sql; $precompiler = DB_SQL::precompiler($this->data_source); $table = $precompiler->prepare_identifier($table); $column = $precompiler->prepare_identifier($column); $id = (int) $this->query("SELECT MAX({$column}) AS `id` FROM {$table};")->get('id', 0); $this->sql = $sql; return $id; } else { $id = @mysqli_insert_id($this->resource); if ($id === FALSE) { throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: :reason', array(':reason' => @mysqli_error($this->resource))); } return $id; } }
/** * This function returns the last insert id. * * @access public * @override * @param string $table the table to be queried * @param string $column the column representing the table's id * @return integer the last insert id * @throws Throwable_SQL_Exception indicates that the query failed * * @see http://www.firebirdfaq.org/faq243/ */ public function get_last_insert_id($table = NULL, $column = 'id') { if (!$this->is_connected()) { throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: Unable to find connection.'); } try { if (is_string($table)) { $sql = $this->sql; $precompiler = DB_SQL::precompiler($this->data_source); $table = $precompiler->prepare_identifier($table); $column = $precompiler->prepare_identifier($column); $id = (int) $this->query("SELECT MAX({$column}) AS \"id\" FROM {$table};")->get('id', 0); $this->sql = $sql; return $id; } else { $sql = $this->sql; if (preg_match('/^INSERT\\s+INTO\\s+(.*?)\\s+/i', $sql, $matches)) { if (isset($matches[1])) { $table = $matches[1]; $id = (int) $this->query("SELECT \"ID\" AS \"id\" FROM {$table} ORDER BY \"ID\" DESC ROWS 1;")->get('id', 0); $this->sql = $sql; return $id; } } return 0; } } catch (Exception $ex) { throw new Throwable_SQL_Exception(preg_replace('/Failed to query SQL statement./', 'Failed to fetch the last insert id.', $ex->getMessage())); } }
/** * This function returns the last insert id. * * @access public * @override * @param string $table the table to be queried * @param string $column the column representing the table's id * @return integer the last insert id * @throws Throwable_SQL_Exception indicates that the query failed * * @see http://www.php.net/manual/en/function.pg-last-oid.php * @see https://github.com/spadefoot/kohana-orm-leap/issues/44 */ public function get_last_insert_id($table = NULL, $column = 'id') { if (!$this->is_connected()) { throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: Unable to find connection.'); } if (is_string($table)) { $sql = $this->sql; $precompiler = DB_SQL::precompiler($this->data_source); $table = $precompiler->prepare_identifier($table); $column = $precompiler->prepare_identifier($column); $id = (int) $this->query("SELECT MAX({$column}) AS \"id\" FROM {$table};")->get('id', 0); $this->sql = $sql; return $id; } else { // Option #1: Using 'SELECT LASTVAL();' $command = @pg_query($this->resource, 'SELECT LASTVAL();'); if ($command === FALSE) { throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: :reason', array(':reason' => @pg_last_error($this->resource))); } $record = @pg_fetch_row($command); if ($record === FALSE) { throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: :reason', array(':reason' => @pg_last_error($this->resource))); } return $record[0]; // Option #2: Using pg_last_oid($this->resource) //$id = @pg_last_oid($this->resource); //if ($id === FALSE) { // throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: :reason', array(':reason' => @pg_last_error($this->resource))); //} //return $id; } }
/** * This function returns the last insert id. * * @access public * @override * @param string $table the table to be queried * @param string $column the column representing the table's id * @return integer the last insert id * @throws Throwable_SQL_Exception indicates that the query failed */ public function get_last_insert_id($table = NULL, $column = 'id') { if (!$this->is_connected()) { throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: Unable to find connection.'); } try { if (is_string($table)) { $sql = $this->sql; $precompiler = DB_SQL::precompiler($this->data_source); $table = $precompiler->prepare_identifier($table); $column = $precompiler->prepare_identifier($column); $id = (int) $this->query("SELECT MAX({$column}) AS [id] FROM {$table};")->get('id', 0); $this->sql = $sql; return $id; } else { $sql = $this->sql; if (preg_match('/^INSERT\\s+(TOP.+\\s+)?INTO\\s+(.*?)\\s+/i', $sql, $matches)) { $table = isset($matches[2]) ? $matches[2] : NULL; $query = !empty($table) ? "SELECT IDENT_CURRENT('{$table}') AS [id];" : 'SELECT SCOPE_IDENTITY() AS [id];'; $id = (int) $this->query($query)->get('id', 0); $this->sql = $sql; return $id; } return 0; } } catch (Exception $ex) { throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: :reason', array(':reason' => $ex->getMessage())); } }
/** * This function returns the compiled SQL expression as a string. * * @access public * @param mixed $object an instance of the pre-compiler or * data source to be used * @return string the compiled SQL expression */ public function value($object = NULL) { if (is_string($object) or is_array($object) or $object instanceof DB_DataSource) { $object = DB_SQL::precompiler($object); } $expr = $this->expr; if ($object instanceof DB_SQL_Precompiler and !empty($this->params)) { $params = array_map(array($object, 'prepare_value'), $this->params); $expr = strtr($expr, $params); } return $expr; }
/** * This constructor instantiates this class using the specified data source. * * @access public * @param DB_Connection_Driver $connection the connection to be used */ public function __construct(DB_Connection_Driver $connection) { $this->connection = $connection; $this->precompiler = DB_SQL::precompiler($connection->data_source); $this->reset(); }