|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file has all the main functions in it that set up the database connection |
|
5
|
|
|
* and initializes the appropriate adapters. |
|
6
|
|
|
* |
|
7
|
|
|
* @package ElkArte Forum |
|
8
|
|
|
* @copyright ElkArte Forum contributors |
|
9
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file) |
|
10
|
|
|
* |
|
11
|
|
|
* @version 2.0 dev |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
use ElkArte\Database\AbstractTable; |
|
16
|
|
|
use ElkArte\Database\QueryInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Initialize database classes and connection. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $db_server server name |
|
22
|
|
|
* @param string $db_name database name like forum |
|
23
|
|
|
* @param string $db_user userid to attempt db connection |
|
24
|
|
|
* @param string $db_passwd password of user to attempt db connection |
|
25
|
|
|
* @param string $db_prefix prefix of the database, like elkarte_ |
|
26
|
|
|
* @param array $db_options |
|
27
|
|
|
* @param string $db_type |
|
28
|
|
|
* |
|
29
|
|
|
* @return QueryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql') |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
return database(false); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Retrieve existing instance of the active database class. |
|
38
|
|
|
* |
|
39
|
|
|
* @param bool $fatal - Stop the execution or throw an \Exception |
|
40
|
|
|
* @param bool $force - Force the re-creation of the database instance. |
|
41
|
|
|
* If set to true, from that moment onwards the old |
|
42
|
|
|
* instance will be lost and only the new one returned |
|
43
|
|
|
* |
|
44
|
|
|
* @return QueryInterface |
|
45
|
|
|
* @throws \Exception if fatal is false |
|
46
|
|
|
*/ |
|
47
|
387 |
|
function database($fatal = true, $force = false) |
|
48
|
|
|
{ |
|
49
|
387 |
|
static $db = null; |
|
50
|
|
|
|
|
51
|
1 |
|
if ($db === null || $force === true) |
|
52
|
1 |
|
{ |
|
53
|
|
|
global $db_persist, $db_server, $db_user, $db_passwd, $db_port; |
|
54
|
|
|
global $db_type, $db_name, $db_prefix, $mysql_set_mode; |
|
55
|
1 |
|
|
|
56
|
|
|
$db_options = [ |
|
57
|
1 |
|
'persist' => $db_persist, |
|
58
|
1 |
|
'select_db' => true, |
|
59
|
|
|
'port' => $db_port, |
|
60
|
1 |
|
'mysql_set_mode' => (bool) ($mysql_set_mode ?? false) |
|
61
|
1 |
|
]; |
|
62
|
1 |
|
$type = strtolower($db_type); |
|
63
|
|
|
$type = $type === 'mysql' ? 'mysqli' : $type; |
|
64
|
|
|
|
|
65
|
1 |
|
/** @var \ElkArte\Database\Mysqli\Connection $class */ |
|
66
|
|
|
$class = '\\ElkArte\\Database\\' . ucfirst($type) . '\\Connection'; |
|
67
|
|
|
try |
|
68
|
|
|
{ |
|
69
|
|
|
$db = $class::initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options); |
|
70
|
|
|
} |
|
71
|
|
|
catch (\Exception $e) |
|
72
|
|
|
{ |
|
73
|
|
|
if ($fatal === true) |
|
74
|
|
|
{ |
|
75
|
|
|
\ElkArte\Errors\Errors::instance()->display_db_error($e->getMessage()); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
else |
|
78
|
|
|
{ |
|
79
|
|
|
throw $e; |
|
80
|
387 |
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $db; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* This function retrieves an existing instance of \ElkArte\AbstractTable |
|
89
|
|
|
* and returns it. |
|
90
|
|
|
* |
|
91
|
|
|
* @param object|null $db - A database object (e.g. \ElkArte\Mysqli\Query) |
|
92
|
|
|
* @param bool $fatal - Stop the execution or throw an \Exception |
|
93
|
|
|
* |
|
94
|
|
|
* @return AbstractTable |
|
95
|
3 |
|
*/ |
|
96
|
3 |
|
function db_table($db = null, $fatal = false) |
|
97
|
|
|
{ |
|
98
|
3 |
|
global $db_prefix, $db_type; |
|
99
|
|
|
static $db_table = null; |
|
100
|
1 |
|
|
|
101
|
|
|
if ($db_table === null) |
|
102
|
1 |
|
{ |
|
103
|
|
|
if ($db === null) |
|
104
|
1 |
|
{ |
|
105
|
1 |
|
$db = database(); |
|
106
|
1 |
|
} |
|
107
|
|
|
$db_type = strtolower($db_type); |
|
108
|
|
|
$db_type = $db_type === 'mysql' ? 'mysqli' : $db_type; |
|
109
|
1 |
|
$class = '\\ElkArte\\Database\\' . ucfirst($db_type) . '\\Table'; |
|
110
|
|
|
try |
|
111
|
|
|
{ |
|
112
|
|
|
$db_table = new $class($db, $db_prefix); |
|
113
|
|
|
} |
|
114
|
|
|
catch (\Exception $e) |
|
115
|
|
|
{ |
|
116
|
|
|
if ($fatal === true) |
|
117
|
|
|
{ |
|
118
|
|
|
\ElkArte\Errors\Errors::instance()->display_db_error($e->getMessage()); |
|
119
|
|
|
} |
|
120
|
|
|
else |
|
121
|
|
|
{ |
|
122
|
|
|
throw $e; |
|
123
|
|
|
} |
|
124
|
3 |
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $db_table; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* This function returns an instance of \ElkArte\AbstractSearch, |
|
132
|
|
|
* specifically designed for database utilities related to search. |
|
133
|
|
|
* |
|
134
|
|
|
* @return \ElkArte\Database\AbstractSearch |
|
135
|
|
|
*/ |
|
136
|
30 |
|
function db_search() |
|
137
|
30 |
|
{ |
|
138
|
|
|
global $db_type; |
|
139
|
30 |
|
static $db_search = null; |
|
140
|
|
|
|
|
141
|
|
|
if ($db_search === null) |
|
142
|
|
|
{ |
|
143
|
|
|
$db = database(); |
|
144
|
|
|
$db_type = strtolower($db_type); |
|
145
|
|
|
$db_type = $db_type === 'mysql' ? 'mysqli' : $db_type; |
|
146
|
|
|
$class = '\\ElkArte\\Database\\' . ucfirst($db_type) . '\\Search'; |
|
147
|
|
|
try |
|
148
|
|
|
{ |
|
149
|
|
|
$db_search = new $class($db); |
|
150
|
|
|
} |
|
151
|
|
|
catch (\Exception $e) |
|
152
|
|
|
{ |
|
153
|
|
|
\ElkArte\Errors\Errors::instance()->display_db_error($e->getMessage()); |
|
154
|
|
|
} |
|
155
|
30 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $db_search; |
|
158
|
|
|
} |
|
159
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.