From 12c6f45b43e2256acca367d84169b7f515ba97ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gr=C3=B6hbiel?= Date: Fri, 18 Apr 2014 23:55:09 +0200 Subject: [PATCH 1/9] first draft including paging, fields filter for item view --- mysql.php | 23 +++- phprestsql.ini | 1 + phprestsql.php | 349 +++++++++++++++++++++++++++++++------------------ urlparser.php | 58 ++++++++ 4 files changed, 299 insertions(+), 132 deletions(-) create mode 100644 urlparser.php diff --git a/mysql.php b/mysql.php index 498b8cb..f58d856 100644 --- a/mysql.php +++ b/mysql.php @@ -81,20 +81,35 @@ function getColumns($table) { * Get a row from a table. * @param str table * @param str where + * @param str[] contains requested columns * @return resource A resultset resource */ - function getRow($table, $where) { - return mysql_query(sprintf('SELECT * FROM %s WHERE %s', $table, $where)); + function getRow($table, $where, $fields = NULL) { + $inject = ''; + if($fields == NULL) $inject = "*"; + else { + $fieldnames = explode(',', $fields); + foreach($fieldnames as $field) { + $inject .= $field . ','; + } + // remove redundant comma + $inject = substr($inject, 0, strlen($inject)-1); + } + return mysql_query(sprintf('SELECT %s FROM %s WHERE %s', $inject, $table, $where)); } /** * Get the rows in a table. * @param str primary The names of the primary columns to return * @param str table + * @param int from lowerbound for the LIMIT + * @param int to denoting the interval starting at lowerbound + * @param str[] sort contains columns for sorting purposes + * @param str[] filter contains search criteria for the rows * @return resource A resultset resource */ - function getTable($primary, $table) { - return mysql_query(sprintf('SELECT %s FROM %s', $primary, $table)); + function getTable($primary, $table, $from, $to, $sort = NULL, $filter = NULL) { + return mysql_query(sprintf('SELECT %s FROM %s LIMIT %d, %d', $primary, $table, $from, $to)); } /** diff --git a/phprestsql.ini b/phprestsql.ini index 397c16f..e512562 100644 --- a/phprestsql.ini +++ b/phprestsql.ini @@ -1,5 +1,6 @@ [settings] baseURL = "/phprestsql" +paging = 20 [database] type = "mysql" diff --git a/phprestsql.php b/phprestsql.php index 382ba49..7d61663 100644 --- a/phprestsql.php +++ b/phprestsql.php @@ -1,4 +1,5 @@ @@ -41,19 +42,19 @@ class PHPRestSQL { * @var str */ var $method = 'GET'; - + /** * The HTTP request data sent (if any). * @var str */ var $requestData = NULL; - - /** - * The URL extension stripped off of the request URL - * @var str - */ - var $extension = NULL; - + + /** + * The URL extension stripped off of the request URL + * @var str + */ + var $extension = NULL; + /** * The database table to query. * @var str @@ -74,17 +75,66 @@ class PHPRestSQL { /** * Type of display, database, table or row. + * @var str */ var $display = NULL; + /** + * Holds the page type (tables|table|row). + * @var str + */ + var $type = NULL; + + /** + * Stores filter criteria for presenting the table. + * @var str[] + */ + var $filters = array(); + + /** + * Stores sort parameter criteria for tables. + * @var str[] + */ + var $sort = NULL; + + /** + * Stores a subset of columns to be presented on item view. + * @var str[] + */ + var $fields = NULL; + + /** + * Holds the current page number. + * @var int + */ + var $page = NULL; + + /** + * Defines how many resources a page contains. + * @var int + */ + var $per_page = NULL; + + /** + * Denotes the format + * @var str + */ + var $format = NULL; + /** * Constructor. Parses the configuration file "phprestsql.ini", grabs any request data sent, records the HTTP * request method used and parses the request URL to find out the requested table name and primary key values. * @param str iniFile Configuration file to use */ function PHPRestSQL($iniFile = 'phprestsql.ini') { + $this->config = parse_ini_file($iniFile, TRUE); + // Setting default values if parameter is undefined. + $this->per_page = $this->config['settings']['paging']; + $this->page = 0; + $this->format = 'html'; + if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD'])) { if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > 0) { @@ -96,32 +146,49 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') { fclose($httpContent); } - $urlString = substr($_SERVER['REQUEST_URI'], strlen($this->config['settings']['baseURL'])); - $urlParts = explode('/', $urlString); - - $lastPart = array_pop($urlParts); - $dotPosition = strpos($lastPart, '.'); - if ($dotPosition !== FALSE) { - $this->extension = substr($lastPart, $dotPosition + 1); - $lastPart = substr($lastPart, 0, $dotPosition); - } - array_push($urlParts, $lastPart); - - if (isset($urlParts[0]) && $urlParts[0] == '') { - array_shift($urlParts); - } - - if (isset($urlParts[0])) $this->table = $urlParts[0]; - if (count($urlParts) > 1 && $urlParts[1] != '') { - array_shift($urlParts); - foreach ($urlParts as $uid) { - if ($uid != '') { - $this->uid[] = $uid; + $this->method = $_SERVER['REQUEST_METHOD']; + + // Parsing URL to load all parameters and directories. + $parser = new RequestParser($_SERVER['REQUEST_URI'], $this->config['settings']['baseURL']); + $params = $parser->getParameters(); + + // Evaluate the params detected by urlparser. + if(is_array($params)) { + foreach($params as $key => $value) { + switch($key) { + case "format": + $this->format = $value; + break; + case "sort": + $this->sort = $value; + break; + case "page": + $this->page = $value; + break; + case "per_page": + $this->per_page = $value; + break; + case "fields": + $this->fields = $value; + break; + default: + array_push($this->filters, $value); } } } - $this->method = $_SERVER['REQUEST_METHOD']; + $dirs = $parser->getDirs(); + if(count($dirs) == 0) { + $this->type = 'tables'; + } + else if(count($dirs) == 1) { + $this->table = $dirs[0]; + $this->type = 'table'; + } else if(count($dirs) == 2) { + $this->type = 'row'; + $this->uid = $dirs[1]; + $this->table = $dirs[0]; + } } } @@ -138,8 +205,8 @@ function connect() { trigger_error('Could not connect to server', E_USER_ERROR); } } elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) { - $this->config['database']['username'] = $_SERVER['PHP_AUTH_USER']; - $this->config['database']['password'] = $_SERVER['PHP_AUTH_PW']; + $this->config['database']['username'] = $_SERVER['PHP_AUTH_USER']; + $this->config['database']['password'] = $_SERVER['PHP_AUTH_PW']; if (!$this->db->connect($this->config['database'])) { $this->unauthorized(); exit; @@ -148,13 +215,14 @@ function connect() { $this->unauthorized(); exit; } + } /** * Execute the request. */ function exec() { - + $this->connect(); switch ($this->method) { @@ -181,18 +249,7 @@ function exec() { * @return str[] The primary key field names */ function getPrimaryKeys() { - return $this->db->getPrimaryKeys($this->table); - - #$resource = $this->db->getColumns($this->table); - #$primary = NULL; - #if ($resource) { - # while ($row = $this->db->row($resource)) { - # if ($row['Key'] == 'PRI') { - # $primary[] = $row['Field']; - # } - # } - #} - #return $primary; + return $this->db->getPrimaryKeys($this->table); } /** @@ -201,79 +258,115 @@ function getPrimaryKeys() { * database contents. */ function get() { - if ($this->table) { - $primary = $this->getPrimaryKeys(); - if ($primary) { - if ($this->uid && count($primary) == count($this->uid)) { // get a row - $this->display = 'row'; - $where = ''; - foreach($primary as $key => $pri) { - $where .= $pri.' = \''.$this->uid[$key].'\' AND '; - } - $where = substr($where, 0, -5); - $resource = $this->db->getRow($this->table, $where); - if ($resource) { - if ($this->db->numRows($resource) > 0) { - while ($row = $this->db->row($resource)) { - $values = array(); - foreach ($row as $column => $data) { - $field = array( - 'field' => $column, - 'value' => $data - ); - if (substr($column, -strlen($this->config['database']['foreignKeyPostfix'])) == $this->config['database']['foreignKeyPostfix']) { - $field['xlink'] = $this->config['settings']['baseURL'].'/'.substr($column, 0, -strlen($this->config['database']['foreignKeyPostfix'])).'/'.$data; - } - $values[] = $field; - } - $this->output['row'] = $values; - } - $this->generateResponseData(); - } else { - $this->notFound(); - } - } else { - $this->unauthorized(); - } - } else { // get table - $this->display = 'table'; - $resource = $this->db->getTable(join(', ', $primary), $this->table); - if ($resource) { - if ($this->db->numRows($resource) > 0) { - while ($row = $this->db->row($resource)) { - $this->output['table'][] = array( - 'xlink' => $this->config['settings']['baseURL'].'/'.$this->table.'/'.join('/', $row), - 'value' => join(' ', $row) - ); - } + $primary = $this->getPrimaryKeys(); + + switch($this->type) { + case "tables": + $this->get_tables(); + break; + case "table": + $this->get_table($primary); + break; + case "row": + $this->get_row($primary); + break; + } + } + + /** + * Is responsible for fetching the requested content of a row. + * - fields can be used to only present a subset of all columns. + * - format parameter can be used to choose representation + */ + private function get_row($primary) { + $this->display = 'row'; + $where = ''; + foreach($primary as $key => $pri) { + $where .= $pri.' = \''.$this->uid[$key].'\' AND '; + } + $where = substr($where, 0, -5); + $resource = $this->db->getRow($this->table, $where, $this->fields); + + if ($resource) { + if ($this->db->numRows($resource) > 0) { + while ($row = $this->db->row($resource)) { + $values = array(); + foreach ($row as $column => $data) { + $field = array( + 'field' => $column, + 'value' => $data + ); + if (substr($column, -strlen($this->config['database']['foreignKeyPostfix'])) == $this->config['database']['foreignKeyPostfix']) { + $field['xlink'] = $this->config['settings']['baseURL'].'/'.substr($column, 0, -strlen($this->config['database']['foreignKeyPostfix'])).'/'.$data; } - $this->generateResponseData(); - } else { - $this->unauthorized(); + $values[] = $field; } + $this->output['row'] = $values; } + $this->generateResponseData(); + } else { + $this->notFound(); } - } else { // get database - $this->display = 'database'; - $resource = $this->db->getDatabase(); - if ($resource) { - if ($this->db->numRows($resource) > 0) { - while ($row = $this->db->row($resource)) { - $this->output['database'][] = array( - 'xlink' => $this->config['settings']['baseURL'].'/'.reset($row), - 'value' => reset($row) - ); - } - $this->generateResponseData(); - } else { - $this->notFound(); + } else { + $this->unauthorized(); + } + } + + /** + * Is responsibel for loading the requested table content. + * Based on the following GET parameters: page, per_page, sort, col1=filter, format parameter. + */ + private function get_table($primary) { + $this->display = 'table'; + + /* TODO: Read GET parameters to find out about the where clause */ + $from = $this->page * $this->per_page; + $resource = $this->db->getTable(join(', ', $primary), + $this->table, + $from, + $this->per_page, + $this->sort, + $this->filters); + + if ($resource) { + if ($this->db->numRows($resource) > 0) { + while ($row = $this->db->row($resource)) { + $this->output['table'][] = array( + 'xlink' => $this->config['settings']['baseURL'].'/'.$this->table.'/'.join('/', $row), + 'value' => join(' ', $row) + ); + } + } + $this->generateResponseData(); + } else { + $this->unauthorized(); + } + } + + /** + * Loads all tables of a given database. + */ + private function get_tables() { + $this->display = 'database'; + + $resource = $this->db->getDatabase(); + if ($resource) { + if ($this->db->numRows($resource) > 0) { + while ($row = $this->db->row($resource)) { + $this->output['database'][] = array( + 'xlink' => $this->config['settings']['baseURL'].'/'.reset($row), + 'value' => reset($row) + ); } + $this->generateResponseData(); } else { - $this->unauthorized(); + $this->notFound(); } + } else { + $this->unauthorized(); } } - + /** * Execute a POST request. */ @@ -329,7 +422,7 @@ function post() { $resource = $this->db->insertRow($this->table, $names, $values); if ($resource) { if ($this->db->numAffected() > 0) { - $this->created($this->config['settings']['baseURL'].'/'.$this->table.'/'.$this->db->lastInsertId().'/'); + $this->created($this->config['settings']['baseURL'].'/'.$this->table.'/'.$this->db->lastInsertId().'/'); } else { $this->badRequest(); } @@ -419,7 +512,7 @@ function put() { $this->methodNotAllowed('GET, HEAD'); } } - + /** * Execute a DELETE request. A DELETE request removes a row from the database given a table and primary key(s). */ @@ -470,14 +563,14 @@ function parseRequestData() { * Generate the HTTP response data. */ function generateResponseData() { - if ($this->extension) { - if (isset($this->config['mimetypes'][$this->extension])) { - $mimetype = $this->config['mimetypes'][$this->extension]; - if (isset($this->config['renderers'][$mimetype])) { - $renderClass = $this->config['renderers'][$mimetype]; - } - } - } elseif (isset($_SERVER['HTTP_ACCEPT'])) { + if ($this->extension) { + if (isset($this->config['mimetypes'][$this->extension])) { + $mimetype = $this->config['mimetypes'][$this->extension]; + if (isset($this->config['renderers'][$mimetype])) { + $renderClass = $this->config['renderers'][$mimetype]; + } + } + } elseif (isset($_SERVER['HTTP_ACCEPT'])) { $accepts = explode(',', $_SERVER['HTTP_ACCEPT']); $orderedAccepts = array(); foreach ($accepts as $key => $accept) { @@ -506,14 +599,14 @@ function generateResponseData() { } else { $renderClass = array_shift($this->config['renderers']); } - if (isset($renderClass)) { - require_once($renderClass); - $renderer = new PHPRestSQLRenderer(); - $renderer->render($this); - } else { - $this->notAcceptable(); - exit; - } + if (isset($renderClass)) { + require_once($renderClass); + $renderer = new PHPRestSQLRenderer(); + $renderer->render($this); + } else { + $this->notAcceptable(); + exit; + } } /** @@ -587,4 +680,4 @@ function internalServerError() { } -?> +?> \ No newline at end of file diff --git a/urlparser.php b/urlparser.php new file mode 100644 index 0000000..47473f2 --- /dev/null +++ b/urlparser.php @@ -0,0 +1,58 @@ +URL = $URL; + $this->BASEURL = $baseUrl; + $this->parse(); + } + + function parse() { + $pos = strpos($this->URL, $this->BASEURL); + if($pos == 0) { + $substr = substr($this->URL, strlen($this->BASEURL)); + $this->URL = $substr; + } + + // load all get parameters in arrays + foreach ($_GET as $key => $value) { + //TODO: hash map: check if it's e predefined parameter... if no, add to filter bundle + $this->PARAMS[$key] = $value; + } + + if($this->URL == '/') { + return; + } else { + $urlParts = explode('/', $this->URL); + $i = 0; + foreach($urlParts as $val) { + // making sure /?param=value is not a added as direction + if($val !== '' && isset($val) && ($val[0] !== '?' && $val[0] !== '&')) { + // making sure we cut off the params + $pos = strpos($val, "?"); + if($pos == false) $this->DIRS[$i++] = $val; + else $this->DIRS[$i++] = substr($val, 0, $pos); + } + } + } + + } + + function getDirs() { + return $this->DIRS; + } + + function getParameters() { + return $this->PARAMS; + } + +} +?> \ No newline at end of file From bec9875c8a54350681f5658a5ec34bc120123045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gr=C3=B6hbiel?= Date: Sat, 19 Apr 2014 00:16:29 +0200 Subject: [PATCH 2/9] no more deprecated warning for PHP 5.4 users --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.php b/index.php index 1b26c0a..4b28983 100644 --- a/index.php +++ b/index.php @@ -2,7 +2,7 @@ require_once('phprestsql.php'); -$PHPRestSQL =& new PHPRestSQL(); +$PHPRestSQL = new PHPRestSQL(); $PHPRestSQL->exec(); /* From 54806262232ae8116f66ecf5cbf18ef0f6a3f3f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gr=C3=B6hbiel?= Date: Sat, 19 Apr 2014 12:40:58 +0200 Subject: [PATCH 3/9] ironed out tiny bugs --- index.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.php b/index.php index 4b28983..b8e53e6 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,9 @@ exec(); From 9962d1836158f446cdf98a0dfb14aee0214d1aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gr=C3=B6hbiel?= Date: Sat, 19 Apr 2014 13:52:58 +0200 Subject: [PATCH 4/9] WHERE clause and ORDER BY draft --- mysql.php | 51 +++++++++++++++++++++++++++++++++++++++++++++++--- phprestsql.php | 12 ++++++------ 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/mysql.php b/mysql.php index f58d856..f50b21d 100644 --- a/mysql.php +++ b/mysql.php @@ -98,7 +98,7 @@ function getRow($table, $where, $fields = NULL) { return mysql_query(sprintf('SELECT %s FROM %s WHERE %s', $inject, $table, $where)); } - /** + /** * Get the rows in a table. * @param str primary The names of the primary columns to return * @param str table @@ -108,8 +108,53 @@ function getRow($table, $where, $fields = NULL) { * @param str[] filter contains search criteria for the rows * @return resource A resultset resource */ - function getTable($primary, $table, $from, $to, $sort = NULL, $filter = NULL) { - return mysql_query(sprintf('SELECT %s FROM %s LIMIT %d, %d', $primary, $table, $from, $to)); + function getTable($primary, $table, $from, $to, $orderby = NULL, $filters = NULL) { + + // pepare ORDER BY clause + $orderbys = explode(',', $orderby); + if($orderby != NULL) { + $orderby_clause = 'ORDER BY '; + foreach($orderbys as $order) { + if($order[0] == '-') { + $order = ltrim($order, '-'); + $orderby_clause .= ' ' . $order . ' DESC,'; + } else { + $orderby_clause .= ' ' . $order . ' ASC,'; + } + } + $orderby_clause = rtrim($orderby_clause, ","); + } else { + $orderby_clause = ''; + } + + // prepare WHERE clause + $where_clause = ''; + if(count($filters) > 0) { + $where_clause = 'WHERE '; + foreach($filters as $key => $value) { + $operator = $value[0]; + $value = mysql_real_escape_string(substr($value, 1)); + $key = mysql_real_escape_string($key); + switch($operator) { + case '~': + $where_clause .= ' ' . '`' . $key . '`' . ' LIKE ' . '\'%' . $value . '%\'' . ' AND'; + break; + case '=': + $where_clause .= ' ' . '`' . $key . '`' . '=' . $value . ' AND'; + break; + case '<': + $where_clause .= ' ' . '`' . $key . '`' . '<' . $value . 'AND'; + break; + case '>': + $where_clause .= ' ' . '`' . $key . '`' . '>' . $value . ' AND'; + break; + } + } + $where_clause = rtrim($where_clause, "AND"); + } + + $query = sprintf('SELECT %s FROM %s %s %s LIMIT %d, %d', $primary, $table, $where_clause, $orderby_clause, $from, $to); + return mysql_query($query); } /** diff --git a/phprestsql.php b/phprestsql.php index 7d61663..edf68d8 100644 --- a/phprestsql.php +++ b/phprestsql.php @@ -92,10 +92,10 @@ class PHPRestSQL { var $filters = array(); /** - * Stores sort parameter criteria for tables. + * Stores orderby parameter criteria for tables. * @var str[] */ - var $sort = NULL; + var $orderby = NULL; /** * Stores a subset of columns to be presented on item view. @@ -159,8 +159,8 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') { case "format": $this->format = $value; break; - case "sort": - $this->sort = $value; + case "orderby": + $this->orderby = $value; break; case "page": $this->page = $value; @@ -314,7 +314,7 @@ private function get_row($primary) { /** * Is responsibel for loading the requested table content. - * Based on the following GET parameters: page, per_page, sort, col1=filter, format parameter. + * Based on the following GET parameters: page, per_page, orderby, col1=filter, format parameter. */ private function get_table($primary) { $this->display = 'table'; @@ -325,7 +325,7 @@ private function get_table($primary) { $this->table, $from, $this->per_page, - $this->sort, + $this->orderby, $this->filters); if ($resource) { From faebbc3c6ad0efb565547f2485e61ff1998a91ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gr=C3=B6hbiel?= Date: Sat, 19 Apr 2014 13:54:21 +0200 Subject: [PATCH 5/9] WHERE clause and ORDER BY draft --- mysql.php | 16 ++++++++-------- phprestsql.php | 9 ++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/mysql.php b/mysql.php index f50b21d..92633e3 100644 --- a/mysql.php +++ b/mysql.php @@ -40,13 +40,13 @@ class mysql { */ function connect($config) { if ($this->db = @mysql_pconnect( - $config['server'], - $config['username'], - $config['password'] - )) { - if ($this->select_db($config['database'])) { - return TRUE; - } + $config['server'], + $config['username'], + $config['password'] + )) { + if ($this->select_db($config['database'])) { + return TRUE; + } } return FALSE; } @@ -98,7 +98,7 @@ function getRow($table, $where, $fields = NULL) { return mysql_query(sprintf('SELECT %s FROM %s WHERE %s', $inject, $table, $where)); } - /** + /** * Get the rows in a table. * @param str primary The names of the primary columns to return * @param str table diff --git a/phprestsql.php b/phprestsql.php index edf68d8..a0007c6 100644 --- a/phprestsql.php +++ b/phprestsql.php @@ -92,7 +92,7 @@ class PHPRestSQL { var $filters = array(); /** - * Stores orderby parameter criteria for tables. + * Stores sort parameter criteria for tables. * @var str[] */ var $orderby = NULL; @@ -127,7 +127,6 @@ class PHPRestSQL { * @param str iniFile Configuration file to use */ function PHPRestSQL($iniFile = 'phprestsql.ini') { - $this->config = parse_ini_file($iniFile, TRUE); // Setting default values if parameter is undefined. @@ -172,7 +171,7 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') { $this->fields = $value; break; default: - array_push($this->filters, $value); + $this->filters[$key] = $value; } } } @@ -222,7 +221,6 @@ function connect() { * Execute the request. */ function exec() { - $this->connect(); switch ($this->method) { @@ -314,7 +312,7 @@ private function get_row($primary) { /** * Is responsibel for loading the requested table content. - * Based on the following GET parameters: page, per_page, orderby, col1=filter, format parameter. + * Based on the following GET parameters: page, per_page, sort, col1=filter, format parameter. */ private function get_table($primary) { $this->display = 'table'; @@ -361,6 +359,7 @@ private function get_tables() { $this->generateResponseData(); } else { $this->notFound(); + } } else { $this->unauthorized(); From 37370f796b9355605087cb2ac49b5030c2101bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gr=C3=B6hbiel?= Date: Sat, 19 Apr 2014 14:02:54 +0200 Subject: [PATCH 6/9] making LIMIT clause for table view optional, to not break compatibility with non-mysql adapters. --- json.php | 30 +++++++++++++++--------------- mysql.php | 8 ++++++-- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/json.php b/json.php index 9c75c1b..30f62c2 100644 --- a/json.php +++ b/json.php @@ -3,21 +3,21 @@ /** * PHP REST SQL JSON renderer class * This class renders the REST response data as JSON. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * */ class PHPRestSQLRenderer { diff --git a/mysql.php b/mysql.php index 92633e3..99ae117 100644 --- a/mysql.php +++ b/mysql.php @@ -108,8 +108,12 @@ function getRow($table, $where, $fields = NULL) { * @param str[] filter contains search criteria for the rows * @return resource A resultset resource */ - function getTable($primary, $table, $from, $to, $orderby = NULL, $filters = NULL) { + function getTable($primary, $table, $from = NULL, $to = NULL, $orderby = NULL, $filters = NULL) { + // prepare LIMIT clause + if($from == NULL || $to == NULL) $limit_clause = ''; + else $limit_clause = ' LIMIT ' . $from . ',' . $to . ' '; + // pepare ORDER BY clause $orderbys = explode(',', $orderby); if($orderby != NULL) { @@ -153,7 +157,7 @@ function getTable($primary, $table, $from, $to, $orderby = NULL, $filters = NULL $where_clause = rtrim($where_clause, "AND"); } - $query = sprintf('SELECT %s FROM %s %s %s LIMIT %d, %d', $primary, $table, $where_clause, $orderby_clause, $from, $to); + $query = sprintf('SELECT %s FROM %s %s %s %s', $primary, $table, $where_clause, $orderby_clause, $limit_clause); return mysql_query($query); } From b0efe039d7ec8d871037a29ba459408b7cf3ef79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gr=C3=B6hbiel?= Date: Sat, 19 Apr 2014 14:18:03 +0200 Subject: [PATCH 7/9] restoring database adapter compatibility. --- mssql.php | 2 +- mysql.php | 8 +++++--- postgresql.php | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/mssql.php b/mssql.php index 2c5b054..73ad1ae 100644 --- a/mssql.php +++ b/mssql.php @@ -105,7 +105,7 @@ function getRow($table, $where) { * @param str table * @return resource A resultset resource */ - function getTable($primary, $table) { + function getTable($primary, $table, $from = NULL, $to = NULL, $orderby = NULL, $filters = NULL) { $result = mssql_query(sprintf('SELECT %s FROM %s', $primary, $table)); if ($result) { $this->lastQueryResultResource = $result; diff --git a/mysql.php b/mysql.php index 99ae117..42814ae 100644 --- a/mysql.php +++ b/mysql.php @@ -111,9 +111,11 @@ function getRow($table, $where, $fields = NULL) { function getTable($primary, $table, $from = NULL, $to = NULL, $orderby = NULL, $filters = NULL) { // prepare LIMIT clause - if($from == NULL || $to == NULL) $limit_clause = ''; - else $limit_clause = ' LIMIT ' . $from . ',' . $to . ' '; - + $limit_clause = ''; + if(($from != NULL) && ($to != NULL)) { + $limit_clause .= ' LIMIT ' . $from . ', ' . $to . ' '; + } + // pepare ORDER BY clause $orderbys = explode(',', $orderby); if($orderby != NULL) { diff --git a/postgresql.php b/postgresql.php index 0f4574d..c08fcf4 100644 --- a/postgresql.php +++ b/postgresql.php @@ -104,7 +104,7 @@ function getRow($table, $where) { * @param str table * @return resource A resultset resource */ - function getTable($primary, $table) { + function getTable($primary, $table, $from = NULL, $to = NULL, $orderby = NULL, $filters = NULL) { $result = pg_query(sprintf('SELECT %s FROM %s', $primary, $table)); if ($result) { $this->lastQueryResultResource = $result; From 3235ebaa1e924bd0ec621ea7083b8d501f948a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gr=C3=B6hbiel?= Date: Sat, 19 Apr 2014 15:06:22 +0200 Subject: [PATCH 8/9] format parameter introduced --- mysql.php | 4 ++-- phprestsql.php | 14 ++++---------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/mysql.php b/mysql.php index 42814ae..0b75c3e 100644 --- a/mysql.php +++ b/mysql.php @@ -112,10 +112,10 @@ function getTable($primary, $table, $from = NULL, $to = NULL, $orderby = NULL, $ // prepare LIMIT clause $limit_clause = ''; - if(($from != NULL) && ($to != NULL)) { + if(($from !== NULL) && ($to !== NULL)) { $limit_clause .= ' LIMIT ' . $from . ', ' . $to . ' '; } - + // pepare ORDER BY clause $orderbys = explode(',', $orderby); if($orderby != NULL) { diff --git a/phprestsql.php b/phprestsql.php index a0007c6..3eca375 100644 --- a/phprestsql.php +++ b/phprestsql.php @@ -49,12 +49,6 @@ class PHPRestSQL { */ var $requestData = NULL; - /** - * The URL extension stripped off of the request URL - * @var str - */ - var $extension = NULL; - /** * The database table to query. * @var str @@ -116,7 +110,7 @@ class PHPRestSQL { var $per_page = NULL; /** - * Denotes the format + * Denotes the format of the output. * @var str */ var $format = NULL; @@ -562,9 +556,9 @@ function parseRequestData() { * Generate the HTTP response data. */ function generateResponseData() { - if ($this->extension) { - if (isset($this->config['mimetypes'][$this->extension])) { - $mimetype = $this->config['mimetypes'][$this->extension]; + if ($this->format) { + if (isset($this->config['mimetypes'][$this->format])) { + $mimetype = $this->config['mimetypes'][$this->format]; if (isset($this->config['renderers'][$mimetype])) { $renderClass = $this->config['renderers'][$mimetype]; } From 1ec76b89891b24e91439650043c72cbe5af17936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gr=C3=B6hbiel?= Date: Sat, 19 Apr 2014 15:26:12 +0200 Subject: [PATCH 9/9] accepts multiple primary keys again, as original version --- phprestsql.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/phprestsql.php b/phprestsql.php index 3eca375..8728a94 100644 --- a/phprestsql.php +++ b/phprestsql.php @@ -169,7 +169,7 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') { } } } - + $dirs = $parser->getDirs(); if(count($dirs) == 0) { $this->type = 'tables'; @@ -179,7 +179,10 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') { $this->type = 'table'; } else if(count($dirs) == 2) { $this->type = 'row'; - $this->uid = $dirs[1]; + $uids = explode(',', $dirs[1]); + foreach($uids as $uid) { + if($uid !== '') $this->uid[] = $uid; + } $this->table = $dirs[0]; }