Skip to content

Commit

Permalink
Merge branch '0.3' of github.com:martinrusev/Crystal
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Rusev committed Jan 15, 2010
2 parents a5a4ab7 + 7ae600c commit 2d270e4
Show file tree
Hide file tree
Showing 23 changed files with 377 additions and 161 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.mkd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Changelog
Last updated 08.01.2010

#Version 0.3
- Added query_time() function
- Added print_as_table() function
- Added inline configuration settings
4 changes: 2 additions & 2 deletions Crystal/Config/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static public function get_db_value($key, $value)

require(CRYSTAL_BASE . CRYSTAL_DS . 'config' . CRYSTAL_DS . 'database.php');


if(isset($db[$key][$value]) && !empty($db[$key][$value]))
{
return $db[$key][$value];
Expand All @@ -46,7 +46,7 @@ static public function get_db_config($key)


require(CRYSTAL_BASE . CRYSTAL_DS . 'config' . CRYSTAL_DS . 'database.php');


if(isset($db[$key]) && !empty($db[$key]))
{
Expand Down
28 changes: 13 additions & 15 deletions Crystal/Connection/Adapter/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ function __construct($database_config, $params=null)

/** CHECKS FOR PORT **/
$port = (isset($database_config['port'])?$database_config['port']:'3306');
$this->db = mysql_connect($database_config['hostname']. ':'. $port, $database_config['username'], $database_config['password']);
$hostname = (isset($database_config['hostname'])?$database_config['hostname']:'localhost');

$this->db = mysql_connect($hostname. ':'. $port, $database_config['username'], $database_config['password']);

/** SETS DATABASE COLLATION **/
$this->_set_charset($database_config['char_set']);
Expand Down Expand Up @@ -72,20 +74,16 @@ function __construct($database_config, $params=null)

private function _set_charset($charset)
{
try
{ /**
* TODO - replace with general database helper
**/
mysql_query("SET NAMES " . $charset );
}
catch (Exception $e)
{
throw new Crystal_Connection_Adapter_Exception("Cannot set database charset");
}





if(isset($charset) && !empty($charset))
{
mysql_query("SET NAMES " . $charset);
}
else
{
mysql_query("SET NAMES utf8");
}

}


Expand Down
29 changes: 13 additions & 16 deletions Crystal/Connection/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function __construct($database_config, $params=null)

/** CHECKS FOR PORT **/
$port = (isset($database_config['port'])?$database_config['port']:'5432');

$hostname = (isset($database_config['hostname'])?$database_config['hostname']:'localhost');


/** CHECKS FOR database in params **/
Expand All @@ -32,7 +32,7 @@ function __construct($database_config, $params=null)
if(array_key_exists('database', $params))
{

$conn_string = 'host=' . $database_config['hostname'] . ' port=' . $port . ' dbname=' . $params['database']
$conn_string = 'host=' . $hostname . ' port=' . $port . ' dbname=' . $params['database']
. ' user=' . $database_config['username'] . ' password=' . $database_config['password'];


Expand All @@ -41,7 +41,7 @@ function __construct($database_config, $params=null)
}
else
{
$conn_string = 'host=' . $database_config['hostname'] . ' port=' . $port . ' dbname=' . $database_config['database']
$conn_string = 'host=' . $hostname . ' port=' . $port . ' dbname=' . $database_config['database']
. ' user=' . $database_config['username'] . ' password=' . $database_config['password'];
}

Expand Down Expand Up @@ -74,20 +74,17 @@ function __construct($database_config, $params=null)

private function _set_charset($charset)
{
try
{ /**
* TODO - replace with general database helper
**/
pg_query("SET NAMES " . Crystal_Helper_Postgres::add_single_quote($charset));
}
catch (Exception $e)
{
throw new Crystal_Connection_Adapter_Exception("Cannot set database charset");
}




if(isset($charset) && !empty($charset))
{
pg_query("SET NAMES " . Crystal_Helper_Postgres::add_single_quote($charset));
}
else
{
pg_query("SET NAMES 'UTF8'");
}


}


Expand Down
19 changes: 0 additions & 19 deletions Crystal/Connection/Adapter/Sqlite3.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,5 @@ function __construct($database_config)




/*
private function _set_charset($charset)
{
try
{
pg_query("SET NAMES " . Crystal_Methods_Mysql_Helper::add_single_quote($charset));
}
catch (Exception $e)
{
throw new Crystal_Connection_Adapter_Exception("Cannot set database charset");
}
}
*/


}
11 changes: 10 additions & 1 deletion Crystal/Connection/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ function __construct($connection = null, $params = null)
/** GETS PREFERED CONNECTION DETAILS **/
if(isset($connection))
{
$db_config = Crystal_Config_Reader::get_db_config($connection);

if(is_array($connection))
{
$db_config = $connection;
}
else
{
$db_config = Crystal_Config_Reader::get_db_config($connection);
}

}
/** FALLS BACK TO DEFAULT **/
else
Expand Down
12 changes: 10 additions & 2 deletions Crystal/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ function __construct($active_connection)
/** LOADS ADAPTER **/
if(self::_check_connection($active_connection) == TRUE)
{

$this->_driver = Crystal_Config_Reader::get_db_value($active_connection, 'driver');

if(is_array($active_connection))
{
$this->_driver = $active_connection['driver'];
}
else
{
$this->_driver = Crystal_Config_Reader::get_db_value($active_connection, 'driver');
}


}
else
Expand Down
2 changes: 1 addition & 1 deletion Crystal/Manipulation/Mysql/Alter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function __construct($method, $params)
switch ($method)
{
case 'alter_table':
$this->alter = "ALTER TABLE" . Crystal_Helper::add_apostrophe($params[0]);
$this->alter = "ALTER TABLE" . Crystal_Helper_Mysql::add_apostrophe($params[0]);
break;


Expand Down
9 changes: 3 additions & 6 deletions Crystal/Manipulation/Mysql/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ function __construct($active_connection = null)
{

$this->active_connection = $active_connection;
$this->conn = new Crystal_Connection_Manager($this->active_connection);
$this->helper = new Crystal_Helper_Mysql($this->conn);

}


Expand Down Expand Up @@ -138,12 +141,6 @@ public function execute()
$this->conn = new Crystal_Connection_Manager($this->active_connection, $this->database);

}
else
{

$this->conn = new Crystal_Connection_Manager($this->active_connection);
}


$this->query = mysql_query($this->sql);

Expand Down
6 changes: 3 additions & 3 deletions Crystal/Manipulation/Mysql/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ class Crystal_Manipulation_Mysql_Create
function __construct($method, $params)
{


switch ($method)
{
case 'create_database':
$this->create = "CREATE DATABASE " . Crystal_Helper::add_apostrophe($params[0]);
$this->create = "CREATE DATABASE " . Crystal_Helper_Mysql::add_apostrophe($params[0]);
break;


case 'create_table':
$this->create = "CREATE TABLE " . Crystal_Helper::add_apostrophe($params[0]) . " (%s)";
$this->create = "CREATE TABLE " . Crystal_Helper_Mysql::add_apostrophe($params[0]) . " (%s)";

/** TABLE OPTIONS **/
if(isset($params[1]) && !empty($params[1]))
Expand Down
4 changes: 2 additions & 2 deletions Crystal/Manipulation/Mysql/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ function __construct($method, $params)
switch ($method)
{
case 'drop_table':
$this->drop = "DROP TABLE IF EXISTS " . Crystal_Helper::add_apostrophe($params[0]);
$this->drop = "DROP TABLE IF EXISTS " . Crystal_Helper_Mysql::add_apostrophe($params[0]);
break;

case 'drop_database':
$this->drop = "DROP DATABASE " . Crystal_Helper::add_apostrophe($params[0]);
$this->drop = "DROP DATABASE " . Crystal_Helper_Mysql::add_apostrophe($params[0]);
break;

default:
Expand Down
12 changes: 6 additions & 6 deletions Crystal/Manipulation/Mysql/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function __construct($method, $params)
{


$this->fields .= Crystal_Helper::add_apostrophe($column);
$this->fields .= Crystal_Helper_Mysql::add_apostrophe($column);

$this->fields .= self::process_rows($rows, $column, $last_column);

Expand All @@ -64,7 +64,7 @@ function __construct($method, $params)
{


$this->fields .= Crystal_Helper::add_apostrophe($column);
$this->fields .= Crystal_Helper_Mysql::add_apostrophe($column);

$this->fields .= self::process_rows($rows, $column, $last_column);

Expand All @@ -77,7 +77,7 @@ function __construct($method, $params)

foreach($params as $key => $value)
{
$this->fields .= "DROP" . Crystal_Helper::add_apostrophe($value);
$this->fields .= "DROP" . Crystal_Helper_Mysql::add_apostrophe($value);


if($key != $last_column)
Expand Down Expand Up @@ -127,7 +127,7 @@ function process_rows($rows, $column, $last_column)
foreach($rows['choises'] as $key => $value)
{

$this->row .= Crystal_Helper::add_single_quote($value);
$this->row .= Crystal_Helper_Mysql::add_single_quote($value);

if($value != $last_field){$this->row .= ","; }

Expand Down Expand Up @@ -159,7 +159,7 @@ function process_rows($rows, $column, $last_column)

if(array_key_exists('default', $rows))
{
$this->row .= " DEFAULT " . Crystal_Helper::add_single_quote($rows['default']);
$this->row .= " DEFAULT " . Crystal_Helper_Mysql::add_single_quote($rows['default']);

}

Expand All @@ -169,7 +169,7 @@ function process_rows($rows, $column, $last_column)

if(array_key_exists('primary_key', $rows))
{
$this->primary_key = ", PRIMARY KEY ( " . Crystal_Helper::add_apostrophe($column) . ")";
$this->primary_key = ", PRIMARY KEY ( " . Crystal_Helper_Mysql::add_apostrophe($column) . ")";

}

Expand Down
2 changes: 1 addition & 1 deletion Crystal/Manipulation/Mysql/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function process_options($options)

if(array_key_exists('primary_key', $options))
{
$this->options .= "PRIMARY KEY (" . Crystal_Helper::add_apostrophe($options['primary_key']) . " ) ";
$this->options .= "PRIMARY KEY (" . Crystal_Helper_Mysql::add_apostrophe($options['primary_key']) . " ) ";
}


Expand Down
4 changes: 2 additions & 2 deletions Crystal/Manipulation/Mysql/Rename.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ function __construct($method, $params)



$this->rename = "RENAME TABLE" . Crystal_Helper::add_apostrophe($params[0]);
$this->rename = "RENAME TABLE" . Crystal_Helper_Mysql::add_apostrophe($params[0]);

$this-> rename .= "TO";

$this->rename .= Crystal_Helper::add_apostrophe($params[1]);
$this->rename .= Crystal_Helper_Mysql::add_apostrophe($params[1]);


}
Expand Down
2 changes: 1 addition & 1 deletion Crystal/Manipulation/Postgres/Alter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function __construct($method, $params)
switch ($method)
{
case 'alter_table':
$this->alter = "ALTER TABLE" . Crystal_Helper::add_apostrophe($params[0]);
$this->alter = "ALTER TABLE" . Crystal_Helper_Postgres::add_single_quote($params[0]);
break;


Expand Down
10 changes: 5 additions & 5 deletions Crystal/Manipulation/Postgres/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function __construct($method, $params, $table)
{


$this->fields .= Crystal_Query_Postgres_Helper::add_double_quote($column) ." ";
$this->fields .= Crystal_Helper_Postgres::add_double_quote($column) ." ";

$this->fields .= self::process_rows($rows, $column, $last_column, $table);

Expand All @@ -64,7 +64,7 @@ function __construct($method, $params, $table)
{


$this->fields .= Crystal_Query_Postgres_Helper::add_apostrophe($column);
$this->fields .= Crystal_Helper_Postgres::add_single_quote($column);

$this->fields .= self::process_rows($rows, $column, $last_column);

Expand All @@ -77,7 +77,7 @@ function __construct($method, $params, $table)

foreach($params as $key => $value)
{
$this->fields .= "DROP" . Crystal_Query_Postgres_Helper::add_apostrophe($value);
$this->fields .= "DROP" . Crystal_Helper_Postgres::add_single_quote($value);


if($key != $last_column)
Expand Down Expand Up @@ -136,7 +136,7 @@ function process_rows($rows, $column, $last_column, $table)
foreach($rows['choises'] as $key => $value)
{

$this->row .= Crystal_Query_Postgres_Helper::add_single_quote($value);
$this->row .= Crystal_Helper_Postgres::add_single_quote($value);

if($value != $last_field){$this->row .= ","; }

Expand Down Expand Up @@ -168,7 +168,7 @@ function process_rows($rows, $column, $last_column, $table)

if(array_key_exists('default', $rows))
{
$this->row .= " DEFAULT " . Crystal_Query_Postgres_Helper::add_single_quote($rows['default']);
$this->row .= " DEFAULT " . Crystal_Helper_Postgres::add_single_quote($rows['default']);

}

Expand Down
2 changes: 1 addition & 1 deletion Crystal/Manipulation/Postgres/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function process_options($options)

if(array_key_exists('primary_key', $options))
{
$this->options .= "PRIMARY KEY (" . Crystal_Helper::add_apostrophe($options['primary_key']) . " ) ";
$this->options .= "PRIMARY KEY (" . Crystal_Helper_Postgres::add_single_quote($options['primary_key']) . " ) ";
}


Expand Down

0 comments on commit 2d270e4

Please sign in to comment.