summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEvan Prodromou <evan@controlyourself.ca>2009-08-24 11:22:40 -0400
committerEvan Prodromou <evan@status.net>2009-09-23 09:17:01 -0400
commitf31653ca5bc06db4d1a2a2dc8a1943f2d99f735a (patch)
treec704f450366b0e96199ed2cc9da8e364d18791b9 /lib
parent4f833531dd5aa05e56ab00dd95eda4ce8419564d (diff)
make table def method of schema code work
Diffstat (limited to 'lib')
-rw-r--r--lib/schema.php58
1 files changed, 56 insertions, 2 deletions
diff --git a/lib/schema.php b/lib/schema.php
index db2f49a78..624765eb2 100644
--- a/lib/schema.php
+++ b/lib/schema.php
@@ -47,15 +47,65 @@ if (!defined('LACONICA')) {
class Schema
{
- protected $db = null;
+ static $_single = null;
+ protected $conn = null;
- static function get()
+ protected function __construct()
{
+ // XXX: there should be an easier way to do this.
+ $user = new User();
+ $this->conn = $user->getDatabaseConnection();
+ $user->free();
+ unset($user);
+ }
+ static function get()
+ {
+ if (empty(self::$_single)) {
+ self::$_single = new Schema();
+ }
+ return self::$_single;
}
public function getTableDef($name)
{
+ $res =& $this->conn->query('DESCRIBE ' . $name);
+
+ if (PEAR::isError($res)) {
+ throw new Exception($res->getMessage());
+ }
+
+ $td = new TableDef();
+
+ $td->name = $name;
+ $td->columns = array();
+
+ $row = array();
+
+ while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
+
+ $cd = new ColumnDef();
+
+ $cd->name = $row['Field'];
+
+ $packed = $row['Type'];
+
+ if (preg_match('/^(\w+)\((\d+)\)$/', $packed, $match)) {
+ $cd->type = $match[1];
+ $cd->size = $match[2];
+ } else {
+ $cd->type = $packed;
+ }
+
+ $cd->nullable = ($row['Null'] == 'YES') ? true : false;
+ $cd->key = $row['Key'];
+ $cd->default = $row['Default'];
+ $cd->extra = $row['Extra'];
+
+ $td->columns[] = $cd;
+ }
+
+ return $td;
}
public function getColumnDef($table, $column)
@@ -114,6 +164,10 @@ class ColumnDef
public $name;
public $type;
public $size;
+ public $nullable;
+ public $key;
+ public $default;
+ public $extra;
}
class IndexDef