├── composer.json ├── index.php └── src ├── example.php └── library ├── processors ├── UsingProcessor.php ├── UpdateProcessor.php ├── WhereProcessor.php ├── HavingProcessor.php ├── DuplicateProcessor.php ├── DescribeProcessor.php ├── ReplaceProcessor.php ├── DescProcessor.php ├── ColumnListProcessor.php ├── IntoProcessor.php ├── RecordProcessor.php ├── DeleteProcessor.php ├── DefaultProcessor.php ├── GroupByProcessor.php ├── LimitProcessor.php ├── InsertProcessor.php ├── ValuesProcessor.php ├── SelectProcessor.php ├── CreateProcessor.php └── IndexColumnListProcessor.php ├── exceptions ├── InvalidParameterException.php ├── UnsupportedFeatureException.php ├── UnableToCalculatePositionException.php └── UnableToCreateSQLException.php └── builders ├── UpdateBuilder.php ├── DeleteBuilder.php ├── OperatorBuilder.php ├── DirectionBuilder.php ├── UserVariableBuilder.php ├── EngineBuilder.php ├── PositionBuilder.php ├── DatabaseBuilder.php ├── ProcedureBuilder.php ├── DataTypeBuilder.php ├── ReservedBuilder.php ├── LimitBuilder.php ├── AliasBuilder.php ├── RefTypeBuilder.php ├── CreateTableSelectOptionBuilder.php ├── ConstantBuilder.php ├── OrderByAliasBuilder.php ├── InListBuilder.php ├── CreateTableDefinitionBuilder.php ├── JoinBuilder.php ├── LikeBuilder.php ├── ConstraintBuilder.php ├── ShowStatementBuilder.php ├── ColumnTypeBracketExpressionBuilder.php ├── SelectBracketExpressionBuilder.php ├── InsertStatementBuilder.php ├── SetBuilder.php ├── ValuesBuilder.php ├── ColumnReferenceBuilder.php ├── IndexColumnBuilder.php ├── SelectExpressionBuilder.php ├── UpdateStatementBuilder.php ├── CreateBuilder.php ├── DeleteStatementBuilder.php ├── ColumnListBuilder.php ├── CreateStatementBuilder.php ├── InsertBuilder.php ├── CreateTableBuilder.php ├── IndexTypeBuilder.php ├── FromBuilder.php ├── OrderByBuilder.php ├── CheckBuilder.php ├── ColumnDefinitionBuilder.php ├── GroupByBuilder.php ├── LikeExpressionBuilder.php ├── RefClauseBuilder.php ├── IndexSizeBuilder.php ├── TableBuilder.php ├── IndexParserBuilder.php └── RecordBuilder.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "qieangel2013/esparser", 3 | "description": "es php library", 4 | "type": "library", 5 | "keywords": ["sql","elasticsearch","restful"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "qieangel2013", 10 | "email": "904208360@qq.com" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.3" 15 | }, 16 | "classmap" : ["src/library/"], 17 | "minimum-stability": "dev" 18 | } 19 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | =10 and a.d<20 and a.h>56 and a.f in(1,2,3,4,5) group by a.name order by a.id desc limit 10'; 4 | //$sql='select a.*,count(a.id) as sid,a.total_price,sum(a.total) as count_total from table1 group by a.total_price order by count_total,a.co'; 5 | //$sql = 'select * from alp_dish_sales_saas where sid in(994,290) limit 0,10'; 6 | $sql='select category_name cate_name,dish_name,dishsno,sale_date,sum(total_price) total_price,sum(total_count) total_count,category_name,sku_name properties from alp_dish_sales_saas where sale_date>="2017-01-01" and sale_date<="2017-09-03" and sid in(994,290) limit 3,10'; 7 | //$sql='update alp_dish_sales_saas set mid=3 where adsid=15125110'; 8 | //$sql='delete from alp_dish_sales_saas where adsid=15546509'; 9 | $es_config=array( 10 | 'index' =>"alp_dish_sales_saas", 11 | 'type' =>"alp_dish_sales_saas", 12 | 'url' =>"http://127.0.0.1:9200", 13 | 'version' =>"5.x" //1.x 2.x 5.x 6.x,可以不配置,系统会请求获取版本,这样会多一次请求 14 | ); 15 | $parser = new EsParser($sql, true,$es_config);//第三个参数是es的配置参数,一定要配置 16 | print_r($parser->build());//打印结果 17 | $result=$parser->scroll();//深度分页初始化会返回第一条 18 | $result=json_decode($result,true); 19 | print_r($result);//打印深度分页结果 20 | $result1=$parser->scroll($result['scrollid']);//深度分页下一页 21 | print_r(json_decode($result1,true));//打印深度分页结果 22 | $result2=$parser->scroll($result['scrollid']);//深度分页下一页 23 | print_r(json_decode($result2,true));//打印深度分页结果 24 | $result3=$parser->scroll($result['scrollid']);//深度分页下一页 25 | print_r(json_decode($result3,true));//打印深度分页结果 26 | //print_r($parser->explain()); //打印dsl 27 | -------------------------------------------------------------------------------- /src/example.php: -------------------------------------------------------------------------------- 1 | =10 and a.d<20 and a.h>56 and a.f in(1,2,3,4,5) group by a.name order by a.id desc limit 10'; 4 | //$sql='select a.*,count(a.id) as sid,a.total_price,sum(a.total) as count_total from table1 group by a.total_price order by count_total,a.co'; 5 | //$sql = 'select * from alp_dish_sales_saas where sid in(994,290) limit 0,10'; 6 | $sql='select category_name cate_name,dish_name,dishsno,sale_date,sum(total_price) total_price,sum(total_count) total_count,category_name,sku_name properties from alp_dish_sales_saas where sale_date>="2017-01-01" and sale_date<="2017-09-03" and sid in(994,290) limit 3,10'; 7 | //$sql='update alp_dish_sales_saas set mid=3 where adsid=15125110'; 8 | //$sql='delete from alp_dish_sales_saas where adsid=15546509'; 9 | $es_config=array( 10 | 'index' =>"alp_dish_sales_saas", 11 | 'type' =>"alp_dish_sales_saas", 12 | 'url' =>"http://127.0.0.1:9200", 13 | 'version' =>"5.x" //1.x 2.x 5.x 6.x,可以不配置,系统会请求获取版本,这样会多一次请求 14 | ); 15 | $parser = new EsParser($sql, true,$es_config);//第三个参数是es的配置参数,一定要配置 16 | print_r($parser->build());//打印结果 17 | $result=$parser->scroll();//深度分页初始化会返回第一条 18 | $result=json_decode($result,true); 19 | print_r($result);//打印深度分页结果 20 | $result1=$parser->scroll($result['scrollid']);//深度分页下一页 21 | print_r(json_decode($result1,true));//打印深度分页结果 22 | $result2=$parser->scroll($result['scrollid']);//深度分页下一页 23 | print_r(json_decode($result2,true));//打印深度分页结果 24 | $result3=$parser->scroll($result['scrollid']);//深度分页下一页 25 | print_r(json_decode($result3,true));//打印深度分页结果 26 | //print_r($parser->explain()); //打印dsl 27 | -------------------------------------------------------------------------------- /src/library/processors/UsingProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/FromProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the USING statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class UsingProcessor extends FromProcessor { 43 | 44 | } 45 | ?> -------------------------------------------------------------------------------- /src/library/processors/UpdateProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/FromProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the UPDATE statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class UpdateProcessor extends FromProcessor { 43 | 44 | } 45 | ?> -------------------------------------------------------------------------------- /src/library/processors/WhereProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/ExpressionListProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the WHERE statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class WhereProcessor extends ExpressionListProcessor { 43 | 44 | } 45 | ?> -------------------------------------------------------------------------------- /src/library/processors/HavingProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/ExpressionListProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the HAVING statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class HavingProcessor extends ExpressionListProcessor { 43 | 44 | } 45 | ?> -------------------------------------------------------------------------------- /src/library/processors/DuplicateProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/SetProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the DUPLICATE statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class DuplicateProcessor extends SetProcessor { 43 | 44 | public function process($tokens,$isUpdate = false) { 45 | return parent::process($tokens, false); 46 | } 47 | 48 | } 49 | ?> -------------------------------------------------------------------------------- /src/library/processors/DescribeProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/ExplainProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the DESCRIBE statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class DescribeProcessor extends ExplainProcessor { 43 | 44 | protected function isStatement($keys, $needle = "DESCRIBE") { 45 | return parent::isStatement($keys, $needle); 46 | } 47 | } 48 | ?> -------------------------------------------------------------------------------- /src/library/processors/ReplaceProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/InsertProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the REPLACE statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class ReplaceProcessor extends InsertProcessor { 43 | 44 | public function process($tokenList,$token_category = 'REPLACE') { 45 | return parent::process($tokenList, 'REPLACE'); 46 | } 47 | 48 | } 49 | ?> -------------------------------------------------------------------------------- /src/library/processors/DescProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/ExplainProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the DESC statement. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class DescProcessor extends ExplainProcessor { 43 | 44 | protected function isStatement($keys, $needle = "DESC") { 45 | return parent::isStatement($keys, $needle); 46 | } 47 | } 48 | ?> -------------------------------------------------------------------------------- /src/library/exceptions/InvalidParameterException.php: -------------------------------------------------------------------------------- 1 | 10 | * 11 | * All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * * Redistributions of source code must retain the above copyright notice, 17 | * this list of conditions and the following disclaimer. 18 | * * Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 23 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 27 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 31 | * DAMAGE. 32 | */ 33 | 34 | /** 35 | * This exception will occur in the parser, if the given SQL statement 36 | * is not a String type. 37 | * 38 | * @author arothe 39 | * 40 | */ 41 | class InvalidParameterException extends InvalidArgumentException { 42 | 43 | protected $argument; 44 | 45 | public function __construct($argument) { 46 | $this->argument = $argument; 47 | parent::__construct("no SQL string to parse: \n" . $argument, 10); 48 | } 49 | 50 | public function getArgument() { 51 | return $this->argument; 52 | } 53 | } 54 | 55 | ?> 56 | -------------------------------------------------------------------------------- /src/library/processors/ColumnListProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once dirname(__FILE__) . '/AbstractProcessor.php'; 34 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 35 | 36 | /** 37 | * 38 | * This class processes column-lists. 39 | * 40 | * @author arothe 41 | * 42 | */ 43 | class ColumnListProcessor extends AbstractProcessor { 44 | 45 | public function process($tokens) { 46 | $columns = explode(",", $tokens); 47 | $cols = array(); 48 | foreach ($columns as $k => $v) { 49 | $cols[] = array('expr_type' => ExpressionType::COLREF, 'base_expr' => trim($v), 50 | 'no_quotes' => $this->revokeQuotation($v)); 51 | } 52 | return $cols; 53 | } 54 | } 55 | ?> -------------------------------------------------------------------------------- /src/library/exceptions/UnsupportedFeatureException.php: -------------------------------------------------------------------------------- 1 | 10 | * 11 | * All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * * Redistributions of source code must retain the above copyright notice, 17 | * this list of conditions and the following disclaimer. 18 | * * Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 23 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 27 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 31 | * DAMAGE. 32 | */ 33 | 34 | /** 35 | * This exception will occur in the PHPSQLCreator, if the creator finds 36 | * a field name, which is unknown. The developers have created some 37 | * additional output of the parser, but the creator class has not been 38 | * enhanced. Please open an issue in such a case. 39 | * 40 | * @author arothe 41 | * 42 | */ 43 | class UnsupportedFeatureException extends Exception { 44 | 45 | protected $key; 46 | 47 | public function __construct($key) { 48 | $this->key = $key; 49 | parent::__construct($key . " not implemented.", 20); 50 | } 51 | 52 | public function getKey() { 53 | return $this->key; 54 | } 55 | } 56 | 57 | ?> 58 | -------------------------------------------------------------------------------- /src/library/processors/IntoProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/AbstractProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the INTO statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class IntoProcessor extends AbstractProcessor { 43 | 44 | /** 45 | * TODO: This is a dummy function, we cannot parse INTO as part of SELECT 46 | * at the moment 47 | */ 48 | public function process($tokenList) { 49 | $unparsed = $tokenList['INTO']; 50 | foreach ($unparsed as $k => $token) { 51 | if ($this->isWhitespaceToken($token) || $this->isCommaToken($token)) { 52 | unset($unparsed[$k]); 53 | } 54 | } 55 | $tokenList['INTO'] = array_values($unparsed); 56 | return $tokenList; 57 | } 58 | } 59 | ?> -------------------------------------------------------------------------------- /src/library/builders/UpdateBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: UpdateBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | /** 43 | * This class implements the builder for the UPDATE statement parts. 44 | * You can overwrite all functions to achieve another handling. 45 | * 46 | * @author André Rothe 47 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 48 | * 49 | */ 50 | class UpdateBuilder { 51 | 52 | public function build($parsed) { 53 | return "UPDATE " . $parsed[0]['table']; 54 | } 55 | } 56 | ?> 57 | -------------------------------------------------------------------------------- /src/library/processors/RecordProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/AbstractProcessor.php'); 34 | require_once(dirname(__FILE__) . '/ExpressionListProcessor.php'); 35 | 36 | /** 37 | * 38 | * This class processes records. 39 | * 40 | * @author arothe 41 | * 42 | */ 43 | class RecordProcessor extends AbstractProcessor { 44 | 45 | private $expressionListProcessor; 46 | 47 | public function __construct() { 48 | $this->expressionListProcessor = new ExpressionListProcessor(); 49 | } 50 | 51 | public function process($unparsed) { 52 | $unparsed = $this->removeParenthesisFromStart($unparsed); 53 | $values = $this->splitSQLIntoTokens($unparsed); 54 | 55 | foreach ($values as $k => $v) { 56 | if ($this->isCommaToken($v)) { 57 | $values[$k] = ""; 58 | } 59 | } 60 | return $this->expressionListProcessor->process($values); 61 | } 62 | 63 | } 64 | ?> -------------------------------------------------------------------------------- /src/library/builders/DeleteBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: DeleteBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | /** 43 | * This class implements the builder for the [DELETE] part. You can overwrite 44 | * all functions to achieve another handling. 45 | * 46 | * @author André Rothe 47 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 48 | * 49 | */ 50 | class DeleteBuilder { 51 | 52 | public function build($parsed) { 53 | $sql = "DELETE"; 54 | foreach ($parsed['TABLES'] as $k => $v) { 55 | $sql .= $v . ","; 56 | } 57 | return substr($sql, 0, -1); 58 | } 59 | } 60 | ?> 61 | -------------------------------------------------------------------------------- /src/library/processors/DeleteProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/AbstractProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the DELETE statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class DeleteProcessor extends AbstractProcessor { 43 | 44 | public function process($tokens) { 45 | $tables = array(); 46 | $del = $tokens['DELETE']; 47 | 48 | foreach ($tokens['DELETE'] as $expression) { 49 | if ($expression !== 'DELETE' && trim($expression, ' .*') !== "" && !$this->isCommaToken($expression)) { 50 | $tables[] = trim($expression, '.* '); 51 | } 52 | } 53 | 54 | if (empty($tables)) { 55 | foreach ($tokens['FROM'] as $table) { 56 | $tables[] = $table['table']; 57 | } 58 | } 59 | 60 | $tokens['DELETE'] = array('TABLES' => $tables); 61 | return $tokens; 62 | } 63 | } 64 | ?> -------------------------------------------------------------------------------- /src/library/builders/OperatorBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: OperatorBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | 44 | /** 45 | * This class implements the builder for operators. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class OperatorBuilder { 53 | 54 | public function build($parsed) { 55 | if ($parsed['expr_type'] !== ExpressionType::OPERATOR) { 56 | return ""; 57 | } 58 | return $parsed['base_expr']; 59 | } 60 | } 61 | ?> 62 | -------------------------------------------------------------------------------- /src/library/builders/DirectionBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: DirectionBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | /** 43 | * This class implements the builder for directions (e.g. of the order-by clause). 44 | * You can overwrite all functions to achieve another handling. 45 | * 46 | * @author André Rothe 47 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 48 | * 49 | */ 50 | class DirectionBuilder { 51 | 52 | public function build($parsed) { 53 | if (!isset($parsed['direction']) || $parsed['direction'] === false) { 54 | return ""; 55 | } 56 | return (" " . $parsed['direction']); 57 | } 58 | } 59 | ?> 60 | -------------------------------------------------------------------------------- /src/library/exceptions/UnableToCalculatePositionException.php: -------------------------------------------------------------------------------- 1 | 10 | * 11 | * All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * * Redistributions of source code must retain the above copyright notice, 17 | * this list of conditions and the following disclaimer. 18 | * * Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 23 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 27 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 31 | * DAMAGE. 32 | */ 33 | 34 | /** 35 | * This exception will occur, if the PositionCalculator can not find the token 36 | * defined by a base_expr field within the original SQL statement. Please create 37 | * an issue in such a case, it is an application error. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class UnableToCalculatePositionException extends Exception { 43 | 44 | protected $needle; 45 | protected $haystack; 46 | 47 | public function __construct($needle, $haystack) { 48 | $this->needle = $needle; 49 | $this->haystack = $haystack; 50 | parent::__construct("cannot calculate position of " . $needle . " within " . $haystack, 5); 51 | } 52 | 53 | public function getNeedle() { 54 | return $this->needle; 55 | } 56 | 57 | public function getHaystack() { 58 | return $this->haystack; 59 | } 60 | } 61 | 62 | ?> 63 | -------------------------------------------------------------------------------- /src/library/builders/UserVariableBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: UserVariableBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | 44 | /** 45 | * This class implements the builder for an user variable. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class UserVariableBuilder { 53 | 54 | public function build($parsed) { 55 | if ($parsed['expr_type'] !== ExpressionType::USER_VARIABLE) { 56 | return ""; 57 | } 58 | return $parsed['base_expr']; 59 | } 60 | } 61 | ?> 62 | -------------------------------------------------------------------------------- /src/library/builders/EngineBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: EngineBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | 44 | /** 45 | * This class implements the builder for a database within SHOW statement. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class EngineBuilder { 53 | 54 | public function build($parsed) { 55 | if ($parsed['expr_type'] !== ExpressionType::ENGINE) { 56 | return ""; 57 | } 58 | return $parsed['base_expr']; 59 | } 60 | } 61 | ?> 62 | -------------------------------------------------------------------------------- /src/library/builders/PositionBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: PositionBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | 44 | /** 45 | * This class implements the builder for positions of the GROUP-BY clause. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class PositionBuilder { 53 | 54 | public function build($parsed) { 55 | if ($parsed['expr_type'] !== ExpressionType::POSITION) { 56 | return ""; 57 | } 58 | return $parsed['base_expr']; 59 | } 60 | } 61 | ?> 62 | -------------------------------------------------------------------------------- /src/library/builders/DatabaseBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: DatabaseBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | 44 | /** 45 | * This class implements the builder for a database within SHOW statement. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class DatabaseBuilder { 53 | 54 | public function build($parsed) { 55 | if ($parsed['expr_type'] !== ExpressionType::DATABASE) { 56 | return ""; 57 | } 58 | return $parsed['base_expr']; 59 | } 60 | } 61 | ?> 62 | -------------------------------------------------------------------------------- /src/library/builders/ProcedureBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: ProcedureBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | 44 | /** 45 | * This class implements the builder for a procedure within SHOW statement. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class ProcedureBuilder { 53 | 54 | public function build($parsed) { 55 | if ($parsed['expr_type'] !== ExpressionType::PROCEDURE) { 56 | return ""; 57 | } 58 | return $parsed['base_expr']; 59 | } 60 | } 61 | ?> 62 | -------------------------------------------------------------------------------- /src/library/builders/DataTypeBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: DataTypeBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | 44 | /** 45 | * This class implements the builder for the data-type statement part of CREATE TABLE. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class DataTypeBuilder { 53 | 54 | public function build($parsed) { 55 | if ($parsed['expr_type'] !== ExpressionType::DATA_TYPE) { 56 | return ""; 57 | } 58 | return $parsed['base_expr']; 59 | } 60 | } 61 | ?> 62 | -------------------------------------------------------------------------------- /src/library/builders/ReservedBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: ReservedBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | 44 | /** 45 | * This class implements the builder for reserved keywords. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class ReservedBuilder { 53 | 54 | public function isReserved($parsed) { 55 | return ($parsed['expr_type'] === ExpressionType::RESERVED); 56 | } 57 | 58 | public function build($parsed) { 59 | if (!$this->isReserved($parsed)) { 60 | return ""; 61 | } 62 | return $parsed['base_expr']; 63 | } 64 | } 65 | ?> 66 | -------------------------------------------------------------------------------- /src/library/builders/LimitBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: LimitBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | 44 | /** 45 | * This class implements the builder LIMIT statement. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class LimitBuilder { 53 | 54 | public function build($parsed) { 55 | $sql = ($parsed['offset'] ? $parsed['offset'] . ", " : "") . $parsed['rowcount']; 56 | if ($sql === "") { 57 | throw new UnableToCreateSQLException('LIMIT', 'rowcount', $parsed, 'rowcount'); 58 | } 59 | return "LIMIT " . $sql; 60 | } 61 | } 62 | ?> 63 | -------------------------------------------------------------------------------- /src/library/processors/DefaultProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/AbstractProcessor.php'); 34 | require_once(dirname(__FILE__) . '/UnionProcessor.php'); 35 | require_once(dirname(__FILE__) . '/SQLProcessor.php'); 36 | 37 | /** 38 | * 39 | * This class processes the incoming sql string. 40 | * 41 | * @author arothe 42 | * 43 | */ 44 | class DefaultProcessor extends AbstractProcessor { 45 | 46 | public function process($sql) { 47 | 48 | $inputArray = $this->splitSQLIntoTokens($sql); 49 | 50 | // this is the highest level lexical analysis. This is the part of the 51 | // code which finds UNION and UNION ALL query parts 52 | $processor = new UnionProcessor(); 53 | $queries = $processor->process($inputArray); 54 | 55 | // If there was no UNION or UNION ALL in the query, then the query is 56 | // stored at $queries[0]. 57 | if (!$processor->isUnion($queries)) { 58 | $processor = new SQLProcessor(); 59 | $queries = $processor->process($queries[0]); 60 | } 61 | 62 | return $queries; 63 | } 64 | } 65 | ?> -------------------------------------------------------------------------------- /src/library/builders/AliasBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: AliasBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | /** 43 | * This class implements the builder for aliases. 44 | * You can overwrite all functions to achieve another handling. 45 | * 46 | * @author André Rothe 47 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 48 | * 49 | */ 50 | class AliasBuilder { 51 | 52 | public function hasAlias($parsed) { 53 | return isset($parsed['alias']); 54 | } 55 | 56 | public function build($parsed) { 57 | if (!isset($parsed['alias']) || $parsed['alias'] === false) { 58 | return ""; 59 | } 60 | $sql = ""; 61 | if ($parsed['alias']['as']) { 62 | $sql .= " as"; 63 | } 64 | $sql .= " " . $parsed['alias']['name']; 65 | return $sql; 66 | } 67 | } 68 | ?> 69 | -------------------------------------------------------------------------------- /src/library/builders/RefTypeBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: RefTypeBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnsupportedFeatureException.php'; 43 | 44 | /** 45 | * This class implements the references type within a JOIN. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class RefTypeBuilder { 53 | 54 | public function build($parsed) { 55 | if ($parsed === false) { 56 | return ""; 57 | } 58 | if ($parsed === 'ON') { 59 | return " ON "; 60 | } 61 | if ($parsed === 'USING') { 62 | return " USING "; 63 | } 64 | // TODO: add more 65 | throw new UnsupportedFeatureException($parsed); 66 | } 67 | } 68 | ?> 69 | -------------------------------------------------------------------------------- /src/library/processors/GroupByProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/OrderByProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the GROUP-BY statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class GroupByProcessor extends OrderByProcessor { 43 | 44 | public function process($tokens, $select = array()) { 45 | $out = array(); 46 | $parseInfo = $this->initParseInfo(); 47 | 48 | if (!$tokens) { 49 | return false; 50 | } 51 | 52 | foreach ($tokens as $token) { 53 | $trim = strtoupper(trim($token)); 54 | switch ($trim) { 55 | case ',': 56 | $parsed = $this->processOrderExpression($parseInfo, $select); 57 | unset($parsed['direction']); 58 | 59 | $out[] = $parsed; 60 | $parseInfo = $this->initParseInfo(); 61 | break; 62 | default: 63 | $parseInfo['base_expr'] .= $token; 64 | } 65 | } 66 | 67 | $parsed = $this->processOrderExpression($parseInfo, $select); 68 | unset($parsed['direction']); 69 | $out[] = $parsed; 70 | 71 | return $out; 72 | } 73 | } 74 | ?> -------------------------------------------------------------------------------- /src/library/builders/CreateTableSelectOptionBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: CreateTableSelectOptionBuilder.php 932 2014-01-08 13:15:26Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | /** 43 | * This class implements the builder for the select-options statement part of CREATE TABLE. 44 | * You can overwrite all functions to achieve another handling. 45 | * 46 | * @author André Rothe 47 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 48 | * 49 | */ 50 | class CreateTableSelectOptionBuilder { 51 | 52 | public function build($parsed) { 53 | if (!isset($parsed['select-option']) || $parsed['select-option'] === false) { 54 | return ""; 55 | } 56 | $option = $parsed['select-option']; 57 | 58 | $sql = ($option['duplicates'] === false ? '' : (' ' . $option['duplicates'])); 59 | $sql .= ($option['as'] === false ? '' : ' AS'); 60 | return $sql; 61 | } 62 | } 63 | ?> 64 | -------------------------------------------------------------------------------- /src/library/builders/ConstantBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: ConstantBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/AliasBuilder.php'; 43 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 44 | 45 | /** 46 | * This class implements the builder for constants. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class ConstantBuilder { 54 | 55 | protected function buildAlias($parsed) { 56 | $builder = new AliasBuilder(); 57 | return $builder->build($parsed); 58 | } 59 | 60 | public function build($parsed) { 61 | if ($parsed['expr_type'] !== ExpressionType::CONSTANT) { 62 | return ""; 63 | } 64 | $sql = $parsed['base_expr']; 65 | $sql .= $this->buildAlias($parsed); 66 | return $sql; 67 | } 68 | } 69 | ?> 70 | -------------------------------------------------------------------------------- /src/library/builders/OrderByAliasBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: OrderByAliasBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | require_once dirname(__FILE__) . '/DirectionBuilder.php'; 44 | 45 | /** 46 | * This class implements the builder for an alias within the ORDER-BY clause. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class OrderByAliasBuilder { 54 | 55 | protected function buildDirection($parsed) { 56 | $builder = new DirectionBuilder(); 57 | return $builder->build($parsed); 58 | } 59 | 60 | public function build($parsed) { 61 | if ($parsed['expr_type'] !== ExpressionType::ALIAS) { 62 | return ""; 63 | } 64 | return $parsed['base_expr'] . $this->buildDirection($parsed); 65 | } 66 | } 67 | ?> 68 | -------------------------------------------------------------------------------- /src/library/builders/InListBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: InListBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | require_once dirname(__FILE__) . '/SubTreeBuilder.php'; 44 | 45 | /** 46 | * This class implements the builder list of values for the IN statement. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class InListBuilder { 54 | 55 | protected function buildSubTree($parsed, $delim) { 56 | $builder = new SubTreeBuilder(); 57 | return $builder->build($parsed, $delim); 58 | } 59 | 60 | public function build($parsed) { 61 | if ($parsed['expr_type'] !== ExpressionType::IN_LIST) { 62 | return ""; 63 | } 64 | $sql = $this->buildSubTree($parsed, ", "); 65 | return "(" . $sql . ")"; 66 | } 67 | } 68 | ?> 69 | -------------------------------------------------------------------------------- /src/library/builders/CreateTableDefinitionBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: CreateTableDefinitionBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/TableBracketExpressionBuilder.php'; 43 | 44 | /** 45 | * This class implements the builder for the create definitions of CREATE TABLE. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class CreateTableDefinitionBuilder { 53 | 54 | protected function buildTableBracketExpression($parsed) { 55 | $builder = new TableBracketExpressionBuilder(); 56 | return $builder->build($parsed); 57 | } 58 | 59 | public function build($parsed) { 60 | if (!isset($parsed) || $parsed['create-def'] === false) { 61 | return ""; 62 | } 63 | return $this->buildTableBracketExpression($parsed['create-def']); 64 | } 65 | } 66 | ?> 67 | -------------------------------------------------------------------------------- /src/library/builders/JoinBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: JoinBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnsupportedFeatureException.php'; 43 | 44 | /** 45 | * This class implements the builder for the JOIN statement parts (within FROM). 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class JoinBuilder { 53 | 54 | public function build($parsed) { 55 | if ($parsed === 'CROSS') { 56 | return ", "; 57 | } 58 | if ($parsed === 'JOIN') { 59 | return " INNER JOIN "; 60 | } 61 | if ($parsed === 'LEFT') { 62 | return " LEFT JOIN "; 63 | } 64 | if ($parsed === 'RIGHT') { 65 | return " RIGHT JOIN "; 66 | } 67 | // TODO: add more 68 | throw new UnsupportedFeatureException($parsed); 69 | } 70 | } 71 | ?> 72 | -------------------------------------------------------------------------------- /src/library/builders/LikeBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: LikeBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/TableBuilder.php'; 44 | 45 | /** 46 | * This class implements the builder for the LIKE statement part of CREATE TABLE. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class LikeBuilder { 54 | 55 | protected function buildTable($parsed, $index) { 56 | $builder = new TableBuilder(); 57 | return $builder->build($parsed, $index); 58 | } 59 | 60 | public function build($parsed) { 61 | $sql = $this->buildTable($parsed, 0); 62 | if (strlen($sql) === 0) { 63 | throw new UnableToCreateSQLException('LIKE', "", $like, 'table'); 64 | } 65 | return "LIKE " . $sql; 66 | } 67 | } 68 | ?> 69 | -------------------------------------------------------------------------------- /src/library/builders/ConstraintBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: ConstraintBuilder.php 891 2013-12-31 00:20:19Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | require_once dirname(__FILE__) . '/ConstantBuilder.php'; 44 | 45 | /** 46 | * This class implements the builder for the constraint statement part of CREATE TABLE. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class ConstraintBuilder { 54 | 55 | protected function buildConstant($parsed) { 56 | $builder = new ConstantBuilder(); 57 | return $builder->build($parsed); 58 | } 59 | 60 | public function build($parsed) { 61 | if ($parsed['expr_type'] !== ExpressionType::CONSTRAINT) { 62 | return ""; 63 | } 64 | $sql = $this->buildConstant($parsed['sub_tree']); 65 | return "CONSTRAINT" . (empty($sql) ? '' : (' ' . $sql)); 66 | } 67 | 68 | } 69 | ?> 70 | -------------------------------------------------------------------------------- /src/library/exceptions/UnableToCreateSQLException.php: -------------------------------------------------------------------------------- 1 | 10 | * 11 | * All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * * Redistributions of source code must retain the above copyright notice, 17 | * this list of conditions and the following disclaimer. 18 | * * Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 23 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 27 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 31 | * DAMAGE. 32 | */ 33 | 34 | /** 35 | * This exception will occur within the PHPSQLCreator, if the creator can not find a 36 | * method, which can handle the current expr_type field. It could be an error within the parser 37 | * output or a special case has not been modelled within the creator. Please create an issue 38 | * in such a case. 39 | * 40 | * @author arothe 41 | * 42 | */ 43 | class UnableToCreateSQLException extends Exception { 44 | 45 | protected $part; 46 | protected $partkey; 47 | protected $entry; 48 | protected $entrykey; 49 | 50 | public function __construct($part, $partkey, $entry, $entrykey) { 51 | $this->part = $part; 52 | $this->partkey = $partkey; 53 | $this->entry = $entry; 54 | $this->entrykey = $entrykey; 55 | parent::__construct("unknown [" . $entrykey . "] = " .$entry[$entrykey] . " in \"" . $part . "\" [" . $partkey . "] ", 15); 56 | } 57 | 58 | public function getEntry() { 59 | return $this->entry; 60 | } 61 | 62 | public function getEntryKey() { 63 | return $this->entrykey; 64 | } 65 | 66 | public function getSQLPart() { 67 | return $this->part; 68 | } 69 | 70 | public function getSQLPartKey() { 71 | return $this->partkey; 72 | } 73 | } 74 | 75 | ?> 76 | -------------------------------------------------------------------------------- /src/library/processors/LimitProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/AbstractProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the LIMIT statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class LimitProcessor extends AbstractProcessor { 43 | 44 | public function process($tokens) { 45 | $rowcount = ""; 46 | $offset = ""; 47 | 48 | $comma = -1; 49 | $exchange = false; 50 | 51 | for ($i = 0; $i < count($tokens); ++$i) { 52 | $trim = trim($tokens[$i]); 53 | if ($trim === ",") { 54 | $comma = $i; 55 | break; 56 | } 57 | if ($trim === "OFFSET") { 58 | $comma = $i; 59 | $exchange = true; 60 | break; 61 | } 62 | } 63 | 64 | for ($i = 0; $i < $comma; ++$i) { 65 | if ($exchange) { 66 | $rowcount .= $tokens[$i]; 67 | } else { 68 | $offset .= $tokens[$i]; 69 | } 70 | } 71 | 72 | for ($i = $comma + 1; $i < count($tokens); ++$i) { 73 | if ($exchange) { 74 | $offset .= $tokens[$i]; 75 | } else { 76 | $rowcount .= $tokens[$i]; 77 | } 78 | } 79 | 80 | return array('offset' => trim($offset), 'rowcount' => trim($rowcount)); 81 | } 82 | } 83 | ?> -------------------------------------------------------------------------------- /src/library/builders/ShowStatementBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: ShowStatementBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/ShowBuilder.php'; 43 | require_once dirname(__FILE__) . '/WhereBuilder.php'; 44 | 45 | /** 46 | * This class implements the builder for the SHOW statement. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class ShowStatementBuilder { 54 | 55 | protected function buildWHERE($parsed) { 56 | $builder = new WhereBuilder(); 57 | return $builder->build($parsed); 58 | } 59 | 60 | protected function buildSHOW($parsed) { 61 | $builder = new ShowBuilder(); 62 | return $builder->build($parsed); 63 | } 64 | 65 | public function build($parsed) { 66 | $sql = $this->buildSHOW($parsed); 67 | if (isset($parsed['WHERE'])) { 68 | $sql .= " " . $this->buildWHERE($parsed['WHERE']); 69 | } 70 | return $sql; 71 | } 72 | } 73 | ?> 74 | -------------------------------------------------------------------------------- /src/library/builders/ColumnTypeBracketExpressionBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: ColumnTypeBracketExpressionBuilder.php 934 2014-01-08 13:57:16Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/SubTreeBuilder.php'; 43 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 44 | /** 45 | * This class implements the builder for bracket expressions within a column type. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class ColumnTypeBracketExpressionBuilder { 53 | 54 | protected function buildSubTree($parsed, $delim) { 55 | $builder = new SubTreeBuilder(); 56 | return $builder->build($parsed, $delim); 57 | } 58 | 59 | public function build($parsed) { 60 | if ($parsed['expr_type'] !== ExpressionType::BRACKET_EXPRESSION) { 61 | return ""; 62 | } 63 | $sql = $this->buildSubTree($parsed, ","); 64 | $sql = "(" . $sql . ")"; 65 | return $sql; 66 | } 67 | } 68 | ?> 69 | -------------------------------------------------------------------------------- /src/library/builders/SelectBracketExpressionBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: SelectBracketExpressionBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/SubTreeBuilder.php'; 43 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 44 | /** 45 | * This class implements the builder for bracket expressions within a SELECT statement. 46 | * You can overwrite all functions to achieve another handling. 47 | * 48 | * @author André Rothe 49 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 50 | * 51 | */ 52 | class SelectBracketExpressionBuilder { 53 | 54 | protected function buildSubTree($parsed, $delim) { 55 | $builder = new SubTreeBuilder(); 56 | return $builder->build($parsed, $delim); 57 | } 58 | 59 | public function build($parsed) { 60 | if ($parsed['expr_type'] !== ExpressionType::BRACKET_EXPRESSION) { 61 | return ""; 62 | } 63 | $sql = $this->buildSubTree($parsed, " "); 64 | $sql = "(" . $sql . ")"; 65 | return $sql; 66 | } 67 | } 68 | ?> 69 | -------------------------------------------------------------------------------- /src/library/builders/InsertStatementBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: InsertStatementBuilder.php 834 2013-12-18 10:14:26Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/InsertBuilder.php'; 43 | require_once dirname(__FILE__) . '/ValuesBuilder.php'; 44 | 45 | /** 46 | * This class implements the builder for the whole Insert statement. You can overwrite 47 | * all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class InsertStatementBuilder { 54 | 55 | protected function buildVALUES($parsed) { 56 | $builder = new ValuesBuilder(); 57 | return $builder->build($parsed); 58 | } 59 | 60 | protected function buildINSERT($parsed) { 61 | $builder = new InsertBuilder($parsed); 62 | return $builder->build($parsed); 63 | } 64 | 65 | public function build($parsed) { 66 | // TODO: are there more than one tables possible (like [INSERT][1]) 67 | return $this->buildINSERT($parsed['INSERT'][0]) . " " . $this->buildVALUES($parsed['VALUES']); 68 | // TODO: subquery? 69 | } 70 | } 71 | ?> 72 | -------------------------------------------------------------------------------- /src/library/processors/InsertProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/AbstractProcessor.php'); 34 | require_once(dirname(__FILE__) . '/ColumnListProcessor.php'); 35 | require_once(dirname(__FILE__) . '/../utils/ExpressionType.php'); 36 | 37 | /** 38 | * 39 | * This class processes the INSERT statements. 40 | * 41 | * @author arothe 42 | * 43 | */ 44 | class InsertProcessor extends AbstractProcessor { 45 | 46 | public function process($tokenList, $token_category = 'INSERT') { 47 | $table = ""; 48 | $cols = array(); 49 | 50 | $into = $tokenList['INTO']; 51 | foreach ($into as $token) { 52 | if ($this->isWhitespaceToken($token)) 53 | continue; 54 | if ($table === "") { 55 | $table = $token; 56 | } elseif (empty($cols)) { 57 | $cols[] = $token; 58 | } 59 | } 60 | 61 | if (empty($cols)) { 62 | $cols = false; 63 | } else { 64 | $processor = new ColumnListProcessor(); 65 | $cols = $processor->process($this->removeParenthesisFromStart($cols[0])); 66 | } 67 | 68 | unset($tokenList['INTO']); 69 | $tokenList[$token_category][0] = array('table' => $table, 'columns' => $cols, 'base_expr' => $table, 70 | 'no_quotes' => $this->revokeQuotation($table)); 71 | return $tokenList; 72 | } 73 | 74 | } 75 | ?> -------------------------------------------------------------------------------- /src/library/processors/ValuesProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/../utils/ExpressionType.php'); 34 | require_once(dirname(__FILE__) . '/RecordProcessor.php'); 35 | require_once(dirname(__FILE__) . '/AbstractProcessor.php'); 36 | 37 | /** 38 | * 39 | * This class processes the VALUES statements. 40 | * 41 | * @author arothe 42 | * 43 | */ 44 | class ValuesProcessor extends AbstractProcessor { 45 | 46 | private $recordProcessor; 47 | 48 | public function __construct() { 49 | $this->recordProcessor = new RecordProcessor(); 50 | } 51 | 52 | public function process($tokens) { 53 | $unparsed = ""; 54 | foreach ($tokens['VALUES'] as $k => $v) { 55 | if ($this->isWhitespaceToken($v)) { 56 | continue; 57 | } 58 | $unparsed .= $v; 59 | } 60 | 61 | $values = $this->splitSQLIntoTokens($unparsed); 62 | 63 | $parsed = array(); 64 | foreach ($values as $k => $v) { 65 | if ($this->isCommaToken($v)) { 66 | unset($values[$k]); 67 | } else { 68 | $processor = new RecordProcessor(); 69 | $values[$k] = array('expr_type' => ExpressionType::RECORD, 'base_expr' => $v, 70 | 'data' => $this->recordProcessor->process($v)); 71 | } 72 | } 73 | 74 | $tokens['VALUES'] = array_values($values); 75 | return $tokens; 76 | } 77 | 78 | } 79 | ?> -------------------------------------------------------------------------------- /src/library/builders/SetBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: SetBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/SetExpressionBuilder.php'; 44 | 45 | /** 46 | * This class implements the builder for the SET part of INSERT statement. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class SetBuilder { 54 | 55 | protected function buildSetExpression($parsed) { 56 | $builder = new SetExpressionBuilder(); 57 | return $builder->build($parsed); 58 | } 59 | 60 | public function build($parsed) { 61 | $sql = ""; 62 | foreach ($parsed as $k => $v) { 63 | $len = strlen($sql); 64 | $sql .= $this->buildSetExpression($v); 65 | 66 | if ($len == strlen($sql)) { 67 | throw new UnableToCreateSQLException('SET', $k, $v, 'expr_type'); 68 | } 69 | 70 | $sql .= ","; 71 | } 72 | return "SET " . substr($sql, 0, -1); 73 | } 74 | } 75 | ?> 76 | -------------------------------------------------------------------------------- /src/library/builders/ValuesBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: ValuesBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/RecordBuilder.php'; 44 | 45 | /** 46 | * This class implements the builder for the VALUES part of INSERT statement. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class ValuesBuilder { 54 | 55 | protected function buildRecord($parsed) { 56 | $builder = new RecordBuilder(); 57 | return $builder->build($parsed); 58 | } 59 | 60 | public function build($parsed) { 61 | $sql = ""; 62 | foreach ($parsed as $k => $v) { 63 | $len = strlen($sql); 64 | $sql .= $this->buildRecord($v); 65 | 66 | if ($len == strlen($sql)) { 67 | throw new UnableToCreateSQLException('VALUES', $k, $v, 'expr_type'); 68 | } 69 | 70 | $sql .= ","; 71 | } 72 | $sql = substr($sql, 0, -1); 73 | return "VALUES " . $sql; 74 | } 75 | } 76 | ?> 77 | -------------------------------------------------------------------------------- /src/library/builders/ColumnReferenceBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: ColumnReferenceBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/AliasBuilder.php'; 43 | require_once dirname(__FILE__) . '/DirectionBuilder.php'; 44 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 45 | /** 46 | * This class implements the builder for column references. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class ColumnReferenceBuilder { 54 | 55 | protected function buildDirection($parsed) { 56 | $builder = new DirectionBuilder(); 57 | return $builder->build($parsed); 58 | } 59 | 60 | protected function buildAlias($parsed) { 61 | $builder = new AliasBuilder(); 62 | return $builder->build($parsed); 63 | } 64 | 65 | public function build($parsed) { 66 | if ($parsed['expr_type'] !== ExpressionType::COLREF) { 67 | return ""; 68 | } 69 | $sql = $parsed['base_expr']; 70 | $sql .= $this->buildAlias($parsed); 71 | $sql .= $this->buildDirection($parsed); 72 | return $sql; 73 | } 74 | } 75 | ?> 76 | -------------------------------------------------------------------------------- /src/library/builders/IndexColumnBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: IndexColumnBuilder.php 917 2014-01-08 11:47:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnsupportedFeatureException.php'; 43 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 44 | /** 45 | * This class implements the builder for index column entries of the column-list 46 | * parts of CREATE TABLE. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class IndexColumnBuilder { 54 | 55 | protected function buildLength($parsed) { 56 | return ($parsed === false ? '' : ('(' . $parsed . ')')); 57 | } 58 | 59 | protected function buildDirection($parsed) { 60 | return ($parsed === false ? '' : (' ' . $parsed)); 61 | } 62 | 63 | public function build($parsed) { 64 | if ($parsed['expr_type'] !== ExpressionType::INDEX_COLUMN) { 65 | return ""; 66 | } 67 | $sql = $parsed['name']; 68 | $sql .= $this->buildLength($parsed['length']); 69 | $sql .= $this->buildDirection($parsed['dir']); 70 | return $sql; 71 | } 72 | 73 | } 74 | ?> 75 | -------------------------------------------------------------------------------- /src/library/builders/SelectExpressionBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: SelectExpressionBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/SubTreeBuilder.php'; 43 | require_once dirname(__FILE__) . '/AliasBuilder.php'; 44 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 45 | 46 | /** 47 | * This class implements the builder for simple expressions within a SELECT statement. 48 | * You can overwrite all functions to achieve another handling. 49 | * 50 | * @author André Rothe 51 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 52 | * 53 | */ 54 | class SelectExpressionBuilder { 55 | 56 | protected function buildSubTree($parsed, $delim) { 57 | $builder = new SubTreeBuilder(); 58 | return $builder->build($parsed, $delim); 59 | } 60 | 61 | protected function buildAlias($parsed) { 62 | $builder = new AliasBuilder(); 63 | return $builder->build($parsed); 64 | } 65 | 66 | public function build($parsed) { 67 | if ($parsed['expr_type'] !== ExpressionType::EXPRESSION) { 68 | return ""; 69 | } 70 | $sql = $this->buildSubTree($parsed, " "); 71 | $sql .= $this->buildAlias($parsed); 72 | return $sql; 73 | } 74 | } 75 | ?> 76 | -------------------------------------------------------------------------------- /src/library/builders/UpdateStatementBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: UpdateStatementBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/WhereBuilder.php'; 43 | require_once dirname(__FILE__) . '/SetBuilder.php'; 44 | require_once dirname(__FILE__) . '/UpdateBuilder.php'; 45 | 46 | /** 47 | * This class implements the builder for the whole Update statement. You can overwrite 48 | * all functions to achieve another handling. 49 | * 50 | * @author André Rothe 51 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 52 | * 53 | */ 54 | class UpdateStatementBuilder { 55 | 56 | protected function buildWHERE($parsed) { 57 | $builder = new WhereBuilder(); 58 | return $builder->build($parsed); 59 | } 60 | 61 | protected function buildSET($parsed) { 62 | $builder = new SetBuilder(); 63 | return $builder->build($parsed); 64 | } 65 | 66 | protected function buildUPDATE($parsed) { 67 | $builder = new UpdateBuilder(); 68 | return $builder->build($parsed); 69 | } 70 | 71 | public function build($parsed) { 72 | $sql = $this->buildUPDATE($parsed['UPDATE']) . " " . $this->buildSET($parsed['SET']); 73 | if (isset($parsed['WHERE'])) { 74 | $sql .= " " . $this->buildWHERE($parsed['WHERE']); 75 | } 76 | return $sql; 77 | } 78 | } 79 | ?> 80 | -------------------------------------------------------------------------------- /src/library/builders/CreateBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: CreateBuilder.php 833 2013-12-18 10:13:59Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | require_once dirname(__FILE__) . '/CreateTableBuilder.php'; 44 | require_once dirname(__FILE__) . '/SubTreeBuilder.php'; 45 | 46 | /** 47 | * This class implements the builder for the [CREATE] part. You can overwrite 48 | * all functions to achieve another handling. 49 | * 50 | * @author André Rothe 51 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 52 | * 53 | */ 54 | class CreateBuilder { 55 | 56 | protected function buildCreateTable($parsed) { 57 | $builder = new CreateTableBuilder(); 58 | return $builder->build($parsed); 59 | } 60 | 61 | protected function buildSubTree($parsed) { 62 | $builder = new SubTreeBuilder(); 63 | return $builder->build($parsed); 64 | } 65 | 66 | public function build($parsed) { 67 | $create = $parsed['CREATE']; 68 | $sql = $this->buildSubTree($create); 69 | 70 | if (($create['expr_type'] === ExpressionType::TABLE) 71 | || ($create['expr_type'] === ExpressionType::TEMPORARY_TABLE)) { 72 | $sql .= " " . $this->buildCreateTable($parsed['TABLE']); 73 | } 74 | // TODO: add more expr_types here (like VIEW), if available 75 | return "CREATE " . $sql; 76 | } 77 | 78 | } 79 | ?> 80 | -------------------------------------------------------------------------------- /src/library/builders/DeleteStatementBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: DeleteStatementBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/WhereBuilder.php'; 43 | require_once dirname(__FILE__) . '/FromBuilder.php'; 44 | require_once dirname(__FILE__) . '/DeleteBuilder.php'; 45 | 46 | /** 47 | * This class implements the builder for the whole Delete statement. You can overwrite 48 | * all functions to achieve another handling. 49 | * 50 | * @author André Rothe 51 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 52 | * 53 | */ 54 | class DeleteStatementBuilder { 55 | 56 | protected function buildWHERE($parsed) { 57 | $builder = new WhereBuilder(); 58 | return $builder->build($parsed); 59 | } 60 | 61 | protected function buildFROM($parsed) { 62 | $builder = new FromBuilder(); 63 | return $builder->build($parsed); 64 | } 65 | 66 | protected function buildDELETE($parsed) { 67 | $builder = new DeleteBuilder(); 68 | return $builder->build($parsed); 69 | } 70 | 71 | public function processDeleteStatement($parsed) { 72 | $sql = $this->buildDELETE($parsed['DELETE']) . " " . $this->processFROM($parsed['FROM']); 73 | if (isset($parsed['WHERE'])) { 74 | $sql .= " " . $this->processWHERE($parsed['WHERE']); 75 | } 76 | return $sql; 77 | } 78 | 79 | } 80 | ?> 81 | -------------------------------------------------------------------------------- /src/library/builders/ColumnListBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: ColumnListBuilder.php 894 2013-12-31 00:27:03Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/IndexColumnBuilder.php'; 44 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 45 | /** 46 | * This class implements the builder for column-list parts of CREATE TABLE. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class ColumnListBuilder { 54 | 55 | protected function buildIndexColumn($parsed) { 56 | $builder = new IndexColumnBuilder(); 57 | return $builder->build($parsed); 58 | } 59 | 60 | public function build($parsed) { 61 | if ($parsed['expr_type'] !== ExpressionType::COLUMN_LIST) { 62 | return ""; 63 | } 64 | $sql = ""; 65 | foreach ($parsed['sub_tree'] as $k => $v) { 66 | $len = strlen($sql); 67 | $sql .= $this->buildIndexColumn($v); 68 | 69 | if ($len == strlen($sql)) { 70 | throw new UnableToCreateSQLException('CREATE TABLE column-list subtree', $k, $v, 'expr_type'); 71 | } 72 | 73 | $sql .= " "; 74 | } 75 | return "(" . substr($sql, 0, -1) . ")"; 76 | } 77 | 78 | } 79 | ?> 80 | -------------------------------------------------------------------------------- /src/library/builders/CreateStatementBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: CreateStatementBuilder.php 930 2014-01-08 13:07:55Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/LikeBuilder.php'; 43 | require_once dirname(__FILE__) . '/SelectStatementBuilder.php'; 44 | require_once dirname(__FILE__) . '/CreateBuilder.php'; 45 | 46 | /** 47 | * This class implements the builder for the whole Create statement. You can overwrite 48 | * all functions to achieve another handling. 49 | * 50 | * @author André Rothe 51 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 52 | * 53 | */ 54 | class CreateStatementBuilder { 55 | 56 | protected function buildLIKE($parsed) { 57 | $builder = new LikeBuilder(); 58 | return $builder->build($parsed); 59 | } 60 | 61 | protected function buildSelectStatement($parsed) { 62 | $builder = new SelectStatementBuilder(); 63 | return $builder->build($parsed); 64 | } 65 | 66 | protected function buildCREATE($parsed) { 67 | $builder = new CreateBuilder(); 68 | return $builder->build($parsed); 69 | } 70 | 71 | public function build($parsed) { 72 | $sql = $this->buildCREATE($parsed); 73 | if (isset($parsed['LIKE'])) { 74 | $sql .= " " . $this->buildLIKE($parsed['LIKE']); 75 | } 76 | if (isset($parsed['SELECT'])) { 77 | $sql .= " " . $this->buildSelectStatement($parsed); 78 | } 79 | return $sql; 80 | } 81 | } 82 | ?> 83 | -------------------------------------------------------------------------------- /src/library/builders/InsertBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: InsertBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/ColumnReferenceBuilder.php'; 44 | 45 | /** 46 | * This class implements the builder for the [INSERT] statement parts. 47 | * You can overwrite all functions to achieve another handling. 48 | * 49 | * @author André Rothe 50 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 51 | * 52 | */ 53 | class InsertBuilder { 54 | 55 | protected function buildColRef($parsed) { 56 | $builder = new ColumnReferenceBuilder(); 57 | return $builder->build($parsed); 58 | } 59 | 60 | public function build($parsed) { 61 | $sql = "INSERT INTO " . $parsed['table']; 62 | 63 | if ($parsed['columns'] === false) { 64 | return $sql; 65 | } 66 | 67 | $columns = ""; 68 | foreach ($parsed['columns'] as $k => $v) { 69 | $len = strlen($columns); 70 | $columns .= $this->buildColRef($v); 71 | 72 | if ($len == strlen($columns)) { 73 | throw new UnableToCreateSQLException('INSERT[columns]', $k, $v, 'expr_type'); 74 | } 75 | 76 | $columns .= ","; 77 | } 78 | 79 | if ($columns !== "") { 80 | $columns = " (" . substr($columns, 0, -1) . ")"; 81 | } 82 | 83 | $sql .= $columns; 84 | return $sql; 85 | } 86 | 87 | } 88 | ?> 89 | -------------------------------------------------------------------------------- /src/library/processors/SelectProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/SelectExpressionProcessor.php'); 34 | 35 | /** 36 | * 37 | * This class processes the SELECT statements. 38 | * 39 | * @author arothe 40 | * 41 | */ 42 | class SelectProcessor extends SelectExpressionProcessor { 43 | 44 | public function process($tokens) { 45 | $expression = ""; 46 | $expressionList = array(); 47 | foreach ($tokens as $token) { 48 | if ($this->isCommaToken($token)) { 49 | $expression = parent::process(trim($expression)); 50 | $expression['delim'] = ','; 51 | $expressionList[] = $expression; 52 | $expression = ""; 53 | } else { 54 | switch (strtoupper($token)) { 55 | 56 | // add more SELECT options here 57 | case 'DISTINCT': 58 | case 'DISTINCTROW': 59 | case 'HIGH_PRIORITY': 60 | case 'SQL_CACHE': 61 | case 'SQL_NO_CACHE': 62 | case 'SQL_CALC_FOUND_ROWS': 63 | case 'STRAIGHT_JOIN': 64 | case 'SQL_SMALL_RESULT': 65 | case 'SQL_BIG_RESULT': 66 | case 'SQL_BUFFER_RESULT': 67 | $expression = parent::process(trim($token)); 68 | $expression['delim'] = ' '; 69 | $expressionList[] = $expression; 70 | $expression = ""; 71 | break; 72 | 73 | default: 74 | $expression .= $token; 75 | } 76 | } 77 | } 78 | if ($expression) { 79 | $expression = parent::process(trim($expression)); 80 | $expression['delim'] = false; 81 | $expressionList[] = $expression; 82 | } 83 | return $expressionList; 84 | } 85 | } 86 | ?> 87 | -------------------------------------------------------------------------------- /src/library/builders/CreateTableBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: CreateTableBuilder.php 892 2013-12-31 00:21:33Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/CreateTableDefinitionBuilder.php'; 44 | require_once dirname(__FILE__) . '/CreateTableSelectOptionBuilder.php'; 45 | require_once dirname(__FILE__) . '/CreateTableOptionsBuilder.php'; 46 | 47 | /** 48 | * This class implements the builder for the CREATE TABLE statement. You can overwrite 49 | * all functions to achieve another handling. 50 | * 51 | * @author André Rothe 52 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 53 | * 54 | */ 55 | class CreateTableBuilder { 56 | 57 | protected function buildCreateTableDefinition($parsed) { 58 | $builder = new CreateTableDefinitionBuilder(); 59 | return $builder->build($parsed); 60 | } 61 | 62 | protected function buildCreateTableOptions($parsed) { 63 | $builder = new CreateTableOptionsBuilder(); 64 | return $builder->build($parsed); 65 | } 66 | 67 | protected function buildCreateTableSelectOption($parsed) { 68 | $builder = new CreateTableSelectOptionBuilder(); 69 | return $builder->build($parsed); 70 | } 71 | 72 | public function build($parsed) { 73 | $sql = $parsed['name']; 74 | $sql .= $this->buildCreateTableDefinition($parsed); 75 | $sql .= $this->buildCreateTableOptions($parsed); 76 | $sql .= $this->buildCreateTableSelectOption($parsed); 77 | return $sql; 78 | } 79 | 80 | } 81 | ?> 82 | -------------------------------------------------------------------------------- /src/library/builders/IndexTypeBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: IndexTypeBuilder.php 910 2014-01-08 10:46:12Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 44 | require_once dirname(__FILE__) . '/ColumnListBuilder.php'; 45 | require_once dirname(__FILE__) . '/ConstraintBuilder.php'; 46 | require_once dirname(__FILE__) . '/ReservedBuilder.php'; 47 | require_once dirname(__FILE__) . '/IndexTypeBuilder.php'; 48 | 49 | /** 50 | * This class implements the builder for the index type of a PRIMARY KEY 51 | * statement part of CREATE TABLE. 52 | * You can overwrite all functions to achieve another handling. 53 | * 54 | * @author André Rothe 55 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 56 | * 57 | */ 58 | class IndexTypeBuilder { 59 | 60 | protected function buildReserved($parsed) { 61 | $builder = new ReservedBuilder(); 62 | return $builder->build($parsed); 63 | } 64 | 65 | public function build($parsed) { 66 | if ($parsed['expr_type'] !== ExpressionType::INDEX_TYPE) { 67 | return ""; 68 | } 69 | $sql = ""; 70 | foreach ($parsed['sub_tree'] as $k => $v) { 71 | $len = strlen($sql); 72 | $sql .= $this->buildReserved($v); 73 | 74 | if ($len == strlen($sql)) { 75 | throw new UnableToCreateSQLException('CREATE TABLE primary key index type subtree', $k, $v, 'expr_type'); 76 | } 77 | 78 | $sql .= " "; 79 | } 80 | return substr($sql, 0, -1); 81 | } 82 | } 83 | ?> 84 | -------------------------------------------------------------------------------- /src/library/builders/FromBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: FromBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/TableBuilder.php'; 44 | require_once dirname(__FILE__) . '/TableExpressionBuilder.php'; 45 | require_once dirname(__FILE__) . '/SubQueryBuilder.php'; 46 | 47 | /** 48 | * This class implements the builder for the [FROM] part. You can overwrite 49 | * all functions to achieve another handling. 50 | * 51 | * @author André Rothe 52 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 53 | * 54 | */ 55 | class FromBuilder { 56 | 57 | protected function buildTable($parsed, $key) { 58 | $builder = new TableBuilder(); 59 | return $builder->build($parsed, $key); 60 | } 61 | 62 | protected function buildTableExpression($parsed, $key) { 63 | $builder = new TableExpressionBuilder(); 64 | return $builder->build($parsed, $key); 65 | } 66 | 67 | protected function buildSubQuery($parsed, $key) { 68 | $builder = new SubQueryBuilder(); 69 | return $builder->build($parsed, $key); 70 | } 71 | 72 | public function build($parsed) { 73 | $sql = ""; 74 | foreach ($parsed as $k => $v) { 75 | $len = strlen($sql); 76 | $sql .= $this->buildTable($v, $k); 77 | $sql .= $this->buildTableExpression($v, $k); 78 | $sql .= $this->buildSubquery($v, $k); 79 | 80 | if ($len == strlen($sql)) { 81 | throw new UnableToCreateSQLException('FROM', $k, $v, 'expr_type'); 82 | } 83 | } 84 | return "FROM " . $sql; 85 | } 86 | } 87 | ?> 88 | -------------------------------------------------------------------------------- /src/library/builders/OrderByBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: OrderByBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/OrderByAliasBuilder.php'; 44 | require_once dirname(__FILE__) . '/ColumnReferenceBuilder.php'; 45 | 46 | /** 47 | * This class implements the builder for the ORDER-BY clause. 48 | * You can overwrite all functions to achieve another handling. 49 | * 50 | * @author André Rothe 51 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 52 | * 53 | */ 54 | class OrderByBuilder { 55 | 56 | protected function buildFunction($parsed) { 57 | $builder = new FunctionBuilder(); 58 | return $builder->build($parsed); 59 | } 60 | 61 | protected function buildColRef($parsed) { 62 | $builder = new ColumnReferenceBuilder(); 63 | return $builder->build($parsed); 64 | } 65 | 66 | protected function buildOrderByAlias($parsed) { 67 | $builder = new OrderByAliasBuilder(); 68 | return $builder->build($parsed); 69 | } 70 | 71 | public function build($parsed) { 72 | $sql = ""; 73 | foreach ($parsed as $k => $v) { 74 | $len = strlen($sql); 75 | $sql .= $this->buildOrderByAlias($v); 76 | $sql .= $this->buildColRef($v); 77 | $sql .= $this->buildFunction($v); 78 | 79 | if ($len == strlen($sql)) { 80 | throw new UnableToCreateSQLException('ORDER', $k, $v, 'expr_type'); 81 | } 82 | 83 | $sql .= ", "; 84 | } 85 | $sql = substr($sql, 0, -2); 86 | return "ORDER BY " . $sql; 87 | } 88 | } 89 | ?> 90 | -------------------------------------------------------------------------------- /src/library/builders/CheckBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: CheckBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/ReservedBuilder.php'; 44 | require_once dirname(__FILE__) . '/SelectBracketExpressionBuilder.php'; 45 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 46 | /** 47 | * This class implements the builder for the CHECK statement part of CREATE TABLE. 48 | * You can overwrite all functions to achieve another handling. 49 | * 50 | * @author André Rothe 51 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 52 | * 53 | */ 54 | class CheckBuilder { 55 | 56 | protected function buildSelectBracketExpression($parsed) { 57 | $builder = new SelectBracketExpressionBuilder(); 58 | return $builder->build($parsed); 59 | } 60 | 61 | protected function buildReserved($parsed) { 62 | $builder = new ReservedBuilder(); 63 | return $builder->build($parsed); 64 | } 65 | 66 | public function build($parsed) { 67 | if ($parsed['expr_type'] !== ExpressionType::CHECK) { 68 | return ""; 69 | } 70 | $sql = ""; 71 | foreach ($parsed['sub_tree'] as $k => $v) { 72 | $len = strlen($sql); 73 | $sql .= $this->buildReserved($v); 74 | $sql .= $this->buildSelectBracketExpression($v); 75 | 76 | if ($len == strlen($sql)) { 77 | throw new UnableToCreateSQLException('CREATE TABLE check subtree', $k, $v, 'expr_type'); 78 | } 79 | 80 | $sql .= " "; 81 | } 82 | return substr($sql, 0, -1); 83 | } 84 | } 85 | ?> 86 | -------------------------------------------------------------------------------- /src/library/processors/CreateProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once dirname(__FILE__) . '/AbstractProcessor.php'; 34 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 35 | 36 | /** 37 | * 38 | * This class processes the CREATE statements. 39 | * 40 | * @author arothe 41 | * 42 | */ 43 | class CreateProcessor extends AbstractProcessor { 44 | 45 | public function process($tokens) { 46 | $result = array(); 47 | $base_expr = ""; 48 | 49 | foreach ($tokens as $token) { 50 | $trim = strtoupper(trim($token)); 51 | $base_expr .= $token; 52 | 53 | if ($trim === "") { 54 | continue; 55 | } 56 | 57 | switch ($trim) { 58 | 59 | case 'TEMPORARY': 60 | $result['expr_type'] = ExpressionType::TEMPORARY_TABLE; 61 | $result['not-exists'] = false; 62 | $expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim); 63 | break; 64 | 65 | case 'TABLE': 66 | $result['expr_type'] = ExpressionType::TABLE; 67 | $result['not-exists'] = false; 68 | $expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim); 69 | break; 70 | 71 | case 'IF': 72 | $expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim); 73 | break; 74 | 75 | case 'NOT': 76 | $expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim); 77 | break; 78 | 79 | case 'EXISTS': 80 | $result['not-exists'] = true; 81 | $expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim); 82 | break; 83 | 84 | default: 85 | break; 86 | } 87 | } 88 | $result['base_expr'] = trim($base_expr); 89 | $result['sub_tree'] = $expr; 90 | return $result; 91 | } 92 | } 93 | ?> -------------------------------------------------------------------------------- /src/library/builders/ColumnDefinitionBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: ColumnDefinitionBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/ColumnReferenceBuilder.php'; 44 | require_once dirname(__FILE__) . '/ColumnTypeBuilder.php'; 45 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 46 | /** 47 | * This class implements the builder for the columndefinition statement part 48 | * of CREATE TABLE. You can overwrite all functions to achieve another handling. 49 | * 50 | * @author André Rothe 51 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 52 | * 53 | */ 54 | class ColumnDefinitionBuilder { 55 | 56 | protected function buildColRef($parsed) { 57 | $builder = new ColumnReferenceBuilder(); 58 | return $builder->build($parsed); 59 | } 60 | 61 | protected function buildColumnType($parsed) { 62 | $builder = new ColumnTypeBuilder(); 63 | return $builder->build($parsed); 64 | } 65 | 66 | public function build($parsed) { 67 | if ($parsed['expr_type'] !== ExpressionType::COLDEF) { 68 | return ""; 69 | } 70 | $sql = ""; 71 | foreach ($parsed['sub_tree'] as $k => $v) { 72 | $len = strlen($sql); 73 | $sql .= $this->buildColRef($v); 74 | $sql .= $this->buildColumnType($v); 75 | 76 | if ($len == strlen($sql)) { 77 | throw new UnableToCreateSQLException('CREATE TABLE primary key subtree', $k, $v, 'expr_type'); 78 | } 79 | 80 | $sql .= " "; 81 | } 82 | return substr($sql, 0, -1); 83 | } 84 | } 85 | ?> 86 | -------------------------------------------------------------------------------- /src/library/builders/GroupByBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: GroupByBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/PositionBuilder.php'; 44 | require_once dirname(__FILE__) . '/ColumnReferenceBuilder.php'; 45 | require_once dirname(__FILE__) . '/FunctionBuilder.php'; 46 | 47 | /** 48 | * This class implements the builder for the GROUP-BY clause. 49 | * You can overwrite all functions to achieve another handling. 50 | * 51 | * @author André Rothe 52 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 53 | * 54 | */ 55 | class GroupByBuilder { 56 | 57 | protected function buildColRef($parsed) { 58 | $builder = new ColumnReferenceBuilder(); 59 | return $builder->build($parsed); 60 | } 61 | 62 | protected function buildPosition($parsed) { 63 | $builder = new PositionBuilder(); 64 | return $builder->build($parsed); 65 | } 66 | 67 | protected function buildFunction($parsed) { 68 | $builder = new FunctionBuilder(); 69 | return $builder->build($parsed); 70 | } 71 | 72 | public function build($parsed) { 73 | $sql = ""; 74 | foreach ($parsed as $k => $v) { 75 | $len = strlen($sql); 76 | $sql .= $this->buildColRef($v); 77 | $sql .= $this->buildPosition($v); 78 | $sql .= $this->buildFunction($v); 79 | 80 | if ($len == strlen($sql)) { 81 | throw new UnableToCreateSQLException('GROUP', $k, $v, 'expr_type'); 82 | } 83 | 84 | $sql .= ", "; 85 | } 86 | $sql = substr($sql, 0, -2); 87 | return "GROUP BY " . $sql; 88 | } 89 | 90 | } 91 | ?> 92 | -------------------------------------------------------------------------------- /src/library/builders/LikeExpressionBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: LikeExpressionBuilder.php 906 2014-01-07 14:38:08Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | require_once dirname(__FILE__) . '/TableBuilder.php'; 44 | require_once dirname(__FILE__) . '/ReservedBuilder.php'; 45 | 46 | /** 47 | * This class implements the builder for the (LIKE) keyword within a 48 | * CREATE TABLE statement. There are difference to LIKE (without parenthesis), 49 | * the latter is a top-level element of the output array. 50 | * You can overwrite all functions to achieve another handling. 51 | * 52 | * @author André Rothe 53 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 54 | * 55 | */ 56 | class LikeExpressionBuilder { 57 | 58 | protected function buildTable($parsed, $index) { 59 | $builder = new TableBuilder(); 60 | return $builder->build($parsed, $index); 61 | } 62 | 63 | protected function buildReserved($parsed) { 64 | $builder = new ReservedBuilder(); 65 | return $builder->build($parsed); 66 | } 67 | 68 | public function build($parsed) { 69 | if ($parsed['expr_type'] !== ExpressionType::LIKE) { 70 | return ""; 71 | } 72 | $sql = ""; 73 | foreach ($parsed['sub_tree'] as $k => $v) { 74 | $len = strlen($sql); 75 | $sql .= $this->buildReserved($v); 76 | $sql .= $this->buildTable($v, 0); 77 | 78 | if ($len == strlen($sql)) { 79 | throw new UnableToCreateSQLException('CREATE TABLE create-def (like) subtree', $k, $v, 'expr_type'); 80 | } 81 | 82 | $sql .= " "; 83 | } 84 | return substr($sql, 0, -1); 85 | } 86 | } 87 | ?> 88 | -------------------------------------------------------------------------------- /src/library/builders/RefClauseBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: RefClauseBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/ColumnReferenceBuilder.php'; 44 | require_once dirname(__FILE__) . '/OperatorBuilder.php'; 45 | require_once dirname(__FILE__) . '/ConstantBuilder.php'; 46 | 47 | /** 48 | * This class implements the references clause within a JOIN. 49 | * You can overwrite all functions to achieve another handling. 50 | * 51 | * @author André Rothe 52 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 53 | * 54 | */ 55 | class RefClauseBuilder { 56 | 57 | protected function buildColRef($parsed) { 58 | $builder = new ColumnReferenceBuilder(); 59 | return $builder->build($parsed); 60 | } 61 | 62 | protected function buildOperator($parsed) { 63 | $builder = new OperatorBuilder(); 64 | return $builder->build($parsed); 65 | } 66 | 67 | protected function buildConstant($parsed) { 68 | $builder = new ConstantBuilder(); 69 | return $builder->build($parsed); 70 | } 71 | 72 | public function build($parsed) { 73 | if ($parsed === false) { 74 | return ""; 75 | } 76 | $sql = ""; 77 | foreach ($parsed as $k => $v) { 78 | $len = strlen($sql); 79 | $sql .= $this->buildColRef($v); 80 | $sql .= $this->buildOperator($v); 81 | $sql .= $this->buildConstant($v); 82 | 83 | if ($len == strlen($sql)) { 84 | throw new UnableToCreateSQLException('expression ref_clause', $k, $v, 'expr_type'); 85 | } 86 | 87 | $sql .= " "; 88 | } 89 | return "(" . substr($sql, 0, -1) . ")"; 90 | } 91 | } 92 | ?> 93 | -------------------------------------------------------------------------------- /src/library/builders/IndexSizeBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: IndexSizeBuilder.php 918 2014-01-08 11:48:30Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 44 | require_once dirname(__FILE__) . '/ColumnListBuilder.php'; 45 | require_once dirname(__FILE__) . '/ConstraintBuilder.php'; 46 | require_once dirname(__FILE__) . '/ReservedBuilder.php'; 47 | require_once dirname(__FILE__) . '/IndexTypeBuilder.php'; 48 | 49 | /** 50 | * This class implements the builder for the index size of a PRIMARY KEY 51 | * statement part of CREATE TABLE. 52 | * You can overwrite all functions to achieve another handling. 53 | * 54 | * @author André Rothe 55 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 56 | * 57 | */ 58 | class IndexSizeBuilder { 59 | 60 | protected function buildReserved($parsed) { 61 | $builder = new ReservedBuilder(); 62 | return $builder->build($parsed); 63 | } 64 | 65 | protected function buildConstant($parsed) { 66 | $builder = new ConstantBuilder(); 67 | return $builder->build($parsed); 68 | } 69 | 70 | public function build($parsed) { 71 | if ($parsed['expr_type'] !== ExpressionType::INDEX_SIZE) { 72 | return ""; 73 | } 74 | $sql = ""; 75 | foreach ($parsed['sub_tree'] as $k => $v) { 76 | $len = strlen($sql); 77 | $sql .= $this->buildReserved($v); 78 | $sql .= $this->buildConstant($v); 79 | 80 | if ($len == strlen($sql)) { 81 | throw new UnableToCreateSQLException('CREATE TABLE primary key index size subtree', $k, $v, 'expr_type'); 82 | } 83 | 84 | $sql .= " "; 85 | } 86 | return substr($sql, 0, -1); 87 | } 88 | } 89 | ?> 90 | -------------------------------------------------------------------------------- /src/library/builders/TableBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: TableBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | require_once dirname(__FILE__) . '/AliasBuilder.php'; 44 | require_once dirname(__FILE__) . '/JoinBuilder.php'; 45 | require_once dirname(__FILE__) . '/RefTypeBuilder.php'; 46 | require_once dirname(__FILE__) . '/RefClauseBuilder.php'; 47 | 48 | /** 49 | * This class implements the builder for the table name and join options. 50 | * You can overwrite all functions to achieve another handling. 51 | * 52 | * @author André Rothe 53 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 54 | * 55 | */ 56 | class TableBuilder { 57 | 58 | protected function buildAlias($parsed) { 59 | $builder = new AliasBuilder(); 60 | return $builder->build($parsed); 61 | } 62 | 63 | protected function buildJoin($parsed) { 64 | $builder = new JoinBuilder(); 65 | return $builder->build($parsed); 66 | } 67 | 68 | protected function buildRefType($parsed) { 69 | $builder = new RefTypeBuilder(); 70 | return $builder->build($parsed); 71 | } 72 | 73 | protected function buildRefClause($parsed) { 74 | $builder = new RefClauseBuilder(); 75 | return $builder->build($parsed); 76 | } 77 | 78 | public function build($parsed, $index) { 79 | if ($parsed['expr_type'] !== ExpressionType::TABLE) { 80 | return ""; 81 | } 82 | 83 | $sql = $parsed['table']; 84 | $sql .= $this->buildAlias($parsed); 85 | 86 | if ($index !== 0) { 87 | $sql = $this->buildJoin($parsed['join_type']) . $sql; 88 | $sql .= $this->buildRefType($parsed['ref_type']); 89 | $sql .= $this->buildRefClause($parsed['ref_clause']); 90 | } 91 | return $sql; 92 | } 93 | } 94 | ?> 95 | -------------------------------------------------------------------------------- /src/library/builders/IndexParserBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: IndexParserBuilder.php 918 2014-01-08 11:48:30Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 43 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 44 | require_once dirname(__FILE__) . '/ColumnListBuilder.php'; 45 | require_once dirname(__FILE__) . '/ConstraintBuilder.php'; 46 | require_once dirname(__FILE__) . '/ReservedBuilder.php'; 47 | require_once dirname(__FILE__) . '/IndexTypeBuilder.php'; 48 | 49 | /** 50 | * This class implements the builder for the index parser of a PRIMARY KEY 51 | * statement part of CREATE TABLE. 52 | * You can overwrite all functions to achieve another handling. 53 | * 54 | * @author André Rothe 55 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 56 | * 57 | */ 58 | class IndexParserBuilder { 59 | 60 | protected function buildReserved($parsed) { 61 | $builder = new ReservedBuilder(); 62 | return $builder->build($parsed); 63 | } 64 | 65 | protected function buildConstant($parsed) { 66 | $builder = new ConstantBuilder(); 67 | return $builder->build($parsed); 68 | } 69 | 70 | public function build($parsed) { 71 | if ($parsed['expr_type'] !== ExpressionType::INDEX_PARSER) { 72 | return ""; 73 | } 74 | $sql = ""; 75 | foreach ($parsed['sub_tree'] as $k => $v) { 76 | $len = strlen($sql); 77 | $sql .= $this->buildReserved($v); 78 | $sql .= $this->buildConstant($v); 79 | 80 | if ($len == strlen($sql)) { 81 | throw new UnableToCreateSQLException('CREATE TABLE primary key index parser subtree', $k, $v, 'expr_type'); 82 | } 83 | 84 | $sql .= " "; 85 | } 86 | return substr($sql, 0, -1); 87 | } 88 | } 89 | ?> 90 | -------------------------------------------------------------------------------- /src/library/processors/IndexColumnListProcessor.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * * Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 30 | * DAMAGE. 31 | */ 32 | 33 | require_once(dirname(__FILE__) . '/AbstractProcessor.php'); 34 | require_once(dirname(__FILE__) . '/../utils/ExpressionType.php'); 35 | 36 | /** 37 | * 38 | * This class processes the index column lists. 39 | * 40 | * @author arothe 41 | * 42 | */ 43 | class IndexColumnListProcessor extends AbstractProcessor { 44 | 45 | protected function initExpression() { 46 | return array('name' => false, 'no_quotes' => false, 'length' => false, 'dir' => false); 47 | } 48 | 49 | public function process($sql) { 50 | $tokens = $this->splitSQLIntoTokens($sql); 51 | 52 | $expr = $this->initExpression(); 53 | $result = array(); 54 | $base_expr = ""; 55 | 56 | foreach ($tokens as $k => $token) { 57 | 58 | $trim = trim($token); 59 | $base_expr .= $token; 60 | 61 | if ($trim === "") { 62 | continue; 63 | } 64 | 65 | $upper = strtoupper($trim); 66 | 67 | switch ($upper) { 68 | 69 | case 'ASC': 70 | case 'DESC': 71 | # the optional order 72 | $expr['dir'] = $trim; 73 | break; 74 | 75 | case ',': 76 | # the next column 77 | $result[] = array_merge(array('expr_type' => ExpressionType::INDEX_COLUMN, 'base_expr' => $base_expr), 78 | $expr); 79 | $expr = $this->initExpression(); 80 | $base_expr = ""; 81 | break; 82 | 83 | default: 84 | if ($upper[0] === '(' && substr($upper, -1) === ')') { 85 | # the optional length 86 | $expr['length'] = $this->removeParenthesisFromStart($trim); 87 | continue 2; 88 | } 89 | # the col name 90 | $expr['name'] = $trim; 91 | $expr['no_quotes'] = $this->revokeQuotation($trim); 92 | break; 93 | } 94 | } 95 | $result[] = array_merge(array('expr_type' => ExpressionType::INDEX_COLUMN, 'base_expr' => $base_expr), $expr); 96 | return $result; 97 | } 98 | } 99 | ?> -------------------------------------------------------------------------------- /src/library/builders/RecordBuilder.php: -------------------------------------------------------------------------------- 1 | 36 | * @copyright 2010-2014 Justin Swanhart and André Rothe 37 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 38 | * @version SVN: $Id: RecordBuilder.php 830 2013-12-18 09:35:42Z phosco@gmx.de $ 39 | * 40 | */ 41 | 42 | require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; 43 | require_once dirname(__FILE__) . '/../utils/ExpressionType.php'; 44 | require_once dirname(__FILE__) . '/OperatorBuilder.php'; 45 | require_once dirname(__FILE__) . '/ConstantBuilder.php'; 46 | require_once dirname(__FILE__) . '/FunctionBuilder.php'; 47 | 48 | /** 49 | * This class implements the builder for the records within INSERT statement. 50 | * You can overwrite all functions to achieve another handling. 51 | * 52 | * @author André Rothe 53 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 54 | * 55 | */ 56 | class RecordBuilder { 57 | 58 | protected function buildOperator($parsed) { 59 | $builder = new OperatorBuilder(); 60 | return $builder->build($parsed); 61 | } 62 | 63 | protected function buildFunction($parsed) { 64 | $builder = new FunctionBuilder(); 65 | return $builder->build($parsed); 66 | } 67 | 68 | protected function buildConstant($parsed) { 69 | $builder = new ConstantBuilder(); 70 | return $builder->build($parsed); 71 | } 72 | 73 | public function build($parsed) { 74 | if ($parsed['expr_type'] !== ExpressionType::RECORD) { 75 | return ""; 76 | } 77 | $sql = ""; 78 | foreach ($parsed['data'] as $k => $v) { 79 | $len = strlen($sql); 80 | $sql .= $this->buildConstant($v); 81 | $sql .= $this->buildFunction($v); 82 | $sql .= $this->buildOperator($v); 83 | 84 | if ($len == strlen($sql)) { 85 | throw new UnableToCreateSQLException(ExpressionType::RECORD, $k, $v, 'expr_type'); 86 | } 87 | 88 | $sql .= ","; 89 | } 90 | $sql = substr($sql, 0, -1); 91 | return "(" . $sql . ")"; 92 | } 93 | 94 | } 95 | ?> 96 | --------------------------------------------------------------------------------