Created
March 31, 2013 00:03
-
-
Save kissarat/5278902 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'config.php'; | |
class Description { | |
public $data; | |
public $name; | |
/* public static $map = array( | |
'COLUMN_NAME' => 'name', | |
'IS_NULLABLE' => 'required', | |
'DATA_TYPE' => 'type', | |
'CHARACTER_MAXIMUM_LENGTH' => 'length', | |
'COLUMN_DEFAULT' => 'default', | |
'COLUMN_KEY' => 'key' | |
);*/ | |
public function setProperty($column) { | |
} | |
public function __construct($data) { | |
$this->name = $data['COLUMN_NAME']; | |
$this->data['type'] = $data['DATA_TYPE']; | |
if ('NO' == $data['IS_NULLABLE']) | |
$this->data['required'] = 1; | |
if ($value = $data['CHARACTER_MAXIMUM_LENGTH']) | |
$this->data['length'] = (int) $value; | |
if ('PRI' == $data['COLUMN_KEY']) | |
$this->data['key'] = 1; | |
} | |
function toJSON() { | |
return json_encode($this->data); | |
} | |
function toJSONArray() { | |
return '[' . join(',', $this->data) . ']'; | |
} | |
function fromJSON($json) { | |
$this->data = json_decode($json); | |
} | |
function __get($name) { | |
return $this->data[$name]; | |
} | |
function __set($name, $value) { | |
$this->data[$name] = $value; | |
} | |
} | |
class Descriptor { | |
public $name; | |
public $descritions = array(); | |
function __construct($name) { | |
$this->name = $name; | |
} | |
function load(PDO $pdo) { | |
$sql = "select COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, COLUMN_KEY | |
from INFORMATION_SCHEMA.COLUMNS | |
where TABLE_NAME='$this->name' | |
order by ORDINAL_POSITION asc"; | |
$stat = $pdo->query($sql); | |
$stat->setFetchMode(PDO::FETCH_ASSOC); | |
while($row = $stat->fetch()) { | |
$desc = new Description($row); | |
$this->descritions[$desc->name] = $desc; | |
}; | |
} | |
function toJSON() { | |
$jsons = array(); | |
foreach($this->descritions as $desc) { | |
$jsons[] = "\"$desc->name\":" . $desc->toJSON(); | |
} | |
return '{' . join(',', $jsons) . '}'; | |
} | |
} | |
function describe($table) { | |
global $pdo; | |
$desctor = new Descriptor($table); | |
$desctor->load($pdo); | |
echo $desctor->toJSON(); | |
} | |
if (isset($_GET['table'])) { | |
$table = $_GET['table']; | |
/*if ('all' == $table) { | |
$stat = $pdo->query('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="bitch"'); | |
while($table = $stat->fetchColumn()) | |
describe($table); | |
} | |
else*/ | |
describe($table); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment