Repository navigation
Expand file tree
/
Copy pathuserdb.php
More file actions
202 lines (156 loc) · 4.98 KB
/
Copy pathuserdb.php
File metadata and controls
202 lines (156 loc) · 4.98 KB
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
<?php
////////////////////////////////////////////////////////////////
/*
DSN:
$dsn = array(
// internal DSN name
'id' => 'some_id_or_name',
// DB driver wrapper name
'driver' => 'mysql',
// DSN itself
'host' => 'ip',
'port' => 'port',
'username' => 'user',
'password' => 'pwd',
'dbname' => 'database_name',
);
DB driver wrapper class name:
class_name = 'dd_' + driver_name
*/
function createDBDriver($dsn) {
$ddclassname = 'dd_' . $dsn['driver'];
return new $ddclassname($dsn);
}
////////////////////////////////////////////////////////////////
// MySQL database driver
class dd_mysql {
// Create DB driver with given DSN
function __construct($dsn) {
$this->dsnid = $dsn['id'];
mysqli_report(MYSQLI_REPORT_STRICT); // set mysqli to use exceptions instead of warnings
try {
$this->ms = new mysqli($dsn['host'], $dsn['username'], $dsn['password'], $dsn['dbname'], $dsn['port']);
} catch (mysqli_sql_exception $e) {
throw new Exception("Couldn't connect to ds-".$dsn['id']." at ".$dsn['host'].":".$dsn['port']."/".$dsn['dbname'].".");
}
}
function __destruct() {
}
// Query methods
function queryOpen($query_string) {
$query = $this->ms->query($query_string);
return ($query === false) ? null : $query; // replace false (if any) with null
}
function queryClose(&$query) {
$query->close();
$query = null;
}
function fetchNextRecord($query) {
return $query->fetch_assoc();
}
// Simple Query - retrieve one record
function simpleQuery($query_string) {
$query = $this->ms->query($query_string);
if($query !== false) {
$result = $query->fetch_assoc();
$query->close();
return $result;
}
return null;
}
}
////////////////////////////////////////////////////////////////
// Main database
class MasterDB {
function __construct($dsn) {
$this->dd = createDBDriver($dsn);
$this->dsns = array();
}
function __destruct() {
}
// retrieve total user count
function getUserCount() {
$query_string = "SELECT count(`uid`) as `count` FROM `uids`";
$result = $this->dd->simpleQuery($query_string);
if(!$result) {
throw new Exception("Couldn't get user count.");
}
return (int) $result['count'];
}
// get all user ids as an array
function getUserIDList() {
$query_string = "SELECT `uid` FROM `uids`";
$query = $this->dd->queryOpen($query_string);
if (!$query) {
throw new Exception("Couldn't get userid list.");
}
$list = array();
while ($row = $this->dd->fetchNextRecord($query)) {
$list[] = $row['uid'];
}
$this->dd->queryClose($query);
return $list;
}
// get/create DB driver for data source where user data are stored
function getDBForUser($userid) {
$uid = (int) $userid;
$query_string = "SELECT `dsnid` FROM `uids` WHERE `uid`=$uid";
$result = $this->dd->simpleQuery($query_string);
if(!$result) {
throw new Exception("Couldn't get DSN id for user#$uid.");
}
$dsnid = $result['dsnid'];
if(isset($this->dsns[$dsnid]) == false) {
$query_string = "SELECT * FROM `dsns` WHERE `id`=$dsnid";
$result = $this->dd->simpleQuery($query_string);
if(!$result) {
throw new Exception("Couldn't get DSN for user#$uid.");
}
$this->dsns[$dsnid] = createDBDriver($result);
}
return $this->dsns[$dsnid];
}
}
function createDBMaster() {
$dsn_master = array(
'id' => 'master',
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'username' => 'root',
'password' => 'passwd',
'dbname' => 'testusers',
);
return new MasterDB($dsn_master);
}
////////////////////////////////////////////////////////////////
// entry
function printUser($dd, $userid) {
$uid = (int) $userid;
$query_string = "SELECT * FROM `users` WHERE `userid`=$uid";
$result = $dd->simpleQuery($query_string);
echo "User #$uid: " . $result['name'] . " " . $result['lastname'] . " (" . $result['dob'] . ")" . PHP_EOL;
}
try {
$mdb = createDBMaster();
$numUsers = $mdb->getUserCount();
echo "Total users: $numUsers" . PHP_EOL;
$useridList = $mdb->getUserIDList();
//var_dump($useridList);
foreach ($useridList as $userid) {
try {
$dd = $mdb->getDBForUser($userid);
if($dd) {
printUser($dd, $userid);
}
}
catch(Exception $e) {
echo '<ERROR>: ', $e->getMessage(), PHP_EOL;
}
}
}
catch(Exception $e) {
echo '<ERROR>: ', $e->getMessage(), PHP_EOL;
}
echo "----------------------------------------------------------------" . PHP_EOL;
?>