Repository navigation
Expand file tree
/
Copy pathSqlite.php
More file actions
66 lines (57 loc) · 1.89 KB
/
Copy pathSqlite.php
File metadata and controls
66 lines (57 loc) · 1.89 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
<?php
namespace Codeception\Lib\Driver;
use Codeception\Configuration;
class Sqlite extends Db
{
protected $hasSnapshot = false;
protected $filename = '';
protected $con = null;
public function __construct($dsn, $user, $password)
{
parent::__construct($dsn, $user, $password);
$this->filename = Configuration::projectDir() . substr($this->dsn, 7);
$this->dsn = 'sqlite:' . $this->filename;
}
public function cleanup()
{
$this->dbh = null;
file_put_contents($this->filename, '');
$this->dbh = self::connect($this->dsn, $this->user, $this->password);
}
public function load($sql)
{
if ($this->hasSnapshot) {
$this->dbh = null;
file_put_contents($this->filename, file_get_contents($this->filename . '_snapshot'));
$this->dbh = new \PDO($this->dsn, $this->user, $this->password);
} else {
if (file_exists($this->filename . '_snapshot')) {
unlink($this->filename . '_snapshot');
}
parent::load($sql);
copy($this->filename, $this->filename . '_snapshot');
$this->hasSnapshot = true;
}
}
/**
* @param string $tableName
*
* @return array[string]
*/
public function getPrimaryKey($tableName)
{
if (!isset($this->primaryKeys[$tableName])) {
$primaryKey = [];
$query = 'PRAGMA table_info(' . $this->getQuotedName($tableName) . ')';
$stmt = $this->executeQuery($query, []);
$columns = $stmt->fetchAll(\PDO::FETCH_ASSOC);
foreach ($columns as $column) {
if ($column['pk'] !== '0') {
$primaryKey []= $column['name'];
}
}
$this->primaryKeys[$tableName] = $primaryKey;
}
return $this->primaryKeys[$tableName];
}
}