-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_rest_db.php
More file actions
executable file
·106 lines (87 loc) · 3.18 KB
/
php_rest_db.php
File metadata and controls
executable file
·106 lines (87 loc) · 3.18 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
<?php
// Facilitates communication with the DB
class DB {
const MYSQL_DATE_FORMAT = 'Y-m-d H:i:s';
private static $db_data_source = null;
private static $db_user = null;
private static $db_pass = null;
private static $type_key = 'type';
private static $pdo;
// Configures a connection attributes
public static function configure($data_source, $username, $password) {
self::$db_data_source = $data_source;
self::$db_user = $username;
self::$db_pass = $password;
}
// Sets the type key used inside queryForArray as key to denote the result type
public static function setTypeKey($type_key) {
self::$type_key = $type_key;
}
// Runs a DB query and converts the result into associative array (serializable to JSON)
// $type - name of the type (may be null)
// $query - query to execute (with ?) for positional parametrs e.g. 'SELECT * FROM table WHERE column > ?'
// $args - array of query arguments e.g. array('col value 1', 'col value 2')
public static function queryForArray($type, $query, $args) {
try {
// Log
if ($type != null) {
Output::dumpTrace("==> Executing Query for ", $type, ": ", $query, " with ", $args );
} else {
Output::dumpTrace("==> Executing Query ", $query, " with ", $args );
}
// Get pdo
$pdo = self::getPdo();
// Prepare & Execute statement
$stmt = $pdo->prepare($query);
$stmt->execute( $args );
// Fetch result
$result = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// Add type information
if ($type != null) {
$row = array(self::$type_key => $type) + $row;
}
// Add row data to result
$result[] = $row;
}
// Dump and return result
Output::dumpVar($result);
return $result;
} catch(Exception $e) {
// Convert DB exception
throw new InternalErrorException('Chyba DB: ' . $e->getMessage());
}
}
// Executes a DB statenent into the DB (INSERT/UPDATE/DELETE).
// $query - query to execute (with ?) for positional parametrs e.g. 'DELETE FROM table WHERE column > ?'
// $args - array of query arguments e.g. array('col value 1', 'col value 2')
public static function executeStatement($query, $args) {
try {
// Get pdo
$pdo = self::getPdo();
// Prepare & Execute statement
Output::dumpTrace("==> Executing Statement ", $query, " with ", $args );
$stmt = $pdo->prepare($query);
$stmt->execute( $args );
// Return number of affected rows
Output::dumpTrace("==> Affected rows ", $stmt->rowCount());
return $stmt->rowCount();
} catch(Exception $e) {
// Finally - Close connection
throw new InternalErrorException('Chyba DB: ' . $e->getMessage());
}
}
// Return an instance of PDO
private static function getPdo() {
if (self::$pdo == null) {
Output::dumpTrace("==> Creating PDO source=", self::$db_data_source, " , user=", self::$db_user, self::$db_pass == null ? ", password=NO" : ", password=YES" );
self::$pdo = new PDO(self::$db_data_source, self::$db_user, self::$db_pass,
array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
}
return self::$pdo;
}
}
?>