summaryrefslogtreecommitdiffstats
path: root/examples/database.class.php
blob: c7ef65bc1f40f76ee375ed3026d4b5065de3c41c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

/*
 * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * For more information, please refer to UNLICENSE
 */

/*
 * This class provides a common interface to a database.  Scrott provides
 * facilities for interacting with any DBMS that we can use in PHP.  To
 * provide support for a new DBMS, extend this class and implement the
 * abstract functions.  Additionally, update the getInstance() function
 * in this class so that the new implementation is used under appropriate
 * configuration.
 */
abstract class Database
{
    private static $instance = NULL;
    protected $db;

    /*
     * Return the database instance object, creating it if this is the
     * first call to this function.  This function will need maintained
     * as new DBMSs are supported.
     */
    public static function getInstance() : Database
    {
        // todo
    }

    /*
     * These functions are to be implemented by DBMS extensions,
     * providing a uniform interface to database engines.
     */
    public abstract function close();
    public abstract function query(string $query) : array;
    public abstract function esc(string $str) : string;

    /*
     * This function will lookup the row from the database on the given
     * table containing the given GUID and initialize the class properties
     * on this object based on the given field list.
     */
    public function initObj(string $table, array $fields, string $guid = NULL)
    {
        if (is_null($guid))
            return;

        $guid = $this->esc($guid);
        $query = "SELECT * FROM " . $table . " WHERE guid = '" . $guid . "'";
        $res = $this->query($query);

        if (!count($res))
            return;

        $res = $res[0];

        foreach ($fields as $field)
        {
            if (isset($res[$field]))
                $this->$field = $res[$field];
        }
    }
}

?>