|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ORM\Dbal; |
|
4
|
|
|
|
|
5
|
|
|
use ORM\Dbal; |
|
6
|
|
|
use ORM\Exception; |
|
7
|
|
|
use ORM\QueryBuilder\QueryBuilder; |
|
8
|
|
|
use PDO; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Database abstraction for PostgreSQL databases |
|
12
|
|
|
* |
|
13
|
|
|
* @package ORM\Dbal |
|
14
|
|
|
* @author Thomas Flori <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Pgsql extends Dbal |
|
17
|
|
|
{ |
|
18
|
|
|
protected static $typeMapping = [ |
|
19
|
|
|
'integer' => Type\Integer::class, |
|
20
|
|
|
'smallint' => Type\Integer::class, |
|
21
|
|
|
'bigint' => Type\Integer::class, |
|
22
|
|
|
|
|
23
|
|
|
'numeric' => Type\Double::class, |
|
24
|
|
|
'real' => Type\Double::class, |
|
25
|
|
|
'double precision' => Type\Double::class, |
|
26
|
|
|
'money' => Type\Double::class, |
|
27
|
|
|
|
|
28
|
|
|
'character varying' => Type\VarChar::class, |
|
29
|
|
|
'character' => Type\VarChar::class, |
|
30
|
|
|
|
|
31
|
|
|
'text' => Type\Text::class, |
|
32
|
|
|
|
|
33
|
|
|
'date' => Type\DateTime::class, |
|
34
|
|
|
'timestamp without time zone' => Type\DateTime::class, |
|
35
|
|
|
'timestamp with time zone' => Type\DateTime::class, |
|
36
|
|
|
'time without time zone' => Type\Time::class, |
|
37
|
|
|
'time with time zone' => Type\Time::class, |
|
38
|
|
|
|
|
39
|
|
|
'json' => Type\Json::class, |
|
40
|
|
|
'boolean' => Type\Boolean::class, |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
2 |
|
public function insert($entity, $useAutoIncrement = true) |
|
44
|
|
|
{ |
|
45
|
2 |
|
$statement = $this->buildInsertStatement($entity); |
|
46
|
2 |
|
$pdo = $this->em->getConnection(); |
|
47
|
|
|
|
|
48
|
2 |
|
if ($useAutoIncrement && $entity::isAutoIncremented()) { |
|
49
|
1 |
|
$statement .= ' RETURNING ' . $entity::getColumnName($entity::getPrimaryKeyVars()[0]); |
|
50
|
1 |
|
$result = $pdo->query($statement); |
|
51
|
1 |
|
return $result->fetchColumn(); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
$pdo->query($statement); |
|
55
|
1 |
|
$this->em->sync($entity, true); |
|
56
|
1 |
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
26 |
|
public function describe($schemaTable) |
|
60
|
|
|
{ |
|
61
|
26 |
|
$table = explode(static::$identifierDivider, $schemaTable); |
|
62
|
26 |
|
list($schema, $table) = count($table) === 2 ? $table : ['public', $table[0]]; |
|
63
|
|
|
|
|
64
|
26 |
|
$query = new QueryBuilder('INFORMATION_SCHEMA.COLUMNS'); |
|
65
|
26 |
|
$query->where('table_name', $table)->andWhere('table_schema', $schema); |
|
66
|
26 |
|
$query->columns([ |
|
67
|
26 |
|
'column_name', 'column_default', 'data_type', 'is_nullable', 'character_maximum_length', |
|
68
|
|
|
'datetime_precision' |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
26 |
|
$result = $this->em->getConnection()->query($query->getQuery()); |
|
72
|
26 |
|
$rawColumns = $result->fetchAll(PDO::FETCH_ASSOC); |
|
73
|
26 |
|
if (count($rawColumns) === 0) { |
|
74
|
2 |
|
throw new Exception('Unknown table ' . $schemaTable); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
24 |
|
$cols = array_map(function ($columnDefinition) { |
|
78
|
24 |
|
return Column::factory($columnDefinition, $this->getType($columnDefinition)); |
|
79
|
24 |
|
}, $rawColumns); |
|
80
|
|
|
|
|
81
|
24 |
|
return $cols; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
24 |
|
protected function getType($columnDefinition) |
|
85
|
|
|
{ |
|
86
|
24 |
|
if (isset(static::$typeMapping[$columnDefinition['data_type']])) { |
|
87
|
17 |
|
return call_user_func([static::$typeMapping[$columnDefinition['data_type']], 'factory'], $columnDefinition); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
7 |
|
return parent::getType($columnDefinition); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
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.