-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
206 lines (184 loc) · 5.51 KB
/
db.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
<?php
// AUTHOR: Lasse Pihlainen
// CREATED: 22.10.2017
// Edited: 23.10.2017, 20.12.2017, 27.2.2018, 17.11.2019
// LICENSE: BSD
class apidb /*extends SQLite3*/ {
protected $dbpath;
protected $table = '';
private $createTable = 0; // if new tables should be created
protected $db;
protected $DEBUG = 1;
protected $date;
protected $debuglines = array();
public function __construct($arg = null) {
if ($arg == null) {
//$this->pe('no arg given');
return;
}
$this->dbpath = dirname(__FILE__).'/'.$arg.'.db';
$this->pi($this->dbpath);
$this->date = date("H:i:s");
$this->createDBConnection();
}
/**
* TODO: return false on fail.
*/
private function createDBConnection() {
$this->db = null;
if ($this->createTable != 1 && !file_exists($this->dbpath)) return;
$this->db = new PDO("sqlite:$this->dbpath");
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return true;
}
public function printMessages() {
print_r($this->debuglines);
}
# print errors
public function pe($arg = null) {
if ($arg == null || $this->DEBUG != 1) return;
echo "[ERROR $this->date] ".$arg . PHP_EOL;
$this->debuglines[] = "[ERROR $this->date] ".$arg. PHP_EOL;
}
# print info
public function pi($arg = null) {
if ($arg == null || $this->DEBUG != 1) return;
echo "[INFO $this->date] ".$arg . PHP_EOL;
$this->debuglines[] = "[INFO $this->date] ".$arg . PHP_EOL;
}
# debug print ARRAY
public function pa($arg = null, $title = false) {
if ($arg == null || $this->DEBUG != 1) return;
echo "[ARRAY $this->date $title] ";
print_r($arg);
//echo "<br><br>\n";
$this->debuglines[] = "[ARRAY $this->date $title] ".print_r($arg, true). PHP_EOL;
}
# create new database for our project
private function createDB () {
if ($this->createTable != 1) return;
$this->pi("Creating Database for Korvamato");
$sqlString = "CREATE VIRTUAL TABLE korvamadot using FTS4(NICK,PVM,QUOTE,INFO1,INFO2,CHANNEL,ARTIST,TITLE,LINK1,LINK2,DELETED);";
$ret = 0;
try {
$ret = $this->db->exec($sqlString);
} catch(PDOException $e) {
$this->pe("PDO exception: ".$e->getMessage());
} catch(Exception $e) {
$this->pe("Exception: " .$e->getMessage());
}
return $ret;
}
public function disco() {
$this->db = null;
}
# insert line into Database
public function insertIntoDB($sqlString = null, $params = null) {
if ($sqlString === null) return false;
$this->pi(__FUNCTION__.": sqlString: " .$sqlString);
try {
if ($pdostmt = $this->db->prepare($sqlString)) {
if ($pdostmt->execute($params)) {
$this->db = null;
//return $pdostmt->lastInsertRowID();
return true;
} else {
$this->pe(__FUNCTION__.": ERROR.. $sqlString");
#$this->pa($pdostmt);
}
} else {
$this->pe(__FUNCTION__.": prepare statement error.");//: ".$pdostmt->errorInfo);
}
} catch(PDOException $e) {
$this->pe(__FUNCTION__.": PDOException: ".print_r($e,1));
} catch(EXCeption $e) {
$this->pe(__FUNCTION__.": Exception: ".$e);
}
$this->db = null;
return false;
}
public function getResultsFromDBQuery($querystr = null) {
if ($querystr === null) return -1;
//$this->db = new PDO("sqlite:$this->dbpath");
try {
if ($pdostmt = $this->db->prepare($querystr)) {
if ($pdostmt->execute()) {
$results = $pdostmt->fetchAll();
$this->db = null;
return $results;
}
}
$this->pe("Prepare didn't match!");
} catch(PDOException $e) {
$this->pe("getResultsFromDBQuery: ".$e);
} catch(Exception $e) {
$this->pe("getResultsFromDBQuery: ".$e);
}
$this->db = null;
return -2;
}
public function bindSQL($query = false, $params = false) {
if ($query == false || $params == false) return false;
$this->pi(__FUNCTION__.':'.__LINE__." bindSQL query: $query, dbpath: $this->dbpath");
$this->pa($params, "bindSQL params");
try {
if ($pdostmt = $this->db->prepare($query)) {
//$this->pa($pdostmt, "bindSQL pdostmt");
if ($result = $pdostmt->execute($params)) {
$this->pa($result, "bindSQL Result from query");
return $result;
}
$this->pe("bindSQL DB execute erroor.");
return false;
}
$this->pe("bindSQL DB prepare Erröör.");
return false;
} catch(PDOException $e) {
$this->pe("bindSQL ".print_r($e,1));
} catch(Exception $e) {
$this->pe("bindSQL Exception: ".$e);
}
$this->pe("bindSQL failed..");
return false;
}
public function getResultHandle($query = null) {
if ($query === null) return false;
$this->pi("getResultHandle, SQL query: $query");
//$this->db = new PDO("sqlite:$this->dbpath");
//$this->pa($this->db, "getResultHandle this->db");
try {
if ($pdostmt = $this->db->prepare($query)) {
if ($line = $pdostmt->execute()) {
//$this->pa($pdostmt, "getResultHandle Result from query, lines: $line");
//$this->db = null;
return $pdostmt;
}
}
$this->pe("getResultHandle DB prepare Errör.");
} catch(PDOException $e) {
$this->pe("getResultHandle PDOException: ".$e);
} catch(Exception $e) {
$this->pe("getResultHandle Exception: ".$e);
}
$this->pe("getResultHandle error..");
return false;
}
public function setDB($name = false) {
if ($name === false) return false;
$this->dbpath = dirname(__FILE__).'/'.$name.'.db';
return $this->createDBConnection();
}
private function setTable($name = false) {
if ($name === false) return false;
// Sanity check
if (preg_match("/^[a-zA-Z0-9_]*$/", $name)) {
$this->table = $name;
} else {
return false;
}
}
public function getMethod($string = false) {
$sql = "SELECT rowid,* from $this->table";
}
}
?>