Beginning of query parser

This commit is contained in:
Timothy Warren 2012-08-02 15:59:11 +00:00
parent 84d5bd492b
commit 6a89b48fe8
6 changed files with 107 additions and 10 deletions

View File

@ -19,7 +19,7 @@
* @package Query * @package Query
* @subpackage Query * @subpackage Query
*/ */
class BadDBDriverException extends UnexpectedValueException {} class BadDBDriverException extends InvalidArgumentException {}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

60
classes/query_parser.php Normal file
View 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

View File

@ -5,15 +5,14 @@
"keywords":["database", "query builder"], "keywords":["database", "query builder"],
"homepage":"https://github.com/aviat4ion/Query", "homepage":"https://github.com/aviat4ion/Query",
"license":"dbad-license", "license":"dbad-license",
"authors": [ "authors": [{
{ "name": "Timothy J. Warren",
"name": "Timothy J. Warren", "email": "tim@timshomepage.net",
"email": "tim@timshomepage.net", "homepage": "https://timshomepage.net",
"homepage": "https://timshomepage.net", "role": "Developer"
"role": "Developer" }],
}
],
"require": { "require": {
"php": ">=5.2.0" "php": ">=5.2.0"
} },
} }

37
tests/core/db_qp_test.php Normal file
View 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.

View File

@ -30,6 +30,7 @@ require_once(QBASE_DIR . 'autoload.php');
// Require base testing classes // Require base testing classes
require_once(QTEST_DIR . '/core/core.php'); require_once(QTEST_DIR . '/core/core.php');
require_once(QTEST_DIR . '/core/db_test.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'); require_once(QTEST_DIR . '/core/db_qb_test.php');
// Include db tests // Include db tests