File: SQLite.pm

package info (click to toggle)
libdbix-class-perl 0.07003-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,396 kB
  • ctags: 764
  • sloc: perl: 7,046; sql: 217; makefile: 43
file content (94 lines) | stat: -rw-r--r-- 2,213 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package DBIx::Class::Test::SQLite;

=head1 NAME

DBIx::Class::Test::SQLite - Base class for running Class::DBI tests against DBIx::Class compat layer, shamelessly ripped from Class::DBI::Test::SQLite

=head1 SYNOPSIS

  use base 'DBIx::Class::Test::SQLite';

  __PACKAGE__->set_table('test');
  __PACKAGE__->columns(All => qw/id name film salary/);

  sub create_sql {
      return q{
          id     INTEGER PRIMARY KEY,
          name   CHAR(40),
          film   VARCHAR(255),
          salary INT
      }
  }
    
=head1 DESCRIPTION

This provides a simple base class for DBIx::Class::CDBICompat tests using
SQLite.  Each class for the test should inherit from this, provide a
create_sql() method which returns a string representing the SQL used to
create the table for the class, and then call set_table() to create the
table, and tie it to the class.

=cut

use strict;
use warnings;

use base qw/DBIx::Class/;

__PACKAGE__->load_components(qw/CDBICompat Core DB/);

use File::Temp qw/tempfile/;
my (undef, $DB) = tempfile();
END { unlink $DB if -e $DB }

my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 1, RaiseError => 1 });

__PACKAGE__->connection(@DSN);
__PACKAGE__->set_sql(_table_pragma => 'PRAGMA table_info(__TABLE__)');
__PACKAGE__->set_sql(_create_me    => 'CREATE TABLE __TABLE__ (%s)');
__PACKAGE__->storage->dbh->do("PRAGMA synchronous = OFF");

=head1 METHODS

=head2 set_table

    __PACKAGE__->set_table('test');

This combines creating the table with the normal DBIx::Class table()
call.

=cut

sub set_table {
    my ($class, $table) = @_;
    $class->table($table);
    $class->_create_test_table;
}

sub _create_test_table {
    my $class = shift;
    my @vals  = $class->sql__table_pragma->select_row;
    $class->sql__create_me($class->create_sql)->execute unless @vals;
}

=head2 create_sql

This is an abstract method you must override.

  sub create_sql {
      return q{
          id     INTEGER PRIMARY KEY,
          name   CHAR(40),
          film   VARCHAR(255),
          salary INT
      }
  }

This should return, as a text string, the schema for the table represented
by this class.

=cut

sub create_sql { die "create_sql() not implemented by $_[0]\n" }

1;