1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soupmix; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
SQL Adapter |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Doctrine\DBAL\Connection; |
10
|
|
|
|
11
|
|
|
final class SQL implements Base |
12
|
|
|
{ |
13
|
|
|
protected $doctrine = null; |
14
|
|
|
protected $dbName = null; |
15
|
|
|
|
16
|
3 |
|
public function __construct($config, Connection $client) |
17
|
|
|
{ |
18
|
3 |
|
$this->doctrine = $client; |
19
|
3 |
|
$this->dbName = $config['db_name']; |
20
|
3 |
|
} |
21
|
|
|
|
22
|
2 |
|
public function getConnection() |
23
|
|
|
{ |
24
|
2 |
|
return $this->doctrine; |
25
|
|
|
} |
26
|
|
|
|
27
|
3 |
|
public function create(string $collection, array $fields) |
28
|
|
|
{ |
29
|
3 |
|
$schemaManager = $this->doctrine->getSchemaManager(); |
30
|
3 |
|
$table = new SQLTable($schemaManager, $collection, $fields); |
31
|
3 |
|
return $table->createTable(); |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
public function drop(string $collection) |
35
|
|
|
{ |
36
|
3 |
|
$schemaManager = $this->doctrine->getSchemaManager(); |
37
|
3 |
|
if ($schemaManager->tablesExist([$collection])) { |
38
|
|
|
return $schemaManager->dropTable($collection); |
39
|
|
|
} |
40
|
3 |
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function truncate(string $collection) |
44
|
|
|
{ |
45
|
|
|
return $this->client->doctrine->query('TRUNCATE TABLE ' . $collection . ''); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function createIndexes(string $collection, array $fields) |
49
|
|
|
{ |
50
|
|
|
$schemaManager = $this->doctrine->getSchemaManager(); |
51
|
|
|
$table = new SQLTable($schemaManager, $collection, $fields); |
52
|
|
|
return $table->createOnlyIndexes(); |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
public function insert(string $collection, array $values) |
56
|
|
|
{ |
57
|
3 |
|
$insertion = $this->doctrine->insert($collection, $values); |
58
|
3 |
|
if ($insertion !== 0) { |
59
|
3 |
|
return $this->doctrine->lastInsertId(); |
60
|
|
|
} |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function update(string $collection, array $filter, array $values) |
65
|
|
|
{ |
66
|
|
|
return $this->doctrine->update($collection, $values, $filter); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
public function delete(string $collection, array $filter) |
70
|
|
|
{ |
71
|
2 |
|
$numberOfDeletedItems = $this->doctrine->delete($collection, $filter); |
72
|
2 |
|
return ($numberOfDeletedItems > 0) ? 1 : 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function get(string $collection, $docId) |
76
|
|
|
{ |
77
|
1 |
|
return $this->doctrine->fetchAssoc('SELECT * FROM ' . $collection . ' WHERE id = ?', array($docId)); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function find(string $collection, ?array $filters, ?array $fields = null, ?array $sort = null, ?int $offset = 0, ?int $limit = 25) |
81
|
|
|
{ |
82
|
1 |
|
$query = $this->query($collection); |
83
|
1 |
|
foreach ($filters as $filter => $value) { |
84
|
1 |
|
if (is_array($value)) { |
85
|
1 |
|
$query->orFilters($value); |
86
|
1 |
|
continue; |
87
|
|
|
} |
88
|
1 |
|
$query->andFilter($filter, $value); |
89
|
|
|
} |
90
|
1 |
|
return $query->returnFields($fields) |
91
|
1 |
|
->sortFields($sort) |
92
|
1 |
|
->offset($offset) |
93
|
1 |
|
->limit($limit) |
94
|
1 |
|
->run(); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public function query(string $collection) |
98
|
|
|
{ |
99
|
2 |
|
return new SQLQueryBuilder($collection, $this); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: