Nile/DBI/Table.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 166 CC 8 SCAR 22.3
CC
8
Cov
0%
restore
30.0
backup
20.0
BUILD
2.0
CRAP 9.3
1 # Copyright Infomation
2 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D.
4 # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com
5 # Email : mewsoft@cpan.org, support@mewsoft.com
6 # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved.
7 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 package Nile::DBI::Table;
9  
10 our $VERSION = '0.55';
11 our $AUTHORITY = 'cpan:MEWSOFT';
12  
13 =pod
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 Nile::DBI::Table - DBI table class for the Nile framework.
20  
21 =head1 SYNOPSIS
22
23 # get table object
24 my $table = $app->db->table("users");
25  
26 # or
27
28 my $table = $app->db->table;
29
30 # set table name
31 $table->name("users");
32
33 # get table name
34 my $name = $table->name;
35  
36 $table->delete;
37 $table->optimize;
38 $table->empty;
39 $table->truncate;
40 my @columns_info = $table->describe;
41  
42 =head1 DESCRIPTION
43
44 Nile::DBI::Table - DBI table class for the Nile framework.
45  
46 This class provides functions for easy managing database tables.
47  
48 =cut
49  
50 use Nile::Base;
51 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52 =head2 name()
53
54 # set table name with constructor
55 my $table = $app->db->table("users");
56  
57 # or
58
59 # get table object
60 my $table = $app->db->table;
61
62 # then set table name
63 $table->name("users");
64
65 # get table name
66 my $name = $table->name;
67
68 Get and set the table name.
69  
70 =cut
71  
72 has 'name' => (
73 is => 'rw',
74 );
75 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76 sub BUILD {
77 my ($self, $arg) = @_;
78 $self->name($arg->{name});
79 }
80 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 =head2 delete()
82
83 my $table = $app->db->table("users");
84  
85 $table->delete;
86
87 # or
88  
89 $app->db->table("users")->delete;
90  
91 Deletes database table completely.
92  
93 =cut
94  
95 sub delete {
96 my ($self) = @_;
97 $self->app->db->do("drop table ".$self->name);
98 $self;
99 }
100 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101 =head2 rename()
102
103 $table->rename("newname");
104  
105 Rename database table.
106  
107 =cut
108  
109 sub rename {
110 my ($self, $name) = @_;
111 $self->app->db->do("rename table ".$self->name . " TO ". $name);
112 $self->name($name);
113 $self;
114 }
115 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116 =head2 optimize()
117
118 $table->optimize;
119  
120 Optimizes database table.
121  
122 =cut
123  
124 sub optimize {
125 my ($self) = @_;
126 $self->app->db->do("optimize table ".$self->name);
127 $self;
128 }
129 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130 =head2 analyze()
131
132 my $table = $table->analyze;
133 $app->dump($table);
134
135 {
136 'Table' => 'blogs.users',
137 'Op' => 'analyze',
138 'Msg_type' => 'status',
139 'Msg_text' => 'OK'
140 }
141  
142 analyze database table.
143  
144 =cut
145  
146 sub analyze {
147 my ($self) = @_;
148 $self->app->db->hash("analyze table ".$self->name);
149 }
150 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151 =head2 check()
152
153 my $table = $table->check;
154 $app->dump($table);
155
156 {
157 'Table' => 'blogs.users',
158 'Op' => 'analyze',
159 'Msg_type' => 'status',
160 'Msg_text' => 'OK'
161 }
162  
163 Check database table for errors.
164  
165 =cut
166  
167 sub check {
168 my ($self) = @_;
169 $self->app->db->hash("check table ".$self->name);
170 }
171 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172 =head2 empty()
173
174 $table->empty;
175  
176 Empties a table completely row by row. This method is slow, see truncate() method.
177  
178 =cut
179  
180 sub empty {
181 my ($self) = @_;
182 $self->app->db->do("delete from ".$self->name);
183 $self;
184 }
185 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186 =head2 truncate()
187
188 $table->truncate;
189  
190 Empties a table completely and takes care of FOREIGN KEY constraints.
191  
192 =cut
193  
194 sub truncate {
195 my ($self) = @_;
196 $self->app->db->do("truncate ".$self->name);
197 $self;
198 }
199 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200 =head2 describe()
201
202 my @table = $table->describe;
203 $app->dump(@table);
204  
205 Provides information about the columns in a table. It is a shortcut for SHOW COLUMNS FROM.
206  
207 =cut
208  
209 sub describe {
210 my ($self) = @_;
211 #$self->app->db->hashes("describe ".$self->name, 'Field');
212 $self->app->db->rows("describe ".$self->name);
213 }
214 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
215 =head2 struct()
216
217 my $struct = $table->struct;
218
219 say "Table name: " . $struct->{"Table"};
220 say "Table struct: " . $struct->{"Create Table"};
221  
222 Shows the CREATE TABLE statement that creates the named table. To use this statement, you must have some privilege for the table.
223  
224 =cut
225  
226 sub struct {
227 my ($self) = @_;
228 $self->app->db->hash("show create table ".$self->name);
229 }
230 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231 sub create {
232 my ($self, $columns, $extra) = @_;
233 $self->app->db->rows("create table ".$self->name . "($columns)$extra");
234 $self;
235 }
236 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237 =head2 tables()
238
239 my @table = $table->tables;
240  
241 Retuns all the tables in the default database.
242  
243 =cut
244  
245 sub tables {
246 my ($self) = @_;
247 $self->app->db->col("SHOW TABLES");
248 }
249 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
250 =head2 backup()
251
252 my $file = $app->file->catfile($app->var->get("data_dir"), "table.txt");
253  
254 # backup data_dir/table.txt
255 $table->backup($file);
256  
257 # backup and gzip it to table.gzip
258 $table->backup($file, compress => "gzip");
259
260 # backup to comma-separated values (CSV) format and zip it to table.zip
261 $table->backup($file, format => "csv", compress => "zip");
262
263 Writes tables rows to file. Requires grant file permission.
264  
265 =cut
266  
267 sub backup {
268 my ($self, $file, %options) = @_;
269  
270 unlink ($file);
271  
272 # The NULL value means “no data.” NULL can be written in any lettercase. A synonym is \N (case sensitive).
273 # For text file import or export operations performed with LOAD DATA INFILE or SELECT ... INTO OUTFILE, NULL is represented by the \N sequence.
274 # defaults: FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\' LINES TERMINATED BY '\n' STARTING BY ''
275  
276 $options{format} = lc($options{format});
277 $options{compress} = lc($options{compress});
278
279 my $format = "";
280
281 if ($options{format} eq "csv") {
282 $format = qq{ FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' };
283 }
284  
285 $self->app->db->run("select * into outfile ".$self->app->db->quote($file).$format." from ".$self->name);
286  
287 if ($options{compress} eq "zip") {
288 $self->app->file->zip($file);
289 }
290 elsif ($options{compress} eq "gzip") {
291 $self->app->file->gzip($file);
292 }
293  
294 $self;
295 }
296 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
297 =head2 restore()
298
299 my $file = $app->file->catfile($app->var->get("data_dir"), "users.txt");
300  
301 # restore table 'users' from backup file data_dir/users.txt
302 $table->restore("users", $file);
303  
304 # unzip backup file "zip" or "gzip" and restore table from unziped file
305 my $zipfile = $app->file->catfile($app->var->get("data_dir"), "users.zip");
306 my $table_file_name = "users.txt";
307 $table->backup("users", $zipfile, $table_file_name, format =>"csv");
308
309 Empties table contents and load data from backup file.
310  
311 =cut
312  
313 sub restore {
314 my ($self, $table, $file, $zipname, $format) = @_;
315  
316 return unless ($table and $file);
317  
318 my ($name, $dir, $ext, $filename) = $self->path_info($file);
319  
320 if ($file =~ /\.zip$/i) {
321 $self->app->file->unzip($file);
322 $file = $self->file->catfile($dir, $zipname);
323 }
324 elsif ($file =~ /\.gzip$/i) {
325 $self->app->file->gunzip($file);
326 $file = $self->file->catfile($dir, $zipname);
327 }
328
329 if ($format eq "csv") {
330 $format = qq{ FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' };
331 }
332  
333 $self->app->db->run("load data infile ".$self->app->db->quote($file)." into table ".$self->name.$format);
334  
335 $self;
336 }
337 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
338 =head2 copy()
339
340 # copy table with contents to new table 'users_new'
341 $table->copy("users_new");
342  
343 Copy an existing table with contents to a new table.
344  
345 =cut
346  
347 sub copy {
348 my ($self, $new) = @_;
349 $new || return;
350 $self->app->db->run("create table ".$new." like ".$self->name);
351 $self->app->db->run("insert ".$new." select * from ".$self->name);
352 }
353 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
354 =head2 clone()
355
356 # clone table to new empty table 'users_new'
357 $table->clone("users_new");
358  
359 Create a new empty table like existing table with the structure and indexes.
360  
361 =cut
362  
363 sub clone {
364 my ($self, $new) = @_;
365 $new || return;
366 $self->app->db->run("create table ".$new." like ".$self->name);
367 }
368 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
369 =head2 add()
370
371 # add column to table
372 $table->add("count SMALLINT(6) DEFAULT 0");
373  
374 $table->add("count SMALLINT(6) DEFAULT 0 FIRST");
375  
376 $table->add("count SMALLINT(6) DEFAULT 0 AFTER email");
377  
378 $table->add("INDEX userid_idx(UserID)");
379  
380 Shortcut for C<"ALTER TABLE table_name ADD ..."> which changes the structure of a table.
381  
382 Use this to add to the table a column, index, primary key, unique, fulltext, spatial, foreign key, etc.
383
384 =cut
385  
386 sub add {
387 my ($self, $qry) = @_;
388 $qry || return;
389 $self->app->db->run("alter table ".$self->name." add ".$qry);
390 }
391 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
392 =head2 drop()
393
394 # drop column 'count' from the table
395 $table->drop("count");
396
397 $table->drop("PRIMARY KEY");
398  
399 $table->drop("FOREIGN KEY fk_name");
400  
401 $table->drop("INDEX index_name");
402  
403 Shortcut for C<"ALTER TABLE table_name DROP ..."> which changes the structure of a table.
404  
405 Use this to drop from the table a column, index, primary key, foreign key, partition, etc.
406
407 =cut
408  
409 sub drop {
410 my ($self, $qry) = @_;
411 $qry || return;
412 $self->app->db->run("alter table ".$self->name." drop ".$qry);
413 }
414 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
415 =head2 change()
416
417 # change column 'count' to 'count1'
418 # CHANGE col_name new_col_name column_definition [FIRST|AFTER col_name]
419 $table->change("count count1 INT DEFAULT 0");
420
421 Shortcut for C<"ALTER TABLE table_name CHANGE ..."> which changes the structure of a table.
422  
423 Use this to change a column name and definition.
424
425 =cut
426  
427 sub change {
428 my ($self, $qry) = @_;
429 $qry || return;
430 $self->app->db->run("alter table ".$self->name." change ".$qry);
431 }
432 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
433 =head2 modify()
434
435 # MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
436 # modify column 'count' definition
437 $table->modify("count INT DEFAULT 0");
438
439 Shortcut for C<"ALTER TABLE table_name MODIFY ..."> which changes the structure of a table.
440  
441 Use this to modify a column definition.
442
443 =cut
444  
445 sub modify {
446 my ($self, $qry) = @_;
447 $qry || return;
448 $self->app->db->run("alter table ".$self->name." modify ".$qry);
449 }
450 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
451 =head2 alter()
452
453 $table->alter("ADD count INT DEFAULT 0");
454  
455 Shortcut for C<"ALTER TABLE table_name ..."> which changes the structure of a table.
456  
457 =cut
458  
459 sub alter {
460 my ($self, $qry) = @_;
461 $qry || return;
462 $self->app->db->run("alter table ".$self->name." ".$qry);
463 }
464 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
465  
466 =pod
467  
468 =head1 Bugs
469  
470 This project is available on github at L<https://github.com/mewsoft/Nile>.
471  
472 =head1 HOMEPAGE
473  
474 Please visit the project's homepage at L<https://metacpan.org/release/Nile>.
475  
476 =head1 SOURCE
477  
478 Source repository is at L<https://github.com/mewsoft/Nile>.
479  
480 =head1 SEE ALSO
481  
482 See L<Nile> for details about the complete framework.
483  
484 =head1 AUTHOR
485  
486 Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org>
487 Website: http://www.mewsoft.com
488  
489 =head1 COPYRIGHT AND LICENSE
490  
491 Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com,
492 L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com>
493  
494 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
495  
496 =cut
497  
498 1;