| ZConf-DBI-utils documentation | Contained in the ZConf-DBI-utils distribution. |
ZConf::DBI::utils - Assorted utilities for ZConf::DBI.
Version 0.0.0
This is primarily meant for quick small things. If you are going to calling a lot of stuff here repeatively/heavily, you are probally going to be better off making use of what ever is being called by the function directly.
This initiates the object.
One arguement is required and it is the 'ZConf::DBI' object.
my $foo=ZConf::DBI::util->new($zcdbi);
if($foo->error){
warn('error code:'.$foo->error.': '.$foo->errorString);
}
This creates a new table using DBIx::Admin::CreateTable->create_table.
Three arguements are required. The first is the data source name. The second is the table name. The third is a SQL string describing the columns.
$foo->create_table('whatever', 'sometable', 'id char(32) primary key, data varchar(255) not null');
if($foo->error){
warn('error code:'.$error.': '.$foo->errorString);
}
This executes the do statement on a DBH created from the data source.
Two arguements are required. The first is the data source. The second is the SQL.
$foo->create_table('whatever', 'drop sequence fubar;');
if($foo->error){
warn('error code:'.$error.': '.$foo->errorString);
}
This drops a table using DBIx::Admin::CreateTable->drop_table.
Two arguements are required. The first is the data source name. The second is the table name.
$foo->create_table('whatever', 'sometable');
if($foo->error){
warn('error code:'.$error.': '.$foo->errorString);
}
Returns the current error code and true if there is an error.
If there is no error, undef is returned.
my $error=$foo->error;
if($error){
warn('error code:'.$error.': '.$foo->errorString);
}
Returns the error string if there is one. If there is not, it will return undef.
my $error=$foo->error;
if($error){
warn('error code:'.$error.': '.$foo->errorString);
}
This returns a array reference of table column names found by DBIx::Admin::TableInfo->columns.
There are three arguements taken. The first, and required, is the data source name. The second, and optional, is the schema name. The third, and optional, is the schema.
my $tables=$foo->table_columns('tigerline', 'geometry_columns');
if($foo->error){
warn('error code:'.$foo->error.': '.$foo->errorString);
}
This returns a hash reference of table column names found by DBIx::Admin::TableInfo->info.
There are two arguements taken. The first, and required, is the data source name. The second, and optional, is the schema name.
my $tables=$foo->table_info('tigerline', 'geometry_columns');
if($foo->error){
warn('error "'.$foo->error.'"');
}
This returns a array refernce of table names found by DBIx::Admin::TableInfo->tables.
There are two arguements taken. The first, and required, is the data source name. The second, and optional, is the schema name.
my $tables=$foo->tables('tigerline');
if($foo->error){
warn('error "'.$foo->error.'"');
}
This blanks the error storage and is only meant for internal usage.
It does the following.
$self->{error}=undef;
$self->{errorString}=undef;
No ZConf::DBI object passed.
The passed object is not a ZConf::DBI object.
No data source name specified.
DBIx::Admin::TableInfo->new returned undef.
No table specified.
Connect errored.
No SQL defined.
Creating the table frailed.
Dropping the table failed.
Zane C. Bowers, <vvelox at vvelox.net>
Please report any bugs or feature requests to bug-zconf-dbi-utils at rt.cpan.org, or through
the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ZConf-DBI-utils. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc ZConf::DBI::utils
You can also look for information at:
Copyright 2010 Zane C. Bowers, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| ZConf-DBI-utils documentation | Contained in the ZConf-DBI-utils distribution. |
package ZConf::DBI::utils; use warnings; use strict; use DBIx::Admin::TableInfo; use DBIx::Admin::CreateTable;
our $VERSION = '0.0.0';
sub new{ my $zcdbi=$_[1]; my $function='new'; my $self={error=>undef, perror=>undef, errorString=>undef, module=>'ZConf-DBI-util', zcdbi=>$zcdbi, }; bless $self; #make sure a object was not passed if (!defined( $self->{zcdbi} )) { $self->{perror}=1; $self->{error}=1; $self->{errorString}='No ZConf::DBI object passed'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return $self; } #make sure it is the correct type of object if (ref($self->{zcdbi}) ne 'ZConf::DBI') { $self->{perror}=1; $self->{error}=2; $self->{errorString}='No ZConf::DBI object passed'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return $self; } return $self; }
sub create_table{ my $self=$_[0]; my $dsName=$_[1]; my $table=$_[2]; my $sql=$_[3]; my $function='create_table'; #makes sure we have a data source if (!defined($dsName)) { $self->{error}=3; $self->{errorString}='No data source name specified'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #makes sure we have a table if (!defined($table)) { $self->{error}=5; $self->{errorString}='No table name specified'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #makes sure we have a table if (!defined($sql)) { $self->{error}=7; $self->{errorString}='No SQL defined'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #connect my $dbh=$self->{zcdbi}->connect($dsName); if ($self->{zcdbi}->error) { $self->{error}=6; $self->{errorString}='Connect errored'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #create the dbix object my($admin) = DBIx::Admin::CreateTable->new( dbh=>$dbh, ); if (!defined($admin)) { $self->{error}=4; $self->{errorString}='DBIx::Admin::CreateTable->new returned undef'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #create it my $returned=$admin->create_table('create table '.$table.' ( '.$sql.' )'); #if the returned value is empty, but defined, it worked if ($returned eq '') { return 1; } #it did not work as the returned value is not equal to '' $self->{error}=8; $self->{errorString}='DBIx::Admin::CreateTable->create_table errored. error="'.$returned.'"'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; }
sub do{ my $self=$_[0]; my $dsName=$_[1]; my $sql=$_[2]; my $function='do'; #makes sure we have a data source if (!defined($dsName)) { $self->{error}=3; $self->{errorString}='No data source name specified'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #makes sure we have a table if (!defined($sql)) { $self->{error}=7; $self->{errorString}='No SQL defined'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #connect my $dbh=$self->{zcdbi}->connect($dsName); if ($self->{zcdbi}->error) { $self->{error}=6; $self->{errorString}='Connect errored'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } return $dbh->do($sql); }
sub drop_table{ my $self=$_[0]; my $dsName=$_[1]; my $table=$_[2]; my $function='drop_table'; #makes sure we have a data source if (!defined($dsName)) { $self->{error}=3; $self->{errorString}='No data source name specified'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #makes sure we have a table if (!defined($table)) { $self->{error}=5; $self->{errorString}='No table name specified'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #connect my $dbh=$self->{zcdbi}->connect($dsName); if ($self->{zcdbi}->error) { $self->{error}=6; $self->{errorString}='Connect errored'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #create the dbix object my($admin) = DBIx::Admin::CreateTable->new( dbh=>$dbh, ); if (!defined($admin)) { $self->{error}=4; $self->{errorString}='DBIx::Admin::CreateTable->new returned undef'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } #create it my $returned=$admin->drop_table($table); #if the returned value is empty, but defined, it worked if ($returned eq '') { return 1; } #it did not work as the returned value is not equal to '' $self->{error}=8; $self->{errorString}='DBIx::Admin::CreateTable->drop_table errored. error="'.$returned.'"'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; }
sub error{ return $_[0]->{error}; }
sub errorString{ return $_[0]->{errorString}; }
sub table_columns{ my $self=$_[0]; my $dsName=$_[1]; my $table=$_[2]; my $schema=$_[3]; my $function='table_columns'; if (!defined($dsName)) { $self->{error}=3; $self->{errorString}='No data source name specified'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } if (!defined($table)) { $self->{error}=5; $self->{errorString}='No table name specified'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } my $dbh=$self->{zcdbi}->connect($dsName); my($admin) = DBIx::Admin::TableInfo->new( dbh=>$dbh, schema=>$schema, ); if (!defined($admin)) { $self->{error}=4; $self->{errorString}='DBIx::Admin::TableInfo->new returned undef'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } return $admin->columns($table); }
sub table_info{ my $self=$_[0]; my $dsName=$_[1]; my $schema=$_[2]; my $function='table_info'; if (!defined($dsName)) { $self->{error}=3; $self->{errorString}='No data source name specified'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } my $dbh=$self->{zcdbi}->connect($dsName); my($admin) = DBIx::Admin::TableInfo->new( dbh=>$dbh, schema=>$schema, ); if (!defined($admin)) { $self->{error}=4; $self->{errorString}='DBIx::Admin::TableInfo->new returned undef'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } return $admin->info; }
sub tables{ my $self=$_[0]; my $dsName=$_[1]; my $schema=$_[2]; my $function='tables'; if (!defined($dsName)) { $self->{error}=3; $self->{errorString}='No data source name specified'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } my $dbh=$self->{zcdbi}->connect($dsName); my($admin) = DBIx::Admin::TableInfo->new( dbh=>$dbh, schema=>$schema, ); if (!defined($admin)) { $self->{error}=4; $self->{errorString}='DBIx::Admin::TableInfo->new returned undef'; warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString}); return undef; } return $admin->tables; }
#blanks the error flags sub errorblank{ my $self=$_[0]; if ($self->{perror}) { warn('ZConf-DevTemplate errorblank: A permanent error is set'); return undef; } $self->{error}=undef; $self->{errorString}=undef; return 1; }
1; # End of ZConf::DBI::utils