From b7d19a199f30dac8ad4c013c349c4544341fb313 Mon Sep 17 00:00:00 2001 From: Vasil Stoychev Date: Sat, 13 Jul 2013 10:52:55 +0300 Subject: [PATCH 1/3] Auto collect table indexes on call, will work with --- lib/arrest-mysql.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/arrest-mysql.php b/lib/arrest-mysql.php index 2b870938..eb1082a1 100644 --- a/lib/arrest-mysql.php +++ b/lib/arrest-mysql.php @@ -63,9 +63,9 @@ public function __construct($db_config, $base_uri = '') $this->db = new Database($db_config); if(!$this->db->init()) throw new Exception($this->db->get_error()); + $this->table_index = array(); $this->db_structure = $this->map_db($db_config['database']); $this->segments = $this->get_uri_segments($base_uri); - $this->table_index = array(); } /** @@ -132,6 +132,23 @@ private function map_db($database) $this->db->query('SHOW COLUMNS FROM '. $table_name); $fields = $this->db->fetch_all(); $tables_arr[$table_name] = $fields; + + // loop thru table columns to find any PRIMARY keys + $fields_count = count($fields); + $fieldKeys = array(); + for ($i = 0; $i < $fields_count; $i++) { + $field = $fields[$i]; + + // @NOTE: If Key is PRI, the column is a PRIMARY KEY or is one of the columns in a multiple-column (composite primary key) PRIMARY KEY. + if (strcasecmp($field['Key'], 'PRI') == 0) { // binary safe case insensitive comparison + array_push($fieldKeys, $field['Field']); + } + } + + // @WARNING: as lib/db.php is using index() as field=value in WHERE CLAUSE, on tables with composite PRIMARY KEY extract last of the keys + if (count($fieldKeys) > 0) { + $this->table_index[$table_name] = end($fieldKeys); + } } return $tables_arr; } From b2013e64ee157383ffa19a9018b38f60faa6b2ba Mon Sep 17 00:00:00 2001 From: Vasil Stoychev Date: Sat, 13 Jul 2013 11:24:50 +0300 Subject: [PATCH 2/3] added debug option on 404 error, to display table name, primary key value passed (if any) and a warning message for most common error, not specifying base_uri (subdir) when initializing arrest-mysql object --- lib/arrest-mysql.php | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/arrest-mysql.php b/lib/arrest-mysql.php index eb1082a1..bcb37383 100644 --- a/lib/arrest-mysql.php +++ b/lib/arrest-mysql.php @@ -15,8 +15,16 @@ */ require('lib/db.php'); +define('DEBUG_TABLE_ERROR_MSG', 'Either the table does not exists or you have misconfigured the subdir (base_uri) in index.php'); + class ArrestMySQL { + /** + * Makes passing boolean parameters easier to read and understand + */ + const DEBUG_ON = true; + const DEBUG_OFF = false; + /** * The instance of Database * @@ -41,6 +49,12 @@ class ArrestMySQL { * @var array */ private $table_index; + /** + * Debug mode + * + * @var boolean + */ + private $debug; /** * Create an instance, optionally setting a base URI @@ -58,11 +72,12 @@ class ArrestMySQL { * @param string $base_uri Optional base URI if not in root folder * @access public */ - public function __construct($db_config, $base_uri = '') + public function __construct($db_config, $base_uri = '', $debug = false) { $this->db = new Database($db_config); if(!$this->db->init()) throw new Exception($this->db->get_error()); + $this->debug = $debug; $this->table_index = array(); $this->db_structure = $this->map_db($db_config['database']); $this->segments = $this->get_uri_segments($base_uri); @@ -205,6 +220,12 @@ private function create() 'message' => 'Not Found', 'code' => 404 )); + if ($this->debug) { + $error['debug'] = array( + 'table' => $table, + 'warning' => DEBUG_TABLE_ERROR_MSG, + ); + } die(json_encode($error)); } @@ -240,6 +261,13 @@ private function read() 'message' => 'Not Found', 'code' => 404 )); + if ($this->debug) { + $error['debug'] = array( + 'table' => $table, + 'index' => $id, + 'warning' => DEBUG_TABLE_ERROR_MSG, + ); + } die(json_encode($error)); } @@ -292,6 +320,13 @@ private function update() 'message' => 'Not Found', 'code' => 404 )); + if ($this->debug) { + $error['debug'] = array( + 'table' => $table, + 'index' => $id, + 'warning' => DEBUG_TABLE_ERROR_MSG, + ); + } die(json_encode($error)); } @@ -335,6 +370,13 @@ private function delete() 'message' => 'Not Found', 'code' => 404 )); + if ($this->debug) { + $error['debug'] = array( + 'table' => $table, + 'index' => $id, + 'warning' => DEBUG_TABLE_ERROR_MSG, + ); + } die(json_encode($error)); } From 69d1606a95035d7e789984f8f129a35e55c2760f Mon Sep 17 00:00:00 2001 From: Vasil Stoychev Date: Sat, 13 Jul 2013 11:46:09 +0300 Subject: [PATCH 3/3] utf8 encode result array in order for json_encode not to fail at unencoded utf8 characters --- lib/arrest-mysql.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/arrest-mysql.php b/lib/arrest-mysql.php index bcb37383..606f1282 100644 --- a/lib/arrest-mysql.php +++ b/lib/arrest-mysql.php @@ -279,6 +279,8 @@ private function read() ->where($index, $id) ->query(); if($result = $this->db->fetch_array()){ + // map to utf8 to avoid broken json + $result = array_map('utf8_encode', $result); die(json_encode($result)); } else { $error = array('error' => array(