5
5
*
6
6
* Manages all communication between database and application
7
7
*/
8
- class SQLQuery {
8
+ abstract class SQLQuery {
9
9
10
+ /** @var object Database handle */
11
+ protected $ _dbh ;
12
+ /** @var integer Number of rows affected by last query */
13
+ protected $ _rowCount ;
14
+
15
+ /**
16
+ * Connect to a database
17
+ *
18
+ * Opens connection to a database specified by parameters
19
+ *
20
+ * @param string $host Database server address
21
+ * @param string $user Database user
22
+ * @param string $password Database password
23
+ * @param string $dbname Name of database
24
+ * @return bool True if connection was successful
25
+ */
26
+ function connect ($ host , $ user , $ password , $ dbname )
27
+ {
28
+ try {
29
+ $ this ->_dbh = new PDO ("mysql:host= $ host;dbname= $ dbname " , $ user , $ password );
30
+ $ this ->_dbh ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
31
+ $ this ->_dbh ->setFetchMode (PDO ::FETCH_ASSOC );
32
+ return true ;
33
+ } catch (PDOException $ ex ) {
34
+ return false ;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Disconnect from a database
40
+ */
41
+ function disconnect ()
42
+ {
43
+ $ this ->_dbh = null ;
44
+ }
45
+
46
+ /**
47
+ * Perform SQL query
48
+ *
49
+ * @param string $query SQL query which should be executed
50
+ * @param array $param Optional parameteres for prepared statements
51
+ * @return mixed Associative array containing query result(s), or false on error or empty result
52
+ */
53
+ function query ($ query , $ param = null )
54
+ {
55
+ if ($ param == null ) {
56
+ $ param = array ();
57
+ }
58
+
59
+ try {
60
+ $ stmt = $ this ->_dbh ->prepare ($ query );
61
+ $ stmt ->execute ($ param );
62
+ $ this ->_rowCount = $ stmt ->rowCount ();
63
+
64
+ if ($ this ->_rowCount == 0 ) {
65
+ return false ;
66
+ }
67
+
68
+ return $ stmt ->fetchAll ();
69
+ } catch (PDOException $ ex ) {
70
+ return false ;
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Retrieve row with specific id
76
+ *
77
+ * Retreives row with specific id from table specified by attached models name
78
+ *
79
+ * @param mixed $id Row id
80
+ * @return array Associative array containg single result from table, or false on error
81
+ */
82
+ function select ($ id )
83
+ {
84
+ $ sql = 'SELECT * FROM :table WHERE id=:id ' ;
85
+ $ param = array (
86
+ 'table ' => $ this ->_table ,
87
+ 'id ' => $ id
88
+ );
89
+
90
+ return query ($ sql , $ param );
91
+ }
92
+
93
+ /**
94
+ * Retreives all rows from table
95
+ *
96
+ * Retrieves all rows from table specified by attached models name
97
+ *
98
+ * @return array Associative array containg all table entries, or false on error
99
+ */
100
+ function selectAll ()
101
+ {
102
+ $ sql = 'SELECT * FROM :table ' ;
103
+ $ param = array (
104
+ 'table ' => $ this ->_table ;
105
+ );
106
+
107
+ return query ($ sql , $ param );
108
+ }
109
+
110
+ /**
111
+ * Returns number of rows affected with last query
112
+ *
113
+ * @return int Number of affected rows
114
+ */
115
+ function numRows ()
116
+ {
117
+ if (isset ($ this ->_rowCount )) {
118
+ return $ this ->_rowCount ;
119
+ }
120
+
121
+ return 0 ;
122
+ }
10
123
}
0 commit comments