Hello all . . .
I need a regex to extract the MySQL queries from an sql file
The SQL file content it looks like this :
#
# dbtables.sql
#
# Simplifies the task of creating all the database tables
# used by the login system.
#
# Can be run from command prompt by typing:
#
# mysql -u yourusername -D yourdatabasename < dbtables.sql
#
# That's with dbtables.sql in the mysql bin directory, but
# you can just include the path to dbtables.sql and that's
# fine too.
#
# Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
# Last Updated: August 13, 2004
#
#
# Table structure for users table
#
DROP TABLE IF EXISTS users;
CREATE TABLE users (
username varchar(30) primary key,
password varchar(32),
userid varchar(32),
userlevel tinyint(1) unsigned not null,
email varchar(50),
timestamp int(11) unsigned not null
);
#
# Table structure for active users table
#
DROP TABLE IF EXISTS active_users;
CREATE TABLE active_users (
username varchar(30) primary key,
timestamp int(11) unsigned not null
);
#
# Table structure for active guests table
#
DROP TABLE IF EXISTS active_guests;
CREATE TABLE active_guests (
ip varchar(15) primary key,
timestamp int(11) unsigned not null
);
#
# Table structure for banned users table
#
DROP TABLE IF EXISTS banned_users;
CREATE TABLE banned_users (
username varchar(30) primary key,
timestamp int(11) unsigned not null
);
Where # means the following text is comments for the programmes and etc.
and the queries end with the english question mark ( ; )
So the question is how to extract all the queries with or without their own comments ? ? ?
One aproach that I have try is this :
/(^#|^[a-zA-Z])(.+)\;/m
And i have analize it like that get anythink that starts with # or a-zA-Z characters followed by any character and ends with ;
but this aproach return all the file content line by line
My PHP code is this :
function extractQueries2()
{
$fileSize = 0;
$query = '';
if(strlen($this->fileName) < 1)
{
return '';
}
else
{
$file = @fopen("$this->fileName", "r");//open file for reading
$rf = fread($file, 32000); // read 32K
fclose( $file );
$rg = "/(^#|^[a-zA-Z])(.+)\;/m";
$rs = $this->do_reg($rf, $rg);
for($i = 0; $i < sizeof($rs) - 1; $i++)
{
echo $rs[$i] . "<br /><br />";
}
}
}
private function do_reg($text, $regex)
{
preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER);
return $result = $result[0];
}
Many Many Thanks . . . ! ! !
Datacenter Hellas