1: <?php
2:
3: /**
4: * This file contains the abstract database driver class.
5: *
6: * @package Core
7: * @subpackage Database
8: * @author Dominik Ziegler
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * This class contains abstract method definitions for each database driver in
19: * CONTENIDO.
20: *
21: * @package Core
22: * @subpackage Database
23: */
24: abstract class cDbDriverAbstract {
25:
26: /**
27: * Local database configuration.
28: *
29: * @var array
30: */
31: protected $_dbCfg = array();
32:
33: /**
34: * Driver handler instance.
35: *
36: * @var cDbDriverHandler
37: */
38: protected $_handler = NULL;
39:
40: /**
41: * Constructor of the database driver.
42: * Currently stores the given configuration locally.
43: *
44: * @param array $dbCfg
45: * database configuration
46: */
47: public function __construct($dbCfg) {
48: $this->_dbCfg = $dbCfg;
49: }
50:
51: /**
52: * Sets the database driver handler.
53: *
54: * @param cDbDriverHandler $handler
55: * database driver handler instance
56: */
57: public function setHandler(cDbDriverHandler $handler) {
58: $this->_handler = $handler;
59: }
60:
61: /**
62: * Returns the database driver handler instance.
63: *
64: * @return cDbDriverHandler|NULL
65: */
66: public function getHandler() {
67: return $this->_handler;
68: }
69:
70: /**
71: * Abstract method for checking database driver base functions.
72: * If this check fails, the database connection will not be established.
73: *
74: * @return bool
75: */
76: abstract public function check();
77:
78: /**
79: * Connects to the database.
80: *
81: * @return object|resource|int|NULL
82: * value depends on used driver and is NULL in case of an error
83: */
84: abstract public function connect();
85:
86: /**
87: * Builds a insert query.
88: * String values in passed fields parameter will be escaped automatically.
89: *
90: * @param string $tableName
91: * The table name
92: * @param array $fields
93: * Associative array of fields to insert
94: * @return string
95: * The INSERT SQL query
96: */
97: abstract public function buildInsert($tableName, array $fields);
98:
99: /**
100: * Builds a update query. String values in passed fields and whereClauses
101: * parameter will be escaped automatically.
102: *
103: * @param string $tableName
104: * The table name
105: * @param array $fields
106: * Assoziative array of fields to update
107: * @param array $whereClauses
108: * Assoziative array of field in where clause.
109: * Multiple entries will be concatenated with AND.
110: * @return string
111: * The UPDATE query
112: */
113: abstract public function buildUpdate($tableName, array $fields, array $whereClauses);
114:
115: /**
116: * Executes the query.
117: *
118: * @param string $statement
119: * The query to execute
120: */
121: abstract public function query($statement);
122:
123: /**
124: * Moves the result to the next record, if exists and returns the status of
125: * the movement
126: *
127: * @return int
128: * Flag about move status 1 on success or 0
129: */
130: abstract public function nextRecord();
131:
132: /**
133: * This method returns the current result set as object or NULL if no result
134: * set is left. If optional param $className is set, the result object is an
135: * instance of class $className.
136: *
137: * @param string $className [optional]
138: * @return object
139: */
140: abstract public function getResultObject($className = NULL);
141:
142: /**
143: * Returns number of affected rows from last executed query (update, delete)
144: *
145: * @return int
146: * Number of affected rows
147: */
148: abstract public function affectedRows();
149:
150: /**
151: * Returns the number of rows from last executed select query.
152: *
153: * @return int
154: * The number of rows from last select query result
155: */
156: abstract public function numRows();
157:
158: /**
159: * Returns the number of fields (columns) from current record set
160: *
161: * @return int
162: * Number of fields
163: */
164: abstract public function numFields();
165:
166: /**
167: * Discard the query result
168: */
169: abstract public function free();
170:
171: /**
172: * Escape string for using in SQL-Statement.
173: *
174: * @param string $string
175: * The string to escape
176: * @return string
177: * Escaped string
178: */
179: abstract public function escape($string);
180:
181: /**
182: * Moves the cursor (position inside current result sets).
183: *
184: * @param int $iPos [optional]
185: * The positon to move to inside the current result set
186: * @return int
187: */
188: abstract public function seek($iPos = 0);
189:
190: /**
191: * Parses the table structure and generates metadata from it.
192: *
193: * Due to compatibility problems with table we changed the behavior
194: * of metadata(). Depending on $full, metadata returns the following values:
195: *
196: * - full is false (default):
197: * $result[]:
198: * [0]["table"] table name
199: * [0]["name"] field name
200: * [0]["type"] field type
201: * [0]["len"] field length
202: * [0]["flags"] field flags
203: *
204: * - full is true
205: * $result[]:
206: * ["num_fields"] number of metadata records
207: * [0]["table"] table name
208: * [0]["name"] field name
209: * [0]["type"] field type
210: * [0]["len"] field length
211: * [0]["flags"] field flags
212: * ["meta"][field name] index of field named "field name"
213: * This last one could be used if you have a field name, but no index.
214: * Test: if (isset($result['meta']['myfield'])) { ...
215: *
216: * @param string $tableName
217: * The table to get metadata or empty string to retrieve metadata
218: * of all tables.
219: * @param bool $full [optional]
220: * Flag to load full metadata.
221: * @return array
222: * Depends on used database and on parameter $full
223: */
224: abstract public function getMetaData($tableName, $full = false);
225:
226: /**
227: * Fetches all table names.
228: *
229: * @return array
230: */
231: abstract public function getTableNames();
232:
233: /**
234: * Fetches server information.
235: *
236: * @return array
237: */
238: abstract public function getServerInfo();
239:
240: /**
241: * Returns error code of last occured error by using databases interface.
242: *
243: * @return int
244: */
245: abstract public function getErrorNumber();
246:
247: /**
248: * Returns error message of last occured error by using databases interface.
249: *
250: * @return string
251: */
252: abstract public function getErrorMessage();
253:
254: /**
255: * Closes the connection and frees the query id.
256: */
257: abstract public function disconnect();
258:
259: }
260: