1#!@PERL_PATH@
2# Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; version 2 of the License.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1335  USA
16
17$version="1.02";
18
19use Getopt::Long;
20
21$opt_help=$opt_Information=$opt_skip_use_db=0;
22$opt_regexp=$opt_dbregexp=".*";
23$opt_start_row=1; $opt_rows=9999999999;
24
25GetOptions("Information","help","regexp=s","start_row=i","rows=i",
26	   "dbregexp=s", "skip-use-db")
27  || usage();
28usage() if ($opt_help || $opt_Information);
29
30$query=$search=$database=$set=""; $eoq=0;
31while (<>)
32{
33  next if (length($query) == 0 && /^\#/); # Skip comments
34  $query.=search($_);
35  if ($eoq)
36  {
37    if ($query =~ /^use /i || $query =~ /^SET / ||
38	($query =~ /$opt_regexp/o && $database =~ /$opt_dbregexp/o))
39    {
40      if ($opt_skip_use_db && $query =~ /^use /i)
41      {
42	$query="";
43	next;
44      }
45      if ($opt_start_row <= 1)
46      {
47	if ($database)
48	{
49	  print $database, $set;
50	  $database=$set="";
51	}
52	print $query;
53	last if (--$opt_rows == 0);
54      }
55      else
56      {
57	$opt_start_row--;
58	if ($query =~ /^use /)
59	{
60	  $database=$query;
61	  $set="";
62	}
63	elsif ($query =~ /^SET/)
64	{
65	  $set=$query;
66	}
67	else
68	{
69	  $set="";
70	}
71      }
72    }
73    $query=""; $search=""; $eoq=0;
74  }
75}
76
77exit 0;
78
79sub search
80{
81  my($row)=shift;
82  my($i);
83
84  for ($i=0 ; $i < length($row) ; $i++)
85  {
86    if (length($search))
87    {
88      if (length($search) > 1)
89      {				# Comment
90	next if (substr($row,$i,length($search)) ne $search);
91	$i+=length($search)-1;
92	$search="";
93      }
94      elsif (substr($row,$i,1) eq '\\') # Escaped char in string
95      {
96	$i++;
97      }
98      elsif (substr($row,$i,1) eq $search)
99      {
100	if (substr($row,$i+1,1) eq $search)	# Double " or '
101	{
102	  $i++;
103	}
104	else
105	{
106	  $search="";
107	}
108      }
109      next;
110    }
111    if (substr($row,$i,2) eq '/*')	# Comment
112    {
113      $search="*/";
114      $i++;
115    }
116    elsif (substr($row,$i,1) eq "'" || substr($row,$i,1) eq '"')
117    {
118      $search=substr($row,$i,1);
119     }
120  }
121  $eoq=1 if (!length($search) && $row =~ /;\s*$/);
122  return $row;
123}
124
125
126sub usage
127{
128    print <<EOF;
129$0  Ver $version
130
131Prints all SQL queries that matches a regexp or contains a 'use
132database' or 'set ..' command to stdout.  A SQL query may contain
133newlines.  This is useful to find things in a MariaDB update log.
134
135$0 takes the following options:
136
137--help or --Information
138  Shows this help
139
140--regexp=#
141  Print queries that matches this.
142
143--start_row=#
144  Start output from this row (first row = 1)
145
146--skip-use-db
147  Don\'t include \'use database\' commands in the output.
148
149--rows=#
150  Quit after this many rows.
151
152Example:
153
154$0 --regexp "problem_table" < update.log
155
156$0 --regexp "problem_table" update-log.1 update-log.2
157EOF
158  exit(0);
159}
160