CGI::Wiki::Setup::SQLite - Set up tables for a CGI::Wiki store in a SQLite database.


CGI-Wiki documentation Contained in the CGI-Wiki distribution.

Index


Code Index:

NAME

Top

CGI::Wiki::Setup::SQLite - Set up tables for a CGI::Wiki store in a SQLite database.

SYNOPSIS

Top

  use CGI::Wiki::Setup::SQLite;
  CGI::Wiki::Setup::MySQLite::setup($dbfile);

DESCRIPTION

Top

Set up a SQLite database for use as a CGI::Wiki store.

FUNCIONS

Top

setup
  use CGI::Wiki::Setup::SQLite;

  CGI::Wiki::Setup::SQLite::setup( $filename );

or

  CGI::Wiki::Setup::SQLite::setup( $dbh );

Takes one argument - either the name of the file that the SQLite database is stored in or an active database handle.

NOTE: If a table that the module wants to create already exists, setup will leave it alone. This means that you can safely run this on an existing CGI::Wiki database to bring the schema up to date with the current CGI::Wiki version. If you wish to completely start again with a fresh database, run cleardb first.

cleardb
  use CGI::Wiki::Setup::SQLite;

  # Clear out all CGI::Wiki tables from the database.
  CGI::Wiki::Setup::SQLite::cleardb( $filename );

or

  CGI::Wiki::Setup::SQLite::cleardb( $dbh );

Takes one argument - either the name of the file that the SQLite database is stored in or an active database handle.

Clears out all CGI::Wiki store tables from the database. NOTE that this will lose all your data; you probably only want to use this for testing purposes or if you really screwed up somewhere. Note also that it doesn't touch any CGI::Wiki search backend tables; if you have any of those in the same or a different database see CGI::Wiki::Setup::DBIxFTS or CGI::Wiki::Setup::SII, depending on which search backend you're using.

ALTERNATIVE CALLING SYNTAX

Top

As requested by Podmaster. Instead of passing arguments to the methods as

  ($filename)

you can pass them as

  ( { dbname => $filename } )

or indeed

  ( { dbh => $dbh } )

Note that's a hashref, not a hash.

AUTHOR

Top

Kake Pugh (kake@earth.li).

COPYRIGHT

Top

SEE ALSO

Top

CGI::Wiki, CGI::Wiki::Setup::DBIxFTS, CGI::Wiki::Setup::SII


CGI-Wiki documentation Contained in the CGI-Wiki distribution.
package CGI::Wiki::Setup::SQLite;

use strict;

use vars qw( $VERSION );
$VERSION = '0.06';

use DBI;
use Carp;

my %create_sql = (
    node => "
CREATE TABLE node (
    name      varchar(200) NOT NULL DEFAULT '',
    version   integer      NOT NULL default 0,
    text      mediumtext   NOT NULL default '',
    modified  datetime     default NULL,
    PRIMARY KEY (name)
)
",
    content => "
CREATE TABLE content (
    name      varchar(200) NOT NULL default '',
    version   integer      NOT NULL default 0,
    text      mediumtext   NOT NULL default '',
    modified  datetime     default NULL,
    comment   mediumtext   NOT NULL default '',
    PRIMARY KEY (name, version)
)
",
    internal_links => "
CREATE TABLE internal_links (
    link_from varchar(200) NOT NULL default '',
    link_to   varchar(200) NOT NULL default '',
    PRIMARY KEY (link_from, link_to)
)
",
    metadata => "
CREATE TABLE metadata (
    node           varchar(200) NOT NULL DEFAULT '',
    version        integer      NOT NULL default 0,
    metadata_type  varchar(200) NOT NULL DEFAULT '',
    metadata_value mediumtext   NOT NULL DEFAULT ''
)
"
);

sub setup {
    my @args = @_;
    my $dbh = _get_dbh( @args );
    my $disconnect_required = _disconnect_required( @args );

    # Check whether tables exist, set them up if not.
    my $sql = "SELECT name FROM sqlite_master
                              WHERE type='table' AND name in ("
            . join( ",", map { $dbh->quote($_) } keys %create_sql ) . ")";
    my $sth = $dbh->prepare($sql) or croak $dbh->errstr;
    $sth->execute;
    my %tables;
    while ( my $table = $sth->fetchrow_array ) {
        $tables{$table} = 1;
    }

    foreach my $required ( keys %create_sql ) {
        if ( $tables{$required} ) {
            print "Table $required already exists... skipping...\n";
        } else {
            print "Creating table $required... done\n";
            $dbh->do($create_sql{$required}) or croak $dbh->errstr;
        }
    }

    # Clean up if we made our own dbh.
    $dbh->disconnect if $disconnect_required;
}

sub cleardb {
    my @args = @_;
    my $dbh = _get_dbh( @args );
    my $disconnect_required = _disconnect_required( @args );

    print "Dropping tables... ";
    my $sql = "SELECT name FROM sqlite_master
                              WHERE type='table' AND name in ("
            . join( ",", map { $dbh->quote($_) } keys %create_sql ) . ")";
    foreach my $tableref (@{$dbh->selectall_arrayref($sql)}) {
        $dbh->do("DROP TABLE $tableref->[0]") or croak $dbh->errstr;
    }
    print "done\n";

    # Clean up if we made our own dbh.
    $dbh->disconnect if $disconnect_required;
}

sub _get_dbh {
    # Database handle passed in.
    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
        return $_[0];
    }

    # Args passed as hashref.
    if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
        my %args = %{$_[0]};
        if ( $args{dbh} ) {
            return $args{dbh};
	} else {
            return _make_dbh( %args );
        }
    }

    # Args passed as list of connection details.
    return _make_dbh( dbname => $_[0] );
}

sub _disconnect_required {
    # Database handle passed in.
    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
        return 0;
    }

    # Args passed as hashref.
    if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
        my %args = %{$_[0]};
        if ( $args{dbh} ) {
            return 0;
	} else {
            return 1;
        }
    }

    # Args passed as list of connection details.
    return 1;
}

sub _make_dbh {
    my %args = @_;
    my $dbh = DBI->connect("dbi:SQLite:dbname=$args{dbname}", "", "",
			   { PrintError => 1, RaiseError => 1,
			     AutoCommit => 1 } )
      or croak DBI::errstr;
    return $dbh;
}

1;