]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/PearDB_sqlite.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiDB / backend / PearDB_sqlite.php
1 <?php
2 /**
3  * SQLite PearDB backend by Matthew Palmer
4  * The SQLite DB will gain popularity with the current MySQL vs PHP license drama.
5  * It's in core since PHP-5.0, MySQL not anymore.
6  *
7  * Initial setup:
8  * sqlite -init /tmp/phpwiki-sqlite.db
9  * sqlite /tmp/phpwiki-sqlite.db < schemas/sqlite.sql
10  */
11
12 require_once 'lib/WikiDB/backend/PearDB.php';
13
14 //TODO: create tables on virgin wiki
15 /*
16     $db = &new DB_sqlite();
17     $db->connect($DBParams['dsn'], array('persistent'=> true) );
18     $result = $db->query("CREATE TABLE $table (comment varchar(50),
19       datetime varchar(50));");
20 */
21
22 class WikiDB_backend_PearDB_sqlite
23     extends WikiDB_backend_PearDB
24 {
25     /**
26      * Pack tables.
27      */
28     function optimize()
29     {
30         // NOP
31     }
32
33     /**
34      * Lock tables.
35      */
36     function _lock_tables($write_lock = true)
37     {
38         // NOP - SQLite does all locking automatically
39     }
40
41     /**
42      * Release all locks.
43      */
44     function _unlock_tables()
45     {
46         // NOP
47     }
48
49     /**
50      * Serialize data
51      */
52     function _serialize($data)
53     {
54         if (empty($data))
55             return '';
56         assert(is_array($data));
57         return base64_encode(serialize($data));
58     }
59
60     /**
61      * Unserialize data
62      */
63     function _unserialize($data)
64     {
65         if (empty($data))
66             return array();
67         // Base64 encoded data does not contain colons.
68         //  (only alphanumerics and '+' and '/'.)
69         if (substr($data, 0, 2) == 'a:')
70             return unserialize($data);
71         return unserialize(base64_decode($data));
72     }
73
74     // same as DB::getSpecialQuery('tables')
75     /*
76     function sqlite_list_tables (&$dblink) {
77        $tables = array ();
78        $sql = "SELECT name FROM sqlite_master WHERE (type = 'table')";
79        if ($res = sqlite_query ($dblink, $sql)) {
80            while (sqlite_has_more($res)) {
81                $tables[] = sqlite_fetch_single($res);
82            }
83        }
84        return $tables;
85     }
86     */
87
88     function _table_exists(&$dblink, $table)
89     {
90         $sql = "SELECT count(name) FROM sqlite_master WHERE ((type = 'table') and (name = '$table'))";
91         if ($res = sqlite_query($dblink, $sql)) {
92             return sqlite_fetch_single($res) > 0;
93         } else {
94             return false; // or throw exception
95         }
96     }
97
98 }
99
100 ;
101
102 // Local Variables:
103 // mode: php
104 // tab-width: 8
105 // c-basic-offset: 4
106 // c-hanging-comment-ender-p: nil
107 // indent-tabs-mode: nil
108 // End: