1: <?php
2:
3: if (!class_exists('QMySqliDatabase'))
4: require(__QCUBED_CORE__ . '/database/QMySqliDatabase.class.php');
5:
6: 7: 8: 9:
10: class QMySqlDatabase extends QMySqliDatabase {
11: const Adapter = 'MySql Legacy Database Adapter for MySQL 4';
12:
13: protected $objDb;
14:
15: public function Connect() {
16:
17: $strServer = $this->Server;
18: $strName = $this->Database;
19: $strUsername = $this->Username;
20: $strPassword = $this->Password;
21: $strPort = $this->Port;
22:
23: if ($strPort)
24: $strServer .= ':' . $strPort;
25:
26:
27: $this->objDb = mysql_connect($strServer, $strUsername, $strPassword, true);
28: if (!$this->objDb)
29: throw new QMySqliDatabaseException("Unable to connect to Database Server: $strServer", -1, null);
30: if (mysql_errno($this->objDb))
31: throw new QMySqliDatabaseException(mysql_error($this->objDb), mysql_errno($this->objDb), null);
32:
33:
34: if (!mysql_select_db($strName, $this->objDb))
35: throw new QMySqliDatabaseException("Unable to select the Database: $strName", -1, null);
36: if (mysql_errno($this->objDb))
37: throw new QMySqliDatabaseException(mysql_error($this->objDb), mysql_errno($this->objDb), null);
38:
39:
40: $this->blnConnectedFlag = true;
41:
42:
43: $this->NonQuery('SET AUTOCOMMIT=1;');
44:
45:
46: if (array_key_exists('encoding', $this->objConfigArray))
47: $this->NonQuery('SET NAMES ' . $this->objConfigArray['encoding'] . ';');
48: }
49:
50: public function __get($strName) {
51: switch ($strName) {
52: case 'AffectedRows':
53: return mysql_affected_rows($this->objDb);
54: default:
55: try {
56: return parent::__get($strName);
57: } catch (QCallerException $objExc) {
58: $objExc->IncrementOffset();
59: throw $objExc;
60: }
61: }
62: }
63:
64: protected function ExecuteQuery($strQuery) {
65:
66: $objResult = mysql_query($strQuery, $this->objDb);
67: if (mysql_errno($this->objDb))
68: throw new QMySqliDatabaseException(mysql_error($this->objDb), mysql_errno($this->objDb), $strQuery);
69:
70:
71: $objMySqlDatabaseResult = new QMySqlDatabaseResult($objResult, $this);
72: return $objMySqlDatabaseResult;
73: }
74:
75: protected function ExecuteNonQuery($strNonQuery) {
76:
77: mysql_query($strNonQuery, $this->objDb);
78: if (mysql_errno($this->objDb))
79: throw new QMySqliDatabaseException(mysql_error($this->objDb), mysql_errno($this->objDb), $strNonQuery);
80: }
81:
82: public function InsertId($strTableName = null, $strColumnName = null) {
83: $result = $this->Query('SELECT LAST_INSERT_ID();');
84: $data = $result->FetchArray();
85: $result->Close();
86: return $data[0];
87: }
88:
89: public function Close() {
90: mysql_close($this->objDb);
91:
92:
93: $this->blnConnectedFlag = false;
94: }
95:
96: public function GetFieldsForTable($strTableName) {
97: $objResult = new QMySqlDatabaseResult(mysql_list_fields($this->Database, $strTableName, $this->objDb), $this);
98: return $objResult->FetchFields();
99: }
100: }
101:
102: 103: 104:
105: class QMySqlDatabaseResult extends QMySqliDatabaseResult {
106: protected $objMySqlResult;
107: protected $objDb;
108:
109: public function __construct($objResult, QMySqlDatabase $objDb) {
110: $this->objMySqlResult = $objResult;
111: $this->objDb = $objDb;
112: }
113:
114: public function FetchArray() {
115: return mysql_fetch_array($this->objMySqlResult);
116: }
117:
118: public function FetchFields() {
119: $objArrayToReturn = array();
120: while ($objField = mysql_fetch_field($this->objMySqlResult)) {
121: array_push($objArrayToReturn, new QMySqlDatabaseField($objField, $this->objDb));
122: }
123: return $objArrayToReturn;
124: }
125:
126: public function FetchField() {
127: if ($objField = mysql_fetch_field($this->objMySqlResult))
128: return new QMySqlDatabaseField($objField, $this->objDb);
129: }
130:
131: public function FetchRow() {
132: return mysql_fetch_row($this->objMySqlResult);
133: }
134:
135: public function MySqlFetchField() {
136: return mysql_fetch_field($this->objMySqlResult);
137: }
138:
139: public function CountRows() {
140: return mysql_num_rows($this->objMySqlResult);
141: }
142:
143: public function CountFields() {
144: return mysql_num_fields($this->objMySqlResult);
145: }
146:
147: public function Close() {
148: mysql_free_result($this->objMySqlResult);
149: }
150: }
151:
152: 153: 154: 155:
156: class QMySqlDatabaseField extends QMySqliDatabaseField {
157: public function __construct($mixFieldData, $objDb = null) {
158: $this->strName = $mixFieldData->name;
159: $this->strOriginalName = $this->strName;
160: $this->strTable = $mixFieldData->table;
161: $this->strOriginalTable = $mixFieldData->table;
162: $this->strDefault = $mixFieldData->def;
163: $this->intMaxLength = null;
164: $this->strComment = null;
165:
166:
167:
168: if ($this->strOriginalTable) {
169: try {
170: $objDescriptionResult = $objDb->Query(sprintf("SHOW FULL FIELDS FROM `%s`", $this->strOriginalTable));
171: while (($objRow = $objDescriptionResult->FetchArray())) {
172: if ($objRow["Field"] == $this->strOriginalName) {
173: if ($objRow["Extra"] == 'auto_increment')
174: $this->blnIdentity = true;
175: else
176: $this->blnIdentity = false;
177:
178: $this->strComment = $objRow["Comment"];
179: $strLengthArray = explode("(", $objRow["Type"]);
180: if ((count($strLengthArray) > 1) &&
181: (strtolower($strLengthArray[0]) != 'enum') &&
182: (strtolower($strLengthArray[0]) != 'set')) {
183: $strLengthArray = explode(")", $strLengthArray[1]);
184: $this->intMaxLength = $strLengthArray[0];
185:
186:
187: $intCommaPosition = strpos($this->intMaxLength, ',');
188: if ($intCommaPosition !== false) {
189: $this->intMaxLength = substr($this->intMaxLength, 0, $intCommaPosition);
190: $this->intMaxLength++;
191: }
192:
193: if (!is_numeric($this->intMaxLength))
194: throw new Exception("Not a valid Column Length: " . $objRow["Type"]);
195: }
196: }
197: }
198: }
199: catch(Exception $e) {
200:
201: }
202: }
203:
204: $this->blnNotNull = QType::Cast($mixFieldData->not_null, QType::Boolean);
205: $this->blnPrimaryKey = QType::Cast($mixFieldData->primary_key, QType::Boolean);
206: $this->blnUnique = QType::Cast($mixFieldData->unique_key, QType::Boolean);
207:
208: switch ($mixFieldData->type) {
209: case 'int':
210: if ($this->intMaxLength == 1)
211: $this->strType = QDatabaseFieldType::Bit;
212: else
213: $this->strType = QDatabaseFieldType::Integer;
214: break;
215: case 'real':
216: case 'float':
217: $this->strType = QDatabaseFieldType::Float;
218: break;
219: case 'double':
220:
221:
222:
223:
224: $this->strType = QDatabaseFieldType::VarChar;
225: break;
226: case 'timestamp':
227:
228: $this->strType = QDatabaseFieldType::VarChar;
229: $this->blnTimestamp = true;
230: break;
231: case 'date':
232: $this->strType = QDatabaseFieldType::Date;
233: break;
234: case 'time':
235: $this->strType = QDatabaseFieldType::Time;
236: break;
237: case 'datetime':
238: $this->strType = QDatabaseFieldType::DateTime;
239: break;
240: case 'blob':
241: $this->strType = QDatabaseFieldType::Blob;
242: break;
243: case 'string':
244: $this->strType = QDatabaseFieldType::VarChar;
245: break;
246: case 'char':
247: $this->strType = QDatabaseFieldType::Char;
248: break;
249: case 'json':
250: $this->strType = QDatabaseFieldType::Json;
251: break;
252: default:
253: throw new Exception("Unable to determine MySqli Database Field Type: " . $mixFieldData->type);
254: break;
255: }
256: }
257: }
258: