-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathSqlite.php
More file actions
97 lines (80 loc) · 2.21 KB
/
Copy pathSqlite.php
File metadata and controls
97 lines (80 loc) · 2.21 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
<?php
namespace PHPCensor\Plugin;
use Exception;
use PDO;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* SQLite Plugin — Provides access to a SQLite database.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Sqlite extends Plugin
{
/**
* @var array
*/
protected $queries = [];
/**
* @var string
*/
protected $path = '';
/**
* @var array
*/
protected $pdoOptions = [];
/**
* @return string
*/
public static function pluginName()
{
return 'sqlite';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$buildSettings = $this->builder->getConfig('build_settings');
if (isset($buildSettings['sqlite'])) {
$sql = $buildSettings['sqlite'];
$this->path = $sql['path'];
}
if (!empty($buildSettings['sqlite']['path'])) {
$this->path = $buildSettings['sqlite']['path'];
}
if (!empty($buildSettings['sqlite']['options']) && \is_array($buildSettings['sqlite']['options'])) {
$this->pdoOptions = $buildSettings['sqlite']['options'];
}
if (!empty($this->options['queries']) && \is_array($this->options['queries'])) {
foreach ($this->options['queries'] as $query) {
$this->queries[] = $this->builder->interpolate($query, true);
}
}
}
/**
* Connects to SQLite and runs a specified set of queries.
* @return bool
*/
public function execute()
{
try {
$pdoOptions = \array_merge([
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
], $this->pdoOptions);
$pdo = new PDO(('sqlite:' . $this->path), null, null, $pdoOptions);
foreach ($this->queries as $query) {
$pdo->query($query);
}
} catch (\Throwable $ex) {
$this->builder->logFailure($ex->getMessage());
return false;
}
return true;
}
}