The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Slick::Database

SYNOPSIS

An OO wrapper around DBI and SQL::Abstract.

Currently Slick::Database supports MySQL and PostgreSQL. Note, you will need to install the driver that you'd like to use manually, as Slick does not come bundled with any.

See Slick::DatabaseExecutor for lower level information on how Slick::Database works.

API

dbi

Returns the underlying DBI driver object associated with the database.

execute

Runs some statement against the underlying DBI driver object associated with the database.

select, select_one, update, delete, insert

    my $users = $s->database('my_postgres')
                ->select('users', [ 'id', 'name' ]); # SELECT id, name FROM users;

    my $user = $s->database('my_postgres')
                ->select_one('users', [ 'id', 'name', 'age' ], { id => 1 }); # SELECT id, name, age FROM users WHERE id = 1;

    $s->database('my_postgres')
      ->insert('users', { name => 'Bob', age => 23 }); # INSERT INTO users (name, age) VALUES ('Bob', 23);

    $s->database('my_postgres')
      ->update('users', { name => 'John' }, { id => 2 }); # UPDATE users SET name = 'John' WHERE id = 2;

    $s->database('my_postgres')
      ->delete('users', { id => 2 }); # DELETE FROM users WHERE id = 2;

Wrapper around SQL::Abstract, see SQL::Abstract for more information on how to use these methods.

See "dbi" if you would like to directly use the DBI connection instead of SQL::Abstract.

migrations

    $s->database('db')->migrations;

Returns a HashRef with all of the migrations associated with the database.

migration

    $s->database('db')->migration(
        'migration_id', # id
        'CREATE TABLE foo (id INT PRIMARY KEY);', # up
        'DROP TABLE foo;' # down
    );

Create a migration and associate it with the database.

migrate_up

    $s->database('db')->migrate_up;

Runs all pending migrations against the database.

If you wish to only migrate a single migration, you can provide the id of the migration you'd like to run:

    $s->database('db')->migrate_up('migration_id');

migrate_down

    $s->database('db')->migrate_down;

Migrates all migrations down effectively destroying your database.

If you wish to only de-migrate a single migration, you can provide the id of the migration you'd like to run:

    $s->database('db')->migrate_down('migration_id');

See also

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 284:

Unknown directive: =over2

Around line 286:

'=item' outside of any '=over'