Skip to content

Commit 94bb3f1

Browse files
committed
Releasing 1.1.0
2 parents 798890c + 606dac1 commit 94bb3f1

File tree

7 files changed

+118
-6
lines changed

7 files changed

+118
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ As above mentioned, you can use your own data classes. There are actually no
270270
restrictions other than the class needs a no-argument constructor. The main
271271
advantage is that this class can have additional methods that have some
272272
logic. You can even define additional attributes that will not be saved in
273-
the database by a DAO. These attributes start with an underscore (_).
273+
the database by a DAO. These attributes start with an underscore.
274274

275275
Here is an example:
276276

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
"TgDatabase\\" : "src/TgDatabase/"
2424
}
2525
},
26+
"extra": {
27+
"branch-alias": {
28+
"dev-master": "1.0-dev"
29+
}
30+
},
2631
"require-dev" : {
2732
"phpunit/phpunit" : "^9"
2833
}
29-
}
34+
}

src/TgDatabase/DAO.php

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/TgDatabase/DataModel.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,27 @@ public function getDatabase() {
5757
public function register($name, $dao) {
5858
$this->models[$name] = $dao;
5959
}
60+
61+
/**
62+
* Performs a check of each DAO whether underlying tables exists and creates them if required.
63+
* @return object with the result(s):
64+
* {
65+
* "tableChecks" : {
66+
* "<dao-registered-name>" : TRUE|FALSE,
67+
* ...
68+
* },
69+
* "success" : TRUE|FALSE
70+
* }
71+
*/
72+
public function checkTables() {
73+
$rc = new \stdClass;
74+
$rc->tableChecks = new \stdClass;
75+
$rc->success = TRUE;
76+
foreach ($this->models AS $name => $dao) {
77+
$rc->tableChecks->$name = $dao->checkTable();
78+
if (!$rc->tableChecks->$name) $rc->success = FALSE;
79+
}
80+
return $rc;
81+
}
6082
}
6183

src/TgDatabase/Database.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,39 @@ public function quoteName($s) {
9494
return '`'.$s.'`';
9595
}
9696

97+
/**
98+
* Checks existance of a table.
99+
* @param string $tableName - name of table to be checked
100+
* @return string TRUE when table exists, FALSE otherwise.
101+
*/
102+
public function tableExists($tableName) {
103+
$res = $this->query('SELECT * FROM '.$tableName);
104+
$rc = $res !== FALSE;
105+
if ($res) $res->free();
106+
return $rc;
107+
}
108+
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+
97130
/**
98131
* Execute the given SQL.
99132
* <p>This can be any arbitrary SQL statement. The function will only replace the table prefix

test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Test script
2+
3+
composer update
4+
RC=./vendor/phpunit/phpunit/phpunit tests
5+
rm -rf vendor composer.lock
6+
exit $RC
7+

tests/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)