summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Wsat/TWsatBaseGenerator.php
blob: 673031cb32f559dc50e669dc91483c85a4c55972 (plain)
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
<?php

/**
 * @author Daniel Sampedro Bello <darthdaniel85@gmail.com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @version $Id$
 * @since 3.3
 * @package System.Wsat
 */
Prado::using('System.Data.Common.TDbMetaData');

class TWsatBaseGenerator
{

    /**
     * @return TDbMetaData for retrieving metadata information, such as
     * table and columns information, from a database connection.
     */
    protected $_dbMetaData;

    /**
     * Output folder where AR classes will be saved.
     */
    protected $_opFile;

    function __construct()
    {
        if (!class_exists("TActiveRecordManager", false))
            throw new Exception("You need to enable the ActiveRecord module in your application configuration file.");
        $ar_manager = TActiveRecordManager::getInstance();
        $_conn = $ar_manager->getDbConnection();
        $_conn->Active = true;
        $this->_dbMetaData = TDbMetaData::getInstance($_conn);
    }

    public function setOpFile($op_file_namespace)
    {
        $op_file = Prado::getPathOfNamespace($op_file_namespace);
        if (empty($op_file))
            throw new Exception("You need to fix your output folder namespace.");
        if (!is_dir($op_file))
            mkdir($op_file, 0777, true);
        $this->_opFile = $op_file;
    }

    public function renderAllTablesInformation()
    {
        foreach ($this->getAllTableNames() as $table_name)
        {
            echo $table_name . "<br>";
            $tableInfo = $this->_dbMetaData->getTableInfo($table_name);
            echo "Table info:" . "<br>";
            echo "<pre>";
            print_r($tableInfo);
            echo "</pre>";
        }
    }

    public function getAllTableNames()
    {
        $tableNames = $this->_dbMetaData->findTableNames();
        $index = array_search('pradocache', $tableNames);
        if ($index)
            array_splice($tableNames, $index, 1);
        return $tableNames;
    }

    public static function pr($data)
    {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
    }

    protected function eq($data)
    {
        return '"' . $data . '"';
    }

}