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
|
<?php
/**
* Handle communication with the database
* This is a very thin layer between you and PDO.
*/
class Database
{
/**
*
* @var \PDO Database handle
*/
private static $dbh = false;
private static $statements = array();
/**
* Get database schema version - used for checking for updates
* @return int Version of db schema
*/
public static function getExpectedSchemaVersion()
{
return 9;
}
public static function needSchemaUpdate()
{
return Property::getCurrentSchemaVersion() < self::getExpectedSchemaVersion();
}
/**
* Connect to the DB if not already connected.
*/
private static function init()
{
if (self::$dbh !== false)
return;
try {
if (CONFIG_SQL_FORCE_UTF8)
self::$dbh = new PDO(CONFIG_SQL_DSN, CONFIG_SQL_USER, CONFIG_SQL_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
else
self::$dbh = new PDO(CONFIG_SQL_DSN, CONFIG_SQL_USER, CONFIG_SQL_PASS);
} catch (PDOException $e) {
Util::traceError('Connecting to the local database failed: ' . $e->getMessage());
}
}
/**
* If you just need the first row of a query you can use this.
*
* @return array|boolean Associative array representing row, or false if no row matches the query
*/
public static function queryFirst($query, $args = array(), $ignoreError = false)
{
$res = self::simpleQuery($query, $args, $ignoreError);
if ($res === false)
return false;
return $res->fetch(PDO::FETCH_ASSOC);
}
/**
* Execute the given query and return the number of rows affected.
* Mostly useful for UPDATEs or INSERTs
*
* @param string $query Query to run
* @param array $args Arguments to query
* @param boolean $ignoreError Ignore query errors and just return false
* @return int|boolean Number of rows affected, or false on error
*/
public static function exec($query, $args = array(), $ignoreError = false)
{
$res = self::simpleQuery($query, $args, $ignoreError);
if ($res === false)
return false;
return $res->rowCount();
}
/**
* Get id (promary key) of last row inserted.
*
* @return int the id
*/
public static function lastInsertId()
{
return self::$dbh->lastInsertId();
}
/**
* Execute the given query and return the corresponding PDOStatement object
* Note that this will re-use PDOStatements, so if you run the same
* query again with different params, do not rely on the first PDOStatement
* still being valid. If you need to do something fancy, use Database::prepare
* @return \PDOStatement The query result object
*/
public static function simpleQuery($query, $args = array(), $ignoreError = false)
{
self::init();
try {
if (!isset(self::$statements[$query])) {
self::$statements[$query] = self::$dbh->prepare($query);
} else {
self::$statements[$query]->closeCursor();
}
if (self::$statements[$query]->execute($args) === false) {
if ($ignoreError)
return false;
Util::traceError("Database Error: \n" . implode("\n", self::$statements[$query]->errorInfo()));
}
return self::$statements[$query];
} catch (Exception $e) {
if ($ignoreError)
return false;
Util::traceError("Database Error: \n" . $e->getMessage());
}
}
/**
* Simply calls PDO::prepare and returns the PDOStatement.
* You must call PDOStatement::execute manually on it.
*/
public static function prepare($query)
{
self:init();
return self::$dbh->prepare($query);
}
}
|