1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: App::import('Core', 'DboMysql');
27: 28: 29: 30: 31: 32: 33: 34:
35: class DboMysqli extends DboMysqlBase {
36: 37: 38: 39: 40:
41: var $description = "Mysqli DBO Driver";
42: 43: 44: 45: 46:
47: var $_baseConfig = array(
48: 'persistent' => true,
49: 'host' => 'localhost',
50: 'login' => 'root',
51: 'password' => '',
52: 'database' => 'cake',
53: 'port' => '3306',
54: 'connect' => 'mysqli_connect'
55: );
56: 57: 58: 59: 60:
61: function connect() {
62: $config = $this->config;
63: $this->connected = false;
64:
65: if (is_numeric($config['port'])) {
66: $config['socket'] = null;
67: } else {
68: $config['socket'] = $config['port'];
69: $config['port'] = null;
70: }
71:
72: $this->connection = mysqli_connect($config['host'], $config['login'], $config['password'], $config['database'], $config['port'], $config['socket']);
73:
74: if ($this->connection !== false) {
75: $this->connected = true;
76: }
77:
78: $this->_useAlias = (bool)version_compare(mysqli_get_server_info($this->connection), "4.1", ">=");
79:
80: if (!empty($config['encoding'])) {
81: $this->setEncoding($config['encoding']);
82: }
83: return $this->connected;
84: }
85: 86: 87: 88: 89:
90: function enabled() {
91: return extension_loaded('mysqli');
92: }
93: 94: 95: 96: 97:
98: function disconnect() {
99: if (isset($this->results) && is_resource($this->results)) {
100: mysqli_free_result($this->results);
101: }
102: $this->connected = !@mysqli_close($this->connection);
103: return !$this->connected;
104: }
105: 106: 107: 108: 109: 110: 111:
112: function _execute($sql) {
113: if (preg_match('/^\s*call/i', $sql)) {
114: return $this->_executeProcedure($sql);
115: }
116: return mysqli_query($this->connection, $sql);
117: }
118: 119: 120: 121: 122: 123: 124:
125: function _executeProcedure($sql) {
126: $answer = mysqli_multi_query($this->connection, $sql);
127:
128: $firstResult = mysqli_store_result($this->connection);
129:
130: if (mysqli_more_results($this->connection)) {
131: while ($lastResult = mysqli_next_result($this->connection));
132: }
133: return $firstResult;
134: }
135: 136: 137: 138: 139:
140: function listSources() {
141: $cache = parent::listSources();
142: if ($cache != null) {
143: return $cache;
144: }
145: $result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';');
146:
147: if (!$result) {
148: return array();
149: }
150:
151: $tables = array();
152:
153: while ($line = mysqli_fetch_array($result)) {
154: $tables[] = $line[0];
155: }
156: parent::listSources($tables);
157: return $tables;
158: }
159: 160: 161: 162: 163: 164: 165: 166:
167: function value($data, $column = null, $safe = false) {
168: $parent = parent::value($data, $column, $safe);
169:
170: if ($parent != null) {
171: return $parent;
172: }
173: if ($data === null || (is_array($data) && empty($data))) {
174: return 'NULL';
175: }
176: if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') {
177: return "''";
178: }
179: if (empty($column)) {
180: $column = $this->introspectType($data);
181: }
182:
183: switch ($column) {
184: case 'boolean':
185: return $this->boolean((bool)$data);
186: break;
187: case 'integer' :
188: case 'float' :
189: case null :
190: if ($data === '') {
191: return 'NULL';
192: }
193: if ((is_int($data) || is_float($data) || $data === '0') || (
194: is_numeric($data) && strpos($data, ',') === false &&
195: $data[0] != '0' && strpos($data, 'e') === false)) {
196: return $data;
197: }
198: default:
199: $data = "'" . mysqli_real_escape_string($this->connection, $data) . "'";
200: break;
201: }
202:
203: return $data;
204: }
205: 206: 207: 208: 209:
210: function lastError() {
211: if (mysqli_errno($this->connection)) {
212: return mysqli_errno($this->connection).': '.mysqli_error($this->connection);
213: }
214: return null;
215: }
216: 217: 218: 219: 220: 221:
222: function lastAffected() {
223: if ($this->_result) {
224: return mysqli_affected_rows($this->connection);
225: }
226: return null;
227: }
228: 229: 230: 231: 232: 233:
234: function lastNumRows() {
235: if ($this->hasResult()) {
236: return mysqli_num_rows($this->_result);
237: }
238: return null;
239: }
240: 241: 242: 243: 244: 245:
246: function lastInsertId($source = null) {
247: $id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false);
248: if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) {
249: return $id[0]['insertID'];
250: }
251: return null;
252: }
253: 254: 255: 256: 257:
258: function resultSet(&$results) {
259: if (isset($this->results) && is_resource($this->results) && $this->results != $results) {
260: mysqli_free_result($this->results);
261: }
262: $this->results =& $results;
263: $this->map = array();
264: $numFields = mysqli_num_fields($results);
265: $index = 0;
266: $j = 0;
267: while ($j < $numFields) {
268: $column = mysqli_fetch_field_direct($results, $j);
269: if (!empty($column->table)) {
270: $this->map[$index++] = array($column->table, $column->name);
271: } else {
272: $this->map[$index++] = array(0, $column->name);
273: }
274: $j++;
275: }
276: }
277: 278: 279: 280: 281:
282: function fetchResult() {
283: if ($row = mysqli_fetch_row($this->results)) {
284: $resultRow = array();
285: $i = 0;
286: foreach ($row as $index => $field) {
287: $table = $column = null;
288: if (count($this->map[$index]) == 2) {
289: list($table, $column) = $this->map[$index];
290: }
291: $resultRow[$table][$column] = $row[$index];
292: $i++;
293: }
294: return $resultRow;
295: }
296: return false;
297: }
298: 299: 300: 301: 302:
303: function getEncoding() {
304: return mysqli_client_encoding($this->connection);
305: }
306: 307: 308: 309: 310:
311: function hasResult() {
312: return is_object($this->_result);
313: }
314: }
315: ?>