This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmysqldatabase.php
653 lines (570 loc) · 18.7 KB
/
mysqldatabase.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<?php
/**
* @package mysql-database
*/
/**
* MySQL Database
*
* A singleton object which provides convenience methods for interfacing with
* a MySQL database in PHP 5. You can get the object's instance using the
* static {@link getInstance()} method. Being a singleton object, this class
* only supports one open database connection at a time and idealy suited to
* single-threaded applications. You can read
* about {@link http://php.net/manual/en/language.oop5.patterns.php the singleton
* pattern in the PHP manual}.
*
* <b>Getting Started</b>
* <code>
* $db = MySqlDatabase::getInstance();
*
* try {
* $db->connect('localhost', 'user', 'password', 'database_name');
* }
* catch (Exception $e) {
* die($e->getMessage());
* }
* </code>
*
* @package mysql-database
* @author Micah Carrick
* @copyright (c) 2010 - Micah Carrick
* @version 2.0
* @license BSD
*/
class MySqlDatabase
{
/**
* The MySQL link identifier created by {@link connect()}
*
* @var resource
*/
public $link;
/**
* @var string
*/
private $conn_str;
/**
* @var MySqlDatabase
*/
private static $instance;
const MYSQL_DATE_FORMAT = 'Y-m-d';
const MYSQL_TIME_FORMAT = 'H:i:s';
const MYSQL_DATETIME_FORMAT = 'Y-m-d H:i:s';
const INSERT_GET_AUTO_INCREMENT_ID = 1;
const INSERT_GET_AFFECTED_ROWS = 2;
/**
* Constructor
*
* Private constructor as part of the singleton pattern implementation.
*/
private function __construct() {}
/**
* Connect
*
* Establish a connection to a MySQL database. Returns the MySQL link
* link identifier or throws an exception if there is an error.
*
* <code>
* // get an instance of the Database singleton
* $db = MySqlDatabase::getInstance();
*
* // connect to a MySQL database (use your own login information)
* try {
* $db->connect('localhost', 'user', 'password', 'database_name');
* }
* catch (Exception $e) {
* die($e->getMessage());
* }
* </code>
*
* @param string
* @param string
* @param string
* @param string
* @param boolean
* @return resource
*/
public function connect($host, $user, $password, $database=false, $persistant=false)
{
if ($persistant) {
$this->link = @mysql_pconnect($host, $user, $password);
} else {
$this->link = @mysql_connect($host, $user, $password);
}
if (!$this->link)
{
throw new Exception('Unable to establish database connection: '
.mysql_error());
}
if ($database) $this->useDatabase($database);
$version = mysql_get_server_info();
$this->conn_str = "'$database' on '$user@$host' (MySQL $version)";
return $this->link;
}
/**
* Delete
*
* Executes the DELETE statement specified in the query and returns the
* value from either the PHP {@link mysql_affected_rows()} function. Throws
* and exception if there is a MySQL error in the query.
*
* Note: With MySQL versions prior to 4.1.2, the affected rows on DELETE
* statements with no WHERE clause is 0. See {@link mysql_affected_rows()}
* for more information.
*
* @param string
* @return integer
*/
public function delete($query)
{
return $this->updateOrDelete($query);
}
/**
* Get Connection String
*
* Gets a string representing the connection.
*
* <code>
* echo $db->getConnectionString();
* // example output: 'test_database' on 'web_user@localhost' (MySQL 5.1.47)
* </code>
*
* @return string
*/
public function getConnectionString()
{
return $this->conn_str;
}
/**
* Get Instance
*
* Gets the singleton instance for this object. This method should be called
* statically in order to use the Database object:
*
* <code>
* $db = MySqlDatabase::getInstance();
* </code>
*
* @return MySqlDatabase
*/
public static function getInstance()
{
if (!isset(self::$instance))
{
self::$instance = new MySqlDatabase();
}
return self::$instance;
}
/**
* Fetch One From Each Row
*
* Convenience method to get a single value from every row in a given
* query. This is usefull in situations where you know that the result will
* only have only one column of data and you need that all in a simple
* array.
*
* <code>
*
* $query = "SELECT name FROM users";
* $names = $db->fetchOneFromEachRow($query);
* echo 'Users: ' . implode(', ', $names);
* </code>
*
* @param string
* @return array
*/
public function fetchOneFromEachRow($query)
{
$rval = array();
foreach ($this->iterate($query, MySqlResultSet::DATA_NUMERIC_ARRAY) as $row) {
$rval[] = $row[0];
}
return $rval;
}
/**
* Fetch One Row
*
* Convenience method to get a single row from a given query. This is
* usefull in situations where you know that the result will only contain
* one record and therefore do not need to iterate over it.
*
* You can
* optionally specify the type of data to be returned (object or array)
* using one of the MySqlResultSet Data Constants. The default is
* {@link MySqlResultSet::DATA_OBJECT}.
*
* <code>
* // get one row of data
* $query = "SELECT first, last FROM users WHERE user_id = 24 LIMIT 1";
* $row = $db->fetchOneRow($query);
* echo $row->foo;
* echo $row->bar;
* </code>
*
* @param string
* @param integer
* @return mixed
*/
public function fetchOneRow($query, $data_type=MySqlResultSet::DATA_OBJECT)
{
$result = new MySqlResultSet($query, $data_type, $this->link);
$result->rewind();
$row = $result->current();
return $row;
}
/**
* Fetch One
*
* Convenience method to get a single value from a single row. Returns the
* value if the query returned a record, false if there were no results, or
* throws an exception if there was an error with the query.
*
* <code>
* // get the number of records in the 'users' table
* $count = $db->fetchOne("SELECT COUNT(*) FROM users");
* </code>
*
* @param string
* @return mixed
*/
public function fetchOne($query)
{
$result = new MySqlResultSet($query, MySqlResultSet::DATA_NUMERIC_ARRAY,
$this->link);
$result->rewind();
$row = $result->current();
if (!$row) return false;
else return $row[0];
}
/**
* Import SQL File
*
* Runs the queries defined in an SQL script file. The double-hyphen style
* comments must have a single space after the hyphens. Hash style comments
* and C-style comments are also supported.
*
* An optional user callback function can be specified to get information
* about each MySQL statement. The user callback function takes 3
* parameters: the line number as an integer, the query as a string, and the
* result of the query as a boolean.
*
* <code>
* function import_sql_callback($line_number, $sql_query, $result)
* {
* echo "Line $line_number: $sql_query ";
* if ($result) echo "(OK)<br/>";
* else echo "(FAIL)<br/>";
* }
* </code>
*
* You can optionally specify whether or not to abort importing statements
* when an SQL error occurs (defaults to 'true') in which case an exception
* will be thrown for any MySQL error.
*
* Returns the number of queries executed from the script or throws an
* exception if there is an error.
*
* <code>
* // no callback, throw exception on MySQL errors
* $number = $db->importSqlFile('queries.sql');
*
* // callback for each query, skip queries with MySQL errors
* $number = $db->importSqlFile('queries.sql', 'import_sql_callback', false);
* </code>
*
* TODO: Ensure this works with huge files. Might need to use fopen()
*
* @param string
* @param string
* @param boolean
* @return integer
*/
public function importSqlFile($filename, $callback=false, $abort_on_error=true)
{
if ($callback && !is_callable($callback)) {
throw new Exception("Invalid callback function.");
}
$lines = $this->loadFile($filename);
$num_queries = 0;
$sql_line = 0;
$sql = '';
$in_comment = false;
foreach ($lines as $num => $line) {
$line = trim($line);
$num++;
if (empty($sql)) $sql_line = $num;
// ignore comments
if ($in_comment) {
$comment = strpos($line, '*/');
if ($comment !== false) {
$in_comment = false;
$line = substr($line, $comment+2);
} else {
continue;
}
} else {
$comment = strpos($line, '/*');
if ($comment !== false) {
if (strpos($line, '*/') === false) {
$in_comment = true;
}
$line = substr($line, 0, $comment);
} else {
// single line comments
foreach (array('-- ', '#') as $chars) {
$comment = strpos($line, $chars);
if ($comment !== false) {
$line = substr($line, 0, $comment);
}
}
}
}
// check if the statement is ready to be queried
$end = strpos($line, ';');
if ($end === false) {
$sql .= $line;
} else {
$sql .= substr($line, 0, $end);
$result = $this->quickQuery($sql);
$num_queries++;
if (!$result && $abort_on_error) {
$file = basename($filename);
$error = mysql_error($this->link);
throw new Exception("Error in $file on line $sql_line: $error");
}
if ($callback) {
call_user_func($callback, $sql_line, $sql, $result);
}
$sql = ''; // clear for next statement
}
}
return $num_queries;
}
/**
* Is Connected
*
* Determines if there is a connection open to the database.
*
* @return boolean
*/
public function isConnected()
{
if (!empty($this->link)) {
return @mysql_ping($this->link);
} else {
return false;
}
}
// insertPhpArray
// insertSqlArray
// sqlval()
/**
* Insert
*
* Executes the INSERT statement specified in the query and returns the
* value from either the PHP {@link mysql_insert_id()} function or the
* php {@link mysql_affected_rows()} function depending on the value of the
* $return_type parameter.
*
* <code>
* $db = MySqlDatabase::getInstance();
* $query = "INSERT INTO foobar (col1, col2) VALUES (1, 2), (2, 3)";
* $rows = $db->insert($query, MySqlDatabase::INSERT_GET_AFFECTED_ROWS);
* echo $rows; // output: 2
* </code>
*
*
* @param string
* @param integer
* @return integer
*/
public function insert($query, $r_type=MySqlDatabase::INSERT_GET_AUTO_INCREMENT_ID)
{
$r = $this->query($query);
if ($r_type == MySqlDatabase::INSERT_GET_AFFECTED_ROWS) {
return @mysql_affected_rows($this->link);
} else {
return @mysql_insert_id($this->link);
}
}
/**
* DO NOT USE
*
* This was never finished... I don't think. The goal was to take a table
* name, an array of column names, and an array of values and generate a
* multiple record insert. You should not use this, but, you could help
* out and finish or rewrite this method.
*
*
* @param deprecated
*/
public function smartInsert($table, $columns, $values)
{
if (empty($table) || !is_string($table)) {
throw new Exception('The $table parameter must be specified as a string.');
}
$table_sql = '`' . @mysql_real_escape_string($table) . '`';
$query = "INSERT INTO $table_sql ";
// columns
if (is_string($columns)) {
$columns = explode(',', $columns);
}
if (is_array($columns)) {
foreach ($columns as &$col) {
if (!is_string($col)) {
throw new Exception('The $columns parameter must be a string or an array of strings');
}
$col = @mysql_real_escape_string($col);
}
$column_sql = implode(',', $columns);
$column_count = count($columns);
} else {
throw new Exception('The $columns parameter must be a string or an array of strings.');
}
try {
$column_info = array();
foreach ($this->iterate("SHOW COLUMNS FROM $table_sql") as $row) {
$column_info[] = $row;
}
}
catch (Exception $e) {
throw new Exception("Could not get column information for table $table_sql.");
}
$query .= "($column_sql) ";
// values
if (is_array($values)) {
for ($i=0; $i < count($values); $i++) {
$info = $column_info[$i];
$value = $values[i];
// Where the heck did I leave off?
}
} else {
// TODO: if only 1 column, then this will work
throw new Exception('The $values parameter must be a string or an array.');
}
if (isset($column_count) && $column_count <> $value_count) {
throw new Exception("Column count ($column_count) does not match values count ($value_count).");
}
$query .= "VALUES ($value_sql) ";
echo $query;
}
/**
* Iterate Result Set
*
* Returns a {@link MySQL_ResultSet} iteratable object for a query. The $type
* parameter indicates the data being iterated should be an object,
* a numerically indexed array, an associative array, or an array with
* both numeric and associative indexes. Defaults to objects.
*
* <code>
* $sql_query = "SELECT col1, col2 FROM table";
*
* // iterate as objects
* foreach ($db->iterate("SELECT col1, col2 FROM table") as $row) {
* echo $row->col1 . '<br/>';
* echo $row->col2 . '<br/>';
* }
*
* // iterate as both associative and numerically indexed array
* foreach ($db->iterate($sql_query, MySQL_Db::DATA_ARRAY) as $row) {
* echo $row[0] . '<br/>';
* echo $row['col1'] . '<br/>';
* }
* </code>
*
* @param string
* @param integer
* @return boolean
*/
public function iterate($sql, $data_type=MySqlResultSet::DATA_OBJECT)
{
return new MySqlResultSet($sql, $data_type, $this->link);
}
/**
* Load File
*
* Loads the specified filename into an array of lines. Throws an exception
* if there is an error.
*
* @param string
* @return boolean
*/
private function loadFile($filename)
{
if (!file_exists($filename)) {
throw new Exception("File does not exist: $filename");
}
$file = @file($filename, FILE_IGNORE_NEW_LINES);
if (!$file) {
throw new Exception("Could not open $filename");
}
return $file;
}
public function query($query)
{
$r = @mysql_query($query, $this->link);
if (!$r) {
throw new Exception("Query Error: " . mysql_error());
}
return $r;
}
/**
* Quick Query
*
* Executes a MySQL query and returns a boolean value indicating success
* or failure. This method will close any resources opened from
* SELECT, SHOW, DESCRIBE, or EXPLAIN statements and would not be very
* usefull for those types of queries. This method is used internally for
* importing SQL scripts.
*
* @param string
* @return boolean
*/
public function quickQuery($query)
{
$r = @mysql_query($query, $this->link);
if (!$r) return false;
if (is_resource($r)) mysql_free_result($r);
return true;
}
/**
* Update
*
* Executes the UPDATE statement specified in the query and returns the
* value from either the PHP {@link mysql_affected_rows()} function. Throws
* and exception if there is a MySQL error in the query.
*
* Note: The number of rows affected include only those in which the new
* value was not the same as the old value. See {@link mysql_affected_rows()}
* for more information.
*
* @param string
* @return integer
*/
public function update($query)
{
return $this->updateOrDelete($query);
}
private function updateOrDelete($query)
{
$r = $this->query($query);
return @mysql_affected_rows($this->link);
}
/**
* Use Database
*
* Selects the database to use. Throws an exception if there is an error
* using the specified database.
*
* @param string
* @return integer
*/
public function useDatabase($database)
{
if (!@mysql_select_db($database, $this->link))
{
throw new Exception('Unable to select database: ' . mysql_error($this->link));
}
}
}
?>