File Coverage

lib/Plack/App/WebMySQL/Sql.pm
Criterion Covered Total %
statement 8 199 4.0
branch 0 62 0.0
condition n/a
subroutine 3 14 21.4
pod 0 11 0.0
total 11 286 3.8


"; ";} #get field names \n"; #finished field names "; "; ";} #this field has a null value \n"; \n"; #print rows found "; ";} #get field names \n"; #finished field names "; "; ";} #this field has a null value \n"; \n"; #print rows found "; \n"; #finished field names "; ";} #this field has a null value "; \n"; #print rows found
line stmt bran cond sub pod time code
1             #the dumb terminal webmysql module
2             #mt 29/11/2003 2.5 updated getDatabases sub incase "SHOW DATABASES" is disabled
3             #mt 14/03/2005 2.7 added explainquery function
4             # added runqueryvert function
5             package Plack::App::WebMySQL::Sql;
6             BEGIN {
7 1     1   566 use Plack::App::WebMySQL;
  1         1  
  1         69  
8 1     1   3 use Exporter();
  1         1  
  1         28  
9 1     1   7 @ISA = qw(Exporter);
10 1         1508 @EXPORT = qw(testConnect
11             getTables
12             getFields
13             getFieldsShort
14             getDatabases
15             runQuery
16             getTableRows
17             runNonSelect
18             getVariable
19             explainQuery
20             runQueryVert);
21             }
22             ############################################################################################################
23             sub testConnect{ #tests if we can connect to the mysql server
24 0     0 0   my($host, $user, $password, $database) = @_;
25 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
26 0 0         if($dbh){
27 0           $dbh -> disconnect();
28 0           return 1;
29             }
30             else{
31 0           $error = "Cant connect to MySQL server: " . $DBI::errstr;
32 0           return 0;
33             }
34             }
35             ##########################################################################################################
36             sub getTables{ #returns an array of tables for the current database
37 0     0 0   my($host, $user, $password, $database) = @_;
38 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
39 0 0         if($dbh){
40 0           my $query = $dbh -> prepare("SHOW TABLES;");
41 0 0         if($query -> execute()){
42 0           my @tables;
43 0           while(my $table = $query -> fetchrow_array()){push(@tables, $table);} #create an array of the tables found
  0            
44 0           $query -> finish();
45 0           return @tables; #send back the tables to the calling sub
46             }
47 0           else{$error = "Cant find table list: " . $dbh -> errstr;}
48 0           $dbh -> disconnect();
49             }
50 0           else{$error = "Cant connect to MySQL server: " . $DBI::errstr;}
51 0           return undef;
52             }
53             ##########################################################################################################
54             sub getFields{ #returns an array of fields for the current table
55 0     0 0   my($host, $user, $password, $database, $tables) = @_;
56 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
57 0 0         if($dbh){
58 0           my @fields;
59 0           foreach(split(/, /, $tables)){ #get the fields for all of the selected tables
60 0           my $query = $dbh -> prepare("DESCRIBE $_;");
61 0 0         if($query -> execute()){
62 0           while(my @dInfo = $query -> fetchrow_array()){push(@fields, "$_.$dInfo[0]");} #create an array of the fields found
  0            
63 0           $query -> finish();
64             }
65             else{
66 0           $error = "Cant retrieve fields list for $_ table: " . $dbh -> errstr;
67 0           last;
68             }
69             }
70 0           $dbh -> disconnect();
71 0 0         if(!$error){return @fields;} #send back the fields to the calling sub
  0            
72             }
73 0           else{$error = "Cant connect to MySQL server: " . $DBI::errstr;}
74 0           return undef;
75             }
76             ##########################################################################################################
77             sub getFieldsShort{ #returns an array of fields for the current table not includung the table name
78 0     0 0   my($host, $user, $password, $database, $tables) = @_;
79 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
80 0 0         if($dbh){
81 0           my @fields;
82 0           foreach(split(/, /, $tables)){ #get the fields for all of the selected tables
83 0           my $query = $dbh -> prepare("DESCRIBE $_;");
84 0 0         if($query -> execute()){
85 0           while(my @dInfo = $query -> fetchrow_array()){push(@fields, $dInfo[0]);} #create an array of the fields found
  0            
86 0           $query -> finish();
87             }
88             else{
89 0           $error = "Cant retrieve fields list for $_ table: " . $dbh -> errstr;
90 0           last;
91             }
92             }
93 0           $dbh -> disconnect();
94 0 0         if(!$error){return @fields;} #send back the fields to the calling sub
  0            
95             }
96 0           else{$error = "Cant connect to MySQL server: " . $DBI::errstr;}
97 0           return undef;
98             }
99             ##########################################################################################################
100             sub getDatabases{ #returns an array of databases for the current connection
101 0     0 0   my($host, $user, $password, $database) = @_;
102 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
103 0           my @dbs;
104 0 0         if($dbh){
105 0           my $query = $dbh -> prepare("SHOW DATABASES;");
106 0 0         if($query -> execute()){
107 0           while(my $db = $query -> fetchrow_array()){push(@dbs, $db);} #create an array of the tables found
  0            
108 0           $query -> finish();
109             }
110             else{ #try to the databases from the grant command
111 0           $query = $dbh -> prepare("SHOW GRANTS FOR $user\@$host;");
112 0 0         if($query -> execute()){
113 0           while(my $perm = $query -> fetchrow_array()){
114 0 0         if($perm =~ m/^GRANT .+ ON (.+)\..+ TO '$user'\@'$host'$/){push(@dbs, $1);} #create an array of the tables found
  0            
115             }
116 0           $query -> finish();
117             }
118 0           else{push(@dbs, $database);} #SHOW DATABASE did not work so just return the current database
119             }
120 0           $dbh -> disconnect();
121             }
122 0           else{$error = "Cant connect to MySQL server: " . $DBI::errstr;}
123 0           return @dbs; #send back the tables to the calling sub
124             }
125             ##################################################################################################################
126             sub runQuery{
127 0     0 0   my($host, $user, $password, $database, $code) = @_;
128 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
129 0 0         if($dbh){
130 0           my $query = $dbh -> prepare($code);
131 0 0         if($query -> execute()){
132 0           my $html = "
133 0           my $names = $query ->{'NAME'}; #all returned field names
134 0           for(my $i = 0; $i < $query ->{'NUM_OF_FIELDS'}; $i++){$html .= "$$names[$i]
  0            
135 0           $html .= "
136 0           while(my @fields = $query -> fetchrow_array()){
137 0           $html .= "
138 0           foreach(@fields){
139 0 0         if($_){ #this field has a value
140 0           $_ =~ s/
141 0           $_ =~ s/>/>/g; #html dont like greater than signs
142 0           $html .= "$_
143             }
144 0           else{$html .= " 
145             }
146 0           $html .= "
147             }
148 0           $html .= "
{'NUM_OF_FIELDS'} . "\">" . $query -> rows() . "Rows found
149 0           $query -> finish();
150 0           return $html;
151             }
152 0           else{$error = "Problem with query: " . $dbh -> errstr;}
153 0           $dbh -> disconnect();
154             }
155 0           else{$error = "Cant connect to MySQL server: " . $DBI::errstr;}
156 0           return undef;
157             }
158             ##########################################################################################################
159             sub getTableRows{ #returns how many rows in a table
160 0     0 0   my($host, $user, $password, $database, $table) = @_;
161 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
162 0 0         if($dbh){
163 0           my $query = $dbh -> prepare("SELECT COUNT(*) FROM $table;");
164 0           my $rows;
165 0 0         if($query -> execute()){
166 0           $rows = $query -> fetchrow_array();
167 0           $query -> finish();
168             }
169 0           else{$error = "Cant retrieve number of rows for $_ table: " . $dbh -> errstr;}
170 0           $dbh -> disconnect();
171 0 0         if(!$error){return $rows;} #send back the fields to the calling sub
  0            
172             }
173 0           else{$error = "Cant connect to MySQL server: " . $DBI::errstr;}
174 0           return undef;
175             }
176             #############################################################################################################
177             sub runNonSelect{
178 0     0 0   my($host, $user, $password, $database, $code) = @_;
179 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
180 0 0         if($dbh){
181 0           my $affected;
182 0 0         if(!($affected = $dbh -> do($code))){$error = "Problem with query: " . $dbh -> errstr;}
  0            
183 0           $dbh -> disconnect();
184 0 0         if(!$error){return $affected;} #send back the fields to the calling sub
  0            
185             }
186 0           else{$error = "Cant connect to MySQL server: " . $DBI::errstr;}
187 0           return undef;
188             }
189             ##########################################################################################################
190             sub getVariable{ #returns a server variable
191 0     0 0   my($host, $user, $password, $database, $var) = @_;
192 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
193 0 0         if($dbh){
194 0           my $value = "";
195 0           my $query = $dbh -> prepare("SHOW VARIABLES LIKE '$var';");
196 0 0         if($query -> execute()){
197 0           (undef, $value) = $query -> fetchrow_array();
198 0           $query -> finish();
199             }
200 0           else{$error = "Cant retrieve variable for $var: " . $dbh -> errstr;}
201 0           $dbh -> disconnect();
202 0 0         if(!$error){return $value;} #send back the fields to the calling sub
  0            
203             }
204 0           else{$error = "Cant connect to MySQL server: " . $DBI::errstr;}
205 0           return "";
206             }
207             ##################################################################################################################
208             sub explainQuery{
209 0     0 0   my($host, $user, $password, $database, $code) = @_;
210 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
211 0 0         if($dbh){
212 0           my $query = $dbh -> prepare("EXPLAIN " . $code);
213 0 0         if($query -> execute()){
214 0           my $html = "
215 0           my $names = $query ->{'NAME'}; #all returned field names
216 0           for(my $i = 0; $i < $query ->{'NUM_OF_FIELDS'}; $i++){$html .= "$$names[$i]
  0            
217 0           $html .= "
218 0           while(my @fields = $query -> fetchrow_array()){
219 0           $html .= "
220 0           foreach(@fields){
221 0 0         if($_){ #this field has a value
222 0           $_ =~ s/
223 0           $_ =~ s/>/>/g; #html dont like greater than signs
224 0           $html .= "$_
225             }
226 0           else{$html .= " 
227             }
228 0           $html .= "
229             }
230 0           $html .= "
{'NUM_OF_FIELDS'} . "\">" . $query -> rows() . "Rows found
231 0           $query -> finish();
232 0           return $html;
233             }
234 0           else{$error = "Problem with query: " . $dbh -> errstr;}
235 0           $dbh -> disconnect();
236             }
237 0           else{$error = "Cant connect to MySQL server: " . $DBI::errstr;}
238 0           return undef;
239             }
240             ##################################################################################################################
241             sub runQueryVert{ #displays results verticaly
242 0     0 0   my($host, $user, $password, $database, $code) = @_;
243 0           my $dbh = DBI -> connect("DBI:mysql:database=$database;host=$host", $user, $password);
244 0 0         if($dbh){
245 0           my $query = $dbh -> prepare($code);
246 0 0         if($query -> execute()){
247 0           my $html = "";
248 0           my $names = $query ->{'NAME'}; #all returned field names
249 0           my @rows;
250 0           for(my $i = 0; $i < $query ->{'NUM_OF_FIELDS'}; $i++){
251 0           $rows[$i] = "
$$names[$i]
252             } #get field names
253             #$html .= "
254 0           while(my @fields = $query -> fetchrow_array()){
255 0           for(my $rCount = 0; $rCount <= @fields; $rCount++){
256 0 0         if($fields[$rCount]){ #this field has a value
257 0           $fields[$rCount] =~ s/
258 0           $fields[$rCount] =~ s/>/>/g; #html dont like greater than signs
259 0           $rows[$rCount] .= "$fields[$rCount]
260             }
261 0           else{$row[$rCount] .= " 
262             }
263             }
264 0           for(my $i = 0; $i < $query ->{'NUM_OF_FIELDS'}; $i++){
265 0           $rows[$i] .= "
266             }
267             #$html .= "
{'NUM_OF_FIELDS'} . "\">" . $query -> rows() . "Rows found
268 0           $query -> finish();
269 0           $html = join("", @rows);
270 0           return $html;
271             }
272 0           else{$error = "Problem with query: " . $dbh -> errstr;}
273 0           $dbh -> disconnect();
274             }
275 0           else{$error = "Cant connect to MySQL server: " . $DBI::errstr;}
276 0           return undef;
277             }
278             ###############################################################################
279             return 1;
280             END {}