Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements and issues fixed #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
5 changes: 4 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once('phprestsql.php');
require_once('urlparser.php');

$PHPRestSQL =& new PHPRestSQL();
$PHPRestSQL = new PHPRestSQL();
$PHPRestSQL->exec();

/*
Expand Down
30 changes: 15 additions & 15 deletions json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion mssql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
88 changes: 77 additions & 11 deletions mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -81,20 +81,86 @@ 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 = NULL, $to = NULL, $orderby = NULL, $filters = NULL) {

// prepare LIMIT clause
$limit_clause = '';
if(($from !== NULL) && ($to !== NULL)) {
$limit_clause .= ' LIMIT ' . $from . ', ' . $to . ' ';
}

// 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 %s', $primary, $table, $where_clause, $orderby_clause, $limit_clause);
return mysql_query($query);
}

/**
Expand Down
1 change: 1 addition & 0 deletions phprestsql.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[settings]
baseURL = "/phprestsql"
paging = 20

[database]
type = "mysql"
Expand Down
Loading