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: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174:
<?php
namespace Adi\QuickPDO;
class Schema {
private $db;
function __construct(DB $db) {
$this->db = $db;
}
public function getColumns($table) {
$result = array();
$sql = "SELECT column_name, is_nullable = 'YES' AS is_nullable, data_type, character_maximum_length, column_default, numeric_precision, numeric_scale
FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ?
ORDER BY ordinal_position";
foreach ($this->db->query($sql, $table)->fetchAll() as $row) {
$result[$row['column_name']] = array(
'name' => $row['column_name'],
'type' => $row['data_type'],
'is_nullable' => (boolean) $row['is_nullable'],
'character_maximum_length' => $row['character_maximum_length'],
'numeric_precision' => $row['numeric_precision'],
'numeric_scale' => $row['numeric_scale'],
'default_value' => $row['column_default'],
);
}
return $result;
}
public function getTables() {
$result = array();
$type = $this->db->getType();
$database = $this->db->getName();
switch ($type) {
case "mysql" :
$sql = "SELECT TABLE_NAME AS `table` FROM information_schema.tables WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME";
$data = $this->db->query($sql, $database)->fetchAll();
break;
case "pgsql" :
$sql = "SELECT c.relname AS \"table\" FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY c.relname";
$data = $this->db->query($sql)->fetchAll();
break;
default : throw new Exception("Database type '{$type}' is not yet supported.");
}
foreach ($data as $row) {
$result[] = $row['table'];
}
return $result;
}
public function getPK($table) {
$result = array();
$type = $this->db->getType();
$database = $this->db->getName();
switch ($type) {
case "mysql" :
$sql = "SHOW INDEX FROM `{$table}` WHERE key_name = 'PRIMARY'";
$filed = 'Column_name';
$table_check_sql = "SELECT count(*) FROM information_schema.tables WHERE table_schema = '{$database}' AND table_name = '{$table}'";
break;
case "pgsql" :
$sql = " SELECT
c.column_name, c.data_type
FROM
information_schema.table_constraints tc
JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name
where constraint_type = 'PRIMARY KEY' and tc.table_name = '{$table}';";
$filed = 'column_name';
$table_check_sql = "SELECT count(*) FROM information_schema.tables WHERE table_catalog = '{$database}' AND table_name = '{$table}'";
break;
default : throw new Exception("Database type '{$type}' is not yet supported.");
}
if (!$this->db->value($table_check_sql)) {
throw new Exception("Table '{$table}' doesn't exist.");
}
foreach ($this->db->query($sql)->fetchAll() as $row) {
$result[] = $row[$filed];
}
return $result;
}
}