NAME

Nile::DBI::Table - DBI table class for the Nile framework.

SYNOPSIS

# get table object
my $table = $app->db->table("users");

# or

my $table = $app->db->table;

# set table name
$table->name("users");

# get table name
my $name = $table->name;

$table->delete;
$table->optimize;
$table->empty;
$table->truncate;
my @columns_info = $table->describe;

DESCRIPTION

Nile::DBI::Table - DBI table class for the Nile framework.

This class provides functions for easy managing database tables.

name()

# set table name with constructor
my $table = $app->db->table("users");

# or

# get table object
my $table = $app->db->table;

# then set table name
$table->name("users");

# get table name
my $name = $table->name;

Get and set the table name.

delete()

my $table = $app->db->table("users");

$table->delete;

# or

$app->db->table("users")->delete;

Deletes database table completely.

rename()

$table->rename("newname");

Rename database table.

optimize()

$table->optimize;

Optimizes database table.

analyze()

my $table = $table->analyze;
$app->dump($table);

{
    'Table' => 'blogs.users',
    'Op' => 'analyze',
    'Msg_type' => 'status',
    'Msg_text' => 'OK'
}

analyze database table.

check()

my $table = $table->check;
$app->dump($table);

{
    'Table' => 'blogs.users',
    'Op' => 'analyze',
    'Msg_type' => 'status',
    'Msg_text' => 'OK'
}

Check database table for errors.

empty()

$table->empty;

Empties a table completely row by row. This method is slow, see truncate() method.

truncate()

$table->truncate;

Empties a table completely and takes care of FOREIGN KEY constraints.

describe()

my @table = $table->describe;
$app->dump(@table);

Provides information about the columns in a table. It is a shortcut for SHOW COLUMNS FROM.

struct()

my $struct = $table->struct;

say "Table name: " . $struct->{"Table"};
say "Table struct: " . $struct->{"Create Table"};

Shows the CREATE TABLE statement that creates the named table. To use this statement, you must have some privilege for the table.

tables()

my @table = $table->tables;

Retuns all the tables in the default database.

backup()

my $file = $app->file->catfile($app->var->get("data_dir"), "table.txt");

# backup data_dir/table.txt
$table->backup($file);

# backup and gzip it to table.gzip
$table->backup($file, compress => "gzip");

# backup to comma-separated values (CSV) format and zip it to table.zip
$table->backup($file, format => "csv", compress => "zip");

Writes tables rows to file. Requires grant file permission.

restore()

my $file = $app->file->catfile($app->var->get("data_dir"), "users.txt");

# restore table 'users' from backup file data_dir/users.txt
$table->restore("users", $file);

# unzip backup file "zip" or "gzip" and restore table from unziped file
my $zipfile = $app->file->catfile($app->var->get("data_dir"), "users.zip");
my $table_file_name = "users.txt";
$table->backup("users", $zipfile, $table_file_name, format =>"csv");

Empties table contents and load data from backup file.

copy()

# copy table with contents to new table 'users_new'
$table->copy("users_new");

Copy an existing table with contents to a new table.

clone()

# clone table to new empty table 'users_new'
$table->clone("users_new");

Create a new empty table like existing table with the structure and indexes.

add()

# add column to table
$table->add("count SMALLINT(6) DEFAULT 0");

$table->add("count SMALLINT(6) DEFAULT 0 FIRST");

$table->add("count SMALLINT(6) DEFAULT 0 AFTER email");

$table->add("INDEX userid_idx(UserID)");

Shortcut for "ALTER TABLE table_name ADD ..." which changes the structure of a table.

Use this to add to the table a column, index, primary key, unique, fulltext, spatial, foreign key, etc.

drop()

# drop column 'count' from the table
$table->drop("count");

$table->drop("PRIMARY KEY");

$table->drop("FOREIGN KEY fk_name");

$table->drop("INDEX index_name");

Shortcut for "ALTER TABLE table_name DROP ..." which changes the structure of a table.

Use this to drop from the table a column, index, primary key, foreign key, partition, etc.

change()

# change column 'count' to 'count1'
# CHANGE col_name new_col_name column_definition [FIRST|AFTER col_name]
$table->change("count count1 INT DEFAULT 0");

Shortcut for "ALTER TABLE table_name CHANGE ..." which changes the structure of a table.

Use this to change a column name and definition.

modify()

# MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
# modify column 'count' definition
$table->modify("count INT DEFAULT 0");

Shortcut for "ALTER TABLE table_name MODIFY ..." which changes the structure of a table.

Use this to modify a column definition.

alter()

$table->alter("ADD count INT DEFAULT 0");

Shortcut for "ALTER TABLE table_name ..." which changes the structure of a table.

Bugs

This project is available on github at https://github.com/mewsoft/Nile.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Nile.

SOURCE

Source repository is at https://github.com/mewsoft/Nile.

SEE ALSO

See Nile for details about the complete framework.

AUTHOR

Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> Website: http://www.mewsoft.com

COPYRIGHT AND LICENSE

Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, https://github.com/mewsoft/Nile, http://www.mewsoft.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.