Skip to content

Commit

Permalink
Updated select, order_by and where(), updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Rusev committed Jan 20, 2010
1 parent 03e0ad7 commit 33513ce
Show file tree
Hide file tree
Showing 25 changed files with 1,216 additions and 458 deletions.
5 changes: 5 additions & 0 deletions .buildpath
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<buildpath>
<buildpathentry kind="src" path=""/>
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
</buildpath>
57 changes: 38 additions & 19 deletions Crystal/Helper/Mysql.php
Expand Up @@ -8,25 +8,26 @@
* @author Martin Rusev
* @link http://crystal.martinrusev.net
* @since Version 0.1
* @version 0.1
* @version 0.3
*/

// ------------------------------------------------------------------------
class Crystal_Helper_Mysql
{
private $conn;

public function __construct($db_connection)
public function __construct($db_connection = null)
{
$this->conn = $db_connection;

}

static function add_apostrophe($string)
static public function add_apostrophe($string)
{

if(is_string($string))
{
return " `" . mysql_real_escape_string($string) . "` ";
return " `".self::escape_string($string)."` ";
}
elseif(is_numeric($string) or $string == False)
{
Expand All @@ -45,13 +46,13 @@ static function add_apostrophe($string)


/** ACCEPTS ONLY STRING **/
static function sanitize_string($string)
static public function sanitize_string($string)
{

if(is_string($string))
{

return mysql_real_escape_string($string);
return self::escape_string($string);
}
elseif(is_numeric($string) or $string == False)
{
Expand All @@ -68,12 +69,12 @@ static function sanitize_string($string)
}


static function add_single_quote($string)
static public function add_single_quote($string)
{

if(is_string($string))
{
return " '" . mysql_real_escape_string($string) . "' ";
return " '" . self::escape_string($string) . "' ";
}
elseif(is_numeric($string) or $string == False)
{
Expand All @@ -91,11 +92,11 @@ static function add_single_quote($string)



static function add_double_quote($string)
static public function add_double_quote($string)
{
if(is_string($string))
{
return '"' . mysql_real_escape_string($string) . '"';
return '"' . self::escape_string($string) . '"';
}
elseif(is_numeric($string) or $string == False)
{
Expand All @@ -110,17 +111,15 @@ static function add_double_quote($string)

}

static function escape_update_values($cols)
static public function escape_update_values($cols)
{



foreach($cols as $key => $value)
{

$updated_cols[] = self::add_apostrophe($key) . "= " . self::add_single_quote($value) . " ";


}

$temp = implode(',', $updated_cols);
Expand All @@ -131,7 +130,7 @@ static function escape_update_values($cols)
}


static function escape_update_values_safe($cols)
static public function escape_update_values_safe($cols)
{

foreach($cols as $key => $value)
Expand All @@ -149,7 +148,7 @@ static function escape_update_values_safe($cols)

}

static function clean_db_result($rows)
static public function clean_db_result($rows)
{

if(isset($rows) && !empty($rows))
Expand All @@ -161,7 +160,7 @@ static function clean_db_result($rows)
if(!is_numeric($column))
{

$rows[$key] = stripslashes($column);
$rows[$key] = stripslashes($column);

}

Expand All @@ -173,11 +172,31 @@ static function clean_db_result($rows)
}


}


public function escape_string($string)
{

if (function_exists('mysql_real_escape_string'))
{
$string = mysql_real_escape_string($string);
}
elseif (function_exists('mysql_escape_string'))
{

$string = mysql_escape_string($string);
}
else
{
$string = addslashes($string);
}



}
return $string;



}



Expand Down
2 changes: 2 additions & 0 deletions Crystal/Parser/Exception.php
@@ -0,0 +1,2 @@
<?php
class Crystal_Parser_Exception extends Crystal_Exception{}
99 changes: 99 additions & 0 deletions Crystal/Parser/String.php
@@ -0,0 +1,99 @@
<?php
class Crystal_Parser_String
{

static function parse($string)
{

/** Check for comma **/
$comma = strpos($string, ',');

/** Multiple params **/
if($comma != False)
{
$params = explode(',', $string);

/** Check every param for special exceptions ':' **/
foreach($params as $k => $v)
{
/** Check for colon **/
$colon = strpos($v, ':');
if($colon != False)
{

$params[$k] = self::_process_string_with_colon($v);


}
else
{
$params[$k] = trim($v);
}

}

return $params;

}
else
{
/** Single column **/
$colon = strpos($string, ':');

if($colon != False)
{
$formated_string = self::_process_string_with_colon($string);

return $formated_string;
}
else
{
return trim($string);
}

}




}


private function _process_string_with_colon($string)
{

/** Explode params to array
*
* $explode_params[0] is the column name
* $explode_params[1] is the special character :
* $explode_params[2] is the value
*
*/
$explode_params = explode(' ', trim($string));

$params = array();
$params['column'] = trim($explode_params[0]);
$params['value'] = trim($explode_params[2]);


/** Now checking for special params **/
$as = strpos($string, ':as');

if($as != False)
{
$params['param'] = 'AS';
}
else
{
$params['param'] = '=';
}

return $params;

}





}
2 changes: 1 addition & 1 deletion Crystal/Query/Mysql/Count.php
Expand Up @@ -20,7 +20,7 @@ class Crystal_Query_Mysql_Count
function __construct($table)
{

$this->count = "SELECT COUNT(*) as total FROM" .Crystal_Helper_Mysql::add_apostrophe($table);
$this->count = "SELECT COUNT(*) as total FROM" . Crystal_Helper_Mysql::add_apostrophe($table);

}

Expand Down
2 changes: 1 addition & 1 deletion Crystal/Query/Mysql/Get.php
Expand Up @@ -12,7 +12,7 @@
*/

// ------------------------------------------------------------------------
class Crystal_Query_Mysql_Get
class Crystal_Query_Mysql_Get
{


Expand Down

0 comments on commit 33513ce

Please sign in to comment.