function Prepare($sql, $cursor = false) { static $BINDNUM = 0; $stmt = OCIParse($this->_connectionID, $sql); if (!$stmt) { return false; } $BINDNUM += 1; $sttype = @OCIStatementType($stmt); if ($sttype == 'BEGIN' || $sttype == 'DECLARE') { return array($sql, $stmt, 0, $BINDNUM, $cursor ? OCINewCursor($this->_connectionID) : false); } return array($sql, $stmt, 0, $BINDNUM); }
function Prepare($sql,$cursor=false) { static $BINDNUM = 0; $stmt = OCIParse($this->_connectionID,$sql); if (!$stmt) { $this->_errorMsg = false; $this->_errorCode = false; $arr = @OCIError($this->_connectionID); if ($arr === false) return false; $this->_errorMsg = $arr['message']; $this->_errorCode = $arr['code']; return false; } $BINDNUM += 1; $sttype = @OCIStatementType($stmt); if ($sttype == 'BEGIN' || $sttype == 'DECLARE') { return array($sql,$stmt,0,$BINDNUM, ($cursor) ? OCINewCursor($this->_connectionID) : false); } return array($sql,$stmt,0,$BINDNUM); }
function Prepare($sql,$cursor=false) { static $BINDNUM = 0; $stmt = OCIParse($this->_connectionID,$sql); if (!$stmt) return false; $BINDNUM += 1; if (@OCIStatementType($stmt) == 'BEGIN') { return array($sql,$stmt,0,$BINDNUM, ($cursor) ? OCINewCursor($this->_connectionID) : false); } return array($sql,$stmt,0,$BINDNUM); }
function query($Query_String) { /* No empty queries, please, since PHP4 chokes on them. */ if ($Query_String == "") { /* The empty query string is passed on from the constructor, * when calling the class without a query, e.g. in situations * like these: '$db = new DB_Sql_Subclass;' */ return 0; } $this->connect(); $this->Query_ID = OCIParse($this->Link_ID, $Query_String); if (!$this->Query_ID) { $this->Error = OCIError($this->Query_ID); $this->Halt($this->Error); } else { $old_error_reporting = error_reporting(); error_reporting($old_error_reporting & ~E_WARNING); if (sizeof($this->Binds) > 0) { foreach ($this->Binds as $parameter_name => $parameter_values) { if ($parameter_values[2] == OCI_B_CURSOR) { $this->Binds[$parameter_name][0] = OCINewCursor($this->Link_ID); } if ($parameter_values[2] == 0) { OCIBindByName($this->Query_ID, ":" . $parameter_name, $this->Binds[$parameter_name][0], $parameter_values[1]); } else { OCIBindByName($this->Query_ID, ":" . $parameter_name, $this->Binds[$parameter_name][0], $parameter_values[1], $parameter_values[2]); } } } OCIExecute($this->Query_ID); error_reporting($old_error_reporting); $this->Error = OCIError($this->Query_ID); } $this->Row = 0; if ($this->Debug) { printf("Debug: query = %s<br>\n", $Query_String); } if ($this->Error["code"] != 0 && $this->sqoe) { $this->Halt($this->Error); } #echo "<BR><FONT color=red><B>".$this->Error["message"]."<BR>Query :\"$Query_String\"</B></FONT>"; if (sizeof($this->Binds) > 0) { $bi = 0; foreach ($this->Binds as $parameter_name => $parameter_values) { if ($parameter_values[2] == OCI_B_CURSOR) { OCIExecute($this->Binds[$parameter_name][0]); $this->Error = OCIError($this->Binds[$parameter_name][0]); $this->Query_ID = $this->Binds[$parameter_name][0]; } $this->Record[$parameter_name] = $parameter_values[0]; $this->Record[$bi++] = $parameter_values[0]; } $this->Binds = array(); } return $this->Query_ID; }
function Prepare($sql) { static $BINDNUM = 0; $stmt = OCIParse($this->_connectionID, $sql); if (!$stmt) { return $sql; } // error in statement, let Execute() handle the error $BINDNUM += 1; if (@OCIStatementType($stmt) == 'BEGIN') { return array($sql, $stmt, 0, $BINDNUM, OCINewCursor($this->_connectionID)); } return array($sql, $stmt, 0, $BINDNUM); }
/** * Execute a prepared query statement helper method. * * @param mixed $result_class string which specifies which result class to use * @param mixed $result_wrap_class string which specifies which class to wrap results in * * @return mixed MDB2_Result or integer (affected rows) on success, * a MDB2 error on failure * @access private */ function _execute($result_class = true, $result_wrap_class = false) { if (null === $this->statement) { return parent::_execute($result_class, $result_wrap_class); } $this->db->last_query = $this->query; $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'pre', 'parameters' => $this->values)); if ($this->db->getOption('disable_query')) { $result = $this->is_manip ? 0 : null; return $result; } $connection = $this->db->getConnection(); if (PEAR::isError($connection)) { return $connection; } $result = MDB2_OK; $lobs = $quoted_values = array(); $i = 0; foreach ($this->positions as $parameter) { if (!array_key_exists($parameter, $this->values)) { return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, 'Unable to bind to missing placeholder: ' . $parameter, __FUNCTION__); } $type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null; if ($type == 'clob' || $type == 'blob') { $lobs[$i]['file'] = false; if (is_resource($this->values[$parameter])) { $fp = $this->values[$parameter]; $this->values[$parameter] = ''; while (!feof($fp)) { $this->values[$parameter] .= fread($fp, 8192); } } elseif (is_a($this->values[$parameter], 'OCI-Lob')) { //do nothing } elseif ($this->db->getOption('lob_allow_url_include') && preg_match('/^(\\w+:\\/\\/)(.*)$/', $this->values[$parameter], $match)) { $lobs[$i]['file'] = true; if ($match[1] == 'file://') { $this->values[$parameter] = $match[2]; } } $lobs[$i]['value'] = $this->values[$parameter]; $lobs[$i]['descriptor'] =& $this->values[$parameter]; // Test to see if descriptor has already been created for this // variable (i.e. if it has been bound more than once): if (!is_a($this->values[$parameter], 'OCI-Lob')) { $this->values[$parameter] = @OCINewDescriptor($connection, OCI_D_LOB); if (false === $this->values[$parameter]) { $result = $this->db->raiseError(null, null, null, 'Unable to create descriptor for LOB in parameter: ' . $parameter, __FUNCTION__); break; } } $lob_type = $type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB; if (!@OCIBindByName($this->statement, ':' . $parameter, $lobs[$i]['descriptor'], -1, $lob_type)) { $result = $this->db->raiseError($this->statement, null, null, 'could not bind LOB parameter', __FUNCTION__); break; } } else { if ($type == OCI_B_BFILE) { // Test to see if descriptor has already been created for this // variable (i.e. if it has been bound more than once): if (!is_a($this->values[$parameter], "OCI-Lob")) { $this->values[$parameter] = @OCINewDescriptor($connection, OCI_D_FILE); if (false === $this->values[$parameter]) { $result = $this->db->raiseError(null, null, null, 'Unable to create descriptor for BFILE in parameter: ' . $parameter, __FUNCTION__); break; } } if (!@OCIBindByName($this->statement, ':' . $parameter, $this->values[$parameter], -1, $type)) { $result = $this->db->raiseError($this->statement, null, null, 'Could not bind BFILE parameter', __FUNCTION__); break; } } else { if ($type == OCI_B_ROWID) { // Test to see if descriptor has already been created for this // variable (i.e. if it has been bound more than once): if (!is_a($this->values[$parameter], "OCI-Lob")) { $this->values[$parameter] = @OCINewDescriptor($connection, OCI_D_ROWID); if (false === $this->values[$parameter]) { $result = $this->db->raiseError(null, null, null, 'Unable to create descriptor for ROWID in parameter: ' . $parameter, __FUNCTION__); break; } } if (!@OCIBindByName($this->statement, ':' . $parameter, $this->values[$parameter], -1, $type)) { $result = $this->db->raiseError($this->statement, null, null, 'Could not bind ROWID parameter', __FUNCTION__); break; } } else { if ($type == OCI_B_CURSOR) { // Test to see if cursor has already been allocated for this // variable (i.e. if it has been bound more than once): if (!is_resource($this->values[$parameter]) || !get_resource_type($this->values[$parameter]) == "oci8 statement") { $this->values[$parameter] = @OCINewCursor($connection); if (false === $this->values[$parameter]) { $result = $this->db->raiseError(null, null, null, 'Unable to allocate cursor for parameter: ' . $parameter, __FUNCTION__); break; } } if (!@OCIBindByName($this->statement, ':' . $parameter, $this->values[$parameter], -1, $type)) { $result = $this->db->raiseError($this->statement, null, null, 'Could not bind CURSOR parameter', __FUNCTION__); break; } } else { $maxlength = array_key_exists($parameter, $this->type_maxlengths) ? $this->type_maxlengths[$parameter] : -1; $this->values[$parameter] = $this->db->quote($this->values[$parameter], $type, false); $quoted_values[$i] =& $this->values[$parameter]; if (PEAR::isError($quoted_values[$i])) { return $quoted_values[$i]; } if (!@OCIBindByName($this->statement, ':' . $parameter, $quoted_values[$i], $maxlength)) { $result = $this->db->raiseError($this->statement, null, null, 'could not bind non-abstract parameter', __FUNCTION__); break; } } } } } ++$i; } $lob_keys = array_keys($lobs); if (!PEAR::isError($result)) { $mode = !empty($lobs) || $this->db->in_transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS; if (!@OCIExecute($this->statement, $mode)) { $err = $this->db->raiseError($this->statement, null, null, 'could not execute statement', __FUNCTION__); return $err; } if (!empty($lobs)) { foreach ($lob_keys as $i) { if (null !== $lobs[$i]['value'] && $lobs[$i]['value'] !== '') { if (is_object($lobs[$i]['value'])) { // Probably a NULL LOB // @see http://bugs.php.net/bug.php?id=27485 continue; } if ($lobs[$i]['file']) { $result = $lobs[$i]['descriptor']->savefile($lobs[$i]['value']); } else { $result = $lobs[$i]['descriptor']->save($lobs[$i]['value']); } if (!$result) { $result = $this->db->raiseError(null, null, null, 'Unable to save descriptor contents', __FUNCTION__); break; } } } if (!PEAR::isError($result)) { if (!$this->db->in_transaction) { if (!@OCICommit($connection)) { $result = $this->db->raiseError(null, null, null, 'Unable to commit transaction', __FUNCTION__); } } else { ++$this->db->uncommitedqueries; } } } } if (PEAR::isError($result)) { return $result; } if ($this->is_manip) { $affected_rows = $this->db->_affectedRows($connection, $this->statement); return $affected_rows; } $result = $this->db->_wrapResult($this->statement, $this->result_types, $result_class, $result_wrap_class, $this->limit, $this->offset); $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result)); return $result; }
function sqlCompile($Query_String) { $db = new clsDBdatabase(); $db->Provider->sqoe = 0; $esto = array(chr(10), chr(9), chr(13)); $porEsto = array("\n", "\t", " "); $parse = ' declare c integer := dbms_sql.open_cursor(); begin dbms_sql.parse(c, :STMT, dbms_sql.native); dbms_sql.close_cursor(c); end; '; $plsql = trim(str_replace($esto, $porEsto, $Query_String)); #echo $plsql ; #$Query_String = 'select a from dual'; $db->bind('STMT', $plsql, 4000, SQLT_CHR); #$db->query('BEGIN '.trim(str_replace($esto, $porEsto, $parse)).' END;'); $db->Query_ID = OCIParse($db->Link_ID, 'BEGIN ' . trim(str_replace($esto, $porEsto, $parse)) . ' END;'); if (!$db->Query_ID) { $db->Error = OCIError($db->Link_ID); echo 'ERROR ' . OCIError($db->Link_ID); } if (sizeof($db->Provider->Binds) > 0) { foreach ($db->Provider->Binds as $parameter_name => $parameter_values) { if ($parameter_values[2] == OCI_B_CURSOR) { $this->db[$parameter_name][0] = OCINewCursor($db->Link_ID); } if ($parameter_values[2] == 0) { OCIBindByName($db->Query_ID, ":" . $parameter_name, $db->Provider->Binds[$parameter_name][0], $parameter_values[1]); } else { OCIBindByName($db->Query_ID, ":" . $parameter_name, $db->Provider->Binds[$parameter_name][0], $parameter_values[1], $parameter_values[2]); } } } @OCIExecute($db->Query_ID); $db->Error = OCIError($db->Query_ID); #var_dump($db->Error); $SQLCODE = $db->Error['code']; $SQLERRMSG = explode('ORA-06512', $db->Error['message']); $SQLERRMSG = $SQLERRMSG[0]; $error = new stdClass(); $error->SQLCODE = !$SQLCODE ? 0 : $SQLCODE; $error->SQLERRMSG = $SQLERRMSG; return $error; }