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

« back to all changes in this revision

Viewing changes to www/php/phpBB3/includes/db/mssql_odbc.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_odbc.php,v 1.37 2007/10/05 14:36:32 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
* Unified ODBC functions
 
23
* Unified ODBC functions support any database having ODBC driver, for example Adabas D, IBM DB2, iODBC, Solid, Sybase SQL Anywhere...
 
24
* Here we only support MSSQL Server 2000+ because of the provided schema
 
25
*
 
26
* @note number of bytes returned for returning data depends on odbc.defaultlrl php.ini setting.
 
27
* If it is limited to 4K for example only 4K of data is returned max, resulting in incomplete theme data for example.
 
28
* @note odbc.defaultbinmode may affect UTF8 characters
 
29
*
 
30
* @package dbal
 
31
*/
 
32
class dbal_mssql_odbc extends dbal
 
33
{
 
34
        var $last_query_text = '';
 
35
 
 
36
        /**
 
37
        * Connect to server
 
38
        */
 
39
        function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
 
40
        {
 
41
                $this->persistency = $persistency;
 
42
                $this->user = $sqluser;
 
43
                $this->server = $sqlserver . (($port) ? ':' . $port : '');
 
44
                $this->dbname = $database;
 
45
 
 
46
                $max_size = @ini_get('odbc.defaultlrl');
 
47
                if (!empty($max_size))
 
48
                {
 
49
                        $unit = strtolower(substr($max_size, -1, 1));
 
50
                        $max_size = (int) $max_size;
 
51
 
 
52
                        if ($unit == 'k')
 
53
                        {
 
54
                                $max_size = floor($max_size / 1024);
 
55
                        }
 
56
                        else if ($unit == 'g')
 
57
                        {
 
58
                                $max_size *= 1024;
 
59
                        }
 
60
                        else if (is_numeric($unit))
 
61
                        {
 
62
                                $max_size = floor((int) ($max_size . $unit) / 1048576);
 
63
                        }
 
64
                        $max_size = max(8, $max_size) . 'M';
 
65
 
 
66
                        @ini_set('odbc.defaultlrl', $max_size);
 
67
                }
 
68
 
 
69
                $this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword);
 
70
 
 
71
                return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
 
72
        }
 
73
 
 
74
        /**
 
75
        * Version information about used database
 
76
        */
 
77
        function sql_server_info()
 
78
        {
 
79
                $result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
 
80
 
 
81
                $row = false;
 
82
                if ($result_id)
 
83
                {
 
84
                        $row = @odbc_fetch_array($result_id);
 
85
                        @odbc_free_result($result_id);
 
86
                }
 
87
 
 
88
                if ($row)
 
89
                {
 
90
                        return 'MSSQL (ODBC)<br />' . implode(' ', $row);
 
91
                }
 
92
 
 
93
                return 'MSSQL (ODBC)';
 
94
        }
 
95
 
 
96
        /**
 
97
        * SQL Transaction
 
98
        * @access private
 
99
        */
 
100
        function _sql_transaction($status = 'begin')
 
101
        {
 
102
                switch ($status)
 
103
                {
 
104
                        case 'begin':
 
105
                                return @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
 
106
                        break;
 
107
 
 
108
                        case 'commit':
 
109
                                return @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
 
110
                        break;
 
111
 
 
112
                        case 'rollback':
 
113
                                return @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
 
114
                        break;
 
115
                }
 
116
 
 
117
                return true;
 
118
        }
 
119
 
 
120
        /**
 
121
        * Base query method
 
122
        *
 
123
        * @param        string  $query          Contains the SQL query which shall be executed
 
124
        * @param        int             $cache_ttl      Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
 
125
        * @return       mixed                           When casted to bool the returned value returns true on success and false on failure
 
126
        *
 
127
        * @access       public
 
128
        */
 
129
        function sql_query($query = '', $cache_ttl = 0)
 
130
        {
 
131
                if ($query != '')
 
132
                {
 
133
                        global $cache;
 
134
 
 
135
                        // EXPLAIN only in extra debug mode
 
136
                        if (defined('DEBUG_EXTRA'))
 
137
                        {
 
138
                                $this->sql_report('start', $query);
 
139
                        }
 
140
 
 
141
                        $this->last_query_text = $query;
 
142
                        $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
 
143
                        $this->sql_add_num_queries($this->query_result);
 
144
 
 
145
                        if ($this->query_result === false)
 
146
                        {
 
147
                                if (($this->query_result = @odbc_exec($this->db_connect_id, $query)) === false)
 
148
                                {
 
149
                                        $this->sql_error($query);
 
150
                                }
 
151
 
 
152
                                if (defined('DEBUG_EXTRA'))
 
153
                                {
 
154
                                        $this->sql_report('stop', $query);
 
155
                                }
 
156
 
 
157
                                if ($cache_ttl && method_exists($cache, 'sql_save'))
 
158
                                {
 
159
                                        $this->open_queries[(int) $this->query_result] = $this->query_result;
 
160
                                        $cache->sql_save($query, $this->query_result, $cache_ttl);
 
161
                                }
 
162
                                else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 
163
                                {
 
164
                                        $this->open_queries[(int) $this->query_result] = $this->query_result;
 
165
                                }
 
166
                        }
 
167
                        else if (defined('DEBUG_EXTRA'))
 
168
                        {
 
169
                                $this->sql_report('fromcache', $query);
 
170
                        }
 
171
                }
 
172
                else
 
173
                {
 
174
                        return false;
 
175
                }
 
176
 
 
177
                return ($this->query_result) ? $this->query_result : false;
 
178
        }
 
179
 
 
180
        /**
 
181
        * Build LIMIT query
 
182
        */
 
183
        function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
 
184
        {
 
185
                $this->query_result = false;
 
186
 
 
187
                // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
 
188
                if ($total)
 
189
                {
 
190
                        // We need to grab the total number of rows + the offset number of rows to get the correct result
 
191
                        if (strpos($query, 'SELECT DISTINCT') === 0)
 
192
                        {
 
193
                                $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
 
194
                        }
 
195
                        else
 
196
                        {
 
197
                                $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
 
198
                        }
 
199
                }
 
200
 
 
201
                $result = $this->sql_query($query, $cache_ttl);
 
202
 
 
203
                // Seek by $offset rows
 
204
                if ($offset)
 
205
                {
 
206
                        $this->sql_rowseek($offset, $result);
 
207
                }
 
208
 
 
209
                return $result;
 
210
        }
 
211
 
 
212
        /**
 
213
        * Return number of affected rows
 
214
        */
 
215
        function sql_affectedrows()
 
216
        {
 
217
                return ($this->db_connect_id) ? @odbc_num_rows($this->query_result) : false;
 
218
        }
 
219
 
 
220
        /**
 
221
        * Fetch current row
 
222
        * @note number of bytes returned depends on odbc.defaultlrl php.ini setting. If it is limited to 4K for example only 4K of data is returned max.
 
223
        */
 
224
        function sql_fetchrow($query_id = false, $debug = false)
 
225
        {
 
226
                global $cache;
 
227
 
 
228
                if ($query_id === false)
 
229
                {
 
230
                        $query_id = $this->query_result;
 
231
                }
 
232
 
 
233
                if (isset($cache->sql_rowset[$query_id]))
 
234
                {
 
235
                        return $cache->sql_fetchrow($query_id);
 
236
                }
 
237
 
 
238
                return ($query_id !== false) ? @odbc_fetch_array($query_id) : false;
 
239
        }
 
240
 
 
241
        /**
 
242
        * Seek to given row number
 
243
        * rownum is zero-based
 
244
        */
 
245
        function sql_rowseek($rownum, &$query_id)
 
246
        {
 
247
                global $cache;
 
248
 
 
249
                if ($query_id === false)
 
250
                {
 
251
                        $query_id = $this->query_result;
 
252
                }
 
253
 
 
254
                if (isset($cache->sql_rowset[$query_id]))
 
255
                {
 
256
                        return $cache->sql_rowseek($rownum, $query_id);
 
257
                }
 
258
 
 
259
                if ($query_id === false)
 
260
                {
 
261
                        return false;
 
262
                }
 
263
 
 
264
                $this->sql_freeresult($query_id);
 
265
                $query_id = $this->sql_query($this->last_query_text);
 
266
 
 
267
                if ($query_id === false)
 
268
                {
 
269
                        return false;
 
270
                }
 
271
 
 
272
                // We do not fetch the row for rownum == 0 because then the next resultset would be the second row
 
273
                for ($i = 0; $i < $rownum; $i++)
 
274
                {
 
275
                        if (!$this->sql_fetchrow($query_id))
 
276
                        {
 
277
                                return false;
 
278
                        }
 
279
                }
 
280
 
 
281
                return true;
 
282
        }
 
283
 
 
284
        /**
 
285
        * Get last inserted id after insert statement
 
286
        */
 
287
        function sql_nextid()
 
288
        {
 
289
                $result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
 
290
 
 
291
                if ($result_id)
 
292
                {
 
293
                        if (@odbc_fetch_array($result_id))
 
294
                        {
 
295
                                $id = @odbc_result($result_id, 1);
 
296
                                @odbc_free_result($result_id);
 
297
                                return $id;
 
298
                        }
 
299
                        @odbc_free_result($result_id);
 
300
                }
 
301
 
 
302
                return false;
 
303
        }
 
304
 
 
305
        /**
 
306
        * Free sql result
 
307
        */
 
308
        function sql_freeresult($query_id = false)
 
309
        {
 
310
                global $cache;
 
311
 
 
312
                if ($query_id === false)
 
313
                {
 
314
                        $query_id = $this->query_result;
 
315
                }
 
316
 
 
317
                if (isset($cache->sql_rowset[$query_id]))
 
318
                {
 
319
                        return $cache->sql_freeresult($query_id);
 
320
                }
 
321
 
 
322
                if (isset($this->open_queries[(int) $query_id]))
 
323
                {
 
324
                        unset($this->open_queries[(int) $query_id]);
 
325
                        return @odbc_free_result($query_id);
 
326
                }
 
327
 
 
328
                return false;
 
329
        }
 
330
 
 
331
        /**
 
332
        * Escape string used in sql query
 
333
        */
 
334
        function sql_escape($msg)
 
335
        {
 
336
                return str_replace("'", "''", $msg);
 
337
        }
 
338
 
 
339
        /**
 
340
        * Build LIKE expression
 
341
        * @access private
 
342
        */
 
343
        function _sql_like_expression($expression)
 
344
        {
 
345
                return $expression . " ESCAPE '\\'";
 
346
        }
 
347
 
 
348
        /**
 
349
        * Build db-specific query data
 
350
        * @access private
 
351
        */
 
352
        function _sql_custom_build($stage, $data)
 
353
        {
 
354
                return $data;
 
355
        }
 
356
 
 
357
        /**
 
358
        * return sql error array
 
359
        * @access private
 
360
        */
 
361
        function _sql_error()
 
362
        {
 
363
                return array(
 
364
                        'message'       => @odbc_errormsg(),
 
365
                        'code'          => @odbc_error()
 
366
                );
 
367
        }
 
368
 
 
369
        /**
 
370
        * Close sql connection
 
371
        * @access private
 
372
        */
 
373
        function _sql_close()
 
374
        {
 
375
                return @odbc_close($this->db_connect_id);
 
376
        }
 
377
 
 
378
        /**
 
379
        * Build db-specific report
 
380
        * @access private
 
381
        */
 
382
        function _sql_report($mode, $query = '')
 
383
        {
 
384
                switch ($mode)
 
385
                {
 
386
                        case 'start':
 
387
                        break;
 
388
 
 
389
                        case 'fromcache':
 
390
                                $endtime = explode(' ', microtime());
 
391
                                $endtime = $endtime[0] + $endtime[1];
 
392
 
 
393
                                $result = @odbc_exec($this->db_connect_id, $query);
 
394
                                while ($void = @odbc_fetch_array($result))
 
395
                                {
 
396
                                        // Take the time spent on parsing rows into account
 
397
                                }
 
398
                                @odbc_free_result($result);
 
399
 
 
400
                                $splittime = explode(' ', microtime());
 
401
                                $splittime = $splittime[0] + $splittime[1];
 
402
 
 
403
                                $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 
404
 
 
405
                        break;
 
406
                }
 
407
        }
 
408
}
 
409
 
 
410
?>
 
 
b'\\ No newline at end of file'