|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ubiquity\orm\creator\database; |
|
3
|
|
|
|
|
4
|
|
|
use Ubiquity\orm\creator\ModelsCreator; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class DbModelsCreator extends ModelsCreator{ |
|
8
|
|
|
private $pdoObject; |
|
9
|
|
|
|
|
10
|
|
|
protected function init($config){ |
|
11
|
|
|
parent::init($config); |
|
12
|
|
|
$this->connect($config["database"]); |
|
13
|
|
|
} |
|
14
|
|
|
/** |
|
15
|
|
|
* Réalise la connexion à la base de données |
|
16
|
|
|
*/ |
|
17
|
|
|
private function connect($config) { |
|
18
|
|
|
try { |
|
19
|
|
|
$this->pdoObject = new \PDO( |
|
20
|
|
|
$config["type"].':host=' . $config["serverName"] . ';dbname=' |
|
21
|
|
|
. $config["dbName"] . ';port:' . $config["port"], |
|
22
|
|
|
$config["user"], $config["password"]); |
|
23
|
|
|
$this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
24
|
|
|
$this->pdoObject->exec("SET CHARACTER SET utf8"); |
|
25
|
|
|
|
|
26
|
|
|
} catch (\PDOException $e) { |
|
27
|
|
|
print "Error!: " . $e->getMessage() . "<br/>"; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
protected function getTablesName(){ |
|
33
|
|
|
$sql = 'SHOW TABLES'; |
|
34
|
|
|
$query = $this->pdoObject->query($sql); |
|
35
|
|
|
return $query->fetchAll(\PDO::FETCH_COLUMN); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function getFieldsInfos($tableName) { |
|
39
|
|
|
$fieldsInfos=array(); |
|
40
|
|
|
$recordset = $this->pdoObject->query("SHOW COLUMNS FROM `{$tableName}`"); |
|
41
|
|
|
$fields = $recordset->fetchAll(\PDO::FETCH_ASSOC); |
|
42
|
|
View Code Duplication |
foreach ($fields as $field) { |
|
|
|
|
|
|
43
|
|
|
$fieldsInfos[$field['Field']] = ["Type"=>$field['Type'],"Nullable"=>$field["Null"]]; |
|
44
|
|
|
} |
|
45
|
|
|
return $fieldsInfos; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function getPrimaryKeys($tableName){ |
|
49
|
|
|
$fieldkeys=array(); |
|
50
|
|
|
$recordset = $this->pdoObject->query("SHOW KEYS FROM `{$tableName}` WHERE Key_name = 'PRIMARY'"); |
|
51
|
|
|
$keys = $recordset->fetchAll(\PDO::FETCH_ASSOC); |
|
52
|
|
|
foreach ($keys as $key) { |
|
53
|
|
|
$fieldkeys[] = $key['Column_name']; |
|
54
|
|
|
} |
|
55
|
|
|
return $fieldkeys; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function getForeignKeys($tableName,$pkName){ |
|
59
|
|
|
$recordset = $this->pdoObject->query("SELECT * |
|
60
|
|
|
FROM |
|
61
|
|
|
information_schema.KEY_COLUMN_USAGE |
|
62
|
|
|
WHERE |
|
63
|
|
|
REFERENCED_TABLE_NAME = '".$tableName."' |
|
64
|
|
|
AND REFERENCED_COLUMN_NAME = '".$pkName."' |
|
65
|
|
|
AND TABLE_SCHEMA = '".$this->config["dbName"]."';"); |
|
66
|
|
|
return $recordset->fetchAll(\PDO::FETCH_ASSOC); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.