-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProber.php
94 lines (81 loc) · 2.38 KB
/
Prober.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace tools;
/**
* Description of Prober
*
* @author Vítězslav Dvořák <info@vitexsoftware.cz>
*/
class Prober extends \SpojeNet\PohodaSQL\Agenda
{
public $myTable = 'information_schema';
public function getTables()
{
$this->setMyTable('information_schema.tables');
$tables = [];
foreach ($this->getAll() as $tableId => $tableInfo) {
$tables[$tableInfo['TABLE_NAME']] = $tableInfo['TABLE_NAME'];
}
return $tables;
}
public function getColumns($tableName)
{
$this->setMyTable('information_schema.columns');
$structure = [];
foreach ($this->listingQuery()->where(['TABLE_NAME' => $tableName])->fetchAll() as $columnId => $columnInfo) {
$structure[$columnInfo['COLUMN_NAME']] = [
'type' => $columnInfo['DATA_TYPE'],
'size' => ($columnInfo['DATA_TYPE'] == 'varchar') ? $columnInfo['CHARACTER_MAXIMUM_LENGTH']
: $columnInfo['NUMERIC_PRECISION'],
'default' => ($columnInfo['DATA_TYPE'] == 'bit') ? (($columnInfo['COLUMN_DEFAULT'] == '((0))') ? false : null)
: $columnInfo['COLUMN_DEFAULT']
];
}
return $structure;
}
public function classSkeleton($tableName)
{
$className = ucfirst($tableName);
return '<?php
/**
* PohodaSQL - Property Handler
*
* @author Vítězslav Dvořák <info@vitexsoftware.cz>
* @copyright (C) '.date('Y').' SpojeNetIT s.r.o.
*/
namespace SpojeNet\PohodaSQL;
/**
* Description of '.$className.'
*
* @author Vítězslav Dvořák <info@vitexsoftware.cz>
*/
class '.$className.' extends Agenda
{
/**
* Work with given table
* @var string
*/
public $myTable = \''.$tableName.'\';
/**
* SQL Table structure
* @const array
*/
public $struct = '.var_export($this->getColumns($tableName), true).';
/**
* '.$tableName.' handler
*
* @param mixed $identifier Initial content/identifier
* @param array $options Object options
*/
public function __construct($identifier = null, $options = [])
{
parent::__construct($identifier, $options);
}
}
';
}
public function saveClass($tableName)
{
return file_put_contents(ucfirst($tableName).'.php',
$this->classSkeleton($tableName));
}
}