forked from FileZ/FileZ
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDb.php
More file actions
executable file
·143 lines (122 loc) · 3.96 KB
/
Db.php
File metadata and controls
executable file
·143 lines (122 loc) · 3.96 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
<?php
/**
* @file
* Short description.
*
* Long description.
*
* @package FileZ
*/
/**
* Fz_Db
*/
class Fz_Db {
protected static $_tables = array ();
/**
* Generic function to retrieve multiple rows and create objects
*
* @param string $sql
* @param string $className Class used for object instanciation (?)
* @param string $params Params used to prepare the query
* @param string $limit Limit the number of result (default: O = no limit)
*
* @return array Array of $className objects
*/
public static function findObjectsBySQL ($sql, $className = PDO::FETCH_OBJ, $params = array (), $limit = 0) {
$db = self::getConnection ();
if ($limit > 1)
$sql .= ' LIMIT '.$limit;
$result = array ();
$stmt = $db->prepare ($sql);
$stmt->execute ((array) $params);
while ($obj = $stmt->fetchObject ($className, array (true))) {
if (method_exists($obj, ('resetModifiedColumns')))
$obj->resetModifiedColumns ();
$result[] = $obj;
}
return ($limit == 1 ?
(count ($result) > 0 ? $result [0] : null) :
$result
);
}
/**
* Generic function to retrieve one row
*
* @param string $sql
* @param string $className Class used for object instanciation (?)
* @param string $params Params used to prepare the query
*
* @return object Object of clas $className
*/
public static function findObjectBySQL ($sql, $className = PDO::FETCH_OBJ, $params = array ()) {
return self::findObjectsBySQL ($sql, $className, $params, 1);
}
/**
* Generic function to retrieve multiple rows as an array
*
* @param string $sql
* @param string $className Class used for object instanciation (?)
* @param string $params Params used to prepare the query
* @param string $limit Limit the number of result (default: O = no limit)
*
* @return array Array of $className objects
*/
public static function findAssocBySQL ($sql, $params = array (), $limit = 0) {
$db = self::getConnection ();
if ($limit > 1)
$sql .= ' LIMIT '.$limit;
$result = array ();
$stmt = $db->prepare ($sql);
$stmt->execute ($params);
while ($row = $stmt->fetch (PDO::FETCH_ASSOC))
$result[] = $row;
return ($limit == 1 ?
(count ($result) > 0 ? $result [0] : null) :
$result
);
}
/**
* Return an instance of a table
*
* @param string $table
* @return object
*/
public static function getTable ($table) {
if (! array_key_exists($table, self::$_tables)) {
$dialect = fz_config_get('db', 'db_dialect', '');
$prefix = 'App_Model_DbTable_';
$tableClass = substr ($table, 0, strlen ($prefix)) == $prefix ?
$table : ($prefix.$table);
$tableClass = "$tableClass$dialect";
self::$_tables [$table] = new $tableClass ();
}
return self::$_tables [$table];
}
/**
* Helper for writing prepared queries
*
* @param string $x
* @return string
*/
public static function addColon ($x) { return ':' . $x; }
/**
* Helper for writing prepared queries
*
* @param string $x
* @return string
*/
public static function nameEqColonName ($x) { return $x . ' = :' . $x; }
/**
* Helper for writing prepared queries
*
* @param string $x
* @return string
*/
public static function nameEqSql (&$sql, $columnName) { $sql = $columnName . ' = ' . $sql; }
public static function getConnection () {
if (($conn = option ('db_conn')) !== null)
return $conn;
else
throw new Exception ('Unexisting database connection');
}
}