-
Notifications
You must be signed in to change notification settings - Fork 9
/
QuerySaver.php
85 lines (77 loc) · 2.05 KB
/
QuerySaver.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
<?php
namespace dokuwiki\plugin\sqlite;
use dokuwiki\Extension\Event;
class QuerySaver
{
protected $db;
protected $upstream;
/**
* @param string $dbname The database the queries are saved for
*/
public function __construct($dbname)
{
$this->db = new SQLiteDB('sqlite', DOKU_PLUGIN . 'sqlite/db/');
$this->upstream = $dbname;
}
/**
* Save a query
*
* @param string $name
* @param string $query
*/
public function saveQuery($name, $query)
{
$eventData = [
'sqlitedb' => $this->db,
'upstream' => $this->upstream,
'name' => &$name,
'query' => &$query
];
$event = new Event('PLUGIN_SQLITE_QUERY_SAVE', $eventData);
if ($event->advise_before()) {
$sql = 'INSERT INTO queries (db, name, sql) VALUES (?, ?, ?)';
$this->db->exec($sql, [$this->upstream, $name, $query]);
}
$event->advise_after();
}
/**
* Get a saved query
*
* @param string $name
* @return string The SQL query
*/
public function getQuery($name)
{
$sql = 'SELECT sql FROM queries WHERE db = ? AND name = ?';
return $this->db->queryValue($sql, [$this->upstream, $name]);
}
/**
* Delete a saved query
*
* @param string $name
*/
public function deleteQuery($name)
{
$eventData = [
'sqlitedb' => $this->db,
'upstream' => $this->upstream,
'name' => &$name
];
$event = new Event('PLUGIN_SQLITE_QUERY_DELETE', $eventData);
if ($event->advise_before()) {
$sql = 'DELETE FROM queries WHERE db = ? AND name = ?';
$this->db->exec($sql, [$this->upstream, $name]);
}
$event->advise_after();
}
/**
* Get all saved queries
*
* @return array
*/
public function getQueries()
{
$sql = 'SELECT name, sql FROM queries WHERE db = ?';
return $this->db->queryAll($sql, [$this->upstream]);
}
}