1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
class ReflectionBase implements Reflector {
protected $_pdo;
protected $_meta_inf;
public function __construct ($dbname, PDO $pdo) {
if ($pdo === null)
throw new InvalidArgumentException("Second argument must be a valid PDO instance");
$this->_pdo = $pdo;
$query = "SELECT `CATALOG_NAME`,`SCHEMA_NAME`,`DEFAULT_CHARACTER_SET_NAME`,`DEFAULT_COLLATION_NAME`,`SQL_PATH`".
" FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`=:dbname";
$stmt = $this->_pdo->prepare($query);
$stmt->bindParam(':dbname', $dbname, PDO::PARAM_STR);
if ($stmt->execute()) {
if (!$stmt->rowCount())
throw new ReflectionException("Base $dbname does not exist");
$this->_meta_inf = $stmt->fetch(PDO::FETCH_ASSOC);
}
else {
$info = $this->_pdo->errorInfo();
throw new RuntimeException ("PDOStatement execution failed: {$info[2]} with error code {$info[1]}");
}
}
public function getCatalog () {
return $this->_meta_inf['CATALOG_NAME'];
}
public function getSchema () {
return $this->_meta_inf['SCHEMA_NAME'];
}
public function getDefaultCharset () {
return $this->_meta_inf['DEFAULT_CHARACTER_SET_NAME'];
}
public function getDefaultCollation () {
return $this->_meta_inf['DEFAULT_COLLATION_NAME'];
}
public function getSQLPath () {
return $this->_meta_inf['SQL_PATH'];
}
public function getTable ($tablename) {
$tablename = "`{$this->getSchema()}`.`$tablename`";
return new ReflectionTable ($tablename);
}
public function getTables () {
$query = "SELECT `TABLE_SCHEMA`,`TABLE_NAME` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA`=:schema";
$stmt = $this->_pdo->prepare($query);
$stmt->bindParam(':schema', $this->getSchema(), PDO::PARAM_STR);
if ($stmt->execute()) {
$list = array();
foreach ($stmt as $row) {
list($schema, $table) = $row;
$list[] = new ReflectionTable("``$schema`.`$table`");
}
return $list;
}
else {
$info = $this->_pdo->errorInfo();
throw new RuntimeException ("PDOStatement execution failed: {$info[2]} with error code {$info[1]}");
}
}
public function getView ($viewname) {
// TODO
}
public function getViews () {
// TODO
}
public function getProcedure ($procedurename) {
// TODO
}
public function getProcedures () {
// TODO
}
/**
* -------------------------------------------------------------------------------------------------------------------------------------
* Reflector methods
*/
public static function export ($dbname, $return = false) {
// TODO
}
public function __toString () {
// TODO
}
} |
Partager