PHP Classes

Light PHP SQL Parser Class: Parse SQL to get query type, tables, field values

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 237 This week: 1All time: 8,056 This week: 560Up
Version License PHP version Categories
lightsqlparser 0.1.96Free for non-comm...5PHP 5, Databases, Parsers
Collaborate with this project 

Author

Description

This class can parse SQL to get query type, tables, field values, etc..

It takes an string with a SQL statements and parses it to extract its different components.

Currently the class can extract the SQL query type (SELECT, INSERT, UPDATE, DELETE), the names of the tables involved in the query and the field values that are passed as parameters.

Picture of Marco Cesarato
  Performance   Level  
Name: Marco Cesarato is available for providing paid consulting. Contact Marco Cesarato .
Classes: 9 packages by
Country: Italy Italy
Age: 28
All time rank: 133654 in Italy Italy
Week rank: 416 Up16 in Italy Italy Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php
include('lightsqlparser.class.php');

header("Content-Type: text/plain");

echo
'========= Light SQL Parser DEMO =========' . PHP_EOL;

echo
PHP_EOL . '### UPDATE ###' . PHP_EOL;

$lsp = new LightSQLParser("UPDATE Customers as ae
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;"
);

// OR

/*
$lsp = new LightSQLParser();
$lsp->setQuery("UPDATE Customers as ae
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;");
*/

echo PHP_EOL . 'METHOD' . PHP_EOL;
var_dump($lsp->method());

echo
PHP_EOL . 'TABLES' . PHP_EOL;
var_dump($lsp->tables());

echo
PHP_EOL . 'FIELDS' . PHP_EOL;
var_dump($lsp->fields());

echo
PHP_EOL . '### SELECT ###' . PHP_EOL;

$lsp->setQuery("SELECT surname, given_names, title FROM Person
  JOIN Author on person.ID = Author.personID
  JOIN Book on Book.ID = Author.publicationID
UNION ALL
SELECT surname, given_names, title FROM Person
  JOIN Author on person.ID = Author.personID
  JOIN Article on Article.ID = Author.publicationID"
);

echo
PHP_EOL . 'METHOD' . PHP_EOL;
var_dump($lsp->method());

echo
PHP_EOL . 'TABLES' . PHP_EOL;
var_dump($lsp->tables());

echo
PHP_EOL . 'FIELDS' . PHP_EOL;
var_dump($lsp->fields());

echo
PHP_EOL . '### INSERT ###' . PHP_EOL;

$lsp->setQuery("INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');"
);

echo
PHP_EOL . 'METHOD' . PHP_EOL;
var_dump($lsp->method());

echo
PHP_EOL . 'TABLES' . PHP_EOL;
var_dump($lsp->tables());

echo
PHP_EOL . 'FIELDS' . PHP_EOL;
var_dump($lsp->fields());


Details

PHP Light SQL Parser Class

Version: 0.1.97 beta

Github: https://github.com/marcocesarato/PHP-Light-SQL-Parser-Class

Author: Marco Cesarato

Description

This class can parse SQL to get query type, tables, field values, etc..

It takes an string with a SQL statements and parses it to extract its different components.

Currently the class can extract the SQL query method, the names of the tables involved in the query and the field values that are passed as parameters. This parser is pretty light respect phpsqlparser or others php sql parser.

Requirements

  • php 4+

Install

Composer

  1. Install composer
  2. Type `composer require marcocesarato/sqlparser`
  3. Enjoy

Usage

$lsp = new LightSQLParser("UPDATE Customers as ae SET ContactName = 'Alfred Schmidt', City= 'Frankfurt' WHERE CustomerID = 1;");

OR

$lsp = new LightSQLParser();
$lsp->setQuery("UPDATE Customers as ae SET ContactName = 'Alfred Schmidt', City= 'Frankfurt' WHERE CustomerID = 1;");

Method

How retrieve query method:

$lsp->method();

Output

string(6) "UPDATE"

Tables

How retrieve query tables:

$lsp->tables();

Output

array(1) {
  [0]=>
  string(9) "Customers"
}

Fields

How retrieve query fields:

$lsp->fields();

Output

array(2) {
  [0]=>
  string(11) "ContactName"
  [1]=>
  string(4) "City"
}

Methods

LightSQLParser

| Method | Parameters | Description | | ----------- | ----------------------------------- | -------------------------------------------------- | | __construct | | Constructor | | setQuery | | Set SQL Query string | | method | param $query<br> return string | Get SQL Query method | | fields | param $query<br> return array | Get Query fields (at the moment only SELECTINSERTUPDATE) | | table | param $query<br> return string | Get SQL Query First Table | | tables | return array | Get SQL Query Tables |


  Files folder image Files  
File Role Description
Files folder imageDemo (1 file)
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file demo.php Example Demo
Accessible without login Plain text file LICENSE Lic. License text
Plain text file lightsqlparser.class.php Class Main Class
Accessible without login Plain text file README.md Doc. README

  Files folder image Files  /  Demo  
File Role Description
  Accessible without login Plain text file demo.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file LightSQLParser.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 71%
Total:237
This week:1
All time:8,056
This week:560Up
User Comments (1)