~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to scripts/mysql_find_rows.sh

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
# Copyright (C) 2000, 2004 MySQL AB
 
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-1301  USA
 
16
 
 
17
$version="1.02";
 
18
 
 
19
use 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
 
 
25
GetOptions("Information","help","regexp=s","start_row=i","rows=i",
 
26
           "dbregexp=s", "skip-use-db")
 
27
  || usage();
 
28
usage() if ($opt_help || $opt_Information);
 
29
 
 
30
$query=$search=$database=$set=""; $eoq=0;
 
31
while (<>)
 
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
 
 
77
exit 0;
 
78
 
 
79
sub 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
 
 
126
sub usage
 
127
{
 
128
    print <<EOF;
 
129
$0  Ver $version
 
130
 
 
131
Prints all SQL queries that matches a regexp or contains a 'use
 
132
database' or 'set ..' command to stdout.  A SQL query may contain
 
133
newlines.  This is useful to find things in a MySQL 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
 
 
152
Example:
 
153
 
 
154
$0 --regexp "problem_table" < update.log
 
155
 
 
156
$0 --regexp "problem_table" update-log.1 update-log.2
 
157
EOF
 
158
  exit(0);
 
159
}