Skip to content

Commit 4a6221e

Browse files
committed
[ENHANCEMENT] Switch pointer type (MSSQL connections) - Better performance throughout application
1 parent 7e8b5cf commit 4a6221e

File tree

3 files changed

+122
-142
lines changed

3 files changed

+122
-142
lines changed

lib/addon/creole/drivers/sfDebugConnection.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -151,37 +151,31 @@ public function close()
151151
}
152152

153153
/**
154-
* @see Connection::executeQuery()
154+
*
155155
*/
156-
public function executeQuery($sql, $fetchmode = null)
156+
public function setPointerType($value)
157157
{
158-
$this->lastExecutedQuery = $sql;
159-
$this->numQueriesExecuted++;
160-
161-
$elapsedTime = 0;
162-
if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
158+
if(method_exists($this->childConnection, 'setPointerType'))
163159
{
164-
$sqlTimer = sfTimerManager::getTimer('Database');
165-
$timer = new sfTimer();
160+
$this->childConnection->setPointerType($value);
166161
}
162+
}
167163

168-
$retval = $this->childConnection->executeQuery($sql, $fetchmode);
169-
170-
if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
164+
/**
165+
*
166+
*/
167+
public function getPointerType()
168+
{
169+
if(method_exists($this->childConnection, 'getPointerType'))
171170
{
172-
$sqlTimer->addTime();
173-
$elapsedTime = $timer->getElapsedTime();
171+
$this->childConnection->getPointerType();
174172
}
175-
176-
$this->log(sprintf("{sfCreole} executeQuery(): [%.2f ms] %s", $elapsedTime * 1000, $sql));
177-
178-
return $retval;
179173
}
180174

181175
/**
182176
* @see Connection::executeQuery()
183177
*/
184-
public function executeQueryBuffered($sql, $fetchmode = null)
178+
public function executeQuery($sql, $fetchmode = null)
185179
{
186180
$this->lastExecutedQuery = $sql;
187181
$this->numQueriesExecuted++;
@@ -193,7 +187,7 @@ public function executeQueryBuffered($sql, $fetchmode = null)
193187
$timer = new sfTimer();
194188
}
195189

196-
$retval = $this->childConnection->executeQueryBuffered($sql, $fetchmode);
190+
$retval = $this->childConnection->executeQuery($sql, $fetchmode);
197191

198192
if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
199193
{
@@ -206,10 +200,9 @@ public function executeQueryBuffered($sql, $fetchmode = null)
206200
return $retval;
207201
}
208202

209-
210203
/**
211-
* @see Connection::executeUpdate()
212-
**/
204+
* @see Connection::executeUpdate()
205+
**/
213206
public function executeUpdate($sql)
214207
{
215208
$this->log("{sfCreole} executeUpdate(): $sql");

lib/vendor/creole/drivers/mssqlsrv/MSSQLSRVConnection.php

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class MSSQLSRVConnection extends ConnectionCommon implements Connection {
3838
/** LastStmt used to count last update SQL **/
3939
private $lastStmt = null;
4040

41+
private $pointer_type = SQLSRV_CURSOR_CLIENT_BUFFERED;
4142
/**
4243
* @see Connection::connect()
4344
*/
@@ -134,7 +135,6 @@ public function applyLimit(&$sql, $offset, $limit)
134135
}
135136
}
136137

137-
138138
/**
139139
* @see Connection::close()
140140
*/
@@ -146,36 +146,30 @@ function close()
146146
}
147147

148148
/**
149-
* @see Connection::executeQuery()
149+
* @return string
150150
*/
151-
function executeQuery($sql, $fetchmode = null)
151+
function getPointerType()
152152
{
153-
$this->lastQuery = $sql;
154-
155-
$result = sqlsrv_query($this->dblink, $sql, null, array("Scrollable" => SQLSRV_CURSOR_STATIC));
156-
if($result === false)
157-
{
158-
throw new SQLException('Could not execute query: ' . $sql, $this->sqlError());
159-
}
160-
161-
// get first results with has fields
162-
$numfields = sqlsrv_num_fields( $result );
163-
while(($numfields == false)&&(sqlsrv_num_fields( $result )))
164-
{
165-
$numfields = sqlsrv_fetch_array( $result );
166-
}
153+
return $this->pointer_type ?: SQLSRV_CURSOR_CLIENT_BUFFERED;
154+
}
167155

168-
return new MSSQLSRVResultSet($this, $result, $fetchmode);
156+
/**
157+
* @param $type
158+
*/
159+
function setPointerType($type)
160+
{
161+
$this->pointer_type = $type;
169162
}
170163

171164
/**
172165
* @see Connection::executeQuery()
173166
*/
174-
function executeQueryBuffered($sql, $fetchmode = null)
167+
function executeQuery($sql, $fetchmode = null)
175168
{
176169
$this->lastQuery = $sql;
177170

178-
$result = sqlsrv_query($this->dblink, $sql, null, array("Scrollable" => SQLSRV_CURSOR_CLIENT_BUFFERED));
171+
//var_dump( $this->getPointerType());
172+
$result = sqlsrv_query($this->dblink, $sql, null, array("Scrollable" => $this->getPointerType()));
179173
if($result === false)
180174
{
181175
throw new SQLException('Could not execute query: ' . $sql, $this->sqlError());
@@ -190,7 +184,7 @@ function executeQueryBuffered($sql, $fetchmode = null)
190184

191185
return new MSSQLSRVResultSet($this, $result, $fetchmode);
192186
}
193-
187+
194188
/**
195189
* @see Connection::executeUpdate()
196190
*/
@@ -199,9 +193,9 @@ function executeUpdate($sql)
199193
$this->lastQuery = $sql;
200194

201195
$stmt = sqlsrv_query( $this->dblink, $sql);
202-
196+
203197
if (!$stmt) {
204-
throw new SQLException('Could not execute update', $this->sqlError(), $sql);
198+
throw new SQLException('Could not execute update', $this->sqlError(), $sql);
205199
}
206200

207201
$rows_affected = sqlsrv_rows_affected( $stmt);
@@ -305,23 +299,23 @@ private function sqlError()
305299
{
306300
return print_r( sqlsrv_errors(), true);
307301
}
308-
302+
309303
/**
310304
* returns the last inserted id
311-
*
305+
*
312306
* @return int
313307
* @throws SQLException
314308
*/
315309
function getLastInsertedId()
316-
{
310+
{
317311
if (
318-
(sqlsrv_next_result($this->lastStmt) !== true) ||
319-
(sqlsrv_fetch($this->lastStmt) !== true) ||
320-
(($lastInsertedId = sqlsrv_get_field($this->lastStmt, 0)) === false)
321-
) {
312+
(sqlsrv_next_result($this->lastStmt) !== true) ||
313+
(sqlsrv_fetch($this->lastStmt) !== true) ||
314+
(($lastInsertedId = sqlsrv_get_field($this->lastStmt, 0)) === false)
315+
) {
322316
throw new SQLException('Unable to retrieve last inserted id', $this->sqlError());
323317
}
324-
318+
325319
return $lastInsertedId;
326320
}
327321
}

lib/vendor/creole/drivers/mssqlsrv/MSSQLSRVPreparedStatement.php

Lines changed: 81 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -31,106 +31,99 @@
3131
*/
3232
class MSSQLSRVPreparedStatement extends PreparedStatementCommon implements PreparedStatement {
3333

34-
/**
35-
* @inheritdoc
36-
*
37-
*/
38-
public function __construct(Connection $conn, $sql)
34+
/**
35+
* @inheritdoc
36+
*
37+
*/
38+
public function __construct(Connection $conn, $sql)
39+
{
40+
if (false !== stripos($sql, 'insert into'))
3941
{
40-
if (false !== stripos($sql, 'insert into'))
41-
{
42-
$sql .= '; SELECT SCOPE_IDENTITY() AS ID';
43-
}
44-
45-
parent::__construct($conn, $sql);
42+
$sql .= '; SELECT SCOPE_IDENTITY() AS ID';
4643
}
4744

48-
/**
49-
* Add quotes using str_replace.
50-
* This is not as thorough as MySQL.
51-
*/
52-
protected function escape($subject)
53-
{
54-
// use this instead of magic_quotes_sybase + addslashes(),
55-
// just in case multiple RDBMS being used at the same time
56-
return str_replace("'", "''", $subject);
57-
}
45+
parent::__construct($conn, $sql);
46+
}
5847

59-
/**
60-
* MSSQL must emulate OFFSET/LIMIT support.
61-
*/
62-
public function executeQuery($p1 = null, $fetchmode = null)
63-
{
64-
$params = null;
65-
if ($fetchmode !== null) {
66-
$params = $p1;
67-
} elseif ($p1 !== null) {
68-
if (is_array($p1)) $params = $p1;
69-
else $fetchmode = $p1;
70-
}
48+
/**
49+
* Add quotes using str_replace.
50+
* This is not as thorough as MySQL.
51+
*/
52+
protected function escape($subject)
53+
{
54+
// use this instead of magic_quotes_sybase + addslashes(),
55+
// just in case multiple RDBMS being used at the same time
56+
return str_replace("'", "''", $subject);
57+
}
7158

72-
if ($params) {
73-
for($i=0,$cnt=count($params); $i < $cnt; $i++) {
74-
$this->set($i+1, $params[$i]);
75-
}
76-
}
59+
/**
60+
* MSSQL must emulate OFFSET/LIMIT support.
61+
*/
62+
public function executeQuery($p1 = null, $fetchmode = null)
63+
{
64+
$params = null;
65+
if ($fetchmode !== null) {
66+
$params = $p1;
67+
} elseif ($p1 !== null) {
68+
if (is_array($p1)) $params = $p1;
69+
else $fetchmode = $p1;
70+
}
7771

78-
$this->updateCount = null; // reset
79-
$sql = $this->replaceParams();
72+
if ($params) {
73+
for($i=0,$cnt=count($params); $i < $cnt; $i++) {
74+
$this->set($i+1, $params[$i]);
75+
}
76+
}
8077

81-
if(stripos($sql, 'ORDER BY'))
82-
$offsetBefore = true;
83-
else
84-
$offsetBefore = false;
78+
$this->updateCount = null; // reset
79+
$sql = $this->replaceParams();
8580

86-
if($offsetBefore)
87-
{
88-
if ($this->limit > 0 || $this->offset > 0) {
89-
$this->conn->applyLimit($sql, $this->offset, $this->limit);
90-
}
91-
}
81+
if(stripos($sql, 'ORDER BY'))
82+
$offsetBefore = true;
83+
else
84+
$offsetBefore = false;
9285

93-
if($this->limit)
94-
{
95-
$this->resultSet = $this->conn->executeQueryBuffered($sql, $fetchmode);
96-
}
97-
else
98-
{
99-
$this->resultSet = $this->conn->executeQuery($sql, $fetchmode);
100-
}
86+
if($offsetBefore)
87+
{
88+
if ($this->limit > 0 || $this->offset > 0) {
89+
$this->conn->applyLimit($sql, $this->offset, $this->limit);
90+
}
91+
}
10192

102-
if(!$offsetBefore)
103-
{
104-
$this->resultSet->_setOffset($this->offset);
105-
$this->resultSet->_setLimit($this->limit);
106-
}
93+
$this->resultSet = $this->conn->executeQuery($sql, $fetchmode);
10794

108-
return $this->resultSet;
95+
if(!$offsetBefore)
96+
{
97+
$this->resultSet->_setOffset($this->offset);
98+
$this->resultSet->_setLimit($this->limit);
10999
}
110100

111-
/**
112-
* MSSQL-specific implementation of setBlob().
113-
*
114-
* If you are having trouble getting BLOB data into the database, see the phpdoc comment
115-
* in the MSSQLConnection for some PHP ini values that may need to be set. (This also
116-
* applies to CLOB support.)
117-
*
118-
* @param int $paramIndex
119-
* @param mixed $value Blob object or string.
120-
* @return void
121-
*/
122-
function setBlob($paramIndex, $blob)
123-
{
124-
$this->sql_cache_valid = false;
125-
if ($blob === null) {
126-
$this->setNull($paramIndex);
127-
} else {
128-
// they took magic __toString() out of PHP5.0.0; this sucks
129-
if (is_object($blob)) {
130-
$blob = $blob->__toString();
131-
}
132-
$data = unpack("H*hex", $blob);
133-
$this->boundInVars[$paramIndex] = '0x'.$data['hex']; // no surrounding quotes!
134-
}
101+
return $this->resultSet;
102+
}
103+
104+
/**
105+
* MSSQL-specific implementation of setBlob().
106+
*
107+
* If you are having trouble getting BLOB data into the database, see the phpdoc comment
108+
* in the MSSQLConnection for some PHP ini values that may need to be set. (This also
109+
* applies to CLOB support.)
110+
*
111+
* @param int $paramIndex
112+
* @param mixed $value Blob object or string.
113+
* @return void
114+
*/
115+
function setBlob($paramIndex, $blob)
116+
{
117+
$this->sql_cache_valid = false;
118+
if ($blob === null) {
119+
$this->setNull($paramIndex);
120+
} else {
121+
// they took magic __toString() out of PHP5.0.0; this sucks
122+
if (is_object($blob)) {
123+
$blob = $blob->__toString();
124+
}
125+
$data = unpack("H*hex", $blob);
126+
$this->boundInVars[$paramIndex] = '0x'.$data['hex']; // no surrounding quotes!
135127
}
128+
}
136129
}

0 commit comments

Comments
 (0)