SlideShare a Scribd company logo
1 of 4
<html>
<style>
body {
font-family: Arial;
}
.annotation {
color: gray;
}
</style>
<body>
<?php
/* SAC: SqlAutoCommannotator: Structured Query Language Automatic
Comment Annotator
Sharon Xiao Liu
Assumes all SQL given to it is valid. For MySQL to natural language.
Work in progress: 20150706-20150730.
*/
/* Use OOP. Loop through arrays. PHP split string by line break.
Add a text box.
Generate code by analysing each segment before ;. PCRE. Pattern
matching PHP. Nest pattern matching
statements. Match even if the spacing is inconsistent. Select
multiple columns.
Pattern matching database: AND,OR (always after SELECT) similar to
WHERE. First column: regex, depending on how many parentheses there
are (count number of open parentheses), extract this information,
format the final result.
Design UI.
Add 1 when meet open bracket, or you find a closed bracket
immediately after. Look through the rest of the SQL code. Make a
table of all types of translations for sac.
JQuery UI Tooltip.
*/
$sqlBlock = "SELECT * FROM Customers WHERE Country = 'USA';nSELECT
* FROM Suppliers;nSELECT ID, MiddleName FROM Customers;nSELECT *
FROM Suppliers WHERE House = '87' OR Credit = '90';nSELECT * FROM
Customers WHERE Country = 'Britain' AND Street = 'Belvedere' AND
(House = '13' OR (House = '789' AND Start = '20150706') AND (Contact
= 'Email' OR Contact = 'Phone'));";
class sac {
public function sacWorking ($sqlBlock) {
$sqlBlockTrimEnd = rtrim($sqlBlock, ";");
$sqlBlockExploded = explode(";", $sqlBlockTrimEnd);
$output = "";
function parseBrackets($str) {
$length = strlen($str);
$stack = array();
$result = array();
for($i=0; $i < $length; $i++) {
if($str[$i] == '(') {
$stack[] = $i;
}
if($str[$i] == ')') {
$open = array_pop($stack);
$result[] = substr($str,$open+1, $i-$open-1);
}
}
return $result;
}
function substitution ($afterNested) {
array_unshift($afterNested, "");
$counted = count($afterNested);
foreach ($afterNested as $key => $value) {
for ($i = $key+1; $i<$counted; $i++) {
if (str_replace ($value, $key,
$afterNested[$i]) == true) {
$afterNested[$i] = str_replace
($value, $key, $afterNested[$i]);
}
}
}
$arrayIntoString = "";
foreach ($afterNested as $key => $value) {
if ($key >= 1) {
$arrayIntoString .= $key. ". ". $value. "
";
}
}
$replaceEquals = preg_replace
("/b(w+)bs?=s?'(w+)'/", "the column $1 is $2",
$arrayIntoString);
$andOrPattern[0] = "/AND/";
$andOrPattern[1] = "/OR/";
$andOrReplacement[0] = "and";
$andOrReplacement[1] = "or";
$replaceAndOr = preg_replace ($andOrPattern,
$andOrReplacement, $replaceEquals);
return $replaceAndOr;
}
foreach ($sqlBlockExploded as $singleLine) {
$output .= htmlspecialchars($singleLine. ";",
ENT_QUOTES);
$output .= "<br><div class="annotation">";
if (preg_match("/SELECTs*/", $singleLine) == true)
{
$output .= "Select all columns ";
} elseif
(preg_match_all("/SELECTsb(w+)bsFROM/", $singleLine,
$oneAfterSelect, PREG_PATTERN_ORDER) == true) {
$output .= "Select the column ".
$oneAfterSelect[1][0];
} elseif (preg_match_all("/SELECTs(.*?)sFROM/",
$singleLine, $afterSelect, PREG_PATTERN_ORDER) == true) {
$output .= "Select the columns ";
$explodeAfterSelect = explode (",",
$afterSelect[1][0]);
foreach ($explodeAfterSelect as
$singleColumns) {
if ($singleColumns !=
end($explodeAfterSelect)) {
$output .= $singleColumns . ", ";
} else {
$output .= $singleColumns. " ";
}
}
}
if (preg_match_all("/FROMsb(w+)b/", $singleLine,
$afterFrom, PREG_PATTERN_ORDER) == true) {
$output .= "from the table ".
$afterFrom[1][0];
}
if
(preg_match_all("/WHEREsb(w+)bs?=s?'(w+)'/", $singleLine,
$afterWhere, PREG_PATTERN_ORDER) == true) {
$output .= " where the column ".
$afterWhere[1][0]. " is equal to ". $afterWhere[2][0];
}
if
(preg_match_all("/(AND|OR)sb(w+)bs?=s?'(w+)'/", $singleLine,
$afterAndNoBrackets, PREG_PATTERN_ORDER) == true) {
if ($afterAndNoBrackets[1][0] == "AND") {
$andOr = " and the column ";
} else {
$andOr = " or the column ";
}
$output .= $andOr. $afterAndNoBrackets[2][0].
" is equal to ". $afterAndNoBrackets[3][0];
}
if (preg_match_all("/(AND|OR)s((X+))$/",
$singleLine, $afterAnd, PREG_PATTERN_ORDER) == true) {
if ($afterAnd[1][0] == "AND") {
$andOrBracket = ", and ";
} else {
$andOrBracket = ", or ";
}
$processBrackets = parseBrackets
($afterAnd[2][0]);
$processAndOr =
substitution($processBrackets);
$output .= $andOrBracket. $processAndOr;
}
$output .= ".</div><br>";
}
return $output;
}
public function sacGenerating () {
$regexAnd = "/ANDsb(w+)bs?=s?&#039;(w+)&#039;/";
}
}
$obj = new sac;
echo $obj->sacWorking($sqlBlock);
/*run on XAMPP*/
?>
</body>
</html>

More Related Content

What's hot

Si0302 20140320131934
Si0302 20140320131934Si0302 20140320131934
Si0302 20140320131934
Dex Winadha
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
Bishal Ghimire
 

What's hot (20)

Learn plsql
Learn plsqlLearn plsql
Learn plsql
 
Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in database
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Mysql & Php
Mysql & PhpMysql & Php
Mysql & Php
 
Les02
Les02Les02
Les02
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Oracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guideOracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guide
 
Si0302 20140320131934
Si0302 20140320131934Si0302 20140320131934
Si0302 20140320131934
 
Explain that explain
Explain that explainExplain that explain
Explain that explain
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Lecture 3 sql {basics ddl commands}
Lecture 3 sql {basics  ddl commands}Lecture 3 sql {basics  ddl commands}
Lecture 3 sql {basics ddl commands}
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Lecture 4 sql {basics keys and constraints}
Lecture 4 sql {basics  keys and constraints}Lecture 4 sql {basics  keys and constraints}
Lecture 4 sql {basics keys and constraints}
 
Neatly folding-a-tree
Neatly folding-a-treeNeatly folding-a-tree
Neatly folding-a-tree
 

Similar to 20220112 sac v1

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 

Similar to 20220112 sac v1 (20)

PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Php Chapter 2 3 Training
Php Chapter 2 3 TrainingPhp Chapter 2 3 Training
Php Chapter 2 3 Training
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 Training
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607
 
Php My Sql
Php My SqlPhp My Sql
Php My Sql
 
Php2
Php2Php2
Php2
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 

More from Sharon Liu

SL CV 20240312.docx . Sharon Xiao Liu's Curriculum Vitae.
SL CV 20240312.docx . Sharon Xiao Liu's Curriculum Vitae.SL CV 20240312.docx . Sharon Xiao Liu's Curriculum Vitae.
SL CV 20240312.docx . Sharon Xiao Liu's Curriculum Vitae.
Sharon Liu
 

More from Sharon Liu (20)

20240507 Prototyping Recycling Aluminium.docx
20240507 Prototyping Recycling Aluminium.docx20240507 Prototyping Recycling Aluminium.docx
20240507 Prototyping Recycling Aluminium.docx
 
20240408 Bending Backwards to the Second Step Up.docx
20240408 Bending Backwards to the Second Step Up.docx20240408 Bending Backwards to the Second Step Up.docx
20240408 Bending Backwards to the Second Step Up.docx
 
20240319 Car Simulator Plan.pptx . Plan for a JavaScript Car Driving Simulator.
20240319 Car Simulator Plan.pptx . Plan for a JavaScript Car Driving Simulator.20240319 Car Simulator Plan.pptx . Plan for a JavaScript Car Driving Simulator.
20240319 Car Simulator Plan.pptx . Plan for a JavaScript Car Driving Simulator.
 
20240315 ACMJ Diagrams Set 2.docx . With light, motor, coloured light, and se...
20240315 ACMJ Diagrams Set 2.docx . With light, motor, coloured light, and se...20240315 ACMJ Diagrams Set 2.docx . With light, motor, coloured light, and se...
20240315 ACMJ Diagrams Set 2.docx . With light, motor, coloured light, and se...
 
SL CV 20240312.docx . Sharon Xiao Liu's Curriculum Vitae.
SL CV 20240312.docx . Sharon Xiao Liu's Curriculum Vitae.SL CV 20240312.docx . Sharon Xiao Liu's Curriculum Vitae.
SL CV 20240312.docx . Sharon Xiao Liu's Curriculum Vitae.
 
20240308 Jumping from the Fifth step up.docx
20240308 Jumping from the Fifth step up.docx20240308 Jumping from the Fifth step up.docx
20240308 Jumping from the Fifth step up.docx
 
20240206 Rotated Torus and Full Rotated Sphere 45 degrees down 45 left.docx
20240206 Rotated Torus and Full Rotated Sphere 45 degrees down 45 left.docx20240206 Rotated Torus and Full Rotated Sphere 45 degrees down 45 left.docx
20240206 Rotated Torus and Full Rotated Sphere 45 degrees down 45 left.docx
 
20231218 Plan for Two-Footed Jump on Ice.docx
20231218 Plan for Two-Footed Jump on Ice.docx20231218 Plan for Two-Footed Jump on Ice.docx
20231218 Plan for Two-Footed Jump on Ice.docx
 
20231130 Ice skating Unsupported.docx
20231130 Ice skating Unsupported.docx20231130 Ice skating Unsupported.docx
20231130 Ice skating Unsupported.docx
 
20231102 Plan for Ice Skating.docx
20231102 Plan for Ice Skating.docx20231102 Plan for Ice Skating.docx
20231102 Plan for Ice Skating.docx
 
20231017 xy view of Rotated 45 degrees down and 45 left Sphere.docx
20231017 xy view of Rotated 45 degrees down and 45 left Sphere.docx20231017 xy view of Rotated 45 degrees down and 45 left Sphere.docx
20231017 xy view of Rotated 45 degrees down and 45 left Sphere.docx
 
20231006 Sphere rotated 45 degrees down.docx
20231006 Sphere rotated 45 degrees down.docx20231006 Sphere rotated 45 degrees down.docx
20231006 Sphere rotated 45 degrees down.docx
 
20230904 Recycling Clay.docx
20230904 Recycling Clay.docx20230904 Recycling Clay.docx
20230904 Recycling Clay.docx
 
20230831 a of the equator - Rotated sphere.docx
20230831 a of the equator - Rotated sphere.docx20230831 a of the equator - Rotated sphere.docx
20230831 a of the equator - Rotated sphere.docx
 
20230829 Rotated Ellipse.docx
20230829 Rotated Ellipse.docx20230829 Rotated Ellipse.docx
20230829 Rotated Ellipse.docx
 
20230819 Recall Tests.pptx
20230819 Recall Tests.pptx20230819 Recall Tests.pptx
20230819 Recall Tests.pptx
 
20230810 Radial Plot of an Ellipse.docx
20230810 Radial Plot of an Ellipse.docx20230810 Radial Plot of an Ellipse.docx
20230810 Radial Plot of an Ellipse.docx
 
20230809 South Pole of the Rotated Sphere.docx
20230809 South Pole of the Rotated Sphere.docx20230809 South Pole of the Rotated Sphere.docx
20230809 South Pole of the Rotated Sphere.docx
 
20230804 Rotated Sphere with new North Pole.docx
20230804 Rotated Sphere with new North Pole.docx20230804 Rotated Sphere with new North Pole.docx
20230804 Rotated Sphere with new North Pole.docx
 
20230802 Horizontal Sphere.docx
20230802 Horizontal Sphere.docx20230802 Horizontal Sphere.docx
20230802 Horizontal Sphere.docx
 

Recently uploaded

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 

20220112 sac v1

  • 1. <html> <style> body { font-family: Arial; } .annotation { color: gray; } </style> <body> <?php /* SAC: SqlAutoCommannotator: Structured Query Language Automatic Comment Annotator Sharon Xiao Liu Assumes all SQL given to it is valid. For MySQL to natural language. Work in progress: 20150706-20150730. */ /* Use OOP. Loop through arrays. PHP split string by line break. Add a text box. Generate code by analysing each segment before ;. PCRE. Pattern matching PHP. Nest pattern matching statements. Match even if the spacing is inconsistent. Select multiple columns. Pattern matching database: AND,OR (always after SELECT) similar to WHERE. First column: regex, depending on how many parentheses there are (count number of open parentheses), extract this information, format the final result. Design UI. Add 1 when meet open bracket, or you find a closed bracket immediately after. Look through the rest of the SQL code. Make a table of all types of translations for sac. JQuery UI Tooltip. */ $sqlBlock = "SELECT * FROM Customers WHERE Country = 'USA';nSELECT * FROM Suppliers;nSELECT ID, MiddleName FROM Customers;nSELECT * FROM Suppliers WHERE House = '87' OR Credit = '90';nSELECT * FROM Customers WHERE Country = 'Britain' AND Street = 'Belvedere' AND (House = '13' OR (House = '789' AND Start = '20150706') AND (Contact = 'Email' OR Contact = 'Phone'));"; class sac { public function sacWorking ($sqlBlock) { $sqlBlockTrimEnd = rtrim($sqlBlock, ";"); $sqlBlockExploded = explode(";", $sqlBlockTrimEnd); $output = ""; function parseBrackets($str) { $length = strlen($str); $stack = array(); $result = array(); for($i=0; $i < $length; $i++) { if($str[$i] == '(') {
  • 2. $stack[] = $i; } if($str[$i] == ')') { $open = array_pop($stack); $result[] = substr($str,$open+1, $i-$open-1); } } return $result; } function substitution ($afterNested) { array_unshift($afterNested, ""); $counted = count($afterNested); foreach ($afterNested as $key => $value) { for ($i = $key+1; $i<$counted; $i++) { if (str_replace ($value, $key, $afterNested[$i]) == true) { $afterNested[$i] = str_replace ($value, $key, $afterNested[$i]); } } } $arrayIntoString = ""; foreach ($afterNested as $key => $value) { if ($key >= 1) { $arrayIntoString .= $key. ". ". $value. " "; } } $replaceEquals = preg_replace ("/b(w+)bs?=s?'(w+)'/", "the column $1 is $2", $arrayIntoString); $andOrPattern[0] = "/AND/"; $andOrPattern[1] = "/OR/"; $andOrReplacement[0] = "and"; $andOrReplacement[1] = "or"; $replaceAndOr = preg_replace ($andOrPattern, $andOrReplacement, $replaceEquals); return $replaceAndOr; } foreach ($sqlBlockExploded as $singleLine) { $output .= htmlspecialchars($singleLine. ";", ENT_QUOTES); $output .= "<br><div class="annotation">"; if (preg_match("/SELECTs*/", $singleLine) == true) { $output .= "Select all columns "; } elseif (preg_match_all("/SELECTsb(w+)bsFROM/", $singleLine, $oneAfterSelect, PREG_PATTERN_ORDER) == true) {
  • 3. $output .= "Select the column ". $oneAfterSelect[1][0]; } elseif (preg_match_all("/SELECTs(.*?)sFROM/", $singleLine, $afterSelect, PREG_PATTERN_ORDER) == true) { $output .= "Select the columns "; $explodeAfterSelect = explode (",", $afterSelect[1][0]); foreach ($explodeAfterSelect as $singleColumns) { if ($singleColumns != end($explodeAfterSelect)) { $output .= $singleColumns . ", "; } else { $output .= $singleColumns. " "; } } } if (preg_match_all("/FROMsb(w+)b/", $singleLine, $afterFrom, PREG_PATTERN_ORDER) == true) { $output .= "from the table ". $afterFrom[1][0]; } if (preg_match_all("/WHEREsb(w+)bs?=s?'(w+)'/", $singleLine, $afterWhere, PREG_PATTERN_ORDER) == true) { $output .= " where the column ". $afterWhere[1][0]. " is equal to ". $afterWhere[2][0]; } if (preg_match_all("/(AND|OR)sb(w+)bs?=s?'(w+)'/", $singleLine, $afterAndNoBrackets, PREG_PATTERN_ORDER) == true) { if ($afterAndNoBrackets[1][0] == "AND") { $andOr = " and the column "; } else { $andOr = " or the column "; } $output .= $andOr. $afterAndNoBrackets[2][0]. " is equal to ". $afterAndNoBrackets[3][0]; } if (preg_match_all("/(AND|OR)s((X+))$/", $singleLine, $afterAnd, PREG_PATTERN_ORDER) == true) { if ($afterAnd[1][0] == "AND") { $andOrBracket = ", and "; } else { $andOrBracket = ", or "; } $processBrackets = parseBrackets ($afterAnd[2][0]); $processAndOr = substitution($processBrackets); $output .= $andOrBracket. $processAndOr;
  • 4. } $output .= ".</div><br>"; } return $output; } public function sacGenerating () { $regexAnd = "/ANDsb(w+)bs?=s?&#039;(w+)&#039;/"; } } $obj = new sac; echo $obj->sacWorking($sqlBlock); /*run on XAMPP*/ ?> </body> </html>