internal package
Foswiki::DBI Interface for Foswiki DBI developers
StaticMethod
getDB() → $database Creates a database connection to the configured implementation, connects to it and loads the base schema.
StaticMethod
loadSchema($schemaBase) → $database $schemaBase
is the
base perl class of the caller. The real database schema being loaded
resides in a sub class of the $schemaBase
according to the database
implementation of the system.
For example a MyPlugin
must provide the following classes to support
SQLite and MariaDB:
Foswiki::Plugins::MyPlugin::Schema Foswiki::Plugins::MyPlugin::Schema::MariaDB Foswiki::Plugins::MyPlugin::Schema::SQLite
The schema is then called using:
my $db = Foswiki::DBI::loadSchema("Foswiki::Plugins::MyPlugin::Schema");Given
MariaDB
is the current database implementation, it actually loads the
schema Foswiki::Plugins::MyPlugin::Schema::MariaDB
and returns a singleton
database object of type Foswiki::DBI::Database::MariaDB
.
This singleton object is shared among all subsystems connecting to the
database.
StaticMethod
finish() Close any database connection being made during the session
internal package
Foswiki::DBI::Database abstract class for any type of database connecting to foswiki
ClassMethod
new() Foswiki::DBI::Database::MariaDB
.
Subclasses need to specify the actual DBD driver to connect to the database.
ObjectMethod
getClassName() → $string MariaDB
ObjectMethod
applySchema($schema) Applies the Schema to the connected database. this is called only once when the database is connected. note that the schema must test for existing tables and indexes on its own.
ObjectMethod
schemaVersion($type, $version) → $version getter/setter for the schema version meta data
ObjectMethod
handler() → $dbh Returns the DBD handler that this class is delegating all work to
ObjectMethod
connect() Connects to the database if not already done so and returns a DBI::db handler. this method is called automatically when the db handler is established
ObjectMethod
finish() Foswikik::DBI::finish()
to finalize the database connection
and close any open sockets.
ObjectMethod
eachRow($tableName, %params) → $iterator Foswiki::Iterator::DBIterator
for the given parameters. This is a convenience wrapper for
my $it = Foswiki::Iterator::DBIterator->new($dbh, $stm);The statement handler is created based on the parameters provided. The
%params
parameter is a hash with the following values:
$tableName
are optional.
Example:
my $it = Foswiki::DBI::getDB->eachRow("SomeTable", count => "*" firstName => "Michael" ); while ($it->hasNext) { my $row = $it->next(); my $firstName = $row->{firstName}; my $middleName = $row->{middleName}; my $lastName = $row->{lastName}; my $count = $row->{count}; ... }
internal package
Foswiki::DBI::Schema A schema is used to define tables and indexes in an SQL database. Plugins may define a schema for their own needs by subclassing this class. In general a plugin must define a schema class for each database vendor it desires to support, such as SQLite, MariaDB, Oracle etc.
Two functions need to be implemented:getType()
and getDefinition()
The schema base is then passed on to Foswiki::DBI::loadSchema()
See Foswiki::DBI
for more information.
ClassMethod
new() Constructs an object of this type.
ObjectMethod
getType() → $string Foswiki::Plugins::LikePlugin::Schema::getType()
returns the string "LikePlugin". This string may be used in the
schema definition using the "%prefix%"
placeholder.
ObjectMethod
getDefinition() → $array %prefix%
placeholder being
replaced by the value of getType()
For example, the $array
returned by the subclass may look like this:
sub getDefinition { return [[ 'CREATE TABLE IF NOT EXISTS %prefix%likes ( id INTEGER PRIMARY KEY AUTOINCREMENT, web VARCHAR(255), topic VARCHAR(255), meta_type CHAR(20), meta_id VARCHAR(255), username VARCHAR(255), like_count INTEGER DEFAULT 0, dislike_count INTEGER DEFAULT 0, timestamp INTEGER )', 'CREATE UNIQUE INDEX IF NOT EXISTS %prefix%_idx_likes on %prefix%likes (web, topic, username, meta_type, meta_id)' ], [ "ALTER TABLE %prefix%likes ..." ]]; }In a first version of the schema definition, it create a table
LikePlugin_likes
and an index LikePlugin_idx_likes
.
Later on during the life span of the LikePlugin a modification to the initial definition is required. That's why
there is a second element with an "ALTER TABLE" clause to update any preexisting SQL structure incrementally.
This approach migrates a table structure seamlessly as required. The required updates are tracked by the schema loader
of DBIPlugin. The version of the schema is being tracked in a separate table db_meta
. In the above example
an entry will be added to the db_meta
table for the "LikePlugin" schema being of version 2 (as there are two elements in
the returned $array
.
internal package
Foswiki::Iterator::DBIterator is a
Foswiki::Iterator ClassMethod
new($dbh, $select, $values, $process) next()
below (optional)
DBIterator
may be used in its own but is mostly created as part of Foswiki::DBI::Database::eachRow()
.
Example use:
my $it = Foswiki::Iterator::DBIterator($dbh, "select * from ... where ..."); while ($it->hasNext) { my $row = $it->next(); my $firstName = $row->{firstName}; my $middleName = $row->{middleName}; my $lastName = $row->{lastName}; ... }
ObjectMethod
hasNext() → $boolean next()
.
ObjectMethod
next() → $row $row
return value is a hash reference as being created by DBI::fetchrow_hashref
ObjectMethod
count() → $integer returns the number of rows affected by this iterator
ObjectMethod
reset()