1
+ <?php
2
+
3
+ //
4
+ // Open Web Analytics - An Open Source Web Analytics Framework
5
+ //
6
+ // Copyright 2006 Peter Adams. All rights reserved.
7
+ //
8
+ // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ //
16
+ // $Id$
17
+ //
18
+
19
+
20
+ define ('OWA_DTD_BIGINT ' , 'BIGINT ' );
21
+ define ('OWA_DTD_INT ' , 'INT ' );
22
+ define ('OWA_DTD_TINYINT ' , 'TINYINT(1) ' );
23
+ define ('OWA_DTD_TINYINT2 ' , 'TINYINT(2) ' );
24
+ define ('OWA_DTD_TINYINT4 ' , 'TINYINT(4) ' );
25
+ define ('OWA_DTD_SERIAL ' , 'SERIAL ' );
26
+ define ('OWA_DTD_PRIMARY_KEY ' , 'PRIMARY KEY ' );
27
+ define ('OWA_DTD_VARCHAR10 ' , 'VARCHAR(10) ' );
28
+ define ('OWA_DTD_VARCHAR255 ' , 'VARCHAR(255) ' );
29
+ define ('OWA_DTD_VARCHAR ' , 'VARCHAR(%s) ' );
30
+ define ('OWA_DTD_TEXT ' , 'MEDIUMTEXT ' );
31
+ define ('OWA_DTD_BOOLEAN ' , 'TINYINT(1) ' );
32
+ define ('OWA_DTD_TIMESTAMP ' , 'TIMESTAMP ' );
33
+ define ('OWA_DTD_BLOB ' , 'BLOB ' );
34
+ define ('OWA_DTD_INDEX ' , 'KEY ' );
35
+ define ('OWA_DTD_AUTO_INCREMENT ' , 'AUTO_INCREMENT ' );
36
+ define ('OWA_DTD_NOT_NULL ' , 'NOT NULL ' );
37
+ define ('OWA_DTD_UNIQUE ' , 'PRIMARY KEY(%s) ' );
38
+ define ('OWA_SQL_ADD_COLUMN ' , 'ALTER TABLE %s ADD %s %s ' );
39
+ define ('OWA_SQL_DROP_COLUMN ' , 'ALTER TABLE %s DROP %s ' );
40
+ define ('OWA_SQL_RENAME_COLUMN ' , 'ALTER TABLE %s CHANGE %s %s %s ' );
41
+ define ('OWA_SQL_MODIFY_COLUMN ' , 'ALTER TABLE %s MODIFY %s %s ' );
42
+ define ('OWA_SQL_RENAME_TABLE ' , 'ALTER TABLE %s RENAME %s ' );
43
+ define ('OWA_SQL_CREATE_TABLE ' , 'CREATE TABLE IF NOT EXISTS %s (%s) %s ' );
44
+ define ('OWA_SQL_DROP_TABLE ' , 'DROP TABLE IF EXISTS %s ' );
45
+ define ('OWA_SQL_INSERT_ROW ' , 'INSERT into %s (%s) VALUES (%s) ' );
46
+ define ('OWA_SQL_UPDATE_ROW ' , 'UPDATE %s SET %s %s ' );
47
+ define ('OWA_SQL_DELETE_ROW ' , "DELETE from %s %s " );
48
+ define ('OWA_SQL_CREATE_INDEX ' , 'CREATE INDEX %s ON %s (%s) ' );
49
+ define ('OWA_SQL_DROP_INDEX ' , 'DROP INDEX %s ON %s ' );
50
+ define ('OWA_SQL_INDEX ' , 'INDEX (%s) ' );
51
+ define ('OWA_SQL_BEGIN_TRANSACTION ' , 'BEGIN ' );
52
+ define ('OWA_SQL_END_TRANSACTION ' , 'COMMIT ' );
53
+ define ('OWA_DTD_TABLE_TYPE ' , 'ENGINE = %s ' );
54
+ define ('OWA_DTD_TABLE_TYPE_DEFAULT ' , 'INNODB ' );
55
+ define ('OWA_DTD_TABLE_TYPE_DISK ' , 'INNODB ' );
56
+ define ('OWA_DTD_TABLE_TYPE_MEMORY ' , 'MEMORY ' );
57
+ define ('OWA_SQL_ALTER_TABLE_TYPE ' , 'ALTER TABLE %s ENGINE = %s ' );
58
+ define ('OWA_SQL_JOIN_LEFT_OUTER ' , 'LEFT OUTER JOIN ' );
59
+ define ('OWA_SQL_JOIN_LEFT_INNER ' , 'LEFT INNER JOIN ' );
60
+ define ('OWA_SQL_JOIN_RIGHT_OUTER ' , 'RIGHT OUTER JOIN ' );
61
+ define ('OWA_SQL_JOIN_RIGHT_INNER ' , 'RIGHT INNER JOIN ' );
62
+ define ('OWA_SQL_JOIN ' , 'JOIN ' );
63
+ define ('OWA_SQL_DESCENDING ' , 'DESC ' );
64
+ define ('OWA_SQL_ASCENDING ' , 'ASC ' );
65
+ define ('OWA_SQL_REGEXP ' , 'REGEXP ' );
66
+ define ('OWA_SQL_NOTREGEXP ' , 'NOT REGEXP ' );
67
+ define ('OWA_SQL_LIKE ' , 'LIKE ' );
68
+ define ('OWA_SQL_ADD_INDEX ' , 'ALTER TABLE %s ADD INDEX (%s) %s ' );
69
+ define ('OWA_SQL_COUNT ' , 'COUNT(%s) ' );
70
+ define ('OWA_SQL_SUM ' , 'SUM(%s) ' );
71
+ define ('OWA_SQL_ROUND ' , 'ROUND(%s) ' );
72
+ define ('OWA_SQL_AVERAGE ' , 'AVG(%s) ' );
73
+ define ('OWA_SQL_DISTINCT ' , 'DISTINCT %s ' );
74
+ define ('OWA_SQL_DIVISION ' , '(%s / %s) ' );
75
+ define ('OWA_DTD_CHARACTER_ENCODING_UTF8 ' , 'utf8 ' );
76
+ define ('OWA_DTD_TABLE_CHARACTER_ENCODING ' , 'CHARACTER SET = %s ' );
77
+
78
+
79
+ /**
80
+ * MySQL Data Access Class
81
+ *
82
+ * @author Peter Adams <[email protected] >
83
+ * @copyright Copyright © 2006 Peter Adams <[email protected] >
84
+ * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0
85
+ * @category owa
86
+ * @package owa
87
+ * @version $Revision$
88
+ * @since owa 1.0.0
89
+ */
90
+ class owa_db_mysql extends owa_db {
91
+
92
+ function connect () {
93
+
94
+ if (!$ this ->connection ) {
95
+
96
+ if ($ this ->getConnectionParam ('persistant ' )) {
97
+
98
+ $ this ->connection = mysql_pconnect (
99
+ $ this ->getConnectionParam ('host ' ),
100
+ $ this ->getConnectionParam ('user ' ),
101
+ $ this ->getConnectionParam ('password ' ),
102
+ $ this ->getConnectionParam ('open_new_connection ' )
103
+ );
104
+
105
+ } else {
106
+
107
+ $ this ->connection = mysql_connect (
108
+ $ this ->getConnectionParam ('host ' ),
109
+ $ this ->getConnectionParam ('user ' ),
110
+ $ this ->getConnectionParam ('password ' ),
111
+ $ this ->getConnectionParam ('open_new_connection ' )
112
+ );
113
+ }
114
+
115
+ $ this ->database_selection = mysql_select_db ($ this ->getConnectionParam ('name ' ), $ this ->connection );
116
+
117
+ if (function_exists ('mysql_set_charset ' )) {
118
+ mysql_set_charset ('utf8 ' ,$ this ->connection );
119
+ } else {
120
+ $ this ->query ("SET NAMES 'utf8' " );
121
+ }
122
+
123
+ }
124
+
125
+
126
+ if (!$ this ->connection || !$ this ->database_selection ) {
127
+ $ this ->e ->alert ('Could not connect to database. ' );
128
+ $ this ->connection_status = false ;
129
+ return false ;
130
+ } else {
131
+ $ this ->connection_status = true ;
132
+ return true ;
133
+ }
134
+ }
135
+
136
+
137
+ /**
138
+ * Database Query
139
+ *
140
+ * @param string $sql
141
+ * @access public
142
+ *
143
+ */
144
+ function query ( $ sql ) {
145
+
146
+ if ( $ this ->connection_status == false ) {
147
+
148
+ owa_coreAPI::profile ($ this , __FUNCTION__ , __LINE__ );
149
+
150
+ $ this ->connect ();
151
+
152
+ owa_coreAPI::profile ($ this , __FUNCTION__ , __LINE__ );
153
+ }
154
+
155
+ owa_coreAPI::profile ($ this , __FUNCTION__ , __LINE__ );
156
+
157
+ $ this ->e ->debug (sprintf ('Query: %s ' , $ sql ));
158
+
159
+ $ this ->result = '' ;
160
+
161
+ $ this ->new_result = '' ;
162
+
163
+ if (!empty ($ this ->new_result )) {
164
+ mysql_free_result ($ this ->new_result );
165
+ }
166
+
167
+ owa_coreAPI::profile ($ this , __FUNCTION__ , __LINE__ , $ sql );
168
+
169
+ $ result = @mysql_unbuffered_query ( $ sql , $ this ->connection );
170
+
171
+ owa_coreAPI::profile ($ this , __FUNCTION__ , __LINE__ );
172
+ // Log Errors
173
+
174
+ if ( mysql_errno ( $ this ->connection ) ) {
175
+ $ this ->e ->debug (
176
+ sprintf (
177
+ 'A MySQL error ocured. Error: (%s) %s. Query: %s ' ,
178
+ mysql_errno ( $ this ->connection ),
179
+ htmlspecialchars ( mysql_error ( $ this ->connection ) ),
180
+ $ sql
181
+ )
182
+ );
183
+ }
184
+
185
+ owa_coreAPI::profile ($ this , __FUNCTION__ , __LINE__ );
186
+
187
+ $ this ->new_result = $ result ;
188
+
189
+ return $ this ->new_result ;
190
+ }
191
+
192
+ function close () {
193
+
194
+ @mysql_close ($ this ->connection );
195
+ return ;
196
+
197
+ }
198
+
199
+ /**
200
+ * Fetch result set array
201
+ *
202
+ * @param string $sql
203
+ * @return array
204
+ * @access public
205
+ */
206
+ function get_results ( $ sql ) {
207
+
208
+ if ( $ sql ) {
209
+
210
+ $ this ->query ($ sql );
211
+ }
212
+
213
+ $ num_rows = 0 ;
214
+
215
+ while ( $ row = @mysql_fetch_assoc ($ this ->new_result ) ) {
216
+
217
+ $ this ->result [$ num_rows ] = $ row ;
218
+ $ num_rows ++;
219
+ }
220
+
221
+ if ( $ this ->result ) {
222
+
223
+ return $ this ->result ;
224
+
225
+ } else {
226
+
227
+ return null ;
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Fetch Single Row
233
+ *
234
+ * @param string $sql
235
+ * @return array
236
+ */
237
+ function get_row ($ sql ) {
238
+
239
+ $ this ->query ($ sql );
240
+
241
+ //print_r($this->result);
242
+ $ row = @mysql_fetch_assoc ($ this ->new_result );
243
+
244
+ return $ row ;
245
+ }
246
+
247
+ /**
248
+ * Prepares and escapes string
249
+ *
250
+ * @param string $string
251
+ * @return string
252
+ */
253
+ function prepare ( $ string ) {
254
+
255
+ if ($ this ->connection_status == false ) {
256
+ $ this ->connect ();
257
+ }
258
+
259
+ return mysql_real_escape_string ($ string , $ this ->connection );
260
+
261
+ }
262
+
263
+ function getAffectedRows () {
264
+
265
+ return mysql_affected_rows ();
266
+ }
267
+ }
268
+
269
+ ?>
0 commit comments