Smutty
MVC Framework
View Code: Smutty_Database
Browse: All · Classes · Plugins
1
<?php
2
3
/**
4
* this is the base class which all database implementations need
5
* to extend and implement. it controls access to these implementations
6
* however and getting a db handle should be done by using the
7
* {@link getInstance() getInstance()} method.
8
*
9
*/
10
11
abstract class Smutty_Database extends Smutty_Object {
12
13
/** singleton */
14
private static $instance = null;
15
16
/**
17
* method for getting a handle to the current database
18
* connection. of none has been created then this will
19
* try and read the config to find out what and how to create.
20
*
21
* @return Smutty_Database the singleton
22
*
23
*/
24
25
public static function &getInstance() {
26
// need to use === as false means we've tried to
27
// connect but something has gone wrong!
28
if ( self::$instance === null ) {
29
$cfg = Smutty_Config::getInstance();
30
switch ( strtolower($cfg->get('db.type')) ) {
31
case 'mysql':
32
self::$instance = new Smutty_Database_MySQL();
33
break;
34
default:
35
self::$instance = false; // need to set this to avoid infinite loop of
36
// code in the error handler trying to get
37
// a db handle.
38
Smutty_Error::warning( 'unknown database type db.type in app.cfg', 'SmuttyConfig' );
39
}
40
// connect to db if we can
41
if ( self::$instance )
42
self::$instance->connect();
43
}
44
return self::$instance;
45
}
46
47
/**
48
* connects to the database. db config information could be
49
* db independent so this method must read this info from
50
* the Smutty_Config class itself.
51
*
52
* @return boolean indicating success
53
* @return nothing
54
*
55
*/
56
58
59
/**
60
* executes a query on the database. it should return an
61
* class which extends the Smutty_Database_Result class
62
*
63
* @param String $sql the query to execute
64
* @return Smutty_Database_Result query result
65
*
66
*/
67
69
70
/**
71
* executes an sql statement on the database and returns a
72
* boolean indicating if it went well or not
73
*
74
* @param String $sql the query to execute
75
* @return boolean success
76
*
77
*/
78
80
81
/**
82
* takes a string and escapes any characters in it that need
83
* to be for the current database
84
*
85
* @param String $string the string to escape
86
* @return String escaped string
87
*
88
*/
89
91
92
/**
93
* returns the auto generated id for the last record inserted.
94
* for compat you need to specify the table the record was
95
* inserted for (though this is not used for some dbs)
96
*
97
* @param String $table table record was inserted into
98
* @return int the inserted id
99
*
100
*/
101
103
104
/**
105
* returns the last error to occur
106
*
107
* @return String desc of last error
108
*
109
*/
110
112
113
/**
114
* returns the current date/time, formatted properly for the
115
* current database
116
*
117
* @return String current datetime string
118
*
119
*/
120
122
123
/**
124
* returns the sql used to eatract field information
125
*
126
* the resultset should have the fields:
127
* - name (field name)
128
* - type (field type)
129
* - nullable (yes/no string)
130
*
131
* @param String $table the table to get fields for
132
* @return String sql string
133
*
134
*/
135
137
138
/**
139
* returns the date format for this db. the format should
140
* be ready for the date() function to use
141
*
142
* @return String date format string
143
*
144
*/
145
147
148
/**
149
* run an sql script against the database. returns a
150
* boolean indicating success.
151
*
152
* @param String $file the script to run
153
* @return boolean if script ran ok
154
*
155
*/
156
158
159
}
160
161
?>
The code shown here is the code that is currently running this site. If you want to view the latest SVN version of the code then go to the Subversion repository.