00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 require_once 'creole/PreparedStatement.php';
00023 require_once 'creole/common/PreparedStatementCommon.php';
00024 require_once 'creole/util/Lob.php';
00025
00033 class ODBCPreparedStatement extends PreparedStatementCommon implements PreparedStatement
00034 {
00039 protected function replaceParams()
00040 {
00041 if ($this->conn->getAdapter()->emulatePrepareStmt())
00042 return parent::replaceParams();
00043 else
00044 return $this->sql;
00045 }
00046
00050 protected function _execute($sql, $params, $fetchmode, $isupdate)
00051 {
00052 if ($this->resultSet)
00053 {
00054 $this->resultSet->close();
00055 $this->resultSet = null;
00056 }
00057
00058 $this->updateCount = null;
00059
00060 if ($this->conn->getAdapter()->emulatePrepareStmt())
00061 {
00062 $stmt = @odbc_exec($this->conn->getResource(), $sql);
00063 $ret = ($stmt !== false);
00064 }
00065 else
00066 {
00067
00068
00069
00070 foreach ($this->boundInVars as $idx => $var)
00071 {
00072 if ($var instanceof Lob)
00073 {
00074 $file = ($isupdate ? $var->getInputFile() : $var->getOutputFile());
00075 $this->boundInVars[$idx] = "'$file'";
00076 }
00077 else if (is_string($var))
00078 {
00079 $this->boundInVars[$idx] = trim($var, "\"\'");
00080 }
00081 }
00082
00083 $stmt = @odbc_prepare($this->conn->getResource(), $sql);
00084
00085 if ($stmt === FALSE)
00086 throw new SQLException('Could not prepare query', $this->conn->nativeError(), $sql);
00087
00088 $ret = @odbc_execute($stmt, $this->boundInVars);
00089 }
00090
00091 if ($ret === FALSE)
00092 {
00093 @odbc_free_result($stmt);
00094 throw new SQLException('Could not execute query', $this->conn->nativeError(), $sql);
00095 }
00096
00097 return $this->conn->createResultSet(new ODBCResultResource($stmt), $fetchmode);
00098 }
00099
00103 public function executeQuery()
00104 {
00105 switch (func_num_args()) {
00106 case 2:
00107 list($params, $fetchmode) = func_get_args();
00108 if (!is_array($params)) {
00109 unset($params);
00110 }
00111 break;
00112 case 1:
00113 $params = null;
00114 list($fetchmode) = func_get_args();
00115 break;
00116 case 0:
00117 $params = null;
00118 $fetchmode = null;
00119 break;
00120 }
00121
00122
00123 if (isset($params)) {
00124 for($i=0,$cnt=count($params); $i < $cnt; $i++) {
00125 $this->set($i+1, $params[$i]);
00126 }
00127 }
00128
00129 $sql = $this->replaceParams();
00130
00131 if ($this->conn->getAdapter()->hasLimitOffset())
00132 {
00133 if ($this->limit > 0 || $this->offset > 0)
00134 $this->conn->applyLimit($sql, $this->offset, $this->limit);
00135 }
00136
00137 $this->resultSet = $this->_execute($sql, $params, $fetchmode, false);
00138
00139 if (!$this->conn->getAdapter()->hasLimitOffset())
00140 {
00141 $this->resultSet->_setOffset($this->offset);
00142 $this->resultSet->_setLimit($this->limit);
00143 }
00144
00145 return $this->resultSet;
00146 }
00147
00151 public function executeUpdate($params = null)
00152 {
00153
00154 if ($params) {
00155 for($i=0,$cnt=count($params); $i < $cnt; $i++) {
00156 $this->set($i+1, $params[$i]);
00157 }
00158 }
00159
00160 $sql = $this->replaceParams();
00161 $this->_execute($sql, $params, 0, true);
00162 $this->updateCount = $this->conn->getUpdateCount();
00163
00164 return $this->updateCount;
00165 }
00166
00170 protected function escape($str)
00171 {
00172 if ($this->conn->getAdapter()->emulatePrepareStmt())
00173 return $this->conn->getAdapter()->escape($str);
00174
00175
00176 return $str;
00177 }
00178
00182 function setNull($paramIndex)
00183 {
00184 $this->sql_cache_valid = false;
00185 $this->boundInVars[$paramIndex] = null;
00186 }
00187
00191 function setBlob($paramIndex, $blob)
00192 {
00193 if ($this->conn->getAdapter()->emulatePrepareStmt())
00194 return parent::setBlob($paramIndex, $blob);
00195
00196 $this->sql_cache_valid = false;
00197 if ($blob === null)
00198 {
00199 $this->setNull($paramIndex);
00200 return;
00201 }
00202
00203 if ($blob instanceof Blob)
00204 {
00205 if ($blob->isFromFile() && !$blob->isModified())
00206 {
00207 $this->boundInVars[$paramIndex] = $blob;
00208 return;
00209 }
00210
00211 $blob = $blob->__toString();
00212 }
00213
00214 $this->boundInVars[$paramIndex] = "'" . $this->escape($blob) . "'";
00215 }
00216
00220 function setClob($paramIndex, $clob)
00221 {
00222 if ($this->conn->getAdapter()->emulatePrepareStmt())
00223 return parent::setClob($paramIndex, $clob);
00224
00225 $this->sql_cache_valid = false;
00226 if ($clob === null)
00227 {
00228 $this->setNull($paramIndex);
00229 return;
00230 }
00231
00232 if ($clob instanceof Clob)
00233 {
00234 if ($clob->isFromFile() && !$clob->isModified())
00235 {
00236 $this->boundInVars[$paramIndex] = $clob;
00237 return;
00238 }
00239
00240 $clob = $clob->__toString();
00241 }
00242
00243 $this->boundInVars[$paramIndex] = "'" . $this->escape($clob) . "'";
00244 }
00245
00246 }