|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ORM\Dbal; |
|
4
|
|
|
|
|
5
|
|
|
use ORM\Dbal; |
|
6
|
|
|
use ORM\Exception; |
|
7
|
|
|
|
|
8
|
|
|
class Sqlite extends Dbal |
|
9
|
|
|
{ |
|
10
|
|
|
protected static $typeMapping = [ |
|
11
|
|
|
'integer' => Type\Integer::class, |
|
12
|
|
|
'int' => Type\Integer::class, |
|
13
|
|
|
|
|
14
|
|
|
'double' => Type\Double::class, |
|
15
|
|
|
'real' => Type\Double::class, |
|
16
|
|
|
'float' => Type\Double::class, |
|
17
|
|
|
'numeric' => Type\Double::class, |
|
18
|
|
|
'decimal' => Type\Double::class, |
|
19
|
|
|
|
|
20
|
|
|
'varchar' => Type\VarChar::class, |
|
21
|
|
|
'character' => Type\VarChar::class, |
|
22
|
|
|
|
|
23
|
|
|
'text' => Type\Text::class, |
|
24
|
|
|
|
|
25
|
|
|
'boolean' => Type\Boolean::class, |
|
26
|
|
|
'json' => Type\Json::class, |
|
27
|
|
|
|
|
28
|
|
|
'datetime' => Type\DateTime::class, |
|
29
|
|
|
'date' => Type\DateTime::class, |
|
30
|
|
|
'time' => Type\Time::class, |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
2 |
|
public function insert($entity, $useAutoIncrement = true) |
|
34
|
|
|
{ |
|
35
|
2 |
|
$statement = $this->buildInsertStatement($entity); |
|
36
|
2 |
|
$pdo = $this->em->getConnection(); |
|
37
|
|
|
|
|
38
|
2 |
|
if ($useAutoIncrement && $entity::isAutoIncremented()) { |
|
39
|
1 |
|
$pdo->query($statement); |
|
40
|
1 |
|
return $pdo->lastInsertId(); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
$pdo->query($statement); |
|
44
|
1 |
|
$this->em->sync($entity, true); |
|
45
|
1 |
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
32 |
|
public function describe($schemaTable) |
|
49
|
|
|
{ |
|
50
|
32 |
|
$table = explode(static::$identifierDivider, $schemaTable); |
|
51
|
32 |
|
list($schema, $table) = count($table) === 2 ? $table : [null, $table[0]]; |
|
52
|
32 |
|
$schema = $schema !== null ? $this->escapeIdentifier($schema) . '.' : ''; |
|
53
|
|
|
|
|
54
|
32 |
|
$result = $this->em->getConnection()->query( |
|
55
|
32 |
|
'PRAGMA ' . $schema . 'table_info(' . $this->escapeIdentifier($table) . ')' |
|
56
|
|
|
); |
|
57
|
32 |
|
$rawColumns = $result->fetchAll(\PDO::FETCH_ASSOC); |
|
58
|
|
|
|
|
59
|
32 |
|
if (count($rawColumns) === 0) { |
|
60
|
2 |
|
throw new Exception('Unknown table ' . $table); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
30 |
|
$cols = []; |
|
64
|
30 |
|
foreach ($rawColumns as $i => $rawColumn) { |
|
65
|
30 |
|
$type = $this->normlizeType($rawColumn['type']); |
|
66
|
30 |
|
$class = isset(static::$typeMapping[$type]) ? static::$typeMapping[$type] : Dbal\Type\Text::class; |
|
67
|
|
|
|
|
68
|
30 |
|
$hasDefault = $rawColumn['dflt_value'] !== null; |
|
69
|
|
|
|
|
70
|
30 |
|
if (!$hasDefault && $rawColumn['type'] === 'integer' && $rawColumn['pk'] === '1' && |
|
71
|
30 |
|
!$this->hasMultiplePrimaryKey($rawColumns) |
|
72
|
|
|
) { |
|
73
|
1 |
|
$hasDefault = true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
30 |
|
$cols[] = new Column( |
|
77
|
30 |
|
$rawColumn['name'], |
|
78
|
30 |
|
new $class, |
|
79
|
|
|
$hasDefault, |
|
80
|
30 |
|
$rawColumn['notnull'] === '0' |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
30 |
|
return $cols; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function hasMultiplePrimaryKey($rawColumns) |
|
88
|
|
|
{ |
|
89
|
2 |
|
return count(array_filter(array_map(function ($rawColumn) { |
|
90
|
2 |
|
return $rawColumn['pk']; |
|
91
|
2 |
|
}, $rawColumns))) > 1; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.