From 6a89b48fe88141f25399fbbedcbcd0cfd3e8eb9e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 2 Aug 2012 15:59:11 +0000 Subject: [PATCH] Beginning of query parser --- classes/query_builder.php | 2 +- classes/query_parser.php | 60 ++++++++++++++++++++++++++++++++++ composer.json | 17 +++++----- tests/core/db_qp_test.php | 37 +++++++++++++++++++++ tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes tests/index.php | 1 + 6 files changed, 107 insertions(+), 10 deletions(-) create mode 100644 classes/query_parser.php create mode 100644 tests/core/db_qp_test.php diff --git a/classes/query_builder.php b/classes/query_builder.php index 2bf6cb8..480bccc 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -19,7 +19,7 @@ * @package Query * @subpackage Query */ -class BadDBDriverException extends UnexpectedValueException {} +class BadDBDriverException extends InvalidArgumentException {} // -------------------------------------------------------------------------- diff --git a/classes/query_parser.php b/classes/query_parser.php new file mode 100644 index 0000000..5bc9e4b --- /dev/null +++ b/classes/query_parser.php @@ -0,0 +1,60 @@ + '`([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 \ No newline at end of file diff --git a/composer.json b/composer.json index 3c61104..186bcd6 100644 --- a/composer.json +++ b/composer.json @@ -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" - } + }, + } \ No newline at end of file diff --git a/tests/core/db_qp_test.php b/tests/core/db_qp_test.php new file mode 100644 index 0000000..f19578a --- /dev/null +++ b/tests/core/db_qp_test.php @@ -0,0 +1,37 @@ +parser = new Query_Parser(); + } + + public function TestGeneric() + { + $this->parser->__construct('table1.field1=table2.field2'); + + //echo '
'.print_r($this->parser->matches, TRUE).'
'; + } + + public function TestFunction() + { + $this->parser->__construct('table1.field1 > SUM(3+5)'); + + //echo '
'.print_r($this->parser->matches, TRUE).'
'; + } + +} \ No newline at end of file diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index 62616563c3384e85f5a79d663ec95274fc712b26..f99f20a14e430639f85997b7926eae9628275a64 100644 GIT binary patch delta 1111 zcmZ9LziSjh6vt=ohlw{u#zIgLG4Uj#g?Ctp*m$I{2qX&r0V%Gru}N#OSWF*M3?c}D zMHV?^IX8E!tYJ|DcJ@}ea4I5hNN024n|=F@%ieM~``MefpMCG`^SrL-b=Rc0(@|~l z|D%;xFUYOwk`SNhD@0Ac_$I`o+h3Ra=Xrw`H99tOko21_y*Ntu% z-8A}?(Q`)68-04DTMaEgkDHfU;r#YJt;f69T6gD6r#0%RMkrs}=7HLlp?!X!R%!X} zmZ9xV)JiX;=h!1pT}V2F06QwPcHEzdL9t&rmVajhyE{?qa$uW>YK4}qBX!e0&D2FY zKh9L!zB^RQ@{OI)-#rA^ikpKW2j}ws=6+C-1uP3#7C70Wy)aeRWx%q>Q9#DKFj=4W_~HNSGMkPKFXQIEdj(wCQO1;IsXhCX%9wJe zQts48drTL52tl#HP=dKrUqn(-B&Qr3n)O-8zH^~qT85qR_Ll#wJ5z8W$MWxK5Qf4D7|Bwv9kbxlsbEo9VK%i40 z%R<+wEBs>LuXL69AQZoS!9tR%JfHy zL9F}_f?^H}?G6MBd#hYH6%jY2vpMk1zWv5!Z@HWO?9JQHzW4SS(=n#AF2$YpmnHH4 zqm`)_&oT@-k?Q|j$RIue$C>$h2Gn_U2V0rx@L9V>W0-# ztIt?HWA&`nXAg9%q2QJrG^4%}n zt}|Av-H0CJ6KU!~(jf%cQ<=5rU@H2>e&JdEo%Y@PvD%P**E~|IvbeYB_IN_FCvGqy8G<1L zHwrlzvY;XhSQfA>C}kFuGJE8@KS+jP$iR?+=khY{jQktm%Z@S{mKk^cCza7~rwn&$ z*PYPCE<#W&FqB~K)Mt@a6e%lG&L=J9la}&%?%w<&8G`fT&{KjT2j}uYilib-SeCFX zDRn9-b?TXW@S9`^h7642fgy`4vY2Ht%i>aIaVfK>PW~Yof*}J#2Ifx5(}6&zB9=wI zQ%n3}-*C&Bnm>opL5(8LdMi`wG;|*`b&D37iM7+67^-bQ9+%z5SY722x>rokaC)rj G_x}OpxD6Ko diff --git a/tests/index.php b/tests/index.php index 17a3826..387c74f 100644 --- a/tests/index.php +++ b/tests/index.php @@ -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