Пример #1
0
 /**
  * Generate database documentation
  *
  * @access public
  * @return Response
  */
 public function action_generate()
 {
     $this->template->active = 'generate';
     $data = array();
     if (Input::method() === 'POST') {
         /**
          * get Fieldset instance
          */
         $platform = Input::post('platform');
         $fieldset = static::_get_form($platform);
         /**
          * validation
          */
         $validation = $fieldset->validation();
         if ($validation->run(Input::post())) {
             $config = $validated = $validation->validated();
             Arr::delete($config, 'platform');
             Arr::delete($config, 'name');
             Arr::delete($config, 'submit');
             Arr::delete($config, Config::get('security.csrf_token_key'));
             /**
              * directory
              */
             $dir = DOCROOT . 'dbdocs' . DS . $validated['name'] . DS . 'dbdoc' . DS;
             /**
              * generate
              */
             $dd = Dbdocs::forge('default', $config);
             try {
                 $ret = $dd->generate($dir, true);
                 if ($ret !== true) {
                     Session::set_flash('error', 'System error occurred.');
                 }
                 Session::set_flash('success', 'Generated database documentation "' . $validated['name'] . '" !!');
             } catch (Exception $e) {
                 Session::set_flash('error', 'System error occurred.');
             }
             Response::redirect();
         } else {
             $validated = $validation->validated();
             $data['platform'] = $platform;
             Arr::delete($validated, Config::get('security.csrf_token_key'));
             $data['form'] = $fieldset->populate($validated);
             $this->template->content = View::forge('index/generate', array('data' => $data));
             Session::set_flash('error', $validation->error());
         }
     } else {
         $this->template->content = View::forge('index/generate', array('data' => $data));
     }
 }
Пример #2
0
 /**
  * Generate process
  */
 private static function process()
 {
     /**
      * interactive mode?
      */
     if (\Cli::option('n', false) or \Cli::option('non-interactive', false)) {
         /**
          * option values into config
          */
         foreach (static::$config as $k => &$v) {
             if ($k != 'driver') {
                 $v = \Cli::option($k);
             }
         }
     } else {
         /**
          * enter values into config
          */
         static::prompt();
         static::confirm();
     }
     /**
      * generate documentation
      */
     $dd = \Dbdocs::forge('default', static::$config);
     $ret = $dd->generate(static::$dir, \Cli::option('f') or \Cli::option('force'));
     if ($ret === true) {
         \Cli::write('Generated MySQL Documentation in "' . static::$dir . '"', 'green');
     } else {
         if (is_string($ret)) {
             \Cli::write($ret, 'red');
         } else {
             \Cli::write('System error occurred.', 'red');
         }
     }
 }
Пример #3
0
 /**
  * Test for Dbdocs::forge()
  */
 public function test_forge()
 {
     static::$dd = Dbdocs::forge(\Str::random('unique'), static::$config);
     $this->assertTrue(static::$dd instanceof Dbdocs);
 }
Пример #4
0
<?php

return array('ignore_table_names' => array(Config::get('migrations.table', 'migration')), 'ignore_table_name_regex' => '', 'functions' => array('mod_comment' => function ($comment, $column_name, $table_name) {
    /* @var $dd \Dbdocs\Dbdocs */
    $dd = Dbdocs::instance('default');
    if (0 < preg_match('/^[0-9a-zA-Z_]+\\.[0-9a-zA-Z_]+$/', $comment)) {
        list($c_table_name, $c_column_name) = explode('.', $comment);
        $comment = "<a href=\"table_{$c_table_name}.html#_column_{$c_column_name}\">" . $comment . '</a>' . "\n<span class=\"_foreign_key\" title=\"{$comment}\" ><i class=\"icon-question-sign\"></i></span>";
    }
    return $comment;
}, 'mod_foreign_key' => function ($column_name, $table_name) {
    $ret = array();
    /* @var $dd \Dbdocs\Dbdocs */
    $dd = Dbdocs::instance('default');
    if (isset($dd->fuel_relations[$table_name]['belongs_to'])) {
        foreach ($dd->fuel_relations[$table_name]['belongs_to'] as $belongs_to) {
            if ($belongs_to['key_from'] == $column_name) {
                $ret['table_name'] = $belongs_to['table_to'];
                $ret['column_name'] = $belongs_to['key_to'];
                break;
            }
        }
    }
    if (empty($ret) and 0 < preg_match('/^.+_id$/', $column_name)) {
        $table_name = str_replace('_id', '', $column_name);
        if (in_array(Inflector::singularize($table_name), $dd->sm->listTableNames())) {
            $ret['table_name'] = Inflector::singularize($table_name);
            $ret['column_name'] = 'id';
        } else {
            if (in_array(Inflector::pluralize($table_name), $dd->sm->listTableNames())) {
                $ret['table_name'] = Inflector::pluralize($table_name);
Пример #5
0
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     /**
      * get config
      */
     if (isset($_SERVER['TRAVIS_DB'])) {
         static::$config = static::$travis_dbs[$_SERVER['TRAVIS_DB']];
     } else {
         static::$config = (include APPPATH . 'config' . DS . \Fuel::DEVELOPMENT . DS . 'dbdocs.php');
         static::$config = \Arr::get(static::$config, 'test_config.db');
     }
     /**
      * connect to db
      */
     static::$dd = Dbdocs::forge('default', static::$config);
     /**
      * drop views
      */
     krsort(static::$test_views);
     foreach (static::$test_views as $test_view_name => $sql) {
         try {
             //TODO: check view exist
             static::$dd->sm->dropView($test_view_name);
         } catch (\Doctrine\DBAL\DBALException $e) {
             //do nothing
         }
     }
     /**
      * drop tables
      */
     krsort(static::$test_tables);
     foreach (static::$test_tables as $test_table_name => $infos) {
         static::$dd->sm->tablesExist($test_table_name) and static::$dd->sm->dropTable($test_table_name);
     }
     /**
      * create tables
      */
     ksort(static::$test_tables);
     foreach (static::$test_tables as $test_table_name => $infos) {
         $table = new \Doctrine\DBAL\Schema\Table($test_table_name);
         foreach ($infos['columns'] as $column) {
             empty($column['options']) and $column['options'] = array();
             $table->addColumn($column['name'], $column['type'], $column['options']);
         }
         $table->setPrimaryKey($infos['primary_keys']);
         if (!empty($infos['foreign_keys'])) {
             foreach ($infos['foreign_keys'] as $foreign_key) {
                 list($to_table_name, $to_column_name) = explode(".", $foreign_key['to']);
                 $table->addForeignKeyConstraint($to_table_name, array($foreign_key['from']), array($to_column_name));
             }
         }
         static::$dd->sm->createTable($table);
     }
     /**
      * create views
      */
     ksort(static::$test_views);
     foreach (static::$test_views as $test_view_name => $sql) {
         $view = new \Doctrine\DBAL\Schema\View($test_view_name, $sql);
         static::$dd->sm->createView($view);
     }
 }
Пример #6
0
    public function view()
    {
        /* @var $table \Doctrine\DBAL\Schema\Table */
        $table = $this->__table;
        $this->table_name = $table->getName();
        $this->columns = array();
        $indexes = $table->getIndexes();
        $foreign_keys = $table->getForeignKeys();
        $functions = \Config::get('dbdocs.functions');
        foreach ($table->getColumns() as $column) {
            /* @var $column \Doctrine\DBAL\Schema\Column */
            $comment = \Security::htmlentities($column->getComment());
            if (is_callable($functions['mod_comment'])) {
                $comment = $functions['mod_comment']($comment, $column->getName(), $table->getName());
            }
            $this->columns[$column->getName()] = array('type' => $column->getType()->getName(), 'length' => $column->getLength(), 'null' => !$column->getNotnull(), 'default' => $column->getDefault(), 'comment' => $comment, 'extras' => array());
            foreach ($indexes as $index) {
                if (in_array($column->getName(), $index->getColumns())) {
                    if ($index->isPrimary()) {
                        $this->columns[$column->getName()]['extras'][] = 'PK';
                    } else {
                        if ($index->isUnique()) {
                            $this->columns[$column->getName()]['extras'][] = 'UI';
                        } else {
                            if ($index->isSimpleIndex()) {
                                $this->columns[$column->getName()]['extras'][] = 'I';
                            }
                        }
                    }
                }
            }
            $column->getAutoincrement() and $this->columns[$column->getName()]['extras'][] = 'AI';
            $column->getUnsigned() and $this->columns[$column->getName()]['extras'][] = 'UN';
            foreach ($foreign_keys as $foreign_key) {
                /* @var $foreign_key \Doctrine\DBAL\Schema\ForeignKeyConstraint */
                if (in_array($column->getName(), $foreign_key->getColumns())) {
                    $flip = array_flip($this->columns[$column->getName()]['extras']);
                    if (isset($flip['I'])) {
                        unset($flip['I']);
                        $this->columns[$column->getName()]['extras'] = array_flip($flip);
                    }
                    $this->columns[$column->getName()]['extras'][] = 'FK';
                    $this->columns[$column->getName()]['foreign_key']['table_name'] = $foreign_key->getForeignTableName();
                    $foreign_key_columns = $foreign_key->getForeignColumns();
                    $this->columns[$column->getName()]['foreign_key']['column_name'] = $foreign_key_columns[0];
                }
            }
            if (!isset($this->columns[$column->getName()]['foreign_key'])) {
                if (is_callable($functions['mod_foreign_key'])) {
                    $this->columns[$column->getName()]['foreign_key'] = $functions['mod_foreign_key']($column->getName(), $table->getName());
                }
            }
        }
        /**
         * get real information of columns
         */
        $dd = Dbdocs::instance('default');
        $platform = $dd->conn->getDatabasePlatform()->getName();
        switch ($platform) {
            case 'mysql':
                $rows = $dd->conn->executeQuery('
					select
						*
					from
						information_schema.columns
					where
						table_schema = :table_schema
					and
						table_name = :table_name
					order by
						ordinal_position', array('table_name' => $this->table_name, 'table_schema' => $dd->conn->getDatabase()))->fetchAll();
                foreach ($rows as $row) {
                    $real_columns[$row['COLUMN_NAME']] = array('type' => $row['DATA_TYPE'], 'length' => $row['CHARACTER_MAXIMUM_LENGTH'], 'default' => $row['COLUMN_DEFAULT']);
                }
                break;
            default:
                $real_columns = array();
                break;
        }
        $this->set('columns', \Arr::merge($this->columns, $real_columns), false);
        $this->indexes = static::merge_indexes_and_foreign_keys($indexes, $foreign_keys, $table);
    }