1 package DBIx::Class::Schema::Loader::SQLite;
6 use base qw/DBIx::Class::Schema::Loader::Generic/;
8 use Text::Balanced qw( extract_bracketed );
12 DBIx::Class::Schema::Loader::SQLite - DBIx::Class::Schema::Loader SQLite Implementation.
16 use DBIx::Class::Schema::Loader;
18 # $loader is a DBIx::Class::Schema::Loader::SQLite
19 my $loader = DBIx::Class::Schema::Loader->new(
20 dsn => "dbi:SQLite:dbname=/path/to/dbfile",
25 See L<DBIx::Class::Schema::Loader>.
30 return qw/DBIx::Class::PK::Auto::SQLite/;
33 # XXX this really needs a re-factor
34 sub _load_relationships {
36 foreach my $table ( $self->tables ) {
38 my $dbh = $self->schema->storage->dbh;
39 my $sth = $dbh->prepare(<<"");
40 SELECT sql FROM sqlite_master WHERE tbl_name = ?
42 $sth->execute($table);
43 my ($sql) = $sth->fetchrow_array;
46 # Cut "CREATE TABLE ( )" blabla...
47 $sql =~ /^[\w\s]+\((.*)\)$/si;
50 # strip single-line comments
51 $cols =~ s/\-\-.*\n/\n/g;
53 # temporarily replace any commas inside parens,
54 # so we don't incorrectly split on them below
55 my $cols_no_bracketed_commas = $cols;
56 while ( my $extracted =
57 ( extract_bracketed( $cols, "()", "[^(]*" ) )[0] )
59 my $replacement = $extracted;
60 $replacement =~ s/,/--comma--/g;
61 $replacement =~ s/^\(//;
62 $replacement =~ s/\)$//;
63 $cols_no_bracketed_commas =~ s/$extracted/$replacement/m;
66 # Split column definitions
67 for my $col ( split /,/, $cols_no_bracketed_commas ) {
69 # put the paren-bracketed commas back, to help
70 # find multi-col fks below
71 $col =~ s/\-\-comma\-\-/,/g;
73 $col =~ s/^\s*FOREIGN\s+KEY\s*//i;
75 # Strip punctuations around key and table names
76 $col =~ s/[\[\]'"]/ /g;
81 next if $col !~ /^(.*)\s+REFERENCES\s+(\w+) (?: \s* \( (.*) \) )? /ix;
83 my ($cols, $f_table, $f_cols) = ($1, $2, $3);
85 if($cols =~ /^\(/) { # Table-level
96 my @cols = map { s/\s*//g; $_ } split(/\s*,\s*/,$cols);
97 my @f_cols = map { s/\s*//g; $_ } split(/\s*,\s*/,$f_cols);
98 die "Mismatched column count in rel for $table => $f_table"
101 for(my $i = 0 ; $i < @cols; $i++) {
102 $cond->{$f_cols[$i]} = $cols[$i];
104 eval { $self->_make_cond_rel( $table, $f_table, $cond ) };
107 eval { $self->_make_simple_rel( $table, $f_table, $cols ) };
110 warn qq/\# belongs_to_many failed "$@"\n\n/
111 if $@ && $self->debug;
118 my $dbh = $self->schema->storage->dbh;
119 my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
122 while ( my $row = $sth->fetchrow_hashref ) {
123 next unless lc( $row->{type} ) eq 'table';
124 push @tables, $row->{tbl_name};
130 my ( $self, $table ) = @_;
133 my $dbh = $self->schema->storage->dbh;
134 my $sth = $dbh->prepare("PRAGMA table_info('$table')");
137 while ( my $row = $sth->fetchrow_hashref ) {
138 push @columns, $row->{name};
142 # find primary key. so complex ;-(
143 $sth = $dbh->prepare(<<'SQL');
144 SELECT sql FROM sqlite_master WHERE tbl_name = ?
146 $sth->execute($table);
147 my ($sql) = $sth->fetchrow_array;
149 my ($primary) = $sql =~ m/
150 (?:\(|\,) # either a ( to start the definition or a , for next
151 \s* # maybe some whitespace
153 [^,]* # anything but the end or a ',' for next column
161 my ($pks) = $sql =~ m/PRIMARY\s+KEY\s*\(\s*([^)]+)\s*\)/i;
162 @pks = split( m/\s*\,\s*/, $pks ) if $pks;
164 return ( \@columns, \@pks );
169 L<DBIx::Schema::Class::Loader>