/** * function for inserting user * @param array $values * @return boolean $res */ function useradd_db_insert($values) { $database = admin::getDbInfo(conf::getMainIni('url')); if (!$database) { return db_no_url(); } $db = new db(); $res = $db->insert('account', $values); return $res; }
/** * function for inserting user * @param array $values * @return boolean $res */ function useradd_db_insert($values) { $database = admin::getDbInfo(); if (!$database) { return db_no_url(); } admin::changeDB($database['dbname']); $db = new db(); $res = $db->insert('account', $values); return $res; }
function db_to_sqlite($options = array()) { $check = "which sequel"; if (common::execCommand($check)) { common::echoMessage('You need sequel. Install it like this, e.g.:'); common::echoMessage('sudo aptitude install ruby-sequel libsqlite3-ruby libmysql-ruby'); common::abort(); } else { common::echoStatus('OK', 'g', 'Sequel is installed'); } $ok = false; $info = admin::getDbInfo(); if (!$info) { return db_no_url(); } if ($info['scheme'] == 'mysql') { $ok = true; } if ($info['scheme'] == 'mysqli') { $ok = true; } if (!$ok) { common::echoStatus('ERROR', 'r', 'Driver needs to be mysql or mysqli'); } $fs = new Filesystem(); $fs->remove('sqlite/database.sql'); $username = conf::getMainIni('username'); $password = conf::getMainIni('password'); $command = "sequel "; $command .= "{$info['scheme']}://{$username}:{$password}@{$info['host']}/{$info['dbname']} "; $command .= "-C "; $command .= "sqlite://sqlite/database.sql"; $ret = common::systemCommand($command); $base = conf::pathBase(); if (!$ret) { $fs->chmod('sqlite/database.sql', 0777, 00, true); common::echoMessage('Sqlite database created. Edit config.ini and add:'); common::echoMessage("sqlite:/{$base}/sqlite/database.sql"); } }
/** * function for loading a database file into db specified in config.ini * * @param array options. You can specifiy a file to load in options. * e.g. <code>$options = array('File' => 'backup/sql/latest.sql')</code> * @return int the executed commands shell status 0 on success. */ function cos_db_load_table($options) { if (!isset($options['table'])) { common::abort('Specify a table to load with a backup'); } $dump_dir = "backup/sql/{$options['table']}"; if (!file_exists($dump_dir)) { common::abort('Yet no backups'); } $search = conf::pathBase() . "/backup/sql/{$options['table']}"; $latest = get_latest_db_dump($search); if ($latest == 0) { common::abort('Yet no database dumps'); } $latest = "backup/sql/{$options['table']}/" . $latest . ".sql"; $db = admin::getDbInfo(); if (!$db) { return db_no_url(); } $command = "mysql --default-character-set=utf8 -u" . conf::$vars['coscms_main']['username'] . " -p" . conf::$vars['coscms_main']['password'] . " {$db['dbname']} < {$latest}"; return $ret = common::execCommand($command); }
function clone_db($options = array()) { if (!isset($options['File'])) { common::abort('Specify new database name'); } $db = admin::getDbInfo(conf::getMainIni('url')); $old = $db['dbname']; $new_name = $options['File']; admin::cloneDB($old, $new_name); }
/** * Checks if any table exist in database * @return boolean */ public static function tablesExists() { $db = new db(); $ret = $db->connect(array('dont_die' => 1)); if ($ret == 'NO_DB_CONN') { return false; } $info = admin::getDbInfo(); if (!$info) { common::echoMessage('No databse url in config.ini'); } if ($info['scheme'] == 'mysql' || $info['scheme'] == 'mysqli') { $rows = $db->selectQuery("SHOW TABLES"); if (empty($rows)) { return false; } return true; } if ($info['scheme'] == 'sqlite') { $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='modules'"; $rows = $db->selectQuery($sql); if (empty($rows)) { return false; } return true; } }
/** * checks if a field exists in a table * @param string $table the db table * @param string $field the table field * @return boolean $res true if the field exists false if not. */ public function fieldExists($table, $field) { $info = admin::getDbInfo(); if (!$info) { return false; } if ($info['scheme'] == 'mysql' || $info['scheme'] == 'mysqli') { $sql = "SHOW COLUMNS FROM `{$table}` LIKE '{$field}'"; $rows = $this->selectQuery($sql); if (!empty($rows)) { return true; } else { return false; } } if ($info['scheme'] == 'sqlite') { $stmt = $this->rawQuery("SELECT * FROM {$table} LIMIT 1"); $fields = array_keys($stmt->fetch(PDO::FETCH_ASSOC)); if (in_array($field, $fields)) { return true; } else { return false; } } }
/** * creates a database from config params (url, password, username) * @param array $options * @return int $res result from exec operation */ public static function createDB($options = array()) { $db = admin::getDbInfo(); if (!$db) { return db_no_url(); } $command = "mysqladmin -u" . conf::$vars['coscms_main']['username'] . " -p" . conf::$vars['coscms_main']['password'] . " -h{$db['host']} "; $command .= "--default-character-set=utf8 "; $command .= "CREATE {$db['dbname']}"; return $ret = common::execCommand($command, $options); }