G::DB GDBI
SummaryIncluded librariesPackage variablesSynopsisDescriptionGeneral documentationMethods
Summary
G::DB::GDBI
Package variables
Privates (from "my" definitions)
$defaultuser = $ENV{USER}
$dbh;
$infile = ''
$defaultdb = "g-language"
$defaultpass = ""
$defaultdbi = "mysql"
$defaulthost = "localhost"
Included modules
DBI
G::DB::GDBAPI
Inherit
G::DB::GDBAPI
Synopsis
 use G::DB::GDBI;
 @ISA = (G::DB::GDBI);
Description
 Inherits all necessary classes.
 Intended to be used as a library
Methods
DESTROY
No description
Code
load
No description
Code
new
No description
Code
output
No description
Code
print
No description
Code
qsql
No description
Code
save
No description
Code
sql
No description
Code
view
No description
Code
Methods description
None available.
Methods code
DESTROYdescriptionprevnextTop
sub DESTROY {
    my $self = shift;

    $dbh->disconnect();
    undef %{$self};
}
loaddescriptionprevnextTop
sub load {
    my $this = shift;
    my $table = shift;

    my $sql = "select * from $table";
    my $sth = $dbh->prepare($sql);
    my $ref = $sth->execute;

    unless($ref){
	die $sth->errstr;
    }

    my $r = 0;
    my $f = 0;
    my @rows;
    my %name;
    while(@rows = $sth->fetchrow_array){
	for($f = 0; $f < $sth->{NUM_OF_FIELDS}; $f ++){
	    $this->{$r}->{$sth->{NAME_lc}->[$f]} = $rows[$f];
	    $name{$sth->{NAME_lc}->[$f]} ++;
	}
	$r ++;
    }

    $this->{COLS} = join("\t:\t", keys %name);
    $this->{SQL} = $sql;
    $this->{QUERY} = "Table: $table";

    return $sth;
}
newdescriptionprevnextTop
sub new {
    my $pkg = shift;
    my %arg = @_;
    my $dbi = $defaultdbi;
    my $host = $defaulthost;
    my $user = $defaultuser;
    my $pass = $defaultpass;
    my $db = $defaultdb;

    $host = $arg{"-host"} if(length $arg{"-host"});
    $dbi = $arg{"-dbi"}   if(length $arg{"-dbi"});
    $user = $arg{"-user"} if(length $arg{"-user"});
    $pass = $arg{"-pass"} if(length $arg{"-pass"});
    $db = $arg{"-db"}     if(length $arg{"-db"});

    my $this = {};
    bless $this;
    return $this if ($_[0] eq "blessed");

    $dbh = DBI->connect("dbi:$dbi:$db:$host", $user, $pass) || warn $dbh->errstr;

    return $this;
}
outputdescriptionprevnextTop
sub output {
    my $this = shift;
    my $output = shift || 'out.csv';

    open(OUT, '>' . $output) || die $!;
    print OUT 'id,';
    foreach my $col ($this->cols()){
	next if ($col eq 'id');
	print OUT "$col,";
    }
    print OUT "\n";

    foreach my $row ($this->rows()){
	print OUT $this->{$row}->{id}, ",";
	foreach my $col ($this->cols()){
	    next if ($col eq 'id');
	    print OUT $this->{$row}->{$col}, ",";
	}
	print OUT "\n";
    }
    close(OUT);

    return $output;
}
printdescriptionprevnextTop
sub print {
    my $this = shift;
    my $first = shift || 0;
    my $last = shift || 1e12;

    print 'id,';
    foreach my $col ($this->cols()){
	next if ($col eq 'id');
	print "$col,";
    }
    print "\n";

    foreach my $row ($this->rows()){
	next unless($row >= $first);
	last unless($row <= $last);
	print $this->{$row}->{id}, ",";
	foreach my $col ($this->cols()){
	    next if ($col eq 'id');
	    print $this->{$row}->{$col}, ",";
	}
	print "\n";
    }

    return 1;
}
qsqldescriptionprevnextTop
sub qsql {
    my $this = shift;
    my $sql = $dbh->quote(shift);

    my $sth = $dbh->prepare($sql);
    my $ref = $sth->execute();

    unless($ref){
	die $sth->errstr;
    }

    return $sth;
}
savedescriptionprevnextTop
sub save {
    my $this = shift;
    my $table = shift;
    my $force = shift;

    if($force eq 'force'){
	$dbh->do("drop table $table");
    }

    my $sql = "create table $table (id int, ";
    my @tmp;
    my @cols = $this->cols();
    foreach my $col (@cols){
	push(@tmp, $col . " text");
    }
    $sql .= join(', ', @tmp) . ')';

    my $sth = $dbh->prepare($sql);
    my $ref = $sth->execute;

    unless($ref){
	die("\n  SQL:  $sql\n\n" . $sth->errstr);
    }

    foreach my $row ($this->rows("all")){
	my @tmp = ();
	foreach my $col (@cols){
	    push(@tmp, $dbh->quote($this->{$row}->{$col}));
	}
	my $sql = "insert into $table values($row, " . join(', ', @tmp) . ")";
	my $sth = $dbh->prepare($sql);
	my $ref = $sth->execute();

	unless($ref){
	    die("\n  SQL:  $sql\n\n" . $sth->errstr);
	}
    }

    return 1;
}
sqldescriptionprevnextTop
sub sql {
    my $this = shift;
    my $sql = shift;

    my $sth = $dbh->prepare($sql);
    my $ref = $sth->execute();

    unless($ref){
	die $sth->errstr;
    }

    my $r = 0;
    my $f = 0;
    my @rows;
    my %name;
    while(@rows = $sth->fetchrow_array){
	for($f = 0; $f < $sth->{NUM_OF_FIELDS}; $f ++){
	    $this->{$r}->{$sth->{NAME_lc}->[$f]} = $rows[$f];
	    $name{$sth->{NAME_lc}->[$f]} ++;
	}
	$r ++;
    }

    $this->{COLS} = join("\t:\t", keys %name);
    $this->{SQL} = $sql;
    $this->{QUERY} = "SQL: $sql";

    return $sth;
}
viewdescriptionprevnextTop
sub view {
    my $this = shift;
    my $sql = shift;

    my $sth = $dbh->prepare($sql);
    my $ref = $sth->execute;

    unless($ref){
	die $sth->errstr;
    }

    my $r = 0;
    my $f = 0;
    my @rows;
    my %name;
    while(@rows = $sth->fetchrow_array){
	for($f = 0; $f < $sth->{NUM_OF_FIELDS}; $f ++){
	    $this->{$r}->{$sth->{NAME_lc}->[$f]} = $rows[$f];
	    $name{$sth->{NAME_lc}->[$f]} ++;
	}
	$r ++;
    }

    $this->{COLS} = join("\t:\t", keys %name);
    $this->{SQL} = $sql;
    $this->{QUERY} = $sql;

    return $sth;
}
General documentation
AUTHORTop
Kazuharu Arakawa, gaou@sfc.keio.ac.jp
SEE ALSOTop
perl(1).