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