PHP Classes

SQL Parse and Compile: Parse and compose SQL queries programatically

Recommend this page to a friend!
  Info   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 54%Total: 1,868 All time: 2,113 This week: 455Up
Version License PHP version Categories
parsecompile 0.3Artistic License5.0PHP 5, Databases, Text processing
Description 

Author

This package can be used to parse and compose SQL queries programatically.

It can take an SQL query and parse it to extract the different parts of the query like the type of command, fields, tables, conditions, etc..

It can also be used to do the opposite, i.e. compose SQL queries from values that define each part of the query.


Features:
I. Parser
- insert
- replace
- update
- delete
- select
- union
- subselect
- recognizes flow control function (IF, CASE - WHEN - THEN)
- recognition of many sql functions

II. Composer (Compiler)
- insert
- replace
- update
- delete
- select
- union

III. Wrapper SQL
- object oriented writing of SQL statements from the scratch

i.e.:
#################################################
$insertObject = new Sql();
$insertObject
->setCommand("insert")
->addTableNames("employees")
->addColumnNames(array("LastName","FirstName"))
->addValues(
array(
array("Value"=>"Davolio","Type"=>"text_val"),
array("Value"=>"Nancy","Type"=>"text_val"),
)
);
$sqlout = $insertObject->compile();
#################################################

result:
echo $sqlout;
#################################################
INSERT INTO employees (LastName, FirstName) VALUES ('Davolio', 'Nancy')
#################################################



#################################################
a more advanced example:
#################################################

$sql = 'SELECT
countrylanguage.CountryCode,
country.Name,
country.Continent,
country.Region,
country.SurfaceArea,
city.District,
country.IndepYear,
country.Population,
city.CountryCode,
city.Name,
city.Population,
countrylanguage.Language AS lang,
countrylanguage.IsOfficial,
countrylanguage.Percentage,
country.GovernmentForm,
country.LocalName,
country.GNPOld,
country.GNP,
country.LifeExpectancy
FROM
country co
LEFT JOIN city ct ON co.Code = ct.CountryCode
LEFT JOIN countrylanguage cl ON cl.CountryCode = ct.CountryCode
WHERE
(co.Continent='Asia' AND cl.Language='Pashto')
GROUP BY
co.Name
HAVING
ct.CountryCode = 'AFG'
LIMIT 100
':

// using wrapper class
$sqlDef = new Sql();
$sqlDef->parse($sql);

// adding a left join
$sqlDef->setJoinLeft(
array(
'Left'=> array("Value"=>"employees.employeeID", "Type" => "ident"),
'Op'=> '=',
'Right'=> array("Value"=>1, "Type" => "int_val"),
)
);

$sqlout = $sqlDef->compile();
#################################################

result:

echo $sqlout;
#################################################
SELECT countrylanguage.CountryCode, country.Name, country.Continent, country.Region, country.SurfaceArea, city.District, country.IndepYear, country.Population, city.CountryCode, city.Name, city.Population, countrylanguage.Language AS lang, countrylanguage.IsOfficial, countrylanguage.Percentage, country.GovernmentForm, country.LocalName, country.GNPOld, country.GNP, country.LifeExpectancy
FROM country AS co
LEFT JOIN city AS ct ON co.Code = ct.CountryCode
LEFT JOIN countrylanguage AS cl ON cl.CountryCode = ct.CountryCode
LEFT JOIN employees ON employees.employeeID = 1
WHERE (co.Continent = 'Asia' and cl.Language = 'Pashto')
GROUP BY co.Name
HAVING ct.CountryCode = 'AFG'
LIMIT 0,100
#################################################

IV. Hint
Sql_Compiler is none validating, but throws errors on type mismatch or corrupt statements


Note:
If you have good ideas to improve this set of classes, let me know.

Acknowledgement:
Thanks a lot to George Antoniadis (Author: rephp framework, www.rephp.net) from noodles.gr for his qualified feedback.

Many thanks to all, which gave me qualified feedback and voted for this set of classes at the contest.

Innovation Award
PHP Programming Innovation award nominee
December 2008
Number 2


Prize: One copy of VS.PHP
Programmatically composing simple SQL queries is a relatively easy task. However, more complex queries are harder to compose.

This class provides a solution that not only simplifies the composition of complex queries using a fluent interface, but it can also parse, edit and rewrite predefined SQL queries.

Manuel Lemos
Picture of Tom Schaefer
  Performance   Level  
Innovation award
Innovation award
Nominee: 9x

Winner: 1x

 

Screenshots (2)  
  • sql01.png
  • sql02.png
  Files folder image Files (36)  
File Role Description
Files folder imagesamples (9 files)
Files folder imageSql_Parser (8 files)
Files folder imageSql_Compiler (8 files)
Files folder imageSql_Dialect (1 file)
Files folder imageSql_Interface (2 files)
Accessible without login Plain text file autoload.php Aux. autoload function
Plain text file Sql.class.php Class Wrapper class for parsing and compiling sql, adding joins, condition etc.
Plain text file Sql_Compiler.class.php Class compiler class
Plain text file Sql_Lexer.class.php Class tokenizes a sql string
Plain text file Sql_Object.class.php Class sql object class (singleton)
Plain text file Sql_Parser.class.php Class parser class

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:1,868
This week:0
All time:2,113
This week:455Up
User Ratings User Comments (2)
 All time
Utility:75%StarStarStarStar
Consistency:87%StarStarStarStarStar
Documentation:-
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:54%StarStarStar
Rank:2063
 
Great.
13 years ago (Paweł Napier)
67%StarStarStarStar
That package seems to be cool, but .
14 years ago (Francois Biot)
32%StarStar