-
Notifications
You must be signed in to change notification settings - Fork 24
/
sqlite.class.php
172 lines (153 loc) · 3.5 KB
/
sqlite.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
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
<?php
class SQLite {
public $file;
public $db;
public $query;
public $prepare;
//
const IS_INT = 1;
const IS_STR = 2;
const ASSOC = 1;
const NUM = 2;
const BOTH = 3;
function __construct($base, $mode = 0666, $auto = true) {
$this->db_file = $base;
//
return $auto?$this->open($mode):true;
}
function open($mode) {
if($this->db) return $this->db;
//
if(!$this->db_file) return FALSE;
//
if(!is_file($this->db_file)) return FALSE;
//
if($this->db = sqlite_open ($this->db_file, $mode, $error)) {
//
return $this->db;
}
else{
$this->error($error);
return FALSE;
}
}
function close() {
$this->error = '';
sqlite_close($this->db);
return true;
}
function query($query) {
$query = preg_replace("/escape_string\((.*?)\)/", $this->escape_string("$1"), $query);
$this->query = sqlite_query($this->db, $query);
return $this;
}
function fetch($type=SQLITE_BOTH) {
if($type == 1) {
$type = SQLITE_ASSOC;
}
elseif($type == 2) {
$type = SQLITE_NUM;
}
elseif($type == 3 || $type != SQLITE_BOTH) {
$type = SQLITE_BOTH;
}
//
return sqlite_fetch_array($this->query, $type);
}
function fetchAll($type=SQLITE_BOTH) {
if($type == 1) {
$type = SQLITE_ASSOC;
}
elseif($type == 2) {
$type = SQLITE_NUM;
}
elseif($type == 3 || $type != SQLITE_BOTH) {
$type = SQLITE_BOTH;
}
//
return sqlite_fetch_all($this->query, $type);
}
function prepare($query) {
$this->prepare = $query;
return $this;
}
function bindParam($bind, $value, $types = '') {
//
if($types == 1) {
if(preg_match("/^\d+$/", $value)) $to_prepare = $value;
}
elseif($types == 2) {
if(is_string($value)) $to_prepare = '"'.$this->escape_string($value).'"';
}
elseif(!empty($types)) {
if(preg_match($types, $value)) '"'.$this->escape_string($value).'"';
}
//
if(empty($to_prepare)) $to_prepare = '""';
//
$this->prepare = str_replace($bind, $to_prepare, $this->prepare);
//
return $this;
}
function execute() {
//
$this->query = sqlite_query($this->db, $this->prepare);
return $this;
}
function last_insert_id() {
return sqlite_last_insert_rowid ($this->db);
}
function rows() {
return sqlite_num_rows($this->query);
}
function getColumns($table) {
return $this->query("PRAGMA table_info($table)")->fetchAll(SQLite::ASSOC);
}
function getTables() {
return $this->query("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")->fetchAll(SQLite::ASSOC);
}
function escape_string($string, $quotestyle='both') {
if( function_exists('sqlite_escape_string') ){
$string = sqlite_escape_string($string);
$string = str_replace("''","'",$string); #- no quote escaped so will work like with no sqlite_escape_string available
}
else{
$escapes = array("\x00", "\x0a", "\x0d", "\x1a", "\x09","\\");
$replace = array('\0', '\n', '\r', '\Z' , '\t', "\\\\");
}
switch(strtolower($quotestyle)){
case 'double':
case 'd':
case '"':
$escapes[] = '"';
$replace[] = '\"';
break;
case 'single':
case 's':
case "'":
$escapes[] = "'";
$replace[] = "''";
break;
case 'both':
case 'b':
case '"\'':
case '\'"':
$escapes[] = '"';
$replace[] = '\"';
$escapes[] = "'";
$replace[] = "''";
break;
}
return str_replace($escapes,$replace,$string);
}
function error($error) {
if(!$this->db) {
return '[ERROR] No Db Handler';
}
if(empty($error)) {
return sqlite_last_error ($this->db);
}
return $error;
}
}
?>