Permalink
Browse files

Handle CREATE, ALTER, DROP and TRUNCATE for get_tables(). Handle comm…

…ent blocks for split().

git-svn-id: http://maatkit.googlecode.com/svn/trunk@4217 dfb901c2-3250-0410-b216-0b33211c9131
  • Loading branch information...
1 parent df0dec6 commit f74eac10ad1feabac0302565b9e454ba209b48c5 daniel@percona.com committed Jul 22, 2009
Showing with 88 additions and 2 deletions.
  1. +19 −1 common/QueryParser.pm
  2. +69 −1 common/t/QueryParser.t
View
@@ -59,6 +59,15 @@ sub get_tables {
return unless $query;
MKDEBUG && _d('Getting tables for', $query);
+ # Handle CREATE, ALTER, TRUNCATE and DROP TABLE.
+ if ( $query =~ /^\s*(CREATE|ALTER|TRUNCATE|DROP)\b/i ) {
+ MKDEBUG && _d('Special table type:', $1); # Using $1, watch out!
+ $query =~ s/IF NOT EXISTS//i;
+ my ($tbl) = $query =~ m/TABLE\s+($tbl_ident)(\s+.*)?/i;
+ MKDEBUG && _d('Matches table:', $tbl);
+ return ($tbl);
+ }
+
# These keywords may appear between UPDATE or SELECT and the table refs.
# They need to be removed so that they are not mistaken for tables.
$query =~ s/ (?:LOW_PRIORITY|IGNORE|STRAIGHT_JOIN)//ig;
@@ -178,7 +187,9 @@ sub get_aliases {
sub split {
my ( $self, $query ) = @_;
return unless $query;
- $query =~ s/^\s+//;
+ $query = remove_comments($query);
+ $query =~ s/^\s+//; # Remove leading spaces.
+ $query =~ s/\s{2,}/ /g; # Remove extra spaces.
MKDEBUG && _d('Splitting', $query);
my $verbs = qr{SELECT|INSERT|UPDATE|DELETE|REPLACE|UNION|CREATE}i;
@@ -209,6 +220,13 @@ sub split {
return @statements;
}
+sub remove_comments {
+ my ( $query ) = @_;
+ return unless $query;
+ $query =~ s!/\*.*?\*/! !g;
+ return $query;
+}
+
sub _d {
my ($package, undef, $line) = caller 0;
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
View
@@ -3,7 +3,7 @@
use strict;
use warnings FATAL => 'all';
-use Test::More tests => 96;
+use Test::More tests => 104;
use English qw(-no_match_vars);
require '../QueryRewriter.pm';
@@ -497,6 +497,63 @@ is_deeply(
'Not confused by quoted string'
);
+
+is_deeply(
+ [ $qp->get_tables(
+ 'create table db.tbl (i int)')
+ ],
+ [qw(db.tbl)],
+ 'get_tables: CREATE TABLE'
+);
+
+is_deeply(
+ [ $qp->get_tables(
+ 'create TEMPORARY table db.tbl2 (i int)')
+ ],
+ [qw(db.tbl2)],
+ 'get_tables: CREATE TEMPORARY TABLE'
+);
+
+is_deeply(
+ [ $qp->get_tables(
+ 'create table if not exists db.tbl (i int)')
+ ],
+ [qw(db.tbl)],
+ 'get_tables: CREATE TABLE IF NOT EXISTS'
+);
+
+is_deeply(
+ [ $qp->get_tables(
+ 'create TEMPORARY table IF NOT EXISTS db.tbl3 (i int)')
+ ],
+ [qw(db.tbl3)],
+ 'get_tables: CREATE TEMPORARY TABLE IF NOT EXISTS'
+);
+
+is_deeply(
+ [ $qp->get_tables(
+ 'ALTER TABLE db.tbl ADD COLUMN (j int)')
+ ],
+ [qw(db.tbl)],
+ 'get_tables: ALTER TABLE'
+);
+
+is_deeply(
+ [ $qp->get_tables(
+ 'DROP TABLE db.tbl')
+ ],
+ [qw(db.tbl)],
+ 'get_tables: DROP TABLE'
+);
+
+is_deeply(
+ [ $qp->get_tables(
+ 'truncate table db.tbl')
+ ],
+ [qw(db.tbl)],
+ 'get_tables: TRUNCATE TABLE'
+);
+
ok($qp->has_derived_table(
'select * from ( select 1) as x'),
'simple derived');
@@ -581,6 +638,17 @@ is_deeply(
],
'split 2 statements, CREATE ... SELECT'
);
+
+$sql = "/*<font color = 'blue'>MAIN FUNCTION </font><br>*/ insert into p.b317 (foo) select p.b1927.rtb as pr /* inner join pa7.r on pr.pd = c.pd */ inner join m.da on da.hr=p.hr and da.node=pr.node ;";
+is_deeply(
+ [ $qp->split($sql) ],
+ [
+ 'insert into p.b317 (foo) ',
+ 'select p.b1927.rtb as pr inner join m.da on da.hr=p.hr and da.node=pr.node ;',
+ ],
+ 'split statements with comment blocks'
+);
+
# #############################################################################
# Done.
# #############################################################################

0 comments on commit f74eac1

Please sign in to comment.