public function testFetchDB() { //Test to make sure DB is fetched correctly $db = DinklyDataConnector::fetchDB(); $new_db = new PDO($this->dsn, $this->username, $this->password); $this->assertEquals($new_db, $db); }
function getDbStructure($schema, $model_name = null, $verbose_output = true) { //set active connection to schema and stop if there is no matching schema if (!DinklyDataConfig::setActiveConnection($schema)) { echo "\nNo such schema in config/db.yml\n"; return false; } //Connect to database $db = DinklyDataConnector::fetchDB(); $creds = DinklyDataConfig::getDBCreds(); $db_name = $creds["name"]; $db_name = DinklyBuilder::sanitize($db, $db_name); $model_name = DinklyBuilder::sanitize($db, $model_name); //get columns from specified table or all tables if ($model_name) { $stmt = $db->prepare("SHOW COLUMNS FROM " . $model_name . ""); $stmt->execute(); $table_schema = $stmt->fetchAll(); echo "\nRead table " . $model_name . " columns...\n"; sqlToYml($table_schema, $model_name, $db_name); } else { $stmt = $db->prepare("SHOW TABLES"); $stmt->execute(); $table_names = $stmt->fetchAll(); foreach ($table_names as $table_array) { $table_name = $table_array[0]; $stmt = $db->prepare("SHOW COLUMNS FROM " . $table_name . ""); $stmt->execute(); $table_schema = $stmt->fetchAll(); echo "\nRead table " . $table_name . " columns...\n"; sqlToYml($table_schema, $table_name, $db_name); } } }
/** * Default Constructor * */ public function __construct() { //Let's make this accessible across the admin for display of all dates $this->date_format = null; //We use this for the profile modal $this->logged_user = null; $this->db = DinklyDataConnector::fetchDB(); if (DinklyUser::isLoggedIn()) { $this->logged_user = new DinklyUser(); $this->logged_user->init(DinklyUser::getAuthSessionValue('logged_id')); $this->date_format = $this->date_format = $this->logged_user->getDateFormat() . ' ' . $this->logged_user->getTimeFormat(); return false; } else { if (Dinkly::getCurrentModule() != 'login') { $this->loadModule('admin', 'login', 'default', true, true); } } return true; }
/** * Create missing tables in database based on yaml configurations * * @param string $schema String name of schema to be added * @param string $verbose_output string used to log added models * * */ public static function addMissingModelsToDb($schema, $plugin_name = null, $verbose_output = null) { if (!DinklyDataConfig::setActiveConnection($schema)) { return false; } //If no DB exists, create one try { $db = DinklyDataConnector::fetchDB(); } catch (PDOException $e) { echo 'exception thrown :' . $e->getCode(); if ($e->getCode() == 1049) { $creds = DinklyDataConfig::getDBCreds(); self::createDb($creds['name'], $creds); $db = DinklyDataConnector::fetchDB(); } } $model_names = array(); if ($plugin_name) { $model_names = self::getAllPluginModels($plugin_name, $schema); } else { $model_names = self::getAllModels($schema); } if ($model_names != array()) { //Gather up yaml table names for each model $yaml_table_names = array(); foreach ($model_names as $model) { $model_yaml = self::parseModelYaml($schema, $model, $plugin_name, false); $yaml_table_names[] = $model_yaml['table_name']; } //Gather up the table names for those in the database currently $query = "show tables;"; $results = $db->query($query)->fetchAll(); $db_table_names = array(); foreach ($results as $row) { $db_table_names[] = $row[0]; } //Find out which tables are missing from the db, but defined in the yaml $missing_tables = array(); foreach ($yaml_table_names as $yaml_table_name) { if (!in_array($yaml_table_name, $db_table_names)) { $missing_tables[] = $yaml_table_name; } } //Create our missing tables in the database foreach ($missing_tables as $table) { if ($verbose_output) { echo "Creating table " . $table . "...\n"; } self::buildTable($schema, Dinkly::convertToCamelCase($table, true), $plugin_name, null, $verbose_output, null); } } }