File Coverage

blib/lib/DBIx/Mysql/InformationSchema.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1              
2              
3             package DBIx::Mysql::InformationSchema;
4              
5 1     1   32916 use strict;
  1         2  
  1         37  
6 1     1   5 use warnings;
  1         1  
  1         27  
7 1     1   745 use DBI;
  0            
  0            
8              
9             our $VERSION = '0.04';
10              
11              
12              
13              
14             #
15             # Get a new object, not connected to start.
16             #
17              
18             sub new {
19             my $this = shift;
20             my $class = ref($this) || $this;
21             my $self = {};
22             bless $self,$class;
23             $self->{STATUS} = 0;
24             $self->{DBH} = '';
25             return $self;
26             }
27              
28              
29             # connectdb()
30             # Connect to the INFORMATION_SCHEMA database.
31             # You can simply put your own database handle into $obj->{DBH} if you prefer.
32              
33              
34             sub connectdb {
35             my $thisthing = shift or return undef;
36              
37             my $user = shift ; # user,pass,host and port are all actually optional...
38             my $password = shift ; # the db might allow open access, right?
39              
40             my $host = shift;
41             my $port = shift;
42            
43              
44             my $mysql_information_view = 'information_schema';
45             my $dsn = join ':','DBI','mysql', $mysql_information_view;
46              
47             if ($host) {
48             $port = 3306 unless $port;
49             $dsn = $dsn."\@".$host.':'.$port;
50             }
51              
52              
53            
54             if ($thisthing->{DBH} = DBI->connect($dsn, $user, $password )) {
55             $thisthing->{STATUS} = "connected";
56             } else {
57             $thisthing->{DBIERROR} = $DBI::errstr;
58             $thisthing->{STATUS} = 0; # false but defined.
59             }
60             }
61              
62              
63             # given a particular value - return the value for that db,table,column
64             # value is one of the
65              
66             sub column_info_value {
67             my $thisthing = shift or return undef;
68             my $options = shift ;
69              
70             my $db = $thisthing->{DBH}->quote($options->{DB});
71             my $table = $thisthing->{DBH}->quote($options->{TABLE});
72             my $column = $thisthing->{DBH}->quote($options->{COLUMN});
73             my $value = $thisthing->{DBH}->quote($options->{VALUE});
74              
75             my $sql = "SELECT * from COLUMNS where TABLE_SCHEMA = $db AND TABLE_NAME = $table AND COLUMN_NAME = $column";
76              
77             my $sth = $thisthing->{DBH}->prepare($sql);
78             $sth->execute;
79              
80             # this should only return one row.
81             my $href = $sth->fetchrow_hashref;
82             return $href->{$value};
83             }
84              
85              
86             #
87             # Column_table_rows
88             #
89              
90             sub column_table_rows {
91             my $thisthing = shift or return undef;
92              
93             my $sql = "SELECT * from COLUMNS limit 1";
94             my $sth = $thisthing->{DBH}->prepare($sql);
95             $sth->execute;
96              
97             my $href = $sth->fetchrow_hashref;
98             return keys %{$href};
99              
100             }
101              
102              
103              
104              
105              
106             #
107             # Return a list of all the databases.
108             #
109             sub databases {
110              
111             my $thisthing = shift;
112             my @rows;
113             my $sth = $thisthing->{DBH}->prepare("SELECT DISTINCT TABLE_SCHEMA from TABLES");
114             $sth->execute;
115             while (my $row = $sth->fetchrow_array) {
116             push @rows,$row;
117             }
118             return (@rows);
119             }
120              
121              
122              
123              
124             # Return a list of all the tables for a given database
125              
126             sub tables {
127             my $thisthing = shift or return undef;
128              
129             my $db = shift or return undef;
130             $db = $thisthing->{DBH}->quote($db);
131              
132             my $sql = "SELECT DISTINCT TABLE_NAME from COLUMNS where TABLE_SCHEMA = $db";
133              
134             my $sth = $thisthing->{DBH}->prepare($sql);
135             $sth->execute;
136             my @tables;
137             while (my $row = $sth->fetchrow_array) {
138             push @tables,$row;
139             }
140             return (@tables);
141             }
142              
143              
144              
145             # column_info returns a hash ref for the one row.
146             # metadata for db,table and column.
147              
148              
149             sub column_info {
150              
151             my $thisthing = shift or return undef;
152              
153             my $db = shift or return undef;
154             my $table = shift or return undef;
155             my $column = shift or return undef;
156              
157             $db = $thisthing->{DBH}->quote($db);
158             $table = $thisthing->{DBH}->quote($table);
159             $column = $thisthing->{DBH}->quote($column);
160              
161              
162             my $sql = "SELECT * from COLUMNS where TABLE_SCHEMA = $db AND TABLE_NAME = $table AND COLUMN_NAME = $column";
163              
164             my $sth = $thisthing->{DBH}->prepare($sql);
165             $sth->execute;
166              
167             return $sth->fetchrow_hashref;
168             }
169              
170              
171              
172              
173             #
174             # Given a db and a table,, return alist of columns
175             #
176              
177             sub columns {
178             my $thisthing = shift or return undef;
179              
180             my $db = shift or return undef;
181             my $table = shift or return undef;
182              
183             $db = $thisthing->{DBH}->quote($db);
184             $table = $thisthing->{DBH}->quote($table);
185              
186             my $sql = "SELECT DISTINCT COLUMN_NAME from COLUMNS where TABLE_SCHEMA = $db AND TABLE_NAME = $table";
187              
188             my $sth = $thisthing->{DBH}->prepare($sql);
189             $sth->execute;
190             my @rows;
191             while (my $row = $sth->fetchrow_array) {
192             push @rows,$row;
193             }
194             return (@rows);
195             }
196              
197              
198              
199              
200             # information_schema_tables
201             #
202             # Returns a list of tables in the information_schema view.
203              
204              
205             sub information_schema_tables {
206             my $thisthing = shift or return undef;
207              
208             my $sql = "select TABLE_NAME from TABLES where TABLE_SCHEMA='information_schema' ";
209              
210             my $sth = $thisthing->{DBH}->prepare($sql);
211             $sth->execute;
212              
213             my @tables;
214             while (my $table = $sth->fetchrow_array) {
215             push @tables,$table;
216             }
217             return (@tables);
218             }
219              
220              
221              
222             sub table_constraints {
223             my $thisthing = shift or return undef;
224             my $sql = "SELECT * from TABLE_CONTRAINTS";
225             my $sth = $thisthing->{DBH}->prepare($sql);
226             $sth->execute;
227              
228             my @results;
229              
230             while (my $row = $sth->fetchrow_hashref) {
231             push @results,$row;
232             }
233             return (@results);
234             }
235              
236              
237              
238              
239              
240              
241              
242              
243             sub disconnect {
244             my $thisthing = shift or return undef;
245             $thisthing->{DBH}->disconnect();
246              
247             }
248              
249              
250             sub dbonline {
251             my $thisthing = shift;
252             if ($thisthing->{DBH}->{Driver}->{Name} =~ m/^mysql$/) {
253             return 1;
254             } else {
255             return undef;
256             }
257              
258             }
259              
260              
261              
262              
263              
264              
265             1;
266             __END__