@@ -24,15 +24,60 @@ class DAO {
2424 /**
2525 * Constructor.
2626 * @param Database $database - the database object
27- * @param string $tableName - the table name this object will handle (can include #__ as prefix)
28- * @param string $modelClass - the name of the class that rows will be converted to (optional, default is \stdClass).
29- * @param string $idColumn - the name of the integer, auto-incremental primary key column (optional, default is uid)
27+ * @param string $tableName - the table name this object will handle (can include #__ as prefix)
28+ * @param string $modelClass - the name of the class that rows will be converted to (optional, default is \stdClass).
29+ * @param string $idColumn - the name of the integer, auto-incremental primary key column (optional, default is uid)
30+ * @param boolean $checkTable - whether to check existance of table and create if required (optional, default is FALSE)
3031 */
31- public function __construct ($ database , $ tableName , $ modelClass = 'stdClass ' , $ idColumn = 'uid ' ) {
32+ public function __construct ($ database , $ tableName , $ modelClass = 'stdClass ' , $ idColumn = 'uid ' , $ checkTable = FALSE ) {
3233 $ this ->database = $ database ;
3334 $ this ->tableName = $ tableName ;
3435 $ this ->modelClass = class_exists ($ modelClass ) ? $ modelClass : 'stdClass ' ;
3536 $ this ->idColumn = $ idColumn ;
37+ if ($ checkTable ) $ this ->checkTable ();
38+ }
39+
40+ /**
41+ * Checks the underlying table for existance and calls #createTable() if it does not exist.
42+ * @return boolean TRUE when table exists or was created successfully.
43+ */
44+ public function checkTable () {
45+ if (!$ this ->tableExists ()) {
46+ return $ this ->createTable ();
47+ }
48+ return TRUE ;
49+ }
50+
51+ /**
52+ * Creates the table. Default implementation does nothing and returns FALSE.
53+ * @return TRUE when table was created successfully.
54+ */
55+ public function createTable () {
56+ return FALSE ;
57+ }
58+
59+ /**
60+ * Checks existance of the underlying table.
61+ * @return string TRUE when table exists, FALSE otherwise.
62+ */
63+ public function tableExists () {
64+ return $ this ->database ->tableExists ($ this ->tableName );
65+ }
66+
67+ /**
68+ * Describes the underlying table.
69+ * {
70+ * "Field": "uid",
71+ * "Type": "int(10) unsigned",
72+ * "Null": "NO",
73+ * "Key": "PRI",
74+ * "Default": null,
75+ * "Extra": "auto_increment"
76+ * }
77+ * @return array of columns (empty when error occured).
78+ */
79+ public function describeTable () {
80+ return $ this ->database ->describeTable ($ this ->tableName );
3681 }
3782
3883 /**
0 commit comments