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
|
<?php
namespace Garradin;
use KD2\DB_SQLite3;
class DB extends DB_SQLite3
{
/**
* Application ID pour SQLite
* @link https://www.sqlite.org/pragma.html#pragma_application_id
*/
const APPID = 0x5da2d811;
static protected $_instance = null;
static public function getInstance($create = false)
{
return self::$_instance ?: self::$_instance = new DB($create);
}
private function __clone()
{
// Désactiver le clonage, car on ne veut qu'une seule instance
}
public function __construct($create = false)
{
if (!defined('\SQLITE3_OPEN_READWRITE'))
{
throw new \Exception('Module SQLite3 de PHP non présent. Merci de l\'installer.');
}
$flags = \SQLITE3_OPEN_READWRITE;
if ($create)
{
$flags |= \SQLITE3_OPEN_CREATE;
}
parent::__construct(DB_FILE, $flags);
// Ne pas se connecter ici, on ne se connectera que quand une requête sera faite
}
public function connect()
{
if (parent::connect())
{
// Activer les contraintes des foreign keys
$this->exec('PRAGMA foreign_keys = ON;');
// 10 secondes
$this->db->busyTimeout(10 * 1000);
$this->exec('PRAGMA journal_mode = TRUNCATE;');
$this->db->createFunction('transliterate_to_ascii', ['Garradin\Utils', 'transliterateToAscii']);
}
}
public function close()
{
parent::close();
self::$_instance = null;
}
/**
* Import a file containing SQL commands
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
<
<
<
<
|
<
|
<
<
<
<
<
<
<
<
<
<
|
|
|
|
|
|
|
|
|
|
<
|
|
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
|
<?php
namespace Garradin;
use KD2\DB\SQLite3;
class DB extends SQLite3
{
/**
* Application ID pour SQLite
* @link https://www.sqlite.org/pragma.html#pragma_application_id
*/
const APPID = 0x5da2d811;
static protected $_instance = null;
static public function getInstance($create = false, $readonly = false)
{
if (null === self::$_instance) {
self::$_instance = new DB('sqlite', ['file' => DB_FILE]);
$flags = \SQLITE3_OPEN_READWRITE;
if ($create)
{
$flags |= \SQLITE3_OPEN_CREATE;
}
self::$_instance->flags = $flags;
}
return self::$_instance;
}
private function __clone()
{
// Désactiver le clonage, car on ne veut qu'une seule instance
}
public function connect(): void
{
if (null !== $this->db) {
return;
}
parent::connect();
// Activer les contraintes des foreign keys
$this->db->exec('PRAGMA foreign_keys = ON;');
// 10 secondes
$this->db->busyTimeout(10 * 1000);
$this->exec('PRAGMA journal_mode = TRUNCATE;');
$this->db->createFunction('transliterate_to_ascii', ['Garradin\Utils', 'transliterateToAscii']);
}
public function close(): void
{
parent::close();
self::$_instance = null;
}
/**
* Import a file containing SQL commands
|