Skip to content

Commit e3fd3b3

Browse files
committed
#9 - Add describeTable()
1 parent 772a9d2 commit e3fd3b3

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/TgDatabase/DAO.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ public function tableExists() {
4343
return $this->database->tableExists($this->tableName);
4444
}
4545

46+
/**
47+
* Describes the underlying table.
48+
* {
49+
* "Field": "uid",
50+
* "Type": "int(10) unsigned",
51+
* "Null": "NO",
52+
* "Key": "PRI",
53+
* "Default": null,
54+
* "Extra": "auto_increment"
55+
* }
56+
* @return array of columns (empty when error occured).
57+
*/
58+
public function describeTable() {
59+
return $this->database->describeTable($this->tableName);
60+
}
61+
4662
/**
4763
* Get the object with given id.
4864
* @param int $uid - ID of object (row)

src/TgDatabase/Database.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,33 @@ public function quoteName($s) {
100100
* @return string TRUE when table exists, FALSE otherwise.
101101
*/
102102
public function tableExists($tableName) {
103-
$res = $this->query('SELECT * FROM '.$this->tableName);
103+
$res = $this->query('SELECT * FROM '.$tableName);
104104
$rc = $res !== FALSE;
105105
if ($res) $res->free();
106106
return $rc;
107107
}
108108

109+
/**
110+
* Describes a table.
111+
* {
112+
* "Field": "uid",
113+
* "Type": "int(10) unsigned",
114+
* "Null": "NO",
115+
* "Key": "PRI",
116+
* "Default": null,
117+
* "Extra": "auto_increment"
118+
* }
119+
* @param string $tableName - name of table to be described
120+
* @return array of columns (empty when error occured).
121+
*/
122+
public function describeTable($tableName) {
123+
$rc = array();
124+
foreach ($this->queryList('DESCRIBE '.$tableName) AS $field) {
125+
$rc[$field->Field] = $field;
126+
}
127+
return $rc;
128+
}
129+
109130
/**
110131
* Execute the given SQL.
111132
* <p>This can be any arbitrary SQL statement. The function will only replace the table prefix

0 commit comments

Comments
 (0)