|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Query |
|
4
|
|
|
* |
|
5
|
|
|
* SQL Query Builder / Database Abstraction Layer |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 7 |
|
8
|
|
|
* |
|
9
|
|
|
* @package Query |
|
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
|
11
|
|
|
* @copyright 2012 - 2016 Timothy J. Warren |
|
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
13
|
|
|
* @link https://git.timshomepage.net/aviat4ion/Query |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace Query\Drivers\Firebird; |
|
16
|
|
|
|
|
17
|
|
|
use PDO; |
|
18
|
|
|
use Query\Drivers\AbstractUtil; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Firebird-specific backup, import and creation methods |
|
22
|
|
|
*/ |
|
23
|
|
|
class Util extends AbstractUtil { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Convenience public function to generate sql for creating a db table |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $name |
|
29
|
|
|
* @param array $fields |
|
30
|
|
|
* @param array $constraints |
|
31
|
|
|
* @param bool $ifNotExists |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function createTable($name, $fields, array $constraints=[], $ifNotExists=FALSE) |
|
35
|
|
|
{ |
|
36
|
|
|
return parent::createTable($name, $fields, $constraints, FALSE); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Drop the selected table |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $name |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function deleteTable($name) |
|
46
|
|
|
{ |
|
47
|
|
|
return 'DROP TABLE '.$this->getDriver()->quoteTable($name); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create an SQL backup file for the current database's structure |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function backupStructure(/* @param string $dbPath, @param string $newFile */) |
|
56
|
|
|
{ |
|
57
|
|
|
list($dbPath, $newFile) = func_get_args(); |
|
58
|
|
|
return ibase_backup($this->getDriver()->getService(), $dbPath, $newFile, \IBASE_BKP_METADATA_ONLY); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create an SQL backup file for the current database's data |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $exclude |
|
65
|
|
|
* @param bool $systemTables |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function backupData($exclude=[], $systemTables=FALSE) |
|
69
|
|
|
{ |
|
70
|
|
|
// Determine which tables to use |
|
71
|
|
|
$tables = $this->getDriver()->getTables(); |
|
72
|
|
|
if($systemTables == TRUE) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$tables = array_merge($tables, $this->getDriver()->getSystemTables()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Filter out the tables you don't want |
|
78
|
|
|
if( ! empty($exclude)) |
|
79
|
|
|
{ |
|
80
|
|
|
$tables = array_diff($tables, $exclude); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$outputSql = ''; |
|
84
|
|
|
|
|
85
|
|
|
// Get the data for each object |
|
86
|
|
|
foreach($tables as $t) |
|
87
|
|
|
{ |
|
88
|
|
|
$sql = 'SELECT * FROM "'.trim($t).'"'; |
|
89
|
|
|
$res = $this->getDriver()->query($sql); |
|
|
|
|
|
|
90
|
|
|
$objRes = $res->fetchAll(PDO::FETCH_ASSOC); |
|
91
|
|
|
|
|
92
|
|
|
// Don't add to the file if the table is empty |
|
93
|
|
|
if (count($objRes) < 1) |
|
94
|
|
|
{ |
|
95
|
|
|
continue; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Nab the column names by getting the keys of the first row |
|
99
|
|
|
$columns = @array_keys($objRes[0]); |
|
100
|
|
|
|
|
101
|
|
|
$insertRows = []; |
|
102
|
|
|
|
|
103
|
|
|
// Create the insert statements |
|
104
|
|
|
foreach($objRes as $row) |
|
105
|
|
|
{ |
|
106
|
|
|
$row = array_values($row); |
|
107
|
|
|
|
|
108
|
|
|
// Quote values as needed by type |
|
109
|
|
|
if(stripos($t, 'RDB$') === FALSE) |
|
110
|
|
|
{ |
|
111
|
|
|
$row = array_map([$this->getDriver(), 'quote'], $row); |
|
112
|
|
|
$row = array_map('trim', $row); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$rowString = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; |
|
116
|
|
|
|
|
117
|
|
|
$row = NULL; |
|
118
|
|
|
|
|
119
|
|
|
$insertRows[] = $rowString; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$outputSql .= "\n\nSET TRANSACTION;\n".implode("\n", $insertRows)."\nCOMMIT;"; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $outputSql; |
|
126
|
|
|
} |
|
127
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: