~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/php/phpBB3/includes/db/mssql.php

  • Committer: dcoles
  • Date: 2008-02-13 04:10:55 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:443
Added Forum application along with unmodifed version of phpBB3 "Olympus" 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
*
 
4
* @package dbal
 
5
* @version $Id: mssql.php,v 1.51 2007/10/12 10:04:31 acydburn Exp $
 
6
* @copyright (c) 2005 phpBB Group
 
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
 
8
*
 
9
*/
 
10
 
 
11
/**
 
12
* @ignore
 
13
*/
 
14
if (!defined('IN_PHPBB'))
 
15
{
 
16
        exit;
 
17
}
 
18
 
 
19
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
 
20
 
 
21
/**
 
22
* MSSQL Database Abstraction Layer
 
23
* Minimum Requirement is MSSQL 2000+
 
24
* @package dbal
 
25
*/
 
26
class dbal_mssql extends dbal
 
27
{
 
28
        /**
 
29
        * Connect to server
 
30
        */
 
31
        function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
 
32
        {
 
33
                $this->persistency = $persistency;
 
34
                $this->user = $sqluser;
 
35
                $this->server = $sqlserver . (($port) ? ':' . $port : '');
 
36
                $this->dbname = $database;
 
37
 
 
38
                @ini_set('mssql.charset', 'UTF-8');
 
39
                @ini_set('mssql.textlimit', 2147483647);
 
40
                @ini_set('mssql.textsize', 2147483647);
 
41
 
 
42
                if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.1', '>=')))
 
43
                {
 
44
                        $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword, $new_link) : @mssql_connect($this->server, $this->user, $sqlpassword, $new_link);
 
45
                }
 
46
                else
 
47
                {
 
48
                        $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword) : @mssql_connect($this->server, $this->user, $sqlpassword);
 
49
                }
 
50
 
 
51
                if ($this->db_connect_id && $this->dbname != '')
 
52
                {
 
53
                        if (!@mssql_select_db($this->dbname, $this->db_connect_id))
 
54
                        {
 
55
                                @mssql_close($this->db_connect_id);
 
56
                                return false;
 
57
                        }
 
58
                }
 
59
 
 
60
                return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
 
61
        }
 
62
 
 
63
        /**
 
64
        * Version information about used database
 
65
        */
 
66
        function sql_server_info()
 
67
        {
 
68
                $result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
 
69
 
 
70
                $row = false;
 
71
                if ($result_id)
 
72
                {
 
73
                        $row = @mssql_fetch_assoc($result_id);
 
74
                        @mssql_free_result($result_id);
 
75
                }
 
76
 
 
77
                if ($row)
 
78
                {
 
79
                        return 'MSSQL<br />' . implode(' ', $row);
 
80
                }
 
81
 
 
82
                return 'MSSQL';
 
83
        }
 
84
 
 
85
        /**
 
86
        * SQL Transaction
 
87
        * @access private
 
88
        */
 
89
        function _sql_transaction($status = 'begin')
 
90
        {
 
91
                switch ($status)
 
92
                {
 
93
                        case 'begin':
 
94
                                return @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
 
95
                        break;
 
96
 
 
97
                        case 'commit':
 
98
                                return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id);
 
99
                        break;
 
100
 
 
101
                        case 'rollback':
 
102
                                return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id);
 
103
                        break;
 
104
                }
 
105
 
 
106
                return true;
 
107
        }
 
108
 
 
109
        /**
 
110
        * Base query method
 
111
        *
 
112
        * @param        string  $query          Contains the SQL query which shall be executed
 
113
        * @param        int             $cache_ttl      Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
 
114
        * @return       mixed                           When casted to bool the returned value returns true on success and false on failure
 
115
        *
 
116
        * @access       public
 
117
        */
 
118
        function sql_query($query = '', $cache_ttl = 0)
 
119
        {
 
120
                if ($query != '')
 
121
                {
 
122
                        global $cache;
 
123
 
 
124
                        // EXPLAIN only in extra debug mode
 
125
                        if (defined('DEBUG_EXTRA'))
 
126
                        {
 
127
                                $this->sql_report('start', $query);
 
128
                        }
 
129
 
 
130
                        $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
 
131
                        $this->sql_add_num_queries($this->query_result);
 
132
 
 
133
                        if ($this->query_result === false)
 
134
                        {
 
135
                                if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === false)
 
136
                                {
 
137
                                        $this->sql_error($query);
 
138
                                }
 
139
 
 
140
                                if (defined('DEBUG_EXTRA'))
 
141
                                {
 
142
                                        $this->sql_report('stop', $query);
 
143
                                }
 
144
 
 
145
                                if ($cache_ttl && method_exists($cache, 'sql_save'))
 
146
                                {
 
147
                                        $this->open_queries[(int) $this->query_result] = $this->query_result;
 
148
                                        $cache->sql_save($query, $this->query_result, $cache_ttl);
 
149
                                }
 
150
                                else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 
151
                                {
 
152
                                        $this->open_queries[(int) $this->query_result] = $this->query_result;
 
153
                                }
 
154
                        }
 
155
                        else if (defined('DEBUG_EXTRA'))
 
156
                        {
 
157
                                $this->sql_report('fromcache', $query);
 
158
                        }
 
159
                }
 
160
                else
 
161
                {
 
162
                        return false;
 
163
                }
 
164
 
 
165
                return ($this->query_result) ? $this->query_result : false;
 
166
        }
 
167
 
 
168
        /**
 
169
        * Build LIMIT query
 
170
        */
 
171
        function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
 
172
        {
 
173
                $this->query_result = false;
 
174
 
 
175
                // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
 
176
                if ($total)
 
177
                {
 
178
                        // We need to grab the total number of rows + the offset number of rows to get the correct result
 
179
                        if (strpos($query, 'SELECT DISTINCT') === 0)
 
180
                        {
 
181
                                $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
 
182
                        }
 
183
                        else
 
184
                        {
 
185
                                $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
 
186
                        }
 
187
                }
 
188
 
 
189
                $result = $this->sql_query($query, $cache_ttl);
 
190
 
 
191
                // Seek by $offset rows
 
192
                if ($offset)
 
193
                {
 
194
                        $this->sql_rowseek($offset, $result);
 
195
                }
 
196
 
 
197
                return $result;
 
198
        }
 
199
 
 
200
        /**
 
201
        * Return number of affected rows
 
202
        */
 
203
        function sql_affectedrows()
 
204
        {
 
205
                return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false;
 
206
        }
 
207
 
 
208
        /**
 
209
        * Fetch current row
 
210
        */
 
211
        function sql_fetchrow($query_id = false)
 
212
        {
 
213
                global $cache;
 
214
 
 
215
                if ($query_id === false)
 
216
                {
 
217
                        $query_id = $this->query_result;
 
218
                }
 
219
 
 
220
                if (isset($cache->sql_rowset[$query_id]))
 
221
                {
 
222
                        return $cache->sql_fetchrow($query_id);
 
223
                }
 
224
 
 
225
                if ($query_id === false)
 
226
                {
 
227
                        return false;
 
228
                }
 
229
 
 
230
                $row = @mssql_fetch_assoc($query_id);
 
231
 
 
232
                // I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
 
233
                if ($row)
 
234
                {
 
235
                        foreach ($row as $key => $value)
 
236
                        {
 
237
                                $row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
 
238
                        }
 
239
                }
 
240
 
 
241
                return $row;
 
242
        }
 
243
 
 
244
        /**
 
245
        * Seek to given row number
 
246
        * rownum is zero-based
 
247
        */
 
248
        function sql_rowseek($rownum, &$query_id)
 
249
        {
 
250
                global $cache;
 
251
 
 
252
                if ($query_id === false)
 
253
                {
 
254
                        $query_id = $this->query_result;
 
255
                }
 
256
 
 
257
                if (isset($cache->sql_rowset[$query_id]))
 
258
                {
 
259
                        return $cache->sql_rowseek($rownum, $query_id);
 
260
                }
 
261
 
 
262
                return ($query_id !== false) ? @mssql_data_seek($query_id, $rownum) : false;
 
263
        }
 
264
 
 
265
        /**
 
266
        * Get last inserted id after insert statement
 
267
        */
 
268
        function sql_nextid()
 
269
        {
 
270
                $result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id);
 
271
                if ($result_id)
 
272
                {
 
273
                        if ($row = @mssql_fetch_assoc($result_id))
 
274
                        {
 
275
                                @mssql_free_result($result_id);
 
276
                                return $row['computed'];
 
277
                        }
 
278
                        @mssql_free_result($result_id);
 
279
                }
 
280
 
 
281
                return false;
 
282
        }
 
283
 
 
284
        /**
 
285
        * Free sql result
 
286
        */
 
287
        function sql_freeresult($query_id = false)
 
288
        {
 
289
                global $cache;
 
290
 
 
291
                if ($query_id === false)
 
292
                {
 
293
                        $query_id = $this->query_result;
 
294
                }
 
295
 
 
296
                if (isset($cache->sql_rowset[$query_id]))
 
297
                {
 
298
                        return $cache->sql_freeresult($query_id);
 
299
                }
 
300
 
 
301
                if (isset($this->open_queries[$query_id]))
 
302
                {
 
303
                        unset($this->open_queries[$query_id]);
 
304
                        return @mssql_free_result($query_id);
 
305
                }
 
306
 
 
307
                return false;
 
308
        }
 
309
 
 
310
        /**
 
311
        * Escape string used in sql query
 
312
        */
 
313
        function sql_escape($msg)
 
314
        {
 
315
                return str_replace("'", "''", $msg);
 
316
        }
 
317
 
 
318
        /**
 
319
        * Build LIKE expression
 
320
        * @access private
 
321
        */
 
322
        function _sql_like_expression($expression)
 
323
        {
 
324
                return $expression . " ESCAPE '\\'";
 
325
        }
 
326
 
 
327
        /**
 
328
        * return sql error array
 
329
        * @access private
 
330
        */
 
331
        function _sql_error()
 
332
        {
 
333
                $error = array(
 
334
                        'message'       => @mssql_get_last_message(),
 
335
                        'code'          => ''
 
336
                );
 
337
 
 
338
                // Get error code number
 
339
                $result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
 
340
                if ($result_id)
 
341
                {
 
342
                        $row = @mssql_fetch_assoc($result_id);
 
343
                        $error['code'] = $row['code'];
 
344
                        @mssql_free_result($result_id);
 
345
                }
 
346
 
 
347
                // Get full error message if possible
 
348
                $sql = 'SELECT CAST(description as varchar(255)) as message
 
349
                        FROM master.dbo.sysmessages
 
350
                        WHERE error = ' . $error['code'];
 
351
                $result_id = @mssql_query($sql);
 
352
                
 
353
                if ($result_id)
 
354
                {
 
355
                        $row = @mssql_fetch_assoc($result_id);
 
356
                        if (!empty($row['message']))
 
357
                        {
 
358
                                $error['message'] .= '<br />' . $row['message'];
 
359
                        }
 
360
                        @mssql_free_result($result_id);
 
361
                }
 
362
 
 
363
                return $error;
 
364
        }
 
365
 
 
366
        /**
 
367
        * Build db-specific query data
 
368
        * @access private
 
369
        */
 
370
        function _sql_custom_build($stage, $data)
 
371
        {
 
372
                return $data;
 
373
        }
 
374
 
 
375
        /**
 
376
        * Close sql connection
 
377
        * @access private
 
378
        */
 
379
        function _sql_close()
 
380
        {
 
381
                return @mssql_close($this->db_connect_id);
 
382
        }
 
383
 
 
384
        /**
 
385
        * Build db-specific report
 
386
        * @access private
 
387
        */
 
388
        function _sql_report($mode, $query = '')
 
389
        {
 
390
                switch ($mode)
 
391
                {
 
392
                        case 'start':
 
393
                                $html_table = false;
 
394
                                @mssql_query('SET SHOWPLAN_TEXT ON;', $this->db_connect_id);
 
395
                                if ($result = @mssql_query($query, $this->db_connect_id))
 
396
                                {
 
397
                                        @mssql_next_result($result);
 
398
                                        while ($row = @mssql_fetch_row($result))
 
399
                                        {
 
400
                                                $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 
401
                                        }
 
402
                                }
 
403
                                @mssql_query('SET SHOWPLAN_TEXT OFF;', $this->db_connect_id);
 
404
                                @mssql_free_result($result);
 
405
 
 
406
                                if ($html_table)
 
407
                                {
 
408
                                        $this->html_hold .= '</table>';
 
409
                                }
 
410
                        break;
 
411
 
 
412
                        case 'fromcache':
 
413
                                $endtime = explode(' ', microtime());
 
414
                                $endtime = $endtime[0] + $endtime[1];
 
415
 
 
416
                                $result = @mssql_query($query, $this->db_connect_id);
 
417
                                while ($void = @mssql_fetch_assoc($result))
 
418
                                {
 
419
                                        // Take the time spent on parsing rows into account
 
420
                                }
 
421
                                @mssql_free_result($result);
 
422
 
 
423
                                $splittime = explode(' ', microtime());
 
424
                                $splittime = $splittime[0] + $splittime[1];
 
425
 
 
426
                                $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 
427
 
 
428
                        break;
 
429
                }
 
430
        }
 
431
}
 
432
 
 
433
?>
 
 
b'\\ No newline at end of file'