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