-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlite.php
More file actions
160 lines (114 loc) · 4.19 KB
/
sqlite.php
File metadata and controls
160 lines (114 loc) · 4.19 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
class sqlite {
protected $_db;
protected $_sql;
protected $_where = array();
protected $_params = array();
public function __construct($database) {
$this->_db = new PDO('sqlite:'.$database) or die('Aquí ha pasado algo');
}
public function query($query) {
$this->_sql = filter_var($query, FILTER_SANITIZE_STRING);
$stmt = $this->_buildQuery();
$stmt->execute($params);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$results = $stmt->fetchAll();
return $results;
}
public function get($tableName, $numRows = NULL, $startingNum = NULL) {
$query = "SELECT * FROM $tableName";
$this->_sql = filter_var($query, FILTER_SANITIZE_STRING);
$stmt = $this->_buildQuery($numRows, $startingNum);
$stmt->execute($this->_params);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$results = $stmt->fetchAll();
return $results;
}
public function insert($tableName, $insertData) {
$keys = array_keys($insertData);
$values = array_values($insertData);
$binds_clausule = implode(' , :', $keys);
$keys_clausule = implode(' , ', $keys);
$clausule = "( $keys_clausule ) VALUES ( :$binds_clausule )";
$this->_query = "INSERT into $tableName $clausule";
foreach($keys as $row) {
$new_keys[] = ":$row";
}
$params = array_combine($new_keys, $values);
$stmt = $this->_db->prepare($this->_query);
if($stmt->execute($this->_params)) {
return true;
}
}
public function update($tableName, $updateData) {
$keys = array_keys($updateData);
$values = array_values($updateData);
$counter = 0;
foreach ($keys as $row) {
if($counter == 0) {
$clausule .= "$row = :$row";
} else {
$clausule .= ", $row = :$row";
}
$counter++;
}
$this->_sql = "UPDATE $tableName SET $clausule";
if(!empty($this->_where)) {
$stmt = $this->_buildQuery();
} else {
trigger_error('You must use the "where" method to call "update". ', E_USER_ERROR);
}
foreach($updateData as $key => $value) {
$this->_params[":$key"] = $value;
}
if($stmt->execute($this->_params)) {
return true;
}
}
protected function _buildQuery ($numRows = NULL, $startingNum = NULL, $tableData = false) {
$params = array();
$hasTableData = null;
if(!empty($this->_where)) {
$counter = 0;
foreach($this->_where as $row) {
if ($counter == 0) {
$this->_sql .= " WHERE $row[key] $row[operator] :$row[key]";
} else {
$this->_sql .= " AND $row[key] $row[operator] :$row[key]";
}
$counter++;
if($row[operator] == 'LIKE') {
$this->_params[":$row[key]"] = '%'.$row[value].'%';
} else {
$this->_params[":$row[key]"] = $row[value];
}
}
}
if(gettype($tableData) === 'array') {
$hasTableData = true;
}
if ($numRows && !$startingNum) {
$this->_sql .= " LIMIT 0, $numRows";
} elseif ($numRows && $startingNum) {
$this->_sql .= " LIMIT $startingNum, $numRows";
}
$stmt = $this->_prepareQuery();
return $stmt;
}
protected function _prepareQuery() {
if(!$stmt = $this->_db->prepare($this->_sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY))) {
trigger_error('Problem preparing query: '. $this->_sql, E_USER_ERROR);
}
return $stmt;
}
public function where($whereProp, $whereValue, $operator = NULL) {
if ($operator == NULL) {
$operator = '=';
}
$this->_where[] = array('key' => $whereProp, 'value' => $whereValue, 'operator' => $operator);
return $this->_where;
}
public function __destruct () {
$this->_db = NULL;
}
}