-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database_pdo.class.php
138 lines (116 loc) · 2.88 KB
/
Database_pdo.class.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
<?php
function getMyDatabase()
{
$dbpath = dirname(__FILE__).'/tweets.db';
try {
$pdo = new PDO('sqlite:'.$dbpath);
} catch (PDOException $e) {
print('Error: '.$e->getMessage());
die();
}
return $pdo;
}
abstract class Database_pdo {
protected static $pdo = null;
protected $has_record = false;
abstract protected function getTableName();
protected function getDatabase()
{
if (!self::$pdo)
{
self::$pdo = getMyDatabase();
}
return self::$pdo;
}
private function checkExistence()
{
if (!$this->id) return false;
return self::st_fetchOne('SELECT count(*) FROM '.$this->getTableName().' WHERE id='.$this->id);
}
abstract protected function bindValues($stmt); // for SQLite3
abstract protected function getUpdateSQL();
abstract protected function getCreateSQL();
protected function execute($sql)
{
$stmt = $this->getDatabase()->prepare($sql);
if (!$stmt) {
$error_info = $this->getDatabase()->errorInfo();
//printf("execute('%s')... => %s\n", $sql, $error_info[2]);
return null;
}
$this->bindValues($stmt);
$result = $stmt->execute();
$error_info = $this->getDatabase()->errorInfo();
//printf("execute('%s')... => %s\n", $sql, $error_info[2]);
return $stmt->fetchAll();
}
protected function fetchOne($sql)
{
$stmt = $this->getDatabase()->prepare($sql);
$this->bindValues($stmt);
$result = $stmt->execute();
$r = $stmt->fetch(PDO::FETCH_NUM);
return $r[0];
}
protected function update()
{
$this->execute($this->getUpdateSQL());
}
protected function create()
{
$this->execute($this->getCreateSQL());
if (null === $this->id)
{
$this->id = $this->getDatabase()->lastInsertId();
}
$this->has_record = true;
}
public function save()
{
if ($this->has_record || $this->checkExistence())
{
$this->update();
}
else
{
$this->create();
}
$this->has_record = true;
}
public function truncate()
{
$this->execute('DELETE FROM '.$this->getTableName());
}
// static version
protected static function st_getDatabase()
{
if (!self::$pdo)
{
self::$pdo = getMyDatabase();
}
return self::$pdo;
}
public static function st_execute($sql) // no bindValues
{
$stmt = self::st_getDatabase()->prepare($sql);
$result = $stmt->execute();
return $stmt->fetchAll();
}
protected static function st_fetch($sql)
{
$stmt = self::st_getDatabase()->prepare($sql);
$result = $stmt->execute();
return $stmt->fetch(PDO::FETCH_NUM);
}
protected static function st_fetchOne($sql)
{
$stmt = self::st_getDatabase()->prepare($sql);
$result = $stmt->execute();
$r = $stmt->fetch(PDO::FETCH_NUM);
return $r[0];
}
# public static function maxId()
# {
# return self::st_fetchOne('SELECT max(id) FROM '. self::getTableName());
# }
}