Skip to content
This repository was archived by the owner on May 4, 2018. It is now read-only.

Auto collect table indexes on map_db() execution, working with composite PRIMARY KEYs #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions lib/arrest-mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand All @@ -58,14 +72,15 @@ 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);
$this->table_index = array();
}

/**
Expand Down Expand Up @@ -132,6 +147,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();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be in camelCase, Sorry!

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;
}
Expand Down Expand Up @@ -188,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));
}

Expand Down Expand Up @@ -223,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));
}

Expand All @@ -234,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(
Expand Down Expand Up @@ -275,6 +322,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));
}

Expand Down Expand Up @@ -318,6 +372,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));
}

Expand Down