Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Prefixed internal methods with _
Browse files Browse the repository at this point in the history
  • Loading branch information
romaninsh committed Feb 5, 2016
1 parent e4bf44d commit ff5f3ed
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
10 changes: 8 additions & 2 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## Changes from DB_dsql

- bt() renamed into escape()
### Renamed internal methods and properties
- `consume()` renamed into `_consume()`
- `render_*` methods renamed into `_render_*`
- `bt()` renamed into `_escape()`
- $bt property renamed to escapeChar
- sql_templates renamed to templates
- consume() does not have 2nd argument anymore ($tick). Will always escape unless expression is passed.


### Other changes
- `_consume()` does not have 2nd argument anymore ($tick). Will always escape unless expression is passed.
54 changes: 27 additions & 27 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ function field($field, $table = null, $alias = null)
*
* @return string Parsed template chunk
*/
function render_field()
function _render_field()
{
// will be joined for output
$result=[];

// If no fields were defined, use default_field
if (!isset($this->args['fields']) || !($this->args['fields'])) {
if ($this->defaultField instanceof DB_dsql) {
return $this->consume($this->defaultField);
return $this->_consume($this->defaultField);
}
return (string)$this->defaultField;
}
Expand All @@ -119,7 +119,7 @@ function render_field()
}

// Will parameterize the value and backtick if necessary.
$field=$this->consume($field);
$field=$this->_consume($field);

// TODO: Commented until I figure out what this does
/*
Expand All @@ -131,12 +131,12 @@ function render_field()

if (!is_null($table)) {
// table cannot be expression, so only backtick
$field=$this->escape($table).'.'.$field;
$field=$this->_escape($table).'.'.$field;
}

if ($alias && $alias!==null) {
// alias cannot be expression, so only backtick
$field.=' '.$this->escape($alias);
$field.=' '.$this->_escape($alias);
}
$result[]=$field;
}
Expand All @@ -152,28 +152,28 @@ function render_field()
*
* @return string Quoted expression
*/
function consume($dsql)
function _consume($sql_code)
{
if ($dsql===null) {
if ($sql_code===null) {
return null;
}

/** TODO: TEMPORARILY removed, ATK feature, implement with traits **/
/*
if (is_object($dsql) && $dsql instanceof Field) {
$dsql=$dsql->getExpr();
if (is_object($sql_code) && $sql_code instanceof Field) {
$sql_code=$sql_code->getExpr();
}
*/
if (!is_object($dsql) || !$dsql instanceof Query) {
return $this->escape($dsql);
if (!is_object($sql_code) || !$sql_code instanceof Query) {
return $this->_escape($sql_code);
}
$dsql->params = &$this->params;
$ret = $dsql->_render();
if ($dsql->mode==='select') {
$sql_code->params = &$this->params;
$ret = $sql_code->_render();
if ($sql_code->mode==='select') {
$ret='('.$ret.')';
}
unset($dsql->params);
$dsql->params=[];
unset($sql_code->params);
$sql_code->params=[];
return $ret;
}

Expand All @@ -185,28 +185,28 @@ function consume($dsql)
*
* @return string Quoted string
*/
function escape($identifier)
function _escape($sql_code)
{
// Supports array
if (is_array($identifier)) {
if (is_array($sql_code)) {
$out=[];
foreach ($identifier as $ss) {
$out[]=$this->escape($ss);
foreach ($sql_code as $ss) {
$out[]=$this->_escape($ss);
}
return $out;
}

if (!$this->escapeChar
|| is_object($identifier)
|| $identifier==='*'
|| strpos($identifier, '.')!==false
|| strpos($identifier, '(')!==false
|| strpos($identifier, $this->escapeChar)!==false
|| is_object($sql_code)
|| $sql_code==='*'
|| strpos($sql_code, '.')!==false
|| strpos($sql_code, '(')!==false
|| strpos($sql_code, $this->escapeChar)!==false
) {
return $identifier;
return $sql_code;
}

return $this->escapeChar.$identifier.$this->escapeChar;
return $this->escapeChar.$sql_code.$this->escapeChar;
}

public function table($table)
Expand Down
28 changes: 16 additions & 12 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ function testTable()
function testEscaping()
{
// escaping exclusions
$this->assertEquals('`first_name`', $this->q()->escape('first_name'));
$this->assertEquals('*first_name*', $this->q(['escapeChar'=>'*'])->escape('first_name'));
$this->assertEquals('first_name.table', $this->q()->escape('first_name.table'));
$this->assertEquals('(2+2) age', $this->q()->escape('(2+2) age'));
$this->assertEquals('`first_name`', $this->q()->_escape('first_name'));
$this->assertEquals('*first_name*', $this->q(['escapeChar'=>'*'])->_escape('first_name'));
$this->assertEquals('first_name.table', $this->q()->_escape('first_name.table'));
$this->assertEquals('(2+2) age', $this->q()->_escape('(2+2) age'));
$this->assertEquals('*', $this->q()->_escape('*'));

}

function testFieldBasic()
{
// excludes expressions
$this->assertEquals('`first_name`', $this->q()->field('first_name')->render_field());
$this->assertEquals('`first_name`,`last_name`', $this->q()->field('first_name,last_name')->render_field());
$this->assertEquals('`emplayee`.`first_name`', $this->q()->field('first_name','emplayee')->render_field());
$this->assertEquals('`first_name` `name`', $this->q()->field(['name'=>'first_name'])->render_field());
$this->assertEquals('`employee`.`first_name` `name`', $this->q()->field(['name'=>'first_name'],'employee')->render_field());

$this->assertEquals('*', $this->q()->render_field());
$this->assertEquals('id', $this->q(['defaultField'=>'id'])->render_field());
$this->assertEquals('`first_name`', $this->q()->field('first_name')->_render_field());
$this->assertEquals('`first_name`,`last_name`', $this->q()->field('first_name,last_name')->_render_field());
$this->assertEquals('`emplayee`.`first_name`', $this->q()->field('first_name','emplayee')->_render_field());
$this->assertEquals('`first_name` `name`', $this->q()->field('first_name',null,'name')->_render_field());
$this->assertEquals('`first_name` `name`', $this->q()->field(['name'=>'first_name'])->_render_field());
$this->assertEquals('`employee`.`first_name` `name`', $this->q()->field(['name'=>'first_name'],'employee')->_render_field());

$this->assertEquals('*', $this->q()->_render_field());
$this->assertEquals('id', $this->q(['defaultField'=>'id'])->_render_field());
$this->assertEquals('*', $this->q()->field('*')->_render_field());
$this->assertEquals('first_name.employee', $this->q()->field('first_name.employee')->_render_field());

}

Expand Down

0 comments on commit ff5f3ed

Please sign in to comment.