MDL-39024 fix MSSQL seek past the end of recordset
[moodle.git] / lib / dml / mssql_native_moodle_database.php
CommitLineData
520b730c 1<?php
520b730c 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
520b730c 17/**
18 * Native mssql class representing moodle database interface.
19 *
00902cd9 20 * @package core_dml
520b730c 21 * @copyright 2009 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
f2ed3f05
PS
25defined('MOODLE_INTERNAL') || die();
26
00902cd9
PS
27require_once(__DIR__.'/moodle_database.php');
28require_once(__DIR__.'/mssql_native_moodle_recordset.php');
29require_once(__DIR__.'/mssql_native_moodle_temptables.php');
520b730c 30
31/**
32 * Native mssql class representing moodle database interface.
a0eb2e97 33 *
00902cd9 34 * @package core_dml
a0eb2e97
AB
35 * @copyright 2009 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
520b730c 37 */
38class mssql_native_moodle_database extends moodle_database {
39
40 protected $mssql = null;
5c08b24b 41 protected $last_error_reporting; // To handle mssql driver default verbosity
6055f89d 42 protected $collation; // current DB collation cache
5c08b24b 43
520b730c 44 /**
45 * Detects if all needed PHP stuff installed.
46 * Note: can be used before connect()
47 * @return mixed true if ok, string if something
48 */
49 public function driver_installed() {
50 if (!function_exists('mssql_connect')) {
51 return get_string('mssqlextensionisnotpresentinphp', 'install');
52 }
53 return true;
54 }
55
56 /**
57 * Returns database family type - describes SQL dialect
58 * Note: can be used before connect()
59 * @return string db family name (mysql, postgres, mssql, oracle, etc.)
60 */
61 public function get_dbfamily() {
62 return 'mssql';
63 }
64
65 /**
66 * Returns more specific database driver type
67 * Note: can be used before connect()
4a6c2ab8 68 * @return string db type mysqli, pgsql, oci, mssql, sqlsrv
520b730c 69 */
70 protected function get_dbtype() {
71 return 'mssql';
72 }
73
74 /**
75 * Returns general database library name
76 * Note: can be used before connect()
eb123deb 77 * @return string db type pdo, native
520b730c 78 */
79 protected function get_dblibrary() {
80 return 'native';
81 }
82
83 /**
84 * Returns localised database type name
85 * Note: can be used before connect()
86 * @return string
87 */
88 public function get_name() {
89 return get_string('nativemssql', 'install');
90 }
91
520b730c 92 /**
93 * Returns localised database configuration help.
94 * Note: can be used before connect()
95 * @return string
96 */
97 public function get_configuration_help() {
98 return get_string('nativemssqlhelp', 'install');
99 }
100
101 /**
102 * Returns localised database description
103 * Note: can be used before connect()
104 * @return string
105 */
106 public function get_configuration_hints() {
107 $str = get_string('databasesettingssub_mssql', 'install');
108 $str .= "<p style='text-align:right'><a href=\"javascript:void(0)\" ";
109 $str .= "onclick=\"return window.open('http://docs.moodle.org/en/Installing_MSSQL_for_PHP')\"";
110 $str .= ">";
111 $str .= '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
112 $str .= get_string('moodledocslink', 'install') . '</a></p>';
113 return $str;
114 }
115
116 /**
117 * Connect to db
118 * Must be called before other methods.
6df26010
AB
119 * @param string $dbhost The database host.
120 * @param string $dbuser The database username.
121 * @param string $dbpass The database username's password.
122 * @param string $dbname The name of the database being connected to.
520b730c 123 * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
124 * @param array $dboptions driver specific options
125 * @return bool true
126 * @throws dml_connection_exception if error
127 */
128 public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null) {
129 if ($prefix == '' and !$this->external) {
130 //Enforce prefixes for everybody but mysql
131 throw new dml_exception('prefixcannotbeempty', $this->get_dbfamily());
132 }
133
134 $driverstatus = $this->driver_installed();
135
136 if ($driverstatus !== true) {
137 throw new dml_exception('dbdriverproblem', $driverstatus);
138 }
139
140 $this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
141
000c881e
AB
142 $dbhost = $this->dbhost;
143 if (isset($dboptions['dbport'])) {
144 if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
145 $dbhost .= ','.$dboptions['dbport'];
146 } else {
147 $dbhost .= ':'.$dboptions['dbport'];
148 }
149 }
520b730c 150 ob_start();
a0b7200d 151 if (!empty($this->dboptions['dbpersist'])) { // persistent connection
000c881e 152 $this->mssql = mssql_pconnect($dbhost, $this->dbuser, $this->dbpass, true);
520b730c 153 } else {
000c881e 154 $this->mssql = mssql_connect($dbhost, $this->dbuser, $this->dbpass, true);
520b730c 155 }
156 $dberr = ob_get_contents();
157 ob_end_clean();
158
159 if ($this->mssql === false) {
160 $this->mssql = null;
161 throw new dml_connection_exception($dberr);
162 }
163
164 // already connected, select database and set some env. variables
165 $this->query_start("--mssql_select_db", null, SQL_QUERY_AUX);
166 $result = mssql_select_db($this->dbname, $this->mssql);
167 $this->query_end($result);
168
169 // No need to set charset. It's UTF8, with transparent conversions
170 // back and forth performed both by FreeTDS or ODBTP
171
172 // Allow quoted identifiers
173 $sql = "SET QUOTED_IDENTIFIER ON";
174 $this->query_start($sql, null, SQL_QUERY_AUX);
175 $result = mssql_query($sql, $this->mssql);
176 $this->query_end($result);
177
178 $this->free_result($result);
179
180 // Force ANSI nulls so the NULL check was done by IS NULL and NOT IS NULL
4d276e34 181 // instead of equal(=) and distinct(<>) symbols
520b730c 182 $sql = "SET ANSI_NULLS ON";
183 $this->query_start($sql, null, SQL_QUERY_AUX);
184 $result = mssql_query($sql, $this->mssql);
185 $this->query_end($result);
186
187 $this->free_result($result);
188
189 // Force ANSI warnings so arithmetic/string overflows will be
190 // returning error instead of transparently truncating data
191 $sql = "SET ANSI_WARNINGS ON";
192 $this->query_start($sql, null, SQL_QUERY_AUX);
193 $result = mssql_query($sql, $this->mssql);
194 $this->query_end($result);
195
196 // Concatenating null with anything MUST return NULL
197 $sql = "SET CONCAT_NULL_YIELDS_NULL ON";
198 $this->query_start($sql, null, SQL_QUERY_AUX);
199 $result = mssql_query($sql, $this->mssql);
200 $this->query_end($result);
201
202 $this->free_result($result);
203
204 // Set transactions isolation level to READ_COMMITTED
205 // prevents dirty reads when using transactions +
206 // is the default isolation level of MSSQL
12ffe203 207 // Requires database to run with READ_COMMITTED_SNAPSHOT ON
520b730c 208 $sql = "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";
209 $this->query_start($sql, NULL, SQL_QUERY_AUX);
210 $result = mssql_query($sql, $this->mssql);
211 $this->query_end($result);
212
213 $this->free_result($result);
214
4d276e34 215 // Connection stabilised and configured, going to instantiate the temptables controller
520b730c 216 $this->temptables = new mssql_native_moodle_temptables($this);
217
218 return true;
219 }
220
221 /**
222 * Close database connection and release all resources
223 * and memory (especially circular memory references).
224 * Do NOT use connect() again, create a new instance if needed.
225 */
226 public function dispose() {
4d276e34 227 parent::dispose(); // Call parent dispose to write/close session and other common stuff before closing connection
520b730c 228 if ($this->mssql) {
229 mssql_close($this->mssql);
230 $this->mssql = null;
231 }
232 }
233
234 /**
235 * Called before each db query.
236 * @param string $sql
237 * @param array array of parameters
238 * @param int $type type of query
239 * @param mixed $extrainfo driver specific extra information
240 * @return void
241 */
242 protected function query_start($sql, array $params=null, $type, $extrainfo=null) {
243 parent::query_start($sql, $params, $type, $extrainfo);
5c08b24b 244 // mssql driver tends to send debug to output, we do not need that ;-)
245 $this->last_error_reporting = error_reporting(0);
520b730c 246 }
247
248 /**
249 * Called immediately after each db query.
250 * @param mixed db specific result
251 * @return void
252 */
253 protected function query_end($result) {
5c08b24b 254 // reset original debug level
255 error_reporting($this->last_error_reporting);
520b730c 256 parent::query_end($result);
257 }
258
259 /**
260 * Returns database server info array
6df26010 261 * @return array Array containing 'description' and 'version' info
520b730c 262 */
263 public function get_server_info() {
264 static $info;
265 if (!$info) {
266 $info = array();
267 $sql = 'sp_server_info 2';
268 $this->query_start($sql, null, SQL_QUERY_AUX);
269 $result = mssql_query($sql, $this->mssql);
270 $this->query_end($result);
271 $row = mssql_fetch_row($result);
b809f813 272 $info['description'] = $row[2];
520b730c 273 $this->free_result($result);
274
275 $sql = 'sp_server_info 500';
276 $this->query_start($sql, null, SQL_QUERY_AUX);
277 $result = mssql_query($sql, $this->mssql);
278 $this->query_end($result);
279 $row = mssql_fetch_row($result);
280 $info['version'] = $row[2];
281 $this->free_result($result);
282 }
283 return $info;
284 }
285
520b730c 286 /**
287 * Converts short table name {tablename} to real table name
288 * supporting temp tables (#) if detected
289 *
290 * @param string sql
291 * @return string sql
292 */
293 protected function fix_table_names($sql) {
294 if (preg_match_all('/\{([a-z][a-z0-9_]*)\}/', $sql, $matches)) {
295 foreach($matches[0] as $key=>$match) {
296 $name = $matches[1][$key];
4b1ee2b3 297 if ($this->temptables->is_temptable($name)) {
298 $sql = str_replace($match, $this->temptables->get_correct_name($name), $sql);
299 } else {
300 $sql = str_replace($match, $this->prefix.$name, $sql);
301 }
520b730c 302 }
303 }
304 return $sql;
305 }
306
307 /**
308 * Returns supported query parameter types
6df26010 309 * @return int bitmask of accepted SQL_PARAMS_*
520b730c 310 */
311 protected function allowed_param_types() {
312 return SQL_PARAMS_QM; // Not really, but emulated, see emulate_bound_params()
313 }
314
315 /**
316 * Returns last error reported by database engine.
9331d879 317 * @return string error message
520b730c 318 */
319 public function get_last_error() {
320 return mssql_get_last_message();
321 }
322
323 /**
324 * Return tables in database WITHOUT current prefix
6df26010 325 * @param bool $usecache if true, returns list of cached tables.
520b730c 326 * @return array of table names in lowercase and without prefix
327 */
328 public function get_tables($usecache=true) {
329 if ($usecache and $this->tables !== null) {
330 return $this->tables;
331 }
332 $this->tables = array();
333 $sql = "SELECT table_name
609541df 334 FROM INFORMATION_SCHEMA.TABLES
520b730c 335 WHERE table_name LIKE '$this->prefix%'
336 AND table_type = 'BASE TABLE'";
337 $this->query_start($sql, null, SQL_QUERY_AUX);
338 $result = mssql_query($sql, $this->mssql);
339 $this->query_end($result);
340
341 if ($result) {
342 while ($row = mssql_fetch_row($result)) {
343 $tablename = reset($row);
62cd8cd9
PS
344 if ($this->prefix !== '') {
345 if (strpos($tablename, $this->prefix) !== 0) {
346 continue;
347 }
348 $tablename = substr($tablename, strlen($this->prefix));
520b730c 349 }
520b730c 350 $this->tables[$tablename] = $tablename;
351 }
352 $this->free_result($result);
353 }
354
355 // Add the currently available temptables
356 $this->tables = array_merge($this->tables, $this->temptables->get_temptables());
357 return $this->tables;
358 }
359
360 /**
6df26010
AB
361 * Return table indexes - everything lowercased.
362 * @param string $table The table we want to get indexes from.
363 * @return array An associative array of indexes containing 'unique' flag and 'columns' being indexed
520b730c 364 */
365 public function get_indexes($table) {
366 $indexes = array();
367 $tablename = $this->prefix.$table;
368
369 // Indexes aren't covered by information_schema metatables, so we need to
370 // go to sys ones. Skipping primary key indexes on purpose.
371 $sql = "SELECT i.name AS index_name, i.is_unique, ic.index_column_id, c.name AS column_name
372 FROM sys.indexes i
373 JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
374 JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
375 JOIN sys.tables t ON i.object_id = t.object_id
376 WHERE t.name = '$tablename'
377 AND i.is_primary_key = 0
378 ORDER BY i.name, i.index_id, ic.index_column_id";
379
380 $this->query_start($sql, null, SQL_QUERY_AUX);
381 $result = mssql_query($sql, $this->mssql);
382 $this->query_end($result);
383
384 if ($result) {
385 $lastindex = '';
386 $unique = false;
387 $columns = array();
388 while ($row = mssql_fetch_assoc($result)) {
389 if ($lastindex and $lastindex != $row['index_name']) { // Save lastindex to $indexes and reset info
390 $indexes[$lastindex] = array('unique' => $unique, 'columns' => $columns);
391 $unique = false;
392 $columns = array();
393 }
394 $lastindex = $row['index_name'];
395 $unique = empty($row['is_unique']) ? false : true;
396 $columns[] = $row['column_name'];
397 }
398 if ($lastindex ) { // Add the last one if exists
399 $indexes[$lastindex] = array('unique' => $unique, 'columns' => $columns);
400 }
401 $this->free_result($result);
402 }
403 return $indexes;
404 }
405
406 /**
407 * Returns datailed information about columns in table. This information is cached internally.
408 * @param string $table name
409 * @param bool $usecache
410 * @return array array of database_column_info objects indexed with column names
411 */
412 public function get_columns($table, $usecache=true) {
413 if ($usecache and isset($this->columns[$table])) {
414 return $this->columns[$table];
415 }
416
417 $this->columns[$table] = array();
418
520b730c 419 if (!$this->temptables->is_temptable($table)) { // normal table, get metadata from own schema
420 $sql = "SELECT column_name AS name,
421 data_type AS type,
422 numeric_precision AS max_length,
423 character_maximum_length AS char_max_length,
424 numeric_scale AS scale,
425 is_nullable AS is_nullable,
426 columnproperty(object_id(quotename(table_schema) + '.' +
427 quotename(table_name)), column_name, 'IsIdentity') AS auto_increment,
428 column_default AS default_value
609541df 429 FROM INFORMATION_SCHEMA.COLUMNS
896a2a91 430 WHERE table_name = '{" . $table . "}'
520b730c 431 ORDER BY ordinal_position";
432 } else { // temp table, get metadata from tempdb schema
433 $sql = "SELECT column_name AS name,
434 data_type AS type,
435 numeric_precision AS max_length,
436 character_maximum_length AS char_max_length,
437 numeric_scale AS scale,
438 is_nullable AS is_nullable,
439 columnproperty(object_id(quotename(table_schema) + '.' +
440 quotename(table_name)), column_name, 'IsIdentity') AS auto_increment,
441 column_default AS default_value
609541df 442 FROM tempdb.INFORMATION_SCHEMA.COLUMNS
3c41bba0 443 JOIN tempdb..sysobjects ON name = table_name
444 WHERE id = object_id('tempdb..{" . $table . "}')
520b730c 445 ORDER BY ordinal_position";
446 }
447
896a2a91 448 list($sql, $params, $type) = $this->fix_sql_params($sql, null);
449
520b730c 450 $this->query_start($sql, null, SQL_QUERY_AUX);
451 $result = mssql_query($sql, $this->mssql);
452 $this->query_end($result);
453
454 if (!$result) {
455 return array();
456 }
457
458 while ($rawcolumn = mssql_fetch_assoc($result)) {
459
460 $rawcolumn = (object)$rawcolumn;
461
ac6f1a82 462 $info = new stdClass();
520b730c 463 $info->name = $rawcolumn->name;
464 $info->type = $rawcolumn->type;
465 $info->meta_type = $this->mssqltype2moodletype($info->type);
466
467 // Prepare auto_increment info
468 $info->auto_increment = $rawcolumn->auto_increment ? true : false;
469
470 // Define type for auto_increment columns
471 $info->meta_type = ($info->auto_increment && $info->meta_type == 'I') ? 'R' : $info->meta_type;
472
473 // id columns being auto_incremnt are PK by definition
474 $info->primary_key = ($info->name == 'id' && $info->meta_type == 'R' && $info->auto_increment);
475
476 // Put correct length for character and LOB types
477 $info->max_length = $info->meta_type == 'C' ? $rawcolumn->char_max_length : $rawcolumn->max_length;
478 $info->max_length = ($info->meta_type == 'X' || $info->meta_type == 'B') ? -1 : $info->max_length;
479
480 // Scale
481 $info->scale = $rawcolumn->scale ? $rawcolumn->scale : false;
482
483 // Prepare not_null info
484 $info->not_null = $rawcolumn->is_nullable == 'NO' ? true : false;
485
486 // Process defaults
487 $info->has_default = !empty($rawcolumn->default_value);
559d79d2
PS
488 if ($rawcolumn->default_value === NULL) {
489 $info->default_value = NULL;
490 } else {
491 $info->default_value = preg_replace("/^[\(N]+[']?(.*?)[']?[\)]+$/", '\\1', $rawcolumn->default_value);
492 }
520b730c 493
494 // Process binary
495 $info->binary = $info->meta_type == 'B' ? true : false;
496
497 $this->columns[$table][$info->name] = new database_column_info($info);
498 }
499 $this->free_result($result);
500
501 return $this->columns[$table];
502 }
503
504 /**
6df26010 505 * Normalise values based on varying RDBMS's dependencies (booleans, LOBs...)
520b730c 506 *
507 * @param database_column_info $column column metadata corresponding with the value we are going to normalise
508 * @param mixed $value value we are going to normalise
509 * @return mixed the normalised value
510 */
e3acc8af 511 protected function normalise_value($column, $value) {
e618cdf3
PS
512 $this->detect_objects($value);
513
00902cd9 514 if (is_bool($value)) { // Always, convert boolean to int
520b730c 515 $value = (int)$value;
8a89be3c 516 } // And continue processing because text columns with numeric info need special handling below
520b730c 517
8a89be3c 518 if ($column->meta_type == 'B') { // BLOBs need to be properly "packed", but can be inserted directly if so.
520b730c 519 if (!is_null($value)) { // If value not null, unpack it to unquoted hexadecimal byte-string format
520 $value = unpack('H*hex', $value); // we leave it as array, so emulate_bound_params() can detect it
521 } // easily and "bind" the param ok.
522
523 } else if ($column->meta_type == 'X') { // MSSQL doesn't cast from int to text, so if text column
524 if (is_numeric($value)) { // and is numeric value then cast to string
525 $value = array('numstr' => (string)$value); // and put into array, so emulate_bound_params() will know how
526 } // to "bind" the param ok, avoiding reverse conversion to number
527
528 } else if ($value === '') {
529 if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') {
530 $value = 0; // prevent '' problems in numeric fields
531 }
532 }
533 return $value;
534 }
535
536 /**
537 * Selectively call mssql_free_result(), avoiding some warnings without using the horrible @
538 *
539 * @param mssql_resource $resource resource to be freed if possible
540 */
541 private function free_result($resource) {
542 if (!is_bool($resource)) { // true/false resources cannot be freed
543 mssql_free_result($resource);
544 }
545 }
546
547 /**
548 * Provides mapping between mssql native data types and moodle_database - database_column_info - ones)
549 *
550 * @param string $mssql_type native mssql data type
551 * @return string 1-char database_column_info data type
552 */
553 private function mssqltype2moodletype($mssql_type) {
554 $type = null;
555 switch (strtoupper($mssql_type)) {
556 case 'BIT':
557 $type = 'L';
558 break;
559 case 'INT':
560 case 'SMALLINT':
561 case 'INTEGER':
562 case 'BIGINT':
563 $type = 'I';
564 break;
565 case 'DECIMAL':
566 case 'REAL':
567 case 'FLOAT':
568 $type = 'N';
569 break;
570 case 'VARCHAR':
571 case 'NVARCHAR':
572 $type = 'C';
573 break;
574 case 'TEXT':
575 case 'NTEXT':
576 case 'VARCHAR(MAX)':
577 case 'NVARCHAR(MAX)':
578 $type = 'X';
579 break;
580 case 'IMAGE':
581 case 'VARBINARY(MAX)':
582 $type = 'B';
583 break;
584 case 'DATETIME':
585 $type = 'D';
586 break;
587 }
588 if (!$type) {
589 throw new dml_exception('invalidmssqlnativetype', $mssql_type);
590 }
591 return $type;
592 }
593
594 /**
595 * Do NOT use in code, to be used by database_manager only!
596 * @param string $sql query
597 * @return bool true
6df26010 598 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 599 */
600 public function change_database_structure($sql) {
601 $this->reset_caches();
602
603 $this->query_start($sql, null, SQL_QUERY_STRUCTURE);
5c08b24b 604 $result = mssql_query($sql, $this->mssql);
520b730c 605 $this->query_end($result);
606
607 return true;
608 }
609
610 /**
611 * Very ugly hack which emulates bound parameters in queries
612 * because the mssql driver doesn't support placeholders natively at all
613 */
614 protected function emulate_bound_params($sql, array $params=null) {
615 if (empty($params)) {
616 return $sql;
617 }
00902cd9 618 // ok, we have verified sql statement with ? and correct number of params
148c65bf
PS
619 $parts = explode('?', $sql);
620 $return = array_shift($parts);
520b730c 621 foreach ($params as $param) {
622 if (is_bool($param)) {
623 $return .= (int)$param;
624
625 } else if (is_array($param) && isset($param['hex'])) { // detect hex binary, bind it specially
626 $return .= '0x' . $param['hex'];
627
628 } else if (is_array($param) && isset($param['numstr'])) { // detect numerical strings that *must not*
629 $return .= "N'{$param['numstr']}'"; // be converted back to number params, but bound as strings
630
631 } else if (is_null($param)) {
632 $return .= 'NULL';
633
634 } else if (is_number($param)) { // we can not use is_numeric() because it eats leading zeros from strings like 0045646
4f8b9632 635 $return .= "'".$param."'"; //fix for MDL-24863 to prevent auto-cast to int.
520b730c 636
637 } else if (is_float($param)) {
638 $return .= $param;
639
640 } else {
641 $param = str_replace("'", "''", $param);
642 $return .= "N'$param'";
643 }
644
148c65bf 645 $return .= array_shift($parts);
520b730c 646 }
647 return $return;
648 }
649
650 /**
651 * Execute general sql query. Should be used only when no other method suitable.
a681b6c0 652 * Do NOT use this to make changes in db structure, use database_manager methods instead!
520b730c 653 * @param string $sql query
654 * @param array $params query parameters
655 * @return bool true
6df26010 656 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 657 */
658 public function execute($sql, array $params=null) {
659
660 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
661 $rawsql = $this->emulate_bound_params($sql, $params);
662
663 if (strpos($sql, ';') !== false) {
664 throw new coding_exception('moodle_database::execute() Multiple sql statements found or bound parameters not used properly in query!');
665 }
666
667 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
5c08b24b 668 $result = mssql_query($rawsql, $this->mssql);
520b730c 669 $this->query_end($result);
670 $this->free_result($result);
671
672 return true;
673 }
674
675 /**
676 * Get a number of records as a moodle_recordset using a SQL statement.
677 *
678 * Since this method is a little less readable, use of it should be restricted to
679 * code where it's possible there might be large datasets being returned. For known
680 * small datasets use get_records_sql - it leads to simpler code.
681 *
dafa20e8
AB
682 * The return type is like:
683 * @see function get_recordset.
520b730c 684 *
685 * @param string $sql the SQL select query to execute.
686 * @param array $params array of sql parameters
687 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
688 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
5212b07f 689 * @return moodle_recordset instance
6df26010 690 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 691 */
692 public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
693 $limitfrom = (int)$limitfrom;
694 $limitnum = (int)$limitnum;
695 $limitfrom = ($limitfrom < 0) ? 0 : $limitfrom;
696 $limitnum = ($limitnum < 0) ? 0 : $limitnum;
697 if ($limitfrom or $limitnum) {
4d276e34 698 if ($limitnum >= 1) { // Only apply TOP clause if we have any limitnum (limitfrom offset is handled later)
72a3902f 699 $fetch = $limitfrom + $limitnum;
f80de2f2
EL
700 if (PHP_INT_MAX - $limitnum < $limitfrom) { // Check PHP_INT_MAX overflow
701 $fetch = PHP_INT_MAX;
702 }
6bfe8129
EL
703 $sql = preg_replace('/^([\s(])*SELECT([\s]+(DISTINCT|ALL))?(?!\s*TOP\s*\()/i',
704 "\\1SELECT\\2 TOP $fetch", $sql);
520b730c 705 }
520b730c 706 }
707
708 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
709 $rawsql = $this->emulate_bound_params($sql, $params);
710
711 $this->query_start($sql, $params, SQL_QUERY_SELECT);
5c08b24b 712 $result = mssql_query($rawsql, $this->mssql);
520b730c 713 $this->query_end($result);
714
72a3902f 715 if ($limitfrom) { // Skip $limitfrom records
4c974877
PS
716 if (!@mssql_data_seek($result, $limitfrom)) {
717 // Nothing, most probably seek past the end.
718 mssql_free_result($result);
719 $result = null;
720 }
520b730c 721 }
722
723 return $this->create_recordset($result);
724 }
725
726 protected function create_recordset($result) {
727 return new mssql_native_moodle_recordset($result);
728 }
729
730 /**
731 * Get a number of records as an array of objects using a SQL statement.
732 *
dafa20e8
AB
733 * Return value is like:
734 * @see function get_records.
520b730c 735 *
736 * @param string $sql the SQL select query to execute. The first column of this SELECT statement
737 * must be a unique value (usually the 'id' field), as it will be used as the key of the
738 * returned array.
739 * @param array $params array of sql parameters
740 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
741 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
3503dcad 742 * @return array of objects, or empty array if no records were found
6df26010 743 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 744 */
745 public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
746
747 $rs = $this->get_recordset_sql($sql, $params, $limitfrom, $limitnum);
748
749 $results = array();
750
751 foreach ($rs as $row) {
df5f9165 752 $id = reset($row);
753 if (isset($results[$id])) {
520b730c 754 $colname = key($row);
755 debugging("Did you remember to make the first column something unique in your call to get_records? Duplicate value '$id' found in column '$colname'.", DEBUG_DEVELOPER);
756 }
df5f9165 757 $results[$id] = $row;
520b730c 758 }
759 $rs->close();
760
761 return $results;
762 }
763
764 /**
765 * Selects records and return values (first field) as an array using a SQL statement.
766 *
767 * @param string $sql The SQL query
768 * @param array $params array of sql parameters
3503dcad 769 * @return array of values
6df26010 770 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 771 */
772 public function get_fieldset_sql($sql, array $params=null) {
773
774 $rs = $this->get_recordset_sql($sql, $params);
775
776 $results = array();
777
778 foreach ($rs as $row) {
779 $results[] = reset($row);
780 }
781 $rs->close();
782
783 return $results;
784 }
785
786 /**
787 * Insert new record into database, as fast as possible, no safety checks, lobs not supported.
788 * @param string $table name
789 * @param mixed $params data record as object or array
790 * @param bool $returnit return it of inserted record
791 * @param bool $bulk true means repeated inserts expected
792 * @param bool $customsequence true if 'id' included in $params, disables $returnid
3503dcad 793 * @return bool|int true or new id
6df26010 794 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 795 */
796 public function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false) {
797 if (!is_array($params)) {
798 $params = (array)$params;
799 }
800
801 $returning = "";
7f3f024d 802 $isidentity = false;
520b730c 803
804 if ($customsequence) {
805 if (!isset($params['id'])) {
806 throw new coding_exception('moodle_database::insert_record_raw() id field must be specified if custom sequences used.');
807 }
808 $returnid = false;
97dbac93 809
7f3f024d
EL
810 $columns = $this->get_columns($table);
811 if (isset($columns['id']) and $columns['id']->auto_increment) {
812 $isidentity = true;
813 }
814
815 // Disable IDENTITY column before inserting record with id, only if the
816 // column is identity, from meta information.
817 if ($isidentity) {
818 $sql = 'SET IDENTITY_INSERT {' . $table . '} ON'; // Yes, it' ON!!
819 list($sql, $xparams, $xtype) = $this->fix_sql_params($sql, null);
820 $this->query_start($sql, null, SQL_QUERY_AUX);
821 $result = mssql_query($sql, $this->mssql);
822 $this->query_end($result);
823 $this->free_result($result);
824 }
97dbac93 825
520b730c 826 } else {
827 unset($params['id']);
828 if ($returnid) {
a3fa9932 829 $returning = "OUTPUT inserted.id";
520b730c 830 }
831 }
832
833 if (empty($params)) {
834 throw new coding_exception('moodle_database::insert_record_raw() no fields found.');
835 }
836
837 $fields = implode(',', array_keys($params));
838 $qms = array_fill(0, count($params), '?');
839 $qms = implode(',', $qms);
840
a3fa9932 841 $sql = "INSERT INTO {" . $table . "} ($fields) $returning VALUES ($qms)";
520b730c 842
843 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
844 $rawsql = $this->emulate_bound_params($sql, $params);
845
846 $this->query_start($sql, $params, SQL_QUERY_INSERT);
5c08b24b 847 $result = mssql_query($rawsql, $this->mssql);
a3fa9932
EL
848 // Expected results are:
849 // - true: insert ok and there isn't returned information.
850 // - false: insert failed and there isn't returned information.
851 // - resource: insert executed, need to look for returned (output)
852 // values to know if the insert was ok or no. Posible values
853 // are false = failed, integer = insert ok, id returned.
854 $end = false;
855 if (is_bool($result)) {
856 $end = $result;
857 } else if (is_resource($result)) {
858 $end = mssql_result($result, 0, 0); // Fetch 1st column from 1st row.
859 }
860 $this->query_end($end); // End the query with the calculated $end.
520b730c 861
862 if ($returning !== "") {
a3fa9932 863 $params['id'] = $end;
520b730c 864 }
865 $this->free_result($result);
866
97dbac93 867 if ($customsequence) {
7f3f024d
EL
868 // Enable IDENTITY column after inserting record with id, only if the
869 // column is identity, from meta information.
870 if ($isidentity) {
871 $sql = 'SET IDENTITY_INSERT {' . $table . '} OFF'; // Yes, it' OFF!!
872 list($sql, $xparams, $xtype) = $this->fix_sql_params($sql, null);
873 $this->query_start($sql, null, SQL_QUERY_AUX);
874 $result = mssql_query($sql, $this->mssql);
875 $this->query_end($result);
876 $this->free_result($result);
877 }
97dbac93
PS
878 }
879
520b730c 880 if (!$returnid) {
881 return true;
882 }
883
884 return (int)$params['id'];
885 }
886
887 /**
888 * Insert a record into a table and return the "id" field if required.
889 *
890 * Some conversions and safety checks are carried out. Lobs are supported.
891 * If the return ID isn't required, then this just reports success as true/false.
892 * $data is an object containing needed data
893 * @param string $table The database table to be inserted into
894 * @param object $data A data object with values for one or more fields in the record
895 * @param bool $returnid Should the id of the newly created record entry be returned? If this option is not requested then true/false is returned.
3503dcad 896 * @return bool|int true or new id
6df26010 897 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 898 */
899 public function insert_record($table, $dataobject, $returnid=true, $bulk=false) {
d8fa8e40 900 $dataobject = (array)$dataobject;
520b730c 901
902 $columns = $this->get_columns($table);
903 $cleaned = array();
904
905 foreach ($dataobject as $field => $value) {
d8fa8e40
PS
906 if ($field === 'id') {
907 continue;
908 }
520b730c 909 if (!isset($columns[$field])) {
910 continue;
911 }
912 $column = $columns[$field];
913 $cleaned[$field] = $this->normalise_value($column, $value);
914 }
915
916 return $this->insert_record_raw($table, $cleaned, $returnid, $bulk);
917 }
918
919 /**
920 * Import a record into a table, id field is required.
921 * Safety checks are NOT carried out. Lobs are supported.
922 *
923 * @param string $table name of database table to be inserted into
924 * @param object $dataobject A data object with values for one or more fields in the record
925 * @return bool true
6df26010 926 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 927 */
928 public function import_record($table, $dataobject) {
d8fa8e40 929 $dataobject = (array)$dataobject;
520b730c 930
931 $columns = $this->get_columns($table);
932 $cleaned = array();
933
896a2a91 934 foreach ($dataobject as $field => $value) {
520b730c 935 if (!isset($columns[$field])) {
936 continue;
937 }
896a2a91 938 $column = $columns[$field];
939 $cleaned[$field] = $this->normalise_value($column, $value);
520b730c 940 }
941
97dbac93 942 $this->insert_record_raw($table, $cleaned, false, false, true);
896a2a91 943
97dbac93 944 return true;
520b730c 945 }
946
947 /**
948 * Update record in database, as fast as possible, no safety checks, lobs not supported.
949 * @param string $table name
950 * @param mixed $params data record as object or array
951 * @param bool true means repeated updates expected
952 * @return bool true
6df26010 953 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 954 */
955 public function update_record_raw($table, $params, $bulk=false) {
d8fa8e40
PS
956 $params = (array)$params;
957
520b730c 958 if (!isset($params['id'])) {
959 throw new coding_exception('moodle_database::update_record_raw() id field must be specified.');
960 }
961 $id = $params['id'];
962 unset($params['id']);
963
964 if (empty($params)) {
965 throw new coding_exception('moodle_database::update_record_raw() no fields found.');
966 }
967
968 $sets = array();
969 foreach ($params as $field=>$value) {
970 $sets[] = "$field = ?";
971 }
972
973 $params[] = $id; // last ? in WHERE condition
974
975 $sets = implode(',', $sets);
976 $sql = "UPDATE {" . $table . "} SET $sets WHERE id = ?";
977
978 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
979 $rawsql = $this->emulate_bound_params($sql, $params);
980
981 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
5c08b24b 982 $result = mssql_query($rawsql, $this->mssql);
520b730c 983 $this->query_end($result);
984
985 $this->free_result($result);
986 return true;
987 }
988
989 /**
990 * Update a record in a table
991 *
992 * $dataobject is an object containing needed data
993 * Relies on $dataobject having a variable "id" to
994 * specify the record to update
995 *
996 * @param string $table The database table to be checked against.
997 * @param object $dataobject An object with contents equal to fieldname=>fieldvalue. Must have an entry for 'id' to map to the table specified.
998 * @param bool true means repeated updates expected
999 * @return bool true
6df26010 1000 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 1001 */
1002 public function update_record($table, $dataobject, $bulk=false) {
d8fa8e40 1003 $dataobject = (array)$dataobject;
520b730c 1004
1005 $columns = $this->get_columns($table);
1006 $cleaned = array();
1007
1008 foreach ($dataobject as $field => $value) {
1009 if (!isset($columns[$field])) {
1010 continue;
1011 }
1012 $column = $columns[$field];
1013 $cleaned[$field] = $this->normalise_value($column, $value);
1014 }
1015
1016 return $this->update_record_raw($table, $cleaned, $bulk);
1017 }
1018
1019 /**
1020 * Set a single field in every table record which match a particular WHERE clause.
1021 *
1022 * @param string $table The database table to be checked against.
1023 * @param string $newfield the field to set.
1024 * @param string $newvalue the value to set the field to.
1025 * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
1026 * @param array $params array of sql parameters
1027 * @return bool true
6df26010 1028 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 1029 */
1030 public function set_field_select($table, $newfield, $newvalue, $select, array $params=null) {
1031
1032 if ($select) {
1033 $select = "WHERE $select";
1034 }
1035 if (is_null($params)) {
1036 $params = array();
1037 }
1038
cd278cd4
PS
1039 // convert params to ? types
1040 list($select, $params, $type) = $this->fix_sql_params($select, $params);
1041
00902cd9 1042 // Get column metadata
520b730c 1043 $columns = $this->get_columns($table);
1044 $column = $columns[$newfield];
1045
1046 $newvalue = $this->normalise_value($column, $newvalue);
1047
1048 if (is_null($newvalue)) {
1049 $newfield = "$newfield = NULL";
1050 } else {
1051 $newfield = "$newfield = ?";
1052 array_unshift($params, $newvalue);
1053 }
1054 $sql = "UPDATE {" . $table . "} SET $newfield $select";
1055
1056 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
1057 $rawsql = $this->emulate_bound_params($sql, $params);
1058
1059 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
5c08b24b 1060 $result = mssql_query($rawsql, $this->mssql);
520b730c 1061 $this->query_end($result);
1062
1063 $this->free_result($result);
1064
1065 return true;
1066 }
1067
1068 /**
1069 * Delete one or more records from a table which match a particular WHERE clause.
1070 *
1071 * @param string $table The database table to be checked against.
1072 * @param string $select A fragment of SQL to be used in a where clause in the SQL call (used to define the selection criteria).
1073 * @param array $params array of sql parameters
1074 * @return bool true
6df26010 1075 * @throws dml_exception A DML specific exception is thrown for any errors.
520b730c 1076 */
1077 public function delete_records_select($table, $select, array $params=null) {
1078
1079 if ($select) {
1080 $select = "WHERE $select";
1081 }
1082
1083 $sql = "DELETE FROM {" . $table . "} $select";
1084
1085 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
1086 $rawsql = $this->emulate_bound_params($sql, $params);
1087
1088 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
5c08b24b 1089 $result = mssql_query($rawsql, $this->mssql);
520b730c 1090 $this->query_end($result);
1091
1092 $this->free_result($result);
1093
1094 return true;
1095 }
1096
520b730c 1097 public function sql_cast_char2int($fieldname, $text=false) {
9882b2c8
EL
1098 if (!$text) {
1099 return ' CAST(' . $fieldname . ' AS INT) ';
1100 } else {
1101 return ' CAST(' . $this->sql_compare_text($fieldname) . ' AS INT) ';
1102 }
1103 }
1104
1105 public function sql_cast_char2real($fieldname, $text=false) {
1106 if (!$text) {
1107 return ' CAST(' . $fieldname . ' AS REAL) ';
1108 } else {
1109 return ' CAST(' . $this->sql_compare_text($fieldname) . ' AS REAL) ';
1110 }
520b730c 1111 }
1112
1113 public function sql_ceil($fieldname) {
1114 return ' CEILING(' . $fieldname . ')';
1115 }
1116
6055f89d
PS
1117
1118 protected function get_collation() {
1119 if (isset($this->collation)) {
1120 return $this->collation;
1121 }
1122 if (!empty($this->dboptions['dbcollation'])) {
1123 // perf speedup
1124 $this->collation = $this->dboptions['dbcollation'];
1125 return $this->collation;
1126 }
1127
1128 // make some default
1129 $this->collation = 'Latin1_General_CI_AI';
1130
1131 $sql = "SELECT CAST(DATABASEPROPERTYEX('$this->dbname', 'Collation') AS varchar(255)) AS SQLCollation";
1132 $this->query_start($sql, null, SQL_QUERY_AUX);
e016d126 1133 $result = mssql_query($sql, $this->mssql);
6055f89d
PS
1134 $this->query_end($result);
1135
1136 if ($result) {
e016d126 1137 if ($rawcolumn = mssql_fetch_assoc($result)) {
6055f89d
PS
1138 $this->collation = reset($rawcolumn);
1139 }
1140 $this->free_result($result);
1141 }
1142
1143 return $this->collation;
1144 }
1145
6055f89d
PS
1146 /**
1147 * Returns 'LIKE' part of a query.
1148 *
1149 * @param string $fieldname usually name of the table column
1150 * @param string $param usually bound query parameter (?, :named)
1151 * @param bool $casesensitive use case sensitive search
1152 * @param bool $accensensitive use accent sensitive search (not all databases support accent insensitive)
16114b9d 1153 * @param bool $notlike true means "NOT LIKE"
6055f89d
PS
1154 * @param string $escapechar escape char for '%' and '_'
1155 * @return string SQL code fragment
1156 */
16114b9d 1157 public function sql_like($fieldname, $param, $casesensitive = true, $accentsensitive = true, $notlike = false, $escapechar = '\\') {
6055f89d 1158 if (strpos($param, '%') !== false) {
de640a2d 1159 debugging('Potential SQL injection detected, sql_like() expects bound parameters (? or :named)');
6055f89d
PS
1160 }
1161
1162 $collation = $this->get_collation();
1163
1164 if ($casesensitive) {
1165 $collation = str_replace('_CI', '_CS', $collation);
1166 } else {
1167 $collation = str_replace('_CS', '_CI', $collation);
1168 }
1169 if ($accentsensitive) {
1170 $collation = str_replace('_AI', '_AS', $collation);
1171 } else {
1172 $collation = str_replace('_AS', '_AI', $collation);
1173 }
1174
16114b9d
PS
1175 $LIKE = $notlike ? 'NOT LIKE' : 'LIKE';
1176
1177 return "$fieldname COLLATE $collation $LIKE $param ESCAPE '$escapechar'";
6055f89d
PS
1178 }
1179
520b730c 1180 public function sql_concat() {
1181 $arr = func_get_args();
1182 foreach ($arr as $key => $ele) {
1183 $arr[$key] = ' CAST(' . $ele . ' AS VARCHAR(255)) ';
1184 }
1185 $s = implode(' + ', $arr);
1186 if ($s === '') {
1187 return " '' ";
1188 }
1189 return " $s ";
1190 }
1191
1192 public function sql_concat_join($separator="' '", $elements=array()) {
1193 for ($n=count($elements)-1; $n > 0 ; $n--) {
1194 array_splice($elements, $n, 0, $separator);
1195 }
1196 $s = implode(' + ', $elements);
1197 if ($s === '') {
1198 return " '' ";
1199 }
1200 return " $s ";
1201 }
1202
1203 public function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) {
1204 if ($textfield) {
63b3d8ab 1205 return ' (' . $this->sql_compare_text($fieldname) . " = '') ";
520b730c 1206 } else {
63b3d8ab 1207 return " ($fieldname = '') ";
520b730c 1208 }
1209 }
1210
1211 /**
1212 * Returns the SQL text to be used to calculate the length in characters of one expression.
1213 * @param string fieldname or expression to calculate its length in characters.
1214 * @return string the piece of SQL code to be used in the statement.
1215 */
1216 public function sql_length($fieldname) {
1217 return ' LEN(' . $fieldname . ')';
1218 }
1219
1220 public function sql_order_by_text($fieldname, $numchars=32) {
1221 return ' CONVERT(varchar, ' . $fieldname . ', ' . $numchars . ')';
1222 }
1223
1224 /**
1225 * Returns the SQL for returning searching one string for the location of another.
1226 */
1227 public function sql_position($needle, $haystack) {
1228 return "CHARINDEX(($needle), ($haystack))";
1229 }
1230
1231 /**
ab62bd23 1232 * Returns the proper substr() SQL text used to extract substrings from DB
520b730c 1233 * NOTE: this was originally returning only function name
1234 *
1235 * @param string $expr some string field, no aggregates
4d276e34
PS
1236 * @param mixed $start integer or expression evaluating to int
1237 * @param mixed $length optional integer or expression evaluating to int
520b730c 1238 * @return string sql fragment
1239 */
1240 public function sql_substr($expr, $start, $length=false) {
1241 if (count(func_get_args()) < 2) {
1242 throw new coding_exception('moodle_database::sql_substr() requires at least two parameters', 'Originaly this function wa
1243s only returning name of SQL substring function, it now requires all parameters.');
1244 }
1245 if ($length === false) {
1246 return "SUBSTRING($expr, $start, (LEN($expr) - $start + 1))";
1247 } else {
1248 return "SUBSTRING($expr, $start, $length)";
1249 }
1250 }
1251
520b730c 1252 public function session_lock_supported() {
6d682bae 1253 return true;
520b730c 1254 }
1255
2b0e3941
PS
1256 /**
1257 * Obtain session lock
1258 * @param int $rowid id of the row with session record
1259 * @param int $timeout max allowed time to wait for the lock in seconds
1260 * @return bool success
1261 */
1262 public function get_session_lock($rowid, $timeout) {
520b730c 1263 if (!$this->session_lock_supported()) {
1264 return;
1265 }
2b0e3941
PS
1266 parent::get_session_lock($rowid, $timeout);
1267
1268 $timeoutmilli = $timeout * 1000;
6d682bae 1269
1270 $fullname = $this->dbname.'-'.$this->prefix.'-session-'.$rowid;
d16b0197
EL
1271 // There is one bug in PHP/freetds (both reproducible with mssql_query()
1272 // and its mssql_init()/mssql_bind()/mssql_execute() alternative) for
1273 // stored procedures, causing scalar results of the execution
1274 // to be cast to boolean (true/fals). Here there is one
1275 // workaround that forces the return of one recordset resource.
1276 // $sql = "sp_getapplock '$fullname', 'Exclusive', 'Session', $timeoutmilli";
1277 $sql = "BEGIN
1278 DECLARE @result INT
1279 EXECUTE @result = sp_getapplock @Resource='$fullname',
1280 @LockMode='Exclusive',
1281 @LockOwner='Session',
1282 @LockTimeout='$timeoutmilli'
1283 SELECT @result
1284 END";
520b730c 1285 $this->query_start($sql, null, SQL_QUERY_AUX);
6d682bae 1286 $result = mssql_query($sql, $this->mssql);
520b730c 1287 $this->query_end($result);
1288
2b0e3941
PS
1289 if ($result) {
1290 $row = mssql_fetch_row($result);
1291 if ($row[0] < 0) {
1292 throw new dml_sessionwait_exception();
1293 }
1294 }
1295
6d682bae 1296 $this->free_result($result);
520b730c 1297 }
1298
1299 public function release_session_lock($rowid) {
1300 if (!$this->session_lock_supported()) {
1301 return;
1302 }
46a86dbb
1303 if (!$this->used_for_db_sessions) {
1304 return;
1305 }
1306
520b730c 1307 parent::release_session_lock($rowid);
1308
6d682bae 1309 $fullname = $this->dbname.'-'.$this->prefix.'-session-'.$rowid;
982fd65f 1310 $sql = "sp_releaseapplock '$fullname', 'Session'";
520b730c 1311 $this->query_start($sql, null, SQL_QUERY_AUX);
6d682bae 1312 $result = mssql_query($sql, $this->mssql);
520b730c 1313 $this->query_end($result);
1314
6d682bae 1315 $this->free_result($result);
520b730c 1316 }
1317
520b730c 1318 /**
d5a8d9aa
PS
1319 * Driver specific start of real database transaction,
1320 * this can not be used directly in code.
1321 * @return void
520b730c 1322 */
12ffe203
EL
1323 protected function begin_transaction() {
1324 // requires database to run with READ_COMMITTED_SNAPSHOT ON
520b730c 1325 $sql = "BEGIN TRANSACTION"; // Will be using READ COMMITTED isolation
1326 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1327 $result = mssql_query($sql, $this->mssql);
1328 $this->query_end($result);
1329
1330 $this->free_result($result);
520b730c 1331 }
1332
1333 /**
d5a8d9aa
PS
1334 * Driver specific commit of real database transaction,
1335 * this can not be used directly in code.
1336 * @return void
520b730c 1337 */
12ffe203 1338 protected function commit_transaction() {
520b730c 1339 $sql = "COMMIT TRANSACTION";
1340 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1341 $result = mssql_query($sql, $this->mssql);
1342 $this->query_end($result);
1343
1344 $this->free_result($result);
520b730c 1345 }
1346
1347 /**
d5a8d9aa
PS
1348 * Driver specific abort of real database transaction,
1349 * this can not be used directly in code.
1350 * @return void
520b730c 1351 */
12ffe203 1352 protected function rollback_transaction() {
520b730c 1353 $sql = "ROLLBACK TRANSACTION";
1354 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1355 $result = mssql_query($sql, $this->mssql);
1356 $this->query_end($result);
1357
1358 $this->free_result($result);
520b730c 1359 }
1360}