Beginning of query parser
This commit is contained in:
parent
84d5bd492b
commit
6a89b48fe8
@ -19,7 +19,7 @@
|
||||
* @package Query
|
||||
* @subpackage Query
|
||||
*/
|
||||
class BadDBDriverException extends UnexpectedValueException {}
|
||||
class BadDBDriverException extends InvalidArgumentException {}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
|
60
classes/query_parser.php
Normal file
60
classes/query_parser.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* Query
|
||||
*
|
||||
* Free Query Builder / Database Abstraction Layer
|
||||
*
|
||||
* @package Query
|
||||
* @author Timothy J. Warren
|
||||
* @copyright Copyright (c) 2012
|
||||
* @link https://github.com/aviat4ion/Query
|
||||
* @license http://philsturgeon.co.uk/code/dbad-license
|
||||
*/
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Utility Class to parse sql clauses for properly escaping identifiers
|
||||
*
|
||||
* @package Query
|
||||
* @subpackage Query
|
||||
*/
|
||||
class Query_Parser {
|
||||
|
||||
/**
|
||||
* Regex patterns for various syntax components
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $match_patterns = array(
|
||||
'function' => '`([a-zA-Z0-9_]+\((.*?)\))`',
|
||||
'identifier' => '`([a-zA-Z0-9"_-]+\.?)+`',
|
||||
'operator' => '`=|AND|&&?|~|\|\|?|\^|/|>=?|<=?|-|%|OR|\+|NOT|\!=?|<>|XOR`'
|
||||
);
|
||||
|
||||
/**
|
||||
* Regex matches
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $matches = array(
|
||||
'functions' => array(),
|
||||
'identifiers' => array(),
|
||||
'operators' => array(),
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor/entry point into parser
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
public function __construct($sql = '')
|
||||
{
|
||||
preg_match_all($this->match_patterns['function'], $sql, $this->matches['functions'], PREG_SET_ORDER);
|
||||
preg_match_all($this->match_patterns['identifier'], $sql, $this->matches['identifiers'], PREG_SET_ORDER);
|
||||
preg_match_all($this->match_patterns['operator'], $sql, $this->matches['operators'], PREG_SET_ORDER);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// End of query_parser.php
|
@ -5,15 +5,14 @@
|
||||
"keywords":["database", "query builder"],
|
||||
"homepage":"https://github.com/aviat4ion/Query",
|
||||
"license":"dbad-license",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Timothy J. Warren",
|
||||
"email": "tim@timshomepage.net",
|
||||
"homepage": "https://timshomepage.net",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"authors": [{
|
||||
"name": "Timothy J. Warren",
|
||||
"email": "tim@timshomepage.net",
|
||||
"homepage": "https://timshomepage.net",
|
||||
"role": "Developer"
|
||||
}],
|
||||
"require": {
|
||||
"php": ">=5.2.0"
|
||||
}
|
||||
},
|
||||
|
||||
}
|
37
tests/core/db_qp_test.php
Normal file
37
tests/core/db_qp_test.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* Query
|
||||
*
|
||||
* Free Query Builder / Database Abstraction Layer
|
||||
*
|
||||
* @package Query
|
||||
* @author Timothy J. Warren
|
||||
* @copyright Copyright (c) 2012
|
||||
* @link https://github.com/aviat4ion/Query
|
||||
* @license http://philsturgeon.co.uk/code/dbad-license
|
||||
*/
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
class QPTest extends UnitTestCase {
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->parser = new Query_Parser();
|
||||
}
|
||||
|
||||
public function TestGeneric()
|
||||
{
|
||||
$this->parser->__construct('table1.field1=table2.field2');
|
||||
|
||||
//echo '<pre>'.print_r($this->parser->matches, TRUE).'</pre>';
|
||||
}
|
||||
|
||||
public function TestFunction()
|
||||
{
|
||||
$this->parser->__construct('table1.field1 > SUM(3+5)');
|
||||
|
||||
//echo '<pre>'.print_r($this->parser->matches, TRUE).'</pre>';
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
@ -30,6 +30,7 @@ require_once(QBASE_DIR . 'autoload.php');
|
||||
// Require base testing classes
|
||||
require_once(QTEST_DIR . '/core/core.php');
|
||||
require_once(QTEST_DIR . '/core/db_test.php');
|
||||
require_once(QTEST_DIR . '/core/db_qp_test.php');
|
||||
require_once(QTEST_DIR . '/core/db_qb_test.php');
|
||||
|
||||
// Include db tests
|
||||
|
Loading…
Reference in New Issue
Block a user