public function __construct($server, $username, $password) { if (self::$dbh == NULL) { self::$dbh = db_connect($server, $username, $password); } else { // reuse existing connection } }
public function structure_page($ctl) { if (!isset(Database::$config['databases'][$_REQUEST['db']])) { return trigger_error("Invalid database: {$_REQUEST['db']}"); } $database = new Database($_REQUEST['db']); if (!in_array($_REQUEST['table'], $database->get_tables())) { return trigger_error("Invalid table: {$_REQUEST['table']}"); } $sql = "SELECT * FROM sqlite_master WHERE tbl_name = " . $database->dbh()->quote($_REQUEST['table']) . ""; $sth = $database->dbh()->query($sql); $table_cols = array(); $table_indexes = array(); foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row) { if ($row['type'] == 'table') { foreach (explode(',', preg_replace('/^CREATE TABLE \\S+ \\(/', '', preg_replace('/\\)$/', '', trim($row['sql'])))) as $col_def) { /// PRIMARY KEY if (preg_match('/^PRIMARY KEY\\s*\\(\\s*(\\w+(?:\\s*,\\s*\\w+)*)\\s*\\)/i', trim($col_def), $m)) { $table_indexes['PRIMARY KEY'] = array('definition' => $m[1]); } else { if (preg_match('/^UNIQUE KEY\\s*\\(\\s*(\\w+(?:\\s*,\\s*\\w+)*)\\s*\\)/i', trim($col_def), $m)) { $table_indexes['UNIQUE KEY'] = array('definition' => $m[1]); } else { if (preg_match('/^(PRIMARY|KEY|CONSTRAINT|UNIQUE)/i', trim($col_def), $m)) { continue; } else { if (preg_match('/^(\\w+)\\s*(\\S.+)$/', trim($col_def), $m)) { $table_cols[$m[1]] = array('definition' => $m[2]); if (strpos(strtoupper($m[2]), 'PRIMARY KEY') !== false) { $table_indexes['PRIMARY KEY'] = array('definition' => '(' . $m[1] . ')'); } } } } } } } else { if ($row['type'] == 'index') { $table_indexes[$row['name']] = array('definition' => preg_replace('/^CREATE INDEX \\S+ ON \\w+/i', '', $row['sql'])); } } } return array('database' => $database->database, 'table' => $_REQUEST['table'], 'data' => $data, 'table_cols' => $table_cols, 'table_indexes' => $table_indexes); }
/** * * @param array $dbconfig 数据库连接参数 * @return object $this */ private function connect($dbsetting) { if (self::$dbh) { return $this; } $dbconfig = Config::get($dbsetting); $dsn = "mysql:dbname={$dbconfig['dbname']};host={$dbconfig['host']}"; $dbh = new PDO($dsn, $dbconfig['user'], $dbconfig['pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';")); self::$dbh = $dbh; }
public function table_list_page($ctl) { if (!isset(Database::$config['databases'][$_REQUEST['db']])) { return trigger_error("Invalid database: {$_REQUEST['db']}"); } $database = new Database($_REQUEST['db']); $tables = array(); foreach ($database->get_tables() as $table) { $sql = "SELECT COUNT(*) as row_count FROM {$table}"; $sth = $database->dbh()->query($sql); $data = $sth->fetchAll(PDO::FETCH_ASSOC); $tables[$table] = $data[0]; } return array('database' => $database->database, 'tables' => $tables); }
/** * Fetch the database connection * * @return PDO The Databse connection object */ public static function get() { if (empty(self::$dbh)) { if (!extension_loaded('PDO')) { throw new Exception("Missing PDO Extension"); } self::$dbh = new PDO(self::$dsn, self::$user, self::$pass); self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $driver = self::$dbh->getAttribute(PDO::ATTR_DRIVER_NAME); if ($driver == 'mysql') { // Set UTF-8 Locale self::$dbh->exec("SET NAMES 'utf8'"); } } return self::$dbh; }
/** * Loads and executes queries from a local file. * * @fixme Returns immediately in case of an error, without rolling back * previous changes: implement transactions. * @param Database $db Database object. * @param string $fileName Filename of the file with SQL statements. * * @return boolean True on error, false in case of success. */ public static function sourceSqlFile(&$db, $fileName) { if (!file_exists($fileName)) { return true; } $output = null; $script = file_get_contents($fileName); $queries = preg_split("/;\n/", $script); foreach ($queries as $q) { if (!trim($q)) { continue; } echo "<blockquote>{$q};</blockquote>\n"; $rs = $db->query($q); if (!$rs) { echo "<p><strong>Error</strong>: <tt>" . mysql_error($db->dbh()) . "</tt>.</p>"; return true; } } return false; }