Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
catfan committed Jun 8, 2014
2 parents 7393c67 + 14e4d7c commit 2cf2d9f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
### Main Features

* **Lightweight** - Only 13KB with one file.
* **Lightweight** - Only 14KB with one file.

* **Easy** - Extremely easy to learn and use, friendly construction.

Expand Down
109 changes: 74 additions & 35 deletions medoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*!
* Medoo database framework
* http://medoo.in
* Version 0.9.5.3
* Version 0.9.6
*
* Copyright 2014, Angel Lai
* Released under the MIT license
Expand Down Expand Up @@ -147,7 +147,7 @@ public function quote($string)

protected function column_quote($string)
{
return '"' . str_replace('.', '"."', $string) . '"';
return '"' . str_replace('.', '"."', preg_replace('/(^#|\(JSON\))/', '', $string)) . '"';
}

protected function column_push($columns)
Expand Down Expand Up @@ -205,6 +205,15 @@ protected function inner_conjunct($data, $conjunctor, $outer_conjunctor)
return implode($outer_conjunctor . ' ', $haystack);
}

protected function fn_quote($column, $string)
{
return (strpos($column, '#') === 0 && preg_match('/^[A-Z0-9\_]*\([^)]*\)$/', $string)) ?

$string :

$this->quote($string);
}

protected function data_implode($data, $conjunctor, $outer_conjunctor = null)
{
$wheres = array();
Expand All @@ -224,16 +233,16 @@ protected function data_implode($data, $conjunctor, $outer_conjunctor = null)
}
else
{
preg_match('/([\w\.]+)(\[(\>|\>\=|\<|\<\=|\!|\<\>|\>\<)\])?/i', $key, $match);
$column = $this->column_quote($match[1]);
preg_match('/(#?)([\w\.]+)(\[(\>|\>\=|\<|\<\=|\!|\<\>|\>\<)\])?/i', $key, $match);
$column = $this->column_quote($match[2]);

if (isset($match[3]))
if (isset($match[4]))
{
if ($match[3] == '')
if ($match[4] == '')
{
$wheres[] = $column . ' ' . $match[3] . '= ' . $this->quote($value);
$wheres[] = $column . ' ' . $match[4] . '= ' . $this->quote($value);
}
elseif ($match[3] == '!')
elseif ($match[4] == '!')
{
switch ($type)
{
Expand All @@ -250,18 +259,22 @@ protected function data_implode($data, $conjunctor, $outer_conjunctor = null)
$wheres[] = $column . ' != ' . $value;
break;

case 'boolean':
$wheres[] = $column . ' != ' . ($value ? '1' : '0');
break;

case 'string':
$wheres[] = $column . ' != ' . $this->quote($value);
$wheres[] = $column . ' != ' . $this->fn_quote($key, $value);
break;
}
}
else
{
if ($match[3] == '<>' || $match[3] == '><')
if ($match[4] == '<>' || $match[4] == '><')
{
if ($type == 'array')
{
if ($match[3] == '><')
if ($match[4] == '><')
{
$column .= ' NOT';
}
Expand All @@ -280,15 +293,15 @@ protected function data_implode($data, $conjunctor, $outer_conjunctor = null)
{
if (is_numeric($value))
{
$wheres[] = $column . ' ' . $match[3] . ' ' . $value;
$wheres[] = $column . ' ' . $match[4] . ' ' . $value;
}
else
{
$datetime = strtotime($value);

if ($datetime)
{
$wheres[] = $column . ' ' . $match[3] . ' ' . $this->quote(date('Y-m-d H:i:s', $datetime));
$wheres[] = $column . ' ' . $match[4] . ' ' . $this->quote(date('Y-m-d H:i:s', $datetime));
}
}
}
Expand Down Expand Up @@ -322,7 +335,7 @@ protected function data_implode($data, $conjunctor, $outer_conjunctor = null)
break;

case 'string':
$wheres[] = $column . ' = ' . $this->quote($value);
$wheres[] = $column . ' = ' . $this->fn_quote($key, $value);
break;
}
}
Expand Down Expand Up @@ -385,16 +398,22 @@ protected function where_clause($where)

foreach ($like_query as $column => $keyword)
{
if (is_array($keyword))
$keyword = is_array($keyword) ? $keyword : array($keyword);

foreach ($keyword as $key)
{
foreach ($keyword as $key)
preg_match('/(%?)([a-zA-Z0-9_\-\.]*)(%?)((\[!\])?)/', $column, $column_match);

if ($column_match[1] == '' && $column_match[3] == '')
{
$clause_wrap[] = $this->column_quote($column) . ' LIKE ' . $this->quote('%' . $key . '%');
$column_match[1] = '%';
$column_match[3] = '%';
}
}
else
{
$clause_wrap[] = $this->column_quote($column) . ' LIKE ' . $this->quote('%' . $keyword . '%');

$clause_wrap[] =
$this->column_quote($column_match[2]) .
($column_match[4] != '' ? ' NOT' : '') . ' LIKE ' .
$this->quote($column_match[1] . $key . $column_match[3]);
}
}

Expand All @@ -419,15 +438,36 @@ protected function where_clause($where)

if (isset($where['ORDER']))
{
$rsort = '/(^[a-zA-Z0-9_\-\.]*)(\s*(DESC|ASC))?/';

if (is_array($where['ORDER']))
{
$where_clause .= ' ORDER BY FIELD(' . $this->column_quote($where['ORDER'][0]) . ', ' . $this->array_quote($where['ORDER'][1]) . ')';
if (
isset($where['ORDER'][1]) &&
is_array($where['ORDER'][1])
)
{
$where_clause .= ' ORDER BY FIELD(' . $this->column_quote($where['ORDER'][0]) . ', ' . $this->array_quote($where['ORDER'][1]) . ')';
}
else
{
$stack = array();

foreach ($where['ORDER'] as $column)
{
preg_match($rsort, $column, $order_match);

array_push($stack, '"' . str_replace('.', '"."', $order_match[1]) . '"' . (isset($order_match[3]) ? ' ' . $order_match[3] : ''));
}

$where_clause .= ' ORDER BY ' . implode($stack, ',');
}
}
else
{
preg_match('/(^[a-zA-Z0-9_\-\.]*)(\s*(DESC|ASC))?/', $where['ORDER'], $order_match);
preg_match($rsort, $where['ORDER'], $order_match);

$where_clause .= ' ORDER BY "' . str_replace('.', '"."', $order_match[1]) . '" ' . (isset($order_match[3]) ? $order_match[3] : '');
$where_clause .= ' ORDER BY "' . str_replace('.', '"."', $order_match[1]) . '"' . (isset($order_match[3]) ? ' ' . $order_match[3] : '');
}

if (isset($where['HAVING']))
Expand Down Expand Up @@ -559,7 +599,7 @@ protected function select_context($table, $join, &$columns = null, $where = null
}
else
{
$where == $join;
$where = $join;
}
}
else
Expand Down Expand Up @@ -604,10 +644,12 @@ public function insert($table, $datas)
{
$keys = array_keys($data);
$values = array();
$index = 0;
$columns = array();

foreach ($data as $key => $value)
{
array_push($columns, $this->column_quote($key));

switch (gettype($value))
{
case 'NULL':
Expand All @@ -619,7 +661,6 @@ public function insert($table, $datas)

if (isset($column_match[0]))
{
$keys[ $index ] = $column_match[1];
$values[] = $this->quote(json_encode($value));
}
else
Expand All @@ -635,14 +676,12 @@ public function insert($table, $datas)
case 'integer':
case 'double':
case 'string':
$values[] = $this->quote($value);
$values[] = $this->fn_quote($key, $value);
break;
}

$index++;
}

$this->exec('INSERT INTO "' . $table . '" ("' . implode('", "', $keys) . '") VALUES (' . implode($values, ', ') . ')');
$this->exec('INSERT INTO "' . $table . '" (' . implode(', ', $columns) . ') VALUES (' . implode($values, ', ') . ')');

$lastId[] = $this->pdo->lastInsertId();
}
Expand Down Expand Up @@ -695,7 +734,7 @@ public function update($table, $data, $where = null)
case 'integer':
case 'double':
case 'string':
$fields[] = $column . ' = ' . $this->quote($value);
$fields[] = $column . ' = ' . $this->fn_quote($key, $value);
break;
}
}
Expand All @@ -719,7 +758,7 @@ public function replace($table, $columns, $search = null, $replace = null, $wher
{
foreach ($replacements as $replace_search => $replace_replacement)
{
$replace_query[] = $column . ' = REPLACE("' . $column . '", ' . $this->quote($replace_search) . ', ' . $this->quote($replace_replacement) . ')';
$replace_query[] = $column . ' = REPLACE(' . $this->column_quote($column) . ', ' . $this->quote($replace_search) . ', ' . $this->quote($replace_replacement) . ')';
}
}

Expand All @@ -734,15 +773,15 @@ public function replace($table, $columns, $search = null, $replace = null, $wher

foreach ($search as $replace_search => $replace_replacement)
{
$replace_query[] = $columns . ' = REPLACE("' . $columns . '", ' . $this->quote($replace_search) . ', ' . $this->quote($replace_replacement) . ')';
$replace_query[] = $columns . ' = REPLACE(' . $this->column_quote($columns) . ', ' . $this->quote($replace_search) . ', ' . $this->quote($replace_replacement) . ')';
}

$replace_query = implode(', ', $replace_query);
$where = $replace;
}
else
{
$replace_query = $columns . ' = REPLACE("' . $columns . '", ' . $this->quote($search) . ', ' . $this->quote($replace) . ')';
$replace_query = $columns . ' = REPLACE(' . $this->column_quote($columns) . ', ' . $this->quote($search) . ', ' . $this->quote($replace) . ')';
}
}

Expand Down

0 comments on commit 2cf2d9f

Please sign in to comment.