Skip to content

Commit

Permalink
数据库类库完善
Browse files Browse the repository at this point in the history
  • Loading branch information
breath-co2 committed Jan 5, 2014
1 parent 4c8f30d commit 3d4401c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 199 deletions.
19 changes: 5 additions & 14 deletions drivers/database/mongo/mongo.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected function _connect()
{
Core::debug()->error($error_host,'error_host');

if ($last_error)throw $last_error;
if ($last_error && $last_error instanceof Exception)throw $last_error;
throw new Exception('connect mongodb server error.');
}

Expand Down Expand Up @@ -360,7 +360,7 @@ public function compile($builder, $type = 'selete')
$where = array();
if (! empty($builder['where']))
{
$where = $this->_compile_conditions($builder['where'], $builder['parameters']);
$where = $this->_compile_conditions($builder['where']);
}

if ($type=='insert')
Expand Down Expand Up @@ -1045,7 +1045,7 @@ public function query($options, $as_object = null, $use_connection_type = null)
return $rs;
}

protected static function _compile_set_data($op, $value , $parameters)
protected static function _compile_set_data($op, $value)
{
$op = strtolower($op);
$op_arr = array
Expand All @@ -1062,16 +1062,8 @@ protected static function _compile_set_data($op, $value , $parameters)
{
list ($min, $max) = $value;

if (is_string($min) && array_key_exists($min, $parameters))
{
$min = $parameters[$min];
}
$option['$gte'] = $min;

if (is_string($max) && array_key_exists($max, $parameters))
{
$max = $parameters[$max];
}
$option['$lte'] = $max;
}
elseif ($op==='=')
Expand Down Expand Up @@ -1272,11 +1264,10 @@ protected static function _compile_paste_data(&$tmp_query , $tmp_option , $last_
* Compiles an array of conditions into an SQL partial. Used for WHERE
* and HAVING.
*
* @param object Database instance
* @param array condition statements
* @return string
*/
protected function _compile_conditions(array $conditions, $parameters)
protected function _compile_conditions(array $conditions)
{
$last_logic = '$and';
$tmp_query_list = array();
Expand Down Expand Up @@ -1324,7 +1315,7 @@ protected function _compile_conditions(array $conditions, $parameters)
else
{
list ($column, $op, $value) = $condition;
$tmp_option = Database_Driver_Mongo::_compile_set_data($op, $value , $parameters);
$tmp_option = Database_Driver_Mongo::_compile_set_data($op, $value);
Database_Driver_Mongo::_compile_paste_data($tmp_query, $tmp_option , $last_logic , $logic ,$column);

$last_logic = $logic;
Expand Down
57 changes: 10 additions & 47 deletions drivers/database/mysql/mysql.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ protected function _connect()
{
Core::debug()->error($error_host, 'error_host');

if ($last_error)throw $last_error;
if ($last_error && $last_error instanceof Exception)throw $last_error;
throw new Exception('connect mysql server error.');
}

Expand Down Expand Up @@ -909,7 +909,7 @@ protected function _compile_selete($builder)
if (!empty($builder['where']))
{
// Add selection conditions
$query .= ' WHERE ' . $this->_compile_conditions($builder['where'], $builder['parameters']);
$query .= ' WHERE ' . $this->_compile_conditions($builder['where']);
}

if (!empty($builder['group_by']))
Expand All @@ -921,7 +921,7 @@ protected function _compile_selete($builder)
if (!empty($builder['having']))
{
// Add filtering conditions
$query .= ' HAVING ' . $this->_compile_conditions($builder['having'], $builder['parameters']);
$query .= ' HAVING ' . $this->_compile_conditions($builder['having']);
}

if (!empty($builder['order_by']))
Expand Down Expand Up @@ -991,15 +991,6 @@ protected function _compile_insert($builder, $type = 'INSERT')
$groups = array();
foreach ($builder['values'] as $group)
{
foreach ($group as $i => $value)
{
if (is_string($value) && isset($builder['parameters'][$value]))
{
// Use the parameter value
$group[$i] = $builder['parameters'][$value];
}
}

$groups[] = '(' . implode(', ', array_map($quote, $group)) . ')';
}

Expand All @@ -1018,7 +1009,7 @@ protected function _compile_insert($builder, $type = 'INSERT')
if (!empty($builder['where']))
{
// Add selection conditions
$query .= ' WHERE ' . $this->_compile_conditions($builder['where'], $builder['parameters']);
$query .= ' WHERE ' . $this->_compile_conditions($builder['where']);
}
}

Expand All @@ -1031,12 +1022,12 @@ protected function _compile_update($builder)
$query = 'UPDATE ' . $this->quote_table($builder['table'],false);

// Add the columns to update
$query .= ' SET ' . $this->_compile_set($builder['set'], $builder['parameters']);
$query .= ' SET ' . $this->_compile_set($builder['set']);

if (!empty($builder['where']))
{
// Add selection conditions
$query .= ' WHERE ' . $this->_compile_conditions($builder['where'], $builder['parameters']);
$query .= ' WHERE ' . $this->_compile_conditions($builder['where']);
}

if (!empty($builder['order_by']))
Expand All @@ -1063,14 +1054,14 @@ protected function _compile_update($builder)
protected function _compile_delete($builder)
{
// Start an update query
$query = 'DELETE FROM' . $this->quote_table($builder['table'],false);
$query = 'DELETE FROM' . $this->quote_table($builder['table'], false);

if (!empty($builder['where']))
{
$this->_init_as_table($builder);

// Add selection conditions
$query .= ' WHERE ' . $this->_compile_conditions($builder['where'], $builder['parameters']);
$query .= ' WHERE ' . $this->_compile_conditions($builder['where']);
}

return $query;
Expand All @@ -1079,7 +1070,6 @@ protected function _compile_delete($builder)
/**
* Compiles an array of ORDER BY statements into an SQL partial.
*
* @param object Database instance
* @param array sorting columns
* @return string
*/
Expand All @@ -1106,11 +1096,10 @@ protected function _compile_order_by(array $columns)
* Compiles an array of conditions into an SQL partial. Used for WHERE
* and HAVING.
*
* @param object Database instance
* @param array condition statements
* @return string
*/
protected function _compile_conditions(array $conditions, $parameters)
protected function _compile_conditions(array $conditions)
{
$last_condition = null;

Expand Down Expand Up @@ -1182,18 +1171,6 @@ protected function _compile_conditions(array $conditions, $parameters)
// BETWEEN always has exactly two arguments
list ($min, $max) = $value;

if (is_string($min) && array_key_exists($min, $parameters))
{
// Set the parameter as the minimum
$min = $parameters[$min];
}

if (is_string($max) && array_key_exists($max, $parameters))
{
// Set the parameter as the maximum
$max = $parameters[$max];
}

// Quote the min and max value
$value = $this->quote($min) . ' AND ' . $this->quote($max);
}
Expand All @@ -1203,12 +1180,6 @@ protected function _compile_conditions(array $conditions, $parameters)
}
else
{
if (is_string($value) && array_key_exists($value, $parameters))
{
// Set the parameter as the value
$value = $parameters[$value];
}

// Quote the entire value normally
$value = $this->quote($value);
}
Expand All @@ -1227,7 +1198,6 @@ protected function _compile_conditions(array $conditions, $parameters)
/**
* Compiles an array of JOIN statements into an SQL partial.
*
* @param object Database instance
* @param array join statements
* @return string
*/
Expand Down Expand Up @@ -1282,11 +1252,10 @@ protected function _compile_join_on($join)
/**
* Compiles an array of set values into an SQL partial. Used for UPDATE.
*
* @param object Database instance
* @param array updated values
* @return string
*/
protected function _compile_set(array $values, $parameters)
protected function _compile_set(array $values)
{
$set = array();
foreach ($values as $group)
Expand All @@ -1306,12 +1275,6 @@ protected function _compile_set(array $values, $parameters)
// Quote the column name
$column = $this->_quote_identifier($column);

if (is_string($value) && array_key_exists($value, $parameters))
{
// Use the parameter value
$value = $parameters[$value];
}

if ($w_type)
{
$set[$column] = $column . ' = ' . $column . ' ' . $w_type . ' ' . $this->quote($value);
Expand Down
55 changes: 9 additions & 46 deletions drivers/database/mysqli/mysqli.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ protected function _connect()
{
Core::debug()->error($error_host,'error_host');

if ($last_error)throw $last_error;
if ($last_error && $last_error instanceof Exception)throw $last_error;
throw new Exception('connect mysqli server error.');
}

Expand Down Expand Up @@ -912,7 +912,7 @@ protected function _compile_selete($builder)
if (!empty($builder['where']))
{
// Add selection conditions
$query .= ' WHERE ' . $this->_compile_conditions($builder['where'], $builder['parameters']);
$query .= ' WHERE ' . $this->_compile_conditions($builder['where']);
}

if (!empty($builder['group_by']))
Expand All @@ -924,7 +924,7 @@ protected function _compile_selete($builder)
if (!empty($builder['having']))
{
// Add filtering conditions
$query .= ' HAVING ' . $this->_compile_conditions($builder['having'], $builder['parameters']);
$query .= ' HAVING ' . $this->_compile_conditions($builder['having']);
}

if (!empty($builder['order_by']))
Expand Down Expand Up @@ -994,15 +994,6 @@ protected function _compile_insert($builder, $type = 'INSERT')
$groups = array();
foreach ($builder['values'] as $group)
{
foreach ($group as $i => $value)
{
if (is_string($value) && isset($builder['parameters'][$value]))
{
// Use the parameter value
$group[$i] = $builder['parameters'][$value];
}
}

$groups[] = '(' . implode(', ', array_map($quote, $group)) . ')';
}

Expand All @@ -1021,7 +1012,7 @@ protected function _compile_insert($builder, $type = 'INSERT')
if (!empty($builder['where']))
{
// Add selection conditions
$query .= ' WHERE ' . $this->_compile_conditions($builder['where'], $builder['parameters']);
$query .= ' WHERE ' . $this->_compile_conditions($builder['where']);
}
}

Expand All @@ -1034,12 +1025,12 @@ protected function _compile_update($builder)
$query = 'UPDATE ' . $this->quote_table($builder['table'],false);

// Add the columns to update
$query .= ' SET ' . $this->_compile_set($builder['set'], $builder['parameters']);
$query .= ' SET ' . $this->_compile_set($builder['set']);

if (!empty($builder['where']))
{
// Add selection conditions
$query .= ' WHERE ' . $this->_compile_conditions($builder['where'], $builder['parameters']);
$query .= ' WHERE ' . $this->_compile_conditions($builder['where']);
}

if (!empty($builder['order_by']))
Expand Down Expand Up @@ -1073,7 +1064,7 @@ protected function _compile_delete($builder)
$this->_init_as_table($builder);

// Add selection conditions
$query .= ' WHERE ' . $this->_compile_conditions($builder['where'], $builder['parameters']);
$query .= ' WHERE ' . $this->_compile_conditions($builder['where']);
}

return $query;
Expand All @@ -1082,7 +1073,6 @@ protected function _compile_delete($builder)
/**
* Compiles an array of ORDER BY statements into an SQL partial.
*
* @param object Database instance
* @param array sorting columns
* @return string
*/
Expand All @@ -1109,11 +1099,10 @@ protected function _compile_order_by(array $columns)
* Compiles an array of conditions into an SQL partial. Used for WHERE
* and HAVING.
*
* @param object Database instance
* @param array condition statements
* @return string
*/
protected function _compile_conditions(array $conditions, $parameters)
protected function _compile_conditions(array $conditions)
{
$last_condition = null;

Expand Down Expand Up @@ -1185,18 +1174,6 @@ protected function _compile_conditions(array $conditions, $parameters)
// BETWEEN always has exactly two arguments
list ($min, $max) = $value;

if (is_string($min) && array_key_exists($min, $parameters))
{
// Set the parameter as the minimum
$min = $parameters[$min];
}

if (is_string($max) && array_key_exists($max, $parameters))
{
// Set the parameter as the maximum
$max = $parameters[$max];
}

// Quote the min and max value
$value = $this->quote($min) . ' AND ' . $this->quote($max);
}
Expand All @@ -1206,12 +1183,6 @@ protected function _compile_conditions(array $conditions, $parameters)
}
else
{
if (is_string($value) && array_key_exists($value, $parameters))
{
// Set the parameter as the value
$value = $parameters[$value];
}

// Quote the entire value normally
$value = $this->quote($value);
}
Expand All @@ -1230,7 +1201,6 @@ protected function _compile_conditions(array $conditions, $parameters)
/**
* Compiles an array of JOIN statements into an SQL partial.
*
* @param object Database instance
* @param array join statements
* @return string
*/
Expand Down Expand Up @@ -1285,11 +1255,10 @@ protected function _compile_join_on($join)
/**
* Compiles an array of set values into an SQL partial. Used for UPDATE.
*
* @param object Database instance
* @param array updated values
* @return string
*/
protected function _compile_set(array $values, $parameters)
protected function _compile_set(array $values)
{
$set = array();
foreach ($values as $group)
Expand All @@ -1309,12 +1278,6 @@ protected function _compile_set(array $values, $parameters)
// Quote the column name
$column = $this->_quote_identifier($column);

if (is_string($value) && array_key_exists($value, $parameters))
{
// Use the parameter value
$value = $parameters[$value];
}

if ($w_type)
{
$set[$column] = $column . ' = ' . $column . ' ' . $w_type . ' ' . $this->quote($value);
Expand Down
Loading

0 comments on commit 3d4401c

Please sign in to comment.