forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
206 lines (171 loc) · 6.18 KB
/
db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
$mysqli = 0;
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
function db_connect()
{
global $mysqli, $server, $username, $password, $database, $dbtest;
// ERROR CODES
// 1: success!
// 3: database settings are wrong
// 4: launch setup.php
// Lets try to connect
$mysqli = new mysqli($server, $username, $password, $database);
if ($mysqli->connect_error)
return 3;
else
{
if ($dbtest==true)
{
$result = db_query("SELECT count(table_schema) from information_schema.tables WHERE table_schema = '$database'");
$row = db_fetch_array($result);
if (!$row[0]) return 4;
}
return 1;
}
}
function db_query($query)
{
$ret = $GLOBALS['mysqli']->query($query);
if ($ret == false) {echo $GLOBALS['mysqli']->error;}
return $ret;
}
function db_fetch_array($result)
{
$ret = $result->fetch_array();
if ($ret == false) {echo $GLOBALS['mysqli']->error;}
return $ret;
}
function db_fetch_object($result)
{
$ret = $result->fetch_object();
if ($ret == false) {echo $GLOBALS['mysqli']->error;}
return $ret;
}
function db_num_rows($result)
{
return $result->num_rows;
}
function db_real_escape_string($string)
{
return $GLOBALS['mysqli']->real_escape_string($string);
}
function db_insert_id()
{
return $GLOBALS['mysqli']->insert_id;
}
function table_exists($tablename)
{
$result = db_query('SELECT DATABASE()');
$row = db_fetch_array($result);
$database = $row[0];
$result = db_query("
SELECT COUNT(*) AS count
FROM information_schema.tables
WHERE table_schema = '$database'
AND table_name = '$tablename'
");
$row = db_fetch_array($result);
return $row[0];
}
function field_exists($tablename,$field)
{
$field_exists = 0;
$result = db_query("SHOW COLUMNS FROM $tablename");
while( $row = db_fetch_array($result) ){
if ($row['Field']==$field) $field_exists = 1;
}
return $field_exists;
}
function db_schema_setup($schema)
{
$out = array();
while ($table = key($schema))
{
if (table_exists($table))
{
$out[] = array('Table',$table,"ok");
//-----------------------------------------------------
// Check table fields from schema
//-----------------------------------------------------
while ($field = key($schema[$table]))
{
$type = $schema[$table][$field]['type'];
if (isset($schema[$table][$field]['Null'])) $null = $schema[$table][$field]['Null']; else $null = "YES";
if (isset($schema[$table][$field]['Key'])) $key = $schema[$table][$field]['Key']; else $key = null;
if (isset($schema[$table][$field]['default'])) $default = $schema[$table][$field]['default']; else $default = null;
if (isset($schema[$table][$field]['Extra'])) $extra = $schema[$table][$field]['Extra']; else $extra = null;
if (field_exists($table, $field))
{
$out[] = array('field',$field,"ok");
}
else
{
$query = "ALTER TABLE `$table` ADD `$field` $type";
$out[] = array('field',$field,"added");
db_query($query);
}
$result = db_query("DESCRIBE $table `$field`");
$array = db_fetch_array($result);
$query = "";
$out_str = ""; // Not using this at the moment but good to break this out to the array
if ($array['Type']!=$type) { $out_str .= "Type: $type, "; $query .= ";"; }
if ($array['Default']!=$default) { $out_str .= "Default: $default, "; $query .= " Default '$default'"; }
if ($array['Null']!=$null && $null=="NO") { $out_str .= "Null: $null, "; $query .= " not null"; }
if ($array['Extra']!=$extra && $extra=="auto_increment") { $out_str .= "Extra: $extra"; $query .= " auto_increment"; }
if ($array['Key']!=$key && $key=="PRI") { $out_str .= "Key: $key, "; $query .= " primary key"; }
if ($query) $query = "ALTER TABLE $table MODIFY `$field` $type".$query;
if ($query) db_query($query);
next($schema[$table]);
}
}
else
{
//-----------------------------------------------------
// Create table from schema
//-----------------------------------------------------
$query = "CREATE TABLE " . $table . " (";
while ($field = key($schema[$table]))
{
$type = $schema[$table][$field]['type'];
if (isset($schema[$table][$field]['Null'])) $null = $schema[$table][$field]['Null']; else $null = "YES";
if (isset($schema[$table][$field]['Key'])) $key = $schema[$table][$field]['Key']; else $key = null;
if (isset($schema[$table][$field]['default'])) $default = $schema[$table][$field]['default']; else $default = null;
if (isset($schema[$table][$field]['Extra'])) $extra = $schema[$table][$field]['Extra']; else $extra = null;
$query .= '`'.$field.'`';
$query .= " $type";
if ($default) $query .= " Default '$default'";
if ($null=="NO") $query .= " not null";
if ($extra) $query .= " auto_increment";
if ($key) $query .= " primary key";
next($schema[$table]);
if (key($schema[$table]))
{
$query .= ", ";
}
}
$query .= ")";
$out[] = array('Table',$table,"created");
if ($query) db_query($query);
}
next($schema);
}
return $out;
}
?>