Exemple #1
0
 /**
  * Inserts a new record into database.
  *
  * @param unknown $sql
  *        	the SQL statement for insert.
  * @throws Exception
  * @return unknown
  */
 public function insertDb($sql)
 {
     $lastId = null;
     $connection = $this->connect();
     $stmt = db2_exec($connection, $sql);
     $lastId = db2_last_insert_id($connection);
     if (!$stmt) {
         $message = "\nCould not insert into database. " . db2_stmt_errormsg();
         throw new Exception($message);
     }
     $this->__destruct();
     return $lastId;
 }
Exemple #2
0
 /**
  * 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.db2-last-insert-id.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.');
     }
     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 = @db2_last_insert_id($this->resource);
         if ($id === FALSE) {
             throw new Throwable_SQL_Exception('Message: Failed to fetch the last insert id. Reason: :reason', array(':reason' => @db2_conn_error($this->resource)));
         }
         settype($id, 'integer');
         return $id;
     }
 }
 /**
  * Updates the mInsertId property with the value of the last insert
  *  into a generated column
  *
  * @param $table      String: sanitized table name
  * @param $primaryKey Mixed: string name of the primary key
  * @param $stmt       Resource: prepared statement resource
  *  of the SELECT primary_key FROM FINAL TABLE ( INSERT ... ) form
  */
 private function calcInsertId($table, $primaryKey, $stmt)
 {
     if ($primaryKey) {
         $this->mInsertId = db2_last_insert_id($this->mConn);
     }
 }
Exemple #4
0
 function lastInsertId($name = null)
 {
     return db2_last_insert_id($this->_conn);
 }
Exemple #5
0
 /**
  * Get last generated id
  *
  * @param  null $name Ignored
  * @return integer
  */
 public function getLastGeneratedValue($name = null)
 {
     return db2_last_insert_id($this->resource);
 }
 public static function lastInsertId($conn, $table, $IdCol, $dbtype)
 {
     $idCol = db2_last_insert_id($conn);
     return $idCol;
 }
Exemple #7
0
 public function execute($bind_params = false)
 {
     $ret_val = false;
     //----------------------------------------------
     // Check Statement Resource
     //----------------------------------------------
     if (!$this->stmt) {
         $this->gen_error('Invalid statement resource.');
         return false;
     }
     //----------------------------------------------
     // Check Bind Parameters
     //----------------------------------------------
     if (!is_array($bind_params)) {
         $this->gen_error('Binding parameters must be passed as an array.');
         return false;
     }
     $this->bind_params = $bind_params;
     //----------------------------------------------
     // Execute Query
     //----------------------------------------------
     $exec_status = @db2_execute($this->stmt, $bind_params);
     //----------------------------------------------
     // Check for Errors
     //----------------------------------------------
     if (!$exec_status) {
         if ($this->check_and_print_stmt_error($this->stmt)) {
             return false;
         }
         $this->gen_error('Prepared query execution failed.');
         return false;
     }
     //----------------------------------------------
     // Create Data Result Object if Necessary
     //----------------------------------------------
     if ($this->stmt && gettype($this->stmt) != 'boolean') {
         //----------------------------------------------
         // Affected Rows
         //----------------------------------------------
         $this->affected_rows = db2_num_rows($this->stmt);
         $ret_val = $this->affected_rows;
         //----------------------------------------------
         // Create Data Result Object
         //----------------------------------------------
         $this->data_result = new data_result($this->stmt, $this->data_src);
         //----------------------------------------------
         // Last Insert ID
         //----------------------------------------------
         $this->last_id = db2_last_insert_id($this->handle);
     }
     //----------------------------------------------
     // Return Data Result Object if it exists
     //----------------------------------------------
     if ($this->data_result) {
         $this->num_rows = $this->data_result->num_rows();
         $this->num_fields = $this->data_result->num_fields();
         $ret_val = $this->data_result;
     }
     //----------------------------------------------
     // Check for Errors
     //----------------------------------------------
     if ($this->check_and_print_stmt_error($this->stmt)) {
         return false;
     }
     return $ret_val;
 }
Exemple #8
0
 /**
  * Get last insert id (if available)
  *
  * @return  int
  * 
  * @throws  \Comodojo\Exception\DatabaseException
  */
 public function getInsertId()
 {
     switch ($this->model) {
         case "MYSQLI":
             $return = $this->handler->insert_id;
             break;
         case "MYSQL_PDO":
         case "SQLITE_PDO":
             $return = $this->handler->lastInsertId();
             break;
         case "ORACLE_PDO":
             try {
                 $return = self::oracleLastInsertId($this->handler);
             } catch (DatabaseException $de) {
                 throw $de;
             }
             break;
         case "DBLIB_PDO":
             try {
                 $return = self::dblibLastInsertId($this->handler);
             } catch (DatabaseException $de) {
                 throw $de;
             }
             break;
         case "DB2":
             $return = db2_last_insert_id($this->handler);
             break;
         case "POSTGRESQL":
             //$return = pg_last_oid($this->raw_data);
             $return = self::postgresqlLastInsertId($this->handler);
             break;
     }
     return intval($return);
 }
 /**	
  * Get the auto generated id used in the last query
  * @return Number
  */
 public function getInsertedId()
 {
     db2_last_insert_id($this->conn);
 }
Exemple #10
0
 /**
  * Transform database raw result in a standard array
  *
  * @param   mixed   $data   Query result as returned from database handler
  *
  * @return  array
  */
 private function resultsToArray($data)
 {
     $result = array();
     $id = false;
     $length = 0;
     $rows = 0;
     $iterator = 0;
     switch ($this->model) {
         case "MYSQLI":
             if ((!is_object($data) or !is_a($data, 'mysqli_result')) and $data != TRUE) {
                 throw new DatabaseException('Invalid result data for model ' . $this->model);
             }
             switch ($this->fetch) {
                 case 'NUM':
                     $fetch = MYSQLI_NUM;
                     break;
                 case 'ASSOC':
                     $fetch = MYSQLI_ASSOC;
                     break;
                 default:
                     $fetch = MYSQLI_BOTH;
                     break;
             }
             $this->length = is_object($data) ? $data->num_rows : 0;
             $this->id = $this->dbh->insert_id;
             $this->rows = $this->dbh->affected_rows;
             while ($iterator < $this->length) {
                 $result[$iterator] = $data->fetch_array($fetch);
                 $iterator++;
             }
             if (is_object($data)) {
                 $data->free();
             }
             break;
         case "MYSQL_PDO":
         case "SQLITE_PDO":
             if (!is_object($data)) {
                 throw new DatabaseException('Invalid result data for model ' . $this->model);
             }
             switch ($this->fetch) {
                 case 'NUM':
                     $fetch = \PDO::FETCH_NUM;
                     break;
                 case 'ASSOC':
                     $fetch = \PDO::FETCH_ASSOC;
                     break;
                 default:
                     $fetch = \PDO::FETCH_BOTH;
                     break;
             }
             try {
                 $result = $data->fetchAll($fetch);
             } catch (\PDOException $pe) {
                 $result = true;
             }
             $this->length = sizeof($result);
             $this->id = $this->dbh->lastInsertId();
             $this->rows = $data->rowCount();
             break;
         case "ORACLE_PDO":
             if (!is_object($data)) {
                 throw new DatabaseException('Invalid result data for model ' . $this->model);
             }
             switch ($this->fetch) {
                 case 'NUM':
                     $fetch = \PDO::FETCH_NUM;
                     break;
                 case 'ASSOC':
                     $fetch = \PDO::FETCH_ASSOC;
                     break;
                 default:
                     $fetch = \PDO::FETCH_BOTH;
                     break;
             }
             $result = $data->fetchAll($fetch);
             $this->length = sizeof($result);
             $this->rows = $data->rowCount();
             try {
                 $this->id = $this->oracleLastInsertId();
             } catch (DatabaseException $de) {
                 throw $de;
             }
             break;
         case "DBLIB_PDO":
             if (!is_object($data)) {
                 throw new DatabaseException('Invalid result data for model ' . $this->model);
             }
             switch ($this->fetch) {
                 case 'NUM':
                     $fetch = \PDO::FETCH_NUM;
                     break;
                 case 'ASSOC':
                     $fetch = \PDO::FETCH_ASSOC;
                     break;
                 default:
                     $fetch = \PDO::FETCH_BOTH;
                     break;
             }
             $result = $data->fetchAll($fetch);
             $this->length = sizeof($result);
             $this->rows = $data->rowCount();
             try {
                 $this->id = $this->dblibLastInsertId();
             } catch (DatabaseException $de) {
                 throw $de;
             }
             break;
         case "DB2":
             if (!is_resource($data) or @get_resource_type($data) != "DB2 Statement") {
                 throw new DatabaseException('Invalid result data for model ' . $this->model);
             }
             $this->length = db2_num_fields($data);
             $this->id = db2_last_insert_id($this->dbh);
             $this->rows = db2_num_rows($data);
             switch ($this->fetch) {
                 case 'NUM':
                     while ($row = db2_fetch_row($data)) {
                         array_push($result, $row);
                     }
                     break;
                 case 'ASSOC':
                     while ($row = db2_fetch_assoc($data)) {
                         array_push($result, $row);
                     }
                     break;
                 default:
                     while ($row = db2_fetch_both($data)) {
                         array_push($result, $row);
                     }
                     break;
             }
             break;
         case "POSTGRESQL":
             if (!is_resource($data) or @get_resource_type($data) != "pgsql result") {
                 throw new DatabaseException('Invalid result data for model ' . $this->model);
             }
             $this->length = pg_num_rows($data);
             $this->id = pg_last_oid($data);
             $this->rows = pg_affected_rows($data);
             while ($iterator < $this->length) {
                 switch ($this->fetch) {
                     case 'NUM':
                         $result[$iterator] = pg_fetch_array($data);
                         break;
                     case 'ASSOC':
                         $result[$iterator] = pg_fetch_assoc($data);
                         break;
                     default:
                         $result[$iterator] = pg_fetch_all($data);
                         break;
                 }
                 $iterator++;
             }
             break;
     }
     return array("data" => $result, "length" => $this->length, "id" => $this->id, "affected_rows" => $this->rows);
 }