Passed
Pull Request — master (#1571)
by Michael
10:52
created

XoopsDatabase::exec()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Abstract base class for XOOPS Database access classes
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2025 XOOPS Project (https://xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @subpackage          database
16
 * @since               1.0.0
17
 * @author              Kazumi Ono <[email protected]>
18
 */
19
20
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21
22
/**
23
 * make sure this is only included once!
24
 */
25
if (defined('XOOPS_C_DATABASE_INCLUDED')) {
26
    return null;
27
}
28
29
define('XOOPS_C_DATABASE_INCLUDED', 1);
30
31
/**
32
 * Abstract base class for Database access classes
33
 *
34
 * @abstract
35
 * @author     Kazumi Ono <[email protected]>
36
 * @package    kernel
37
 * @subpackage database
38
 */
39
abstract class XoopsDatabase
40
{
41
    /**
42
     * Prefix for tables in the database
43
     *
44
     * @var string
45
     */
46
    public $prefix = '';
47
48
    /**
49
     * reference to a {@link XoopsLogger} object
50
     *
51
     * @see XoopsLogger
52
     * @var object XoopsLogger
53
     */
54
    public $logger;
55
56
    /**
57
     * If statements that modify the database are selected
58
     *
59
     * @var boolean
60
     */
61
    public $allowWebChanges = false;
62
63
    /**
64
     * XoopsDatabase constructor.
65
     */
66
    public function __construct()
67
    {
68
        // exit('Cannot instantiate this class directly');
69
    }
70
71
    /**
72
     * assign a {@link XoopsLogger} object to the database
73
     *
74
     * @see XoopsLogger
75
     * @param XoopsLogger $logger reference to a {@link XoopsLogger} object
76
     */
77
78
    public function setLogger(XoopsLogger $logger)
79
    {
80
        $this->logger = &$logger;
81
    }
82
83
    /**
84
     * set the prefix for tables in the database
85
     *
86
     * @param string $value table prefix
87
     */
88
    public function setPrefix($value)
89
    {
90
        $this->prefix = $value;
91
    }
92
93
    /**
94
     * attach the prefix.'_' to a given tablename
95
     *
96
     * if tablename is empty, only prefix will be returned
97
     *
98
     * @param  string $tablename tablename
99
     * @return string prefixed tablename, just prefix if tablename is empty
100
     */
101
    public function prefix($tablename = '')
102
    {
103
        if ($tablename != '') {
104
            return $this->prefix . '_' . $tablename;
105
        } else {
106
            return $this->prefix;
107
        }
108
    }
109
110
    /**
111
     * Test the passed result to determine if it is a valid result set
112
     *
113
     * @param mixed $result value to test
114
     *
115
     * @return bool true if $result is a database result set, otherwise false
116
     */
117
    abstract public function isResultSet($result);
118
119
    /**
120
     * Return a human-readable description of the last DB error.
121
     * Subclasses must override to provide engine-specific details.
122
     *
123
     * @return string Error message or empty string if no error.
124
     */
125
    abstract public function error();
126
127
    /**
128
     * Return an engine-specific error code for the last DB error.
129
     * Subclasses must override to provide engine-specific details.
130
     *
131
     * @return int Error code (e.g., MySQL errno) or 0 if no error.
132
     */
133
    abstract public function errno();
134
135
    /**
136
     * Executes a SELECT-like statement and returns a result set handle.
137
     * Implementations may append LIMIT/OFFSET if provided.
138
     *
139
     * @return mixed A driver result object/resource, or false on failure.
140
     */
141
    abstract public function query(string $sql, int $limit = 0, int $start = 0);
142
143
    /**
144
     * Executes a mutating statement (INSERT/UPDATE/DELETE/DDL).
145
     * Historically named "queryF" in XOOPS.
146
     * @deprecated Use exec() for mutating statements.
147
     */
148
    abstract public function queryF(string $sql);
149
150
    /**
151
     * Modern alias for mutating statements. Defaults to queryF() for BC.
152
     */
153
    public function exec(string $sql)
154
    {
155
        // Route to legacy method for BC.
156
        return $this->queryF($sql);
0 ignored issues
show
Deprecated Code introduced by
The function XoopsDatabase::queryF() has been deprecated: Use exec() for mutating statements. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

156
        return /** @scrutinizer ignore-deprecated */ $this->queryF($sql);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
157
    }
158
}
159
160
/**
161
 * Only for backward compatibility
162
 *
163
 * @deprecated
164
 */
165
class Database
166
{
167
    /**
168
     * @return object
169
     */
170
    public function getInstance()
171
    {
172
        if (is_object($GLOBALS['xoopsLogger'])) {
173
            $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . " is deprecated since XOOPS 2.5.4, please use 'XoopsDatabaseFactory::getDatabaseConnection();' instead.");
174
        }
175
        $inst = XoopsDatabaseFactory::getDatabaseConnection();
176
177
        return $inst;
178
    }
179
}
180