forked from lexxpavlov/angular-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlite.php
More file actions
147 lines (131 loc) · 4.09 KB
/
Copy pathSqlite.php
File metadata and controls
147 lines (131 loc) · 4.09 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
<?php
/**
* DbSimple_Sqlite: Sqlite2 database.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* See http://www.gnu.org/copyleft/lesser.html
*
* Placeholders are emulated because of logging purposes.
*
* @author Ivan Borzenkov, http://forum.dklab.ru/users/Ivan1986/
*
* @version 2.x $Id$
*/
require_once dirname(__FILE__).'/Database.php';
/**
* Database class for Sqlite.
*/
class DbSimple_Sqlite extends DbSimple_Database
{
private $link;
public function __construct($dsn)
{
$connect = 'sqlite_'.((isset($dsn['persist']) && $dsn['persist'])?'p':'').'open';
if (!is_callable($connect))
return $this->_setLastError("-1", "SQLite extension is not loaded", $connect);
$err = '';
try
{
$this->link = sqlite_factory($dsn['path'], 0666, $err);
}
catch (Exception $e)
{
$this->_setLastError($e->getCode() , $e->getMessage(), 'sqlite_factory');
}
}
public function CreateFunction($function_name, $callback, $num_args)
{ return $this->link->createFunction($function_name, $callback, $num_args); }
public function CreateAggregate($function_name, $step_func, $finalize_func, $num_args)
{ return $this->link->createAggregate($function_name, $step_func, $finalize_func, $num_args); }
protected function _performGetPlaceholderIgnoreRe()
{
return '
" (?> [^"\\\\]+|\\\\"|\\\\)* " |
\' (?> [^\'\\\\]+|\\\\\'|\\\\)* \' |
` (?> [^`]+ | ``)* ` | # backticks
/\* .*? \*/ # comments
/*';
}
protected function _performEscape($s, $isIdent=false)
{
if (!$isIdent) {
return '\''.sqlite_escape_string($s).'\'';
} else {
return "`" . str_replace('`', '``', $s) . "`";
}
}
protected function _performTransaction($parameters=null)
{
return $this->link->query('BEGIN TRANSACTION');
}
protected function _performCommit()
{
return $this->link->query('COMMIT TRANSACTION');
}
protected function _performRollback()
{
return $this->link->query('ROLLBACK TRANSACTION');
}
protected function _performQuery($queryMain)
{
$this->_lastQuery = $queryMain;
$this->_expandPlaceholders($queryMain, false);
$error_msg = '';
$p = $this->link->query($queryMain[0], SQLITE_ASSOC, $error_msg);
if (!$p)
return $this->_setDbError($p->lastError(), $error_msg, $queryMain[0]);
if ($error_msg)
return $this->_setDbError($p->lastError(), $error_msg, $queryMain[0]);
if (preg_match('/^\s* INSERT \s+/six', $queryMain[0]))
return $this->link->lastInsertRowid();
if ($p->numFields()==0)
return $this->link->changes();
//Если у нас в запросе есть хотя-бы одна колонка - это по любому будет select
return $p->fetchAll(SQLITE_ASSOC);
}
protected function _performTransformQuery(&$queryMain, $how)
{
// If we also need to calculate total number of found rows...
switch ($how)
{
// Prepare total calculation (if possible)
case 'CALC_TOTAL':
// Not possible
return true;
// Perform total calculation.
case 'GET_TOTAL':
// TODO: GROUP BY ... -> COUNT(DISTINCT ...)
$re = '/^
(?> -- [^\r\n]* | \s+)*
(\s* SELECT \s+) #1
(.*?) #2
(\s+ FROM \s+ .*?) #3
((?:\s+ ORDER \s+ BY \s+ .*?)?) #4
((?:\s+ LIMIT \s+ \S+ \s* (?: , \s* \S+ \s*)? )?) #5
$/six';
$m = null;
if (preg_match($re, $queryMain[0], $m)) {
$queryMain[0] = $m[1] . $this->_fieldList2Count($m[2]) . " AS C" . $m[3];
$skipTail = substr_count($m[4] . $m[5], '?');
if ($skipTail) array_splice($queryMain, -$skipTail);
}
return true;
}
return false;
}
protected function _performNewBlob($id=null)
{
}
protected function _performGetBlobFieldNames($result)
{
return array();
}
protected function _performFetch($result)
{
return $result;
}
}
?>