public function action_index() { // clear redirect referrer \Session::delete('submitted_redirect'); // read flash message for display errors. $form_status = \Session::get_flash('form_status'); if (isset($form_status['form_status']) && isset($form_status['form_status_message'])) { $output['form_status'] = $form_status['form_status']; $output['form_status_message'] = $form_status['form_status_message']; } unset($form_status); // list tables $output['list_tables'] = \DB::list_tables(); // if form submitted if (\Input::method() == 'POST') { $table_name = trim(\Input::post('table_name')); $output['table_name'] = $table_name; if (!\Extension\NoCsrf::check()) { // validate token failed $output['form_status'] = 'error'; $output['form_status_message'] = \Lang::get('fslang_invalid_csrf_token'); } elseif ($table_name == null) { $output['form_status'] = 'error'; $output['form_status_message'] = \Lang::get('dbhelper_please_select_db_table'); } else { $output['list_columns'] = \DB::list_columns(\DB::expr('`' . $table_name . '`')); } } // endif; form submitted // <head> output --------------------------------------------------------------------- $output['page_title'] = $this->generateTitle(\Lang::get('dbhelper')); // <head> output --------------------------------------------------------------------- return $this->generatePage('admin/templates/index/index_v', $output, false); }
public function run($db = null) { $tables = \DB::list_tables(null, $db); \Cli::write('Found ' . count($tables) . ' database tables to generate models for.', 'green'); foreach ($tables as $table) { $this->generate_model($table, $db); } }
/** * Return a list of table name in the configured database. * * @return array the table list as: array('table_name' => 'table_class') */ public static function get_listing() { $rval = array(); $table_prefix = Table::get('crud.TBL_PREFIX', false); $tables = \DB::list_tables(); foreach ($tables as $table_name) { $table_class = 'bold'; if (!$table_prefix and strpos($table_name, '_') !== false) { $table_class = 'notice_text'; } if ($table_prefix and strpos($table_name, $table_prefix) === false) { $table_class = 'disabled_text'; } $rval[$table_name] = $table_class; } return $rval; }
?> <div class="bbody"> <input type="hidden" name="save" value="1" /> <table class="tables"> <tr> <td colspan="4" class="bfoot">Setting up the database is a very time consuming task. It may need several minutes to load the page completely!</td> </tr> <tr> <td width="40%"><strong>Table</strong></td> <td width="10%"><strong>Exists</strong></td> <td width="10%"><strong>Entries</strong></td> <td width="40%"><strong>Action</strong></td> </tr> <?php $path = 'package/' . $package . '/db/'; $tables = $db->list_tables(); $dh = opendir($path); while (($file = readdir($dh)) !== false) { $info = pathinfo($path . $file); if ($info['extension'] == 'sql') { $basename = substr($info['basename'], 0, -(strlen($info['extension']) + ($info['extension'] == '' ? 0 : 1))); $t = $db->pre . $basename; unset($counter); $select = array(); if (in_array($t, $tables)) { $exists = '<span class="hl_false">Yes</span>'; $result = $db->query('SELECT COUNT(*) AS c FROM ' . $t); $counter = $db->fetch_assoc($result); $entries = $counter['c']; $select[] = '<option value="3" selected="selected">Delete and recreate table</option>'; $select[] = '<option value="2">Do not change table</option>';
/** * Generate model for a database table. * * Usage (from command line): * * php oil refine fromdb:model <table_name,table_name...> */ public static function model($tables = '') { // do we have any tables defined? if (empty($tables)) { // do we want to generate for all tables? if (!\Cli::option('all', false)) { \Cli::write('No table names specified to generate a model on.', 'red'); exit; } // get the list of all available tables try { $list = \DB::list_tables(null, \Cli::option('db', null)); } catch (\FuelException $e) { \Cli::write('The database driver configured does not support listing tables. Please specify them manually.', 'red'); exit; } $prefix = \DB::table_prefix(); $migration = \Config::get('migrations.table', 'migration'); $tables = array(); // create the table list foreach ($list as $table) { // strip any defined table prefix from the table name if (!empty($prefix) and strpos($table, $prefix) === 0) { $table = substr($table, strlen($prefix)); } // skip the migration table $table == $migration or $tables[] = $table; } } // make sure we have an array to work with is_array($tables) or $tables = explode(',', $tables); // generate for each table defined foreach ($tables as $table) { // start with an empty list \Oil\Generate::$create_files = array(); // and generate call_user_func('Oil\\Generate::model', static::arguments($table)); } }
} else { $offsetrow = $_GET["offsetrow"]; } //timeout de 5 secondes par defaut, -1 pour utiliser sans timeout if (!isset($_GET["duree"])) { $duree = $defaulttimeout; } else { $duree = $_GET["duree"]; } //Limite de lignes a dumper a chaque fois if (!isset($_GET["rowlimit"])) { $rowlimit = $defaultrowlimit; } else { $rowlimit = $_GET["rowlimit"]; } $tab = $DB->list_tables(); $tot = $DB->numrows($tab); if (isset($offsettable)) { if ($offsettable >= 0) { $percent = min(100, round(100 * $offsettable / $tot, 0)); } else { $percent = 100; } } else { $percent = 0; } $conv_utf8 = false; $complete_utf8 = true; $config_table = "glpi_config"; if (TableExists("glpi_configs")) { $config_table = "glpi_configs";
/** * Generate MySQL Documentation for HTML. * * Usage (from command line): * * php oil refine mydoc:html <table_schema> <output_dir = "app/tmp/"> */ public static function html($table_schema = null, $dir = null) { if (empty($table_schema)) { static::help(); exit; } empty($dir) and $dir = APPPATH . 'tmp' . DS; $dir = rtrim($dir, DS) . DS . 'mydoc' . DS; /** * connect to db */ $ret = static::connect($table_schema); /** * delete and create mydoc dir */ if (file_exists($dir)) { if (!\Cli::option('f') and !\Cli::option('force')) { \Cli::write(realpath($dir) . ' already exist, please use -f option to force delete and generate.', 'red'); exit; } $ret = \File::delete_dir($dir); if ($ret === false) { \Cli::write("Could not delete directory \"{$dir}\"", 'red'); exit; } } $ret = mkdir($dir, 0777, true); if ($ret === false) { \Cli::write("Could not create directory \"{$dir}\"", 'red'); exit; } \File::copy_dir(__DIR__ . DS . '..' . DS . 'assets', $dir . 'assets'); /** * generate index.html */ $migration_table_name = \Config::get('migrations.table', 'migration'); $migration = array(); if (\DBUtil::table_exists($migration_table_name)) { $migration = \Db::select()->from($migration_table_name)->order_by('migration', 'desc')->limit(1)->execute()->as_array(); } $html = \View::forge('mydoc/index', array('migration' => $migration))->render(); \File::create($dir, 'index.html', $html); /** * get tables */ $tables = array_flip(\DB::list_tables()); /** * unset ignore tables */ foreach (\Config::get('mydoc.ignore_tables', array()) as $ignore_table_name) { if (isset($tables[$ignore_table_name])) { unset($tables[$ignore_table_name]); } } $ignore_table_regex = \Config::get('mydoc.ignore_table_regex'); foreach ($tables as $table_name => $tmp) { if (!empty($ignore_table_regex)) { if (preg_match($ignore_table_regex, $table_name)) { unset($tables[$table_name]); continue; } } $tables[$table_name] = array('indexes' => array(), 'foreign_keys' => array(), 'triggers' => array()); } /** * check table count */ if (count($tables) === 0) { \Cli::write("No tables in \"{$table_schema}\"", 'red'); exit; } /** * get foreign keys */ $sql = 'select distinct table_name, column_name, referenced_table_name, referenced_column_name from information_schema.key_column_usage where referenced_table_name is not null and referenced_column_name is not null and table_schema = :table_schema'; $foreign_keys = \Db::query($sql)->bind('table_schema', $table_schema)->execute()->as_array(); foreach ($foreign_keys as $foreign_key) { if (isset($tables[$foreign_key['table_name']])) { $tables[$foreign_key['table_name']]['foreign_keys'][$foreign_key['column_name']] = $foreign_key; } } /** * get indexes */ $sql = 'select distinct table_name, index_name, non_unique, column_name, comment from information_schema.statistics where table_schema = :table_schema'; $indexes = \Db::query($sql)->bind('table_schema', $table_schema)->execute()->as_array(); foreach ($indexes as $index) { if (isset($tables[$index['table_name']])) { $tables[$index['table_name']]['indexes'][$index['index_name']][$index['column_name']] = $index; } } /** * get triggers */ $sql = 'select distinct trigger_name, event_manipulation, event_object_table, action_statement, action_timing, definer from information_schema.triggers where trigger_schema = :trigger_schema'; $triggers = \Db::query($sql)->bind('trigger_schema', $table_schema)->execute()->as_array(); foreach ($triggers as $trigger) { if (isset($tables[$trigger['event_object_table']])) { $tables[$trigger['event_object_table']]['triggers'][] = $trigger; } } /** * generate tables.html */ $html = \View::forge('mydoc/tables', array('tables' => array_keys($tables)))->render(); \File::create($dir, 'tables.html', $html); /** * generate table_*.html */ foreach ($tables as $table_name => $infos) { $columns = \DB::list_columns($table_name); foreach ($columns as &$column) { // do we have a data_type defined? If not, use the generic type isset($column['data_type']) or $column['data_type'] = $column['type']; if ($column['data_type'] == 'enum') { $column['data_type'] .= "('" . implode("', '", $column['options']) . "')"; } $column['_length'] = null; foreach (array('length', 'character_maximum_length', 'display') as $idx) { // check if we have such a column, and filter out some default values if (isset($column[$idx]) and !in_array($column[$idx], array('65535', '16777215', '4294967295'))) { $column['_length'] = $column[$idx]; break; } } $column['_extras'] = array(); if (strpos(\Str::lower($column['key']), 'pri') !== false) { $column['_extras'][] = 'PK'; } if (strpos(\Str::lower($column['key']), 'uni') !== false) { $column['_extras'][] = 'UI'; } if (!empty($column['extra'])) { if (strpos($column['extra'], 'auto_increment') !== false) { $column['_extras'][] = 'AI'; } } $column['_foreign_key'] = null; if (!empty($infos['foreign_keys'])) { $foreign_key = \Arr::get($infos['foreign_keys'], $column['name'], array()); if (!empty($foreign_key)) { $column['_foreign_key'] = $foreign_key; $column['_extras'][] = 'FK'; } } if (!empty($column['_foreign_key'])) { $column['_parent_table_name'] = $column['_foreign_key']['referenced_table_name']; } else { $column['_foreign_key'] = array('referenced_table_name' => null, 'referenced_column_name' => null); if (0 < preg_match('/^.+_id$/', $column['name'])) { $parent_table_name = str_replace('_id', '', $column['name']); if (isset($tables[$parent_table_name = \Inflector::singularize($parent_table_name)])) { $column['_foreign_key'] = array('referenced_table_name' => $parent_table_name, 'referenced_column_name' => 'id'); } else { if (isset($tables[$parent_table_name = \Inflector::pluralize($parent_table_name)])) { $column['_foreign_key'] = array('referenced_table_name' => $parent_table_name, 'referenced_column_name' => 'id'); } } } } } $html = \View::forge('mydoc/table', array('table_name' => $table_name, 'columns' => $columns, 'infos' => $infos))->render(); \File::create($dir, 'table_' . $table_name . '.html', $html); } /** * generate indexes.html */ $html = \View::forge('mydoc/indexes', array('tables' => $tables))->render(); \File::create($dir, 'indexes.html', $html); /** * generate triggers.html */ $html = \View::forge('mydoc/triggers', array('tables' => $tables))->render(); \File::create($dir, 'triggers.html', $html); \Cli::write("Generated MySQL Documentation in \"{$dir}\"", 'green'); exit; }
/** * Load database configuration. * * @return bool returns true on success or sets error messages and returns false. */ public static function database() { // load database config if (!\Config::load('db', true)) { Error::set(CRUDE_ERROR, 'Fuel database configuration file not found.'); Error::set(CRUDE_SOLUTION, 'Check that the database configuration file <code>APPPATH' . DS . 'config' . DS . 'db.php</code> exists and is properly formatted. See ' . \Html::anchor('http://fuelphp.com/docs/classes/database/introduction.html', 'Fuel documentation', array('target' => '_blank'))); return false; } // check database connection. Thanks, Jelmer. try { @\Database_Connection::instance()->connect(); } catch (\Database_Exception $e) { // can't seem to properly catch database authentication errors // hack to trap authentication error. there are probably other errors involved here. $msg = $e->getMessage(); if (empty($msg)) { $msg = 'Access to database <code>' . \Config::get('db.' . \Config::get('environment') . '.connection.database') . '</code> was denied.'; } $msg = str_replace('\'', '"', $msg); Error::set(CRUDE_FUEL_ERR, $msg); Error::set(CRUDE_SOLUTION, 'Check that the database configuration file <code>APPPATH' . DS . 'config' . DS . 'db.php</code> contains the correct information to connect to your database. See ' . \Html::anchor('http://fuelphp.com/docs/classes/database/introduction.html', 'Fuel documentation', array('target' => '_blank'))); return false; } // check that tables exist in the database $tables = \DB::list_tables(); if (empty($tables)) { Error::set(CRUDE_ERROR, 'No tables found in database <code>' . \Config::get('db.' . \Config::get('environment') . '.connection.database') . '.</code>'); Error::set(CRUDE_SOLUTION, 'There must be at least one table in the configured database for Crude CRUD to work.'); return false; } return true; }
public function upgrade_pre_004() { DB::register_table('blogroll'); DB::register_table('bloginfo'); DB::register_table('tag2blog'); if (!in_array(DB::table('blogroll'), DB::list_tables())) { Options::set('blogroll__api_version', self::API_VERSION); return; } Post::add_new_type(self::CONTENT_TYPE); $opml = new \SimpleXMLElement('<opml version="1.1"></opml>'); $head = $opml->addChild('head'); $head->addChild('title', Options::get('title')); $head->addChild('dateCreated', gmdate('D, d M Y G:i:s e')); $body = $opml->addChild('body'); $blogs = DB::get_results("SELECT * FROM {blogroll}", array()); foreach ($blogs as $blog) { $outline = $body->addChild('outline'); $outline->addAttribute('text', $blog->name); $outline->addAttribute('htmlUrl', $blog->url); $outline->addAttribute('xmlUrl', $blog->feed); $outline->addAttribute('relation', $blog->rel); $outline->addAttribute('updated', $blog->updated); $outline->addAttribute('content', $blog->description); $outline->addAttribute('type', 'link'); } try { $count = $this->import_opml($opml->body); DB::query('DROP TABLE IF EXISTS {blogroll}'); DB::query('DROP TABLE IF EXISTS {bloginfo}'); DB::query('DROP TABLE IF EXISTS {tag2blog}'); EventLog::log(sprintf(_n('Imported %d blog from previous Blogroll version, and removed obsolete tables', 'Imported %d blogs from previous Blogroll version, and removed obsolete tables', $count, self::DOMAIN), $count)); } catch (\Exception $e) { EventLog::log(_t('Could not Import previous data. please import manually and drop tables.', self::DOMAIN)); } Options::delete('blogroll__db_version'); Options::set('blogroll__api_version', self::API_VERSION); Options::set('blogroll__sort_by', 'id'); }