Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 136
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
mssql_odbc
0.00% covered (danger)
0.00%
0 / 136
0.00% covered (danger)
0.00%
0 / 12
4970
0.00% covered (danger)
0.00%
0 / 1
 sql_connect
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
156
 sql_server_info
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
110
 _sql_transaction
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 sql_query
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
272
 _sql_query_limit
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 sql_affectedrows
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 sql_fetchrow
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 sql_last_inserted_id
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 sql_freeresult
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 _sql_error
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 _sql_close
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 _sql_report
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\db\driver;
15
16/**
17* Unified ODBC functions
18* Unified ODBC functions support any database having ODBC driver, for example Adabas D, IBM DB2, iODBC, Solid, Sybase SQL Anywhere...
19* Here we only support MSSQL Server 2000+ because of the provided schema
20*
21* @note number of bytes returned for returning data depends on odbc.defaultlrl php.ini setting.
22* If it is limited to 4K for example only 4K of data is returned max, resulting in incomplete theme data for example.
23* @note odbc.defaultbinmode may affect UTF8 characters
24*/
25class mssql_odbc extends \phpbb\db\driver\mssql_base
26{
27    var $connect_error = '';
28
29    /**
30    * {@inheritDoc}
31    */
32    function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
33    {
34        $this->persistency = $persistency;
35        $this->user = $sqluser;
36        $this->dbname = $database;
37
38        $port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
39        $this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
40
41        $max_size = @ini_get('odbc.defaultlrl');
42        if (!empty($max_size))
43        {
44            $unit = strtolower(substr($max_size, -1, 1));
45            $max_size = (int) $max_size;
46
47            if ($unit == 'k')
48            {
49                $max_size = floor($max_size / 1024);
50            }
51            else if ($unit == 'g')
52            {
53                $max_size *= 1024;
54            }
55            else if (is_numeric($unit))
56            {
57                $max_size = floor((int) ($max_size . $unit) / 1048576);
58            }
59            $max_size = max(8, $max_size) . 'M';
60
61            @ini_set('odbc.defaultlrl', $max_size);
62        }
63
64        if ($this->persistency)
65        {
66            if (!function_exists('odbc_pconnect'))
67            {
68                $this->connect_error = 'odbc_pconnect function does not exist, is odbc extension installed?';
69                return $this->sql_error('');
70            }
71            $this->db_connect_id = @odbc_pconnect($this->server, $this->user, $sqlpassword);
72        }
73        else
74        {
75            if (!function_exists('odbc_connect'))
76            {
77                $this->connect_error = 'odbc_connect function does not exist, is odbc extension installed?';
78                return $this->sql_error('');
79            }
80            $this->db_connect_id = @odbc_connect($this->server, $this->user, $sqlpassword);
81        }
82
83        return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
84    }
85
86    /**
87    * {@inheritDoc}
88    */
89    function sql_server_info($raw = false, $use_cache = true)
90    {
91        global $cache;
92
93        if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mssqlodbc_version')) === false)
94        {
95            $result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
96
97            $row = false;
98            if ($result_id)
99            {
100                $row = odbc_fetch_array($result_id);
101                odbc_free_result($result_id);
102            }
103
104            $this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
105
106            if (!empty($cache) && $use_cache)
107            {
108                $cache->put('mssqlodbc_version', $this->sql_server_version);
109            }
110        }
111
112        if ($raw)
113        {
114            return (string) $this->sql_server_version;
115        }
116
117        return ($this->sql_server_version) ? 'MSSQL (ODBC)<br />' . $this->sql_server_version : 'MSSQL (ODBC)';
118    }
119
120    /**
121    * {@inheritDoc}
122    */
123    protected function _sql_transaction(string $status = 'begin'): bool
124    {
125        switch ($status)
126        {
127            case 'begin':
128                return (bool) @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
129
130            case 'commit':
131                return (bool) @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
132
133            case 'rollback':
134                return (bool) @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
135        }
136
137        return true;
138    }
139
140    /**
141    * {@inheritDoc}
142    */
143    function sql_query($query = '', $cache_ttl = 0)
144    {
145        if ($query != '')
146        {
147            global $cache;
148
149            if ($this->debug_sql_explain)
150            {
151                $this->sql_report('start', $query);
152            }
153            else if ($this->debug_load_time)
154            {
155                $this->curtime = microtime(true);
156            }
157
158            $this->last_query_text = $query;
159            $this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false;
160            $this->sql_add_num_queries($this->query_result);
161
162            if ($this->query_result === false)
163            {
164                try
165                {
166                    $this->query_result = @odbc_exec($this->db_connect_id, $query);
167                }
168                catch (\Error $e)
169                {
170                    // Do nothing as SQL driver will report the error
171                }
172
173                if ($this->query_result === false)
174                {
175                    $this->sql_error($query);
176                }
177
178                if ($this->debug_sql_explain)
179                {
180                    $this->sql_report('stop', $query);
181                }
182                else if ($this->debug_load_time)
183                {
184                    $this->sql_time += microtime(true) - $this->curtime;
185                }
186
187                if (!$this->query_result)
188                {
189                    return false;
190                }
191
192                $safe_query_id = $this->clean_query_id($this->query_result);
193
194                if ($cache && $cache_ttl)
195                {
196                    $this->open_queries[$safe_query_id] = $this->query_result;
197                    $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
198                }
199                else if (strpos($query, 'SELECT') === 0)
200                {
201                    $this->open_queries[$safe_query_id] = $this->query_result;
202                }
203            }
204            else if ($this->debug_sql_explain)
205            {
206                $this->sql_report('fromcache', $query);
207            }
208        }
209        else
210        {
211            return false;
212        }
213
214        return $this->query_result;
215    }
216
217    /**
218     * {@inheritDoc}
219     */
220    protected function _sql_query_limit(string $query, int $total, int $offset = 0, int $cache_ttl = 0)
221    {
222        $this->query_result = false;
223
224        // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
225        if ($total)
226        {
227            // We need to grab the total number of rows + the offset number of rows to get the correct result
228            if (strpos($query, 'SELECT DISTINCT') === 0)
229            {
230                $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
231            }
232            else
233            {
234                $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
235            }
236        }
237
238        $result = $this->sql_query($query, $cache_ttl);
239
240        // Seek by $offset rows
241        if ($offset)
242        {
243            $this->sql_rowseek($offset, $result);
244        }
245
246        return $result;
247    }
248
249    /**
250    * {@inheritDoc}
251    */
252    function sql_affectedrows()
253    {
254        return ($this->db_connect_id) ? @odbc_num_rows($this->query_result) : false;
255    }
256
257    /**
258    * {@inheritDoc}
259    */
260    function sql_fetchrow($query_id = false)
261    {
262        global $cache;
263
264        if ($query_id === false)
265        {
266            $query_id = $this->query_result;
267        }
268
269        $safe_query_id = $this->clean_query_id($query_id);
270        if ($cache && $cache->sql_exists($safe_query_id))
271        {
272            return $cache->sql_fetchrow($safe_query_id);
273        }
274
275        return ($query_id) ? odbc_fetch_array($query_id) : false;
276    }
277
278    /**
279     * {@inheritdoc}
280     */
281    public function sql_last_inserted_id()
282    {
283        $result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
284
285        if ($result_id)
286        {
287            if (odbc_fetch_array($result_id))
288            {
289                $id = odbc_result($result_id, 1);
290                odbc_free_result($result_id);
291                return $id ? (int) $id : false;
292            }
293            odbc_free_result($result_id);
294        }
295
296        return false;
297    }
298
299    /**
300    * {@inheritDoc}
301    */
302    function sql_freeresult($query_id = false)
303    {
304        global $cache;
305
306        if ($query_id === false)
307        {
308            $query_id = $this->query_result;
309        }
310
311        $safe_query_id = $this->clean_query_id($query_id);
312        if ($cache && $cache->sql_exists($safe_query_id))
313        {
314            $cache->sql_freeresult($safe_query_id);
315        }
316        else if (isset($this->open_queries[$safe_query_id]))
317        {
318            unset($this->open_queries[$safe_query_id]);
319            odbc_free_result($query_id);
320        }
321    }
322
323    /**
324    * {@inheritDoc}
325    */
326    protected function _sql_error(): array
327    {
328        if (function_exists('odbc_errormsg'))
329        {
330            $error = array(
331                'message'    => @odbc_errormsg(),
332                'code'        => @odbc_error(),
333            );
334        }
335        else
336        {
337            $error = array(
338                'message'    => $this->connect_error,
339                'code'        => '',
340            );
341        }
342
343        return $error;
344    }
345
346    /**
347     * {@inheritDoc}
348     */
349    protected function _sql_close(): bool
350    {
351        @odbc_close($this->db_connect_id);
352        return true;
353    }
354
355    /**
356    * {@inheritDoc}
357    */
358    protected function _sql_report(string $mode, string $query = ''): void
359    {
360        switch ($mode)
361        {
362            case 'start':
363            break;
364
365            case 'fromcache':
366                $endtime = explode(' ', microtime());
367                $endtime = $endtime[0] + $endtime[1];
368
369                $result = @odbc_exec($this->db_connect_id, $query);
370                if ($result)
371                {
372                    while ($void = odbc_fetch_array($result))
373                    {
374                        // Take the time spent on parsing rows into account
375                    }
376                    odbc_free_result($result);
377                }
378
379                $splittime = explode(' ', microtime());
380                $splittime = $splittime[0] + $splittime[1];
381
382                $this->sql_report('record_fromcache', $query, $endtime, $splittime);
383
384            break;
385        }
386    }
387}