|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TActiveRecordGenAllAction class file |
|
4
|
|
|
* |
|
5
|
|
|
* @author Brad Anderson <[email protected]> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado |
|
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
8
|
|
|
* @package Prado\Shell\Actions |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Shell\Actions; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\Data\ActiveRecord\TActiveRecordConfig; |
|
14
|
|
|
use Prado\Data\ActiveRecord\TActiveRecordManager; |
|
15
|
|
|
use Prado\Prado; |
|
16
|
|
|
use Prado\Shell\TShellAction; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Create active record skeleton for all tables in DB and its relations |
|
20
|
|
|
* |
|
21
|
|
|
* @author Matthias Endres <me[at]me23[dot]de> |
|
22
|
|
|
* @author Daniel Sampedro Bello <darthdaniel85[at]gmail[dot]com> |
|
23
|
|
|
* @package Prado\Shell\Actions |
|
24
|
|
|
* @since 3.2 |
|
25
|
|
|
*/ |
|
26
|
|
|
class TActiveRecordGenAllAction extends TShellAction |
|
27
|
|
|
{ |
|
28
|
|
|
protected $action = 'generateAll'; |
|
29
|
|
|
protected $parameters = ['output']; |
|
30
|
|
|
protected $optional = ['directory', 'soap', 'overwrite', 'prefix', 'postfix']; |
|
31
|
|
|
protected $description = "Generate Active Record skeleton for all Tables to <output> file using application.xml in [directory]. May also generate [soap] properties.\nGenerated Classes are named like the Table with optional [Prefix] and/or [Postfix]. [Overwrite] is used to overwrite existing Files."; |
|
32
|
|
|
private $_soap = false; |
|
33
|
|
|
private $_prefix = ''; |
|
34
|
|
|
private $_postfix = ''; |
|
35
|
|
|
private $_overwrite = false; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param array $args parameters |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
|
public function performAction($args) |
|
42
|
|
|
{ |
|
43
|
|
|
$app_dir = count($args) > 2 ? $this->getAppDir($args[2]) : $this->getAppDir(); |
|
44
|
|
|
$this->_soap = count($args) > 3 ? ($args[3] == "soap" || $args[3] == "true" ? true : false) : false; |
|
45
|
|
|
$this->_overwrite = count($args) > 4 ? ($args[4] == "overwrite" || $args[4] == "true" ? true : false) : false; |
|
46
|
|
|
$this->_prefix = count($args) > 5 ? $args[5] : ''; |
|
47
|
|
|
$this->_postfix = count($args) > 6 ? $args[6] : ''; |
|
48
|
|
|
|
|
49
|
|
|
if ($app_dir !== false) { |
|
50
|
|
|
$config = $this->getActiveRecordConfig($app_dir); |
|
51
|
|
|
|
|
52
|
|
|
$manager = TActiveRecordManager::getInstance(); |
|
53
|
|
|
$con = $manager->getDbConnection(); |
|
54
|
|
|
$con->Active = true; |
|
|
|
|
|
|
55
|
|
|
$command = null; |
|
56
|
|
|
|
|
57
|
|
|
switch ($con->getDriverName()) { |
|
58
|
|
|
case 'mysqli': |
|
59
|
|
|
case 'mysql': |
|
60
|
|
|
$command = $con->createCommand("SHOW TABLES"); |
|
61
|
|
|
break; |
|
62
|
|
|
case 'sqlite': //sqlite 3 |
|
63
|
|
|
case 'sqlite2': //sqlite 2 |
|
64
|
|
|
$command = $con->createCommand("SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence'"); |
|
65
|
|
|
break; |
|
66
|
|
|
case 'pgsql': |
|
67
|
|
|
case 'mssql': // Mssql driver on windows hosts |
|
68
|
|
|
case 'sqlsrv': // sqlsrv driver on windows hosts |
|
69
|
|
|
case 'dblib': // dblib drivers on linux (and maybe others os) hosts |
|
70
|
|
|
case 'oci': |
|
71
|
|
|
// case 'ibm': |
|
72
|
|
|
default: |
|
73
|
|
|
echo "\n Sorry, generateAll is not implemented for " . $con->getDriverName() . "\n"; |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$dataReader = $command->query(); |
|
78
|
|
|
$dataReader->bindColumn(1, $table); |
|
79
|
|
|
$tables = []; |
|
80
|
|
|
while ($dataReader->read() !== false) { |
|
81
|
|
|
$tables[] = $table; |
|
82
|
|
|
} |
|
83
|
|
|
$con->Active = false; |
|
84
|
|
|
foreach ($tables as $key => $table) { |
|
85
|
|
|
$output = $args[1] . "." . $this->_prefix . ucfirst($table) . $this->_postfix; |
|
86
|
|
|
if ($config !== false && $output !== false) { |
|
87
|
|
|
$this->generate("generate " . $table . " " . $output . " " . $this->_soap . " " . $this->_overwrite); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $l commandline |
|
96
|
|
|
*/ |
|
97
|
|
|
public function generate($l) |
|
98
|
|
|
{ |
|
99
|
|
|
$input = explode(" ", trim($l)); |
|
100
|
|
|
if (count($input) > 2) { |
|
101
|
|
|
$app_dir = '.'; |
|
102
|
|
|
if (Prado::getApplication() !== null) { |
|
103
|
|
|
$app_dir = dirname(Prado::getApplication()->getBasePath()); |
|
104
|
|
|
} |
|
105
|
|
|
$args = [$input[0], $input[1], $input[2], $app_dir]; |
|
106
|
|
|
if (count($input) > 3) { |
|
107
|
|
|
$args[] = 'soap'; |
|
108
|
|
|
} |
|
109
|
|
|
if (count($input) > 4) { |
|
110
|
|
|
$args[] = 'overwrite'; |
|
111
|
|
|
} |
|
112
|
|
|
$cmd = new TActiveRecordGenAction; |
|
113
|
|
|
$cmd->performAction($args); |
|
114
|
|
|
} else { |
|
115
|
|
|
echo "\n Usage: generate table_name Application.pages.RecordClassName\n"; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $dir application directory |
|
121
|
|
|
* @return false|string |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function getAppDir($dir = ".") |
|
124
|
|
|
{ |
|
125
|
|
|
if (is_dir($dir)) { |
|
126
|
|
|
return realpath($dir); |
|
127
|
|
|
} |
|
128
|
|
|
if (false !== ($app_dir = realpath($dir . '/protected/')) && is_dir($app_dir)) { |
|
129
|
|
|
return $app_dir; |
|
130
|
|
|
} |
|
131
|
|
|
echo '** Unable to find directory "' . $dir . "\".\n"; |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $app_dir application directory |
|
137
|
|
|
* @return false|string |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getActiveRecordConfig($app_dir) |
|
140
|
|
|
{ |
|
141
|
|
|
if (false === ($xml = $this->getAppConfigFile($app_dir))) { |
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
if (false !== ($app = $this->initializePradoApplication($app_dir))) { |
|
|
|
|
|
|
145
|
|
|
foreach ($app->getModules() as $module) { |
|
146
|
|
|
if ($module instanceof TActiveRecordConfig) { |
|
147
|
|
|
return $module; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
echo '** Unable to find TActiveRecordConfig module in ' . $xml . "\n"; |
|
151
|
|
|
} |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $app_dir application directory |
|
157
|
|
|
* @param string $namespace output file in namespace format |
|
158
|
|
|
* @return false|string |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getOutputFile($app_dir, $namespace) |
|
161
|
|
|
{ |
|
162
|
|
|
if (is_file($namespace) && strpos($namespace, $app_dir) === 0) { |
|
163
|
|
|
return $namespace; |
|
164
|
|
|
} |
|
165
|
|
|
$file = Prado::getPathOfNamespace($namespace, ""); |
|
166
|
|
|
if ($file !== null && false !== ($path = realpath(dirname($file))) && is_dir($path)) { |
|
167
|
|
|
if (strpos($path, $app_dir) === 0) { |
|
168
|
|
|
return $file; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
echo '** Output file ' . $file . ' must be within directory ' . $app_dir . "\n"; |
|
172
|
|
|
return false; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|