summaryrefslogtreecommitdiff
path: root/framework/Wsat/TWsatARGenerator.php
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Wsat/TWsatARGenerator.php')
-rw-r--r--framework/Wsat/TWsatARGenerator.php136
1 files changed, 136 insertions, 0 deletions
diff --git a/framework/Wsat/TWsatARGenerator.php b/framework/Wsat/TWsatARGenerator.php
new file mode 100644
index 00000000..7b39fce1
--- /dev/null
+++ b/framework/Wsat/TWsatARGenerator.php
@@ -0,0 +1,136 @@
+<?php
+
+/**
+ * Description of TWsatARGenerator
+ *
+ * @author Daniel
+ */
+class TWsatARGenerator {
+
+ /**
+ * @return TActiveRecordManager static instance of record manager.
+ */
+ private $_ar_manager;
+
+ /**
+ * Output folder (by namespace) where AR classes will be generated.
+ */
+ private $_opnamespace;
+
+ function __construct($_opnamespace = "Application.App_Data.AR_Classes") {
+ $this->_ar_manager = TActiveRecordManager::getInstance();
+ $this->_opnamespace = $_opnamespace;
+ }
+
+ // <editor-fold defaultstate="collapsed" desc="Main APIs">
+ public function generate($tableName, $clasName = '') {
+ $conn = $this->_ar_manager->getDbConnection();
+ $gateway = $this->_ar_manager->getRecordGateway();
+ $tableInfo = $gateway->getTableInfo($conn, $tableName);
+
+ if (count($tableInfo->getColumns()) === 0) {
+ throw new TIOException("Unable to find table or view $tableName in " . $conn->getConnectionString() . ".");
+ } else {
+ $properties = array();
+ foreach ($tableInfo->getColumns() as $field => $column)
+ $properties[] = $this->generateProperty($field, $column);
+ }
+
+ $clasName = empty($clasName) ? $this->_getClassName($tableName) : $clasName;
+ $class = $this->generateClass($properties, $tableName, $clasName);
+ $op_file = Prado::getPathOfNamespace($this->_opnamespace);
+ if (!is_dir($op_file)) {
+ mkdir($op_file, 0777, true);
+ }
+
+ $output = $op_file . DIRECTORY_SEPARATOR . $clasName . ".php";
+ file_put_contents($output, $class);
+ }
+
+ public function generateAll() {
+ foreach ($this->_getAllTableNames() as $tableName) {
+ $this->generate($tableName);
+ }
+ }
+
+// </editor-fold>
+//-----------------------------------------------------------------------------
+ // <editor-fold defaultstate="collapsed" desc="Common Methods">
+ private function getDbConnection() {
+ $con = $this->_ar_manager->getDbConnection();
+ $con->Active = true;
+ return $con;
+ }
+
+ private function _getAllTableNames() {
+ $con = $this->getDbConnection();
+ $command = $con->createCommand("Show Tables");
+ $dataReader = $command->query();
+ $dataReader->bindColumn(1, $table);
+ $tables = array();
+ while ($dataReader->read()) {
+ $tables[] = $table;
+ }
+ $con->setActive(false);
+ return $tables;
+ }
+
+ private function _getClassName($tableName) {
+ return ucfirst($tableName);
+ }
+
+ public function renderAllTablesInformation() {
+ $conn = $this->_ar_manager->getDbConnection();
+ $gateway = $this->_ar_manager->getRecordGateway();
+
+ foreach ($this->_getAllTableNames() as $table_name) {
+ echo $table_name . "<br>";
+
+ $tableInfo = $gateway->getTableInfo($conn, $table_name);
+ echo "Table info:" . "<br>";
+ echo "<pre>";
+ var_dump($tableInfo);
+ echo "</pre>";
+ }
+ }
+
+//-----------------------------------------------------------------------------
+
+ protected function generateProperty($field, $column) {
+ $prop = '';
+ $name = '$' . $field;
+
+ /* TODO use in version 2.0 */
+ // $type = $column->getPHPType();
+
+ $prop .= "\tpublic $name;";
+ return $prop;
+ }
+
+ protected function generateClass($properties, $tablename, $class) {
+ $props = implode("\n", $properties);
+ $date = date('Y-m-d h:i:s');
+ return <<<EOD
+<?php
+/**
+ * Auto generated by prado-cli.php on $date.
+ */
+class $class extends TActiveRecord
+{
+ const TABLE='$tablename';
+
+$props
+
+ public static function finder(\$className=__CLASS__)
+ {
+ return parent::finder(\$className);
+ }
+}
+?>
+EOD;
+ }
+
+// </editor-fold>
+}
+
+?>