|
1
|
|
|
<?php |
|
2
|
|
|
namespace Maphper\DataSource; |
|
3
|
|
|
class Database implements \Maphper\DataSource { |
|
4
|
|
|
const EDIT_STRUCTURE = 1; |
|
5
|
|
|
const EDIT_INDEX = 2; |
|
6
|
|
|
const EDIT_OPTIMISE = 4; |
|
7
|
|
|
|
|
8
|
|
|
private $primaryKey; |
|
9
|
|
|
private $table; |
|
10
|
|
|
|
|
11
|
|
|
private $fields = '*'; |
|
12
|
|
|
private $databaseSelect; |
|
13
|
|
|
private $databaseCrud; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
|
16
|
|
|
$options = new DatabaseOptions($db, $options); |
|
17
|
|
|
$adapter = $options->getAdapter(); |
|
18
|
|
|
|
|
19
|
|
|
$this->primaryKey = is_array($primaryKey) ? $primaryKey : [$primaryKey]; |
|
20
|
|
|
$this->table = $table; |
|
21
|
|
|
|
|
22
|
|
|
$this->fields = implode(',', array_map([$adapter, 'quote'], (array) $options->read('fields'))); |
|
23
|
|
|
|
|
24
|
|
|
$defaultSort = $options->read('defaultSort') !== false ? $options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
25
|
|
|
|
|
26
|
|
|
$databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort, $options->getCacheMode()); |
|
|
|
|
|
|
29
|
|
|
$this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
$databaseModify->optimizeColumns(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getPrimaryKey() { |
|
35
|
|
|
return $this->primaryKey; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getColumns() { |
|
39
|
|
|
return $this->databaseSelect->getColumns($this->table); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function deleteById($id) { |
|
43
|
|
|
$this->databaseCrud->deleteById($id); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function findById($id) { |
|
47
|
|
|
return $this->databaseSelect->findById($id, $this->getPrimaryKey()[0]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
51
|
|
|
return $this->databaseSelect->findAggregate($function, $field, $group, $criteria, $options); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function findByField(array $fields, $options = []) { |
|
55
|
|
|
return $this->databaseSelect->findByField($fields, $options); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function deleteByField(array $fields, array $options = []) { |
|
59
|
|
|
$this->databaseCrud->deleteByField($fields, $options); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function save($data) { |
|
63
|
|
|
$this->databaseCrud->save($data, true); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|