1 package DBIx::Class::Schema::Loader::SQLite;
5 use base qw/DBIx::Class::Schema::Loader::Generic/;
7 use Text::Balanced qw( extract_bracketed );
11 DBIx::Class::Schema::Loader::SQLite - DBIx::Class::Schema::Loader SQLite Implementation.
16 use base qw/DBIx::Class::Schema::Loader/;
18 __PACKAGE__->load_from_connection(
19 dsn => "dbi:SQLite:dbname=/path/to/dbfile",
26 See L<DBIx::Class::Schema::Loader>.
31 return qw/PK::Auto::SQLite/;
34 # XXX this really needs a re-factor
35 sub _load_relationships {
37 foreach my $table ( $self->tables ) {
39 my $dbh = $self->schema->storage->dbh;
40 my $sth = $dbh->prepare(<<"");
41 SELECT sql FROM sqlite_master WHERE tbl_name = ?
43 $sth->execute($table);
44 my ($sql) = $sth->fetchrow_array;
47 # Cut "CREATE TABLE ( )" blabla...
48 $sql =~ /^[\w\s]+\((.*)\)$/si;
51 # strip single-line comments
52 $cols =~ s/\-\-.*\n/\n/g;
54 # temporarily replace any commas inside parens,
55 # so we don't incorrectly split on them below
56 my $cols_no_bracketed_commas = $cols;
57 while ( my $extracted =
58 ( extract_bracketed( $cols, "()", "[^(]*" ) )[0] )
60 my $replacement = $extracted;
61 $replacement =~ s/,/--comma--/g;
62 $replacement =~ s/^\(//;
63 $replacement =~ s/\)$//;
64 $cols_no_bracketed_commas =~ s/$extracted/$replacement/m;
67 # Split column definitions
68 for my $col ( split /,/, $cols_no_bracketed_commas ) {
70 # put the paren-bracketed commas back, to help
71 # find multi-col fks below
72 $col =~ s/\-\-comma\-\-/,/g;
74 $col =~ s/^\s*FOREIGN\s+KEY\s*//i;
76 # Strip punctuations around key and table names
77 $col =~ s/[\[\]'"]/ /g;
82 next if $col !~ /^(.*)\s+REFERENCES\s+(\w+) (?: \s* \( (.*) \) )? /ix;
84 my ($cols, $f_table, $f_cols) = ($1, $2, $3);
86 if($cols =~ /^\(/) { # Table-level
97 my @cols = map { s/\s*//g; $_ } split(/\s*,\s*/,$cols);
98 my @f_cols = map { s/\s*//g; $_ } split(/\s*,\s*/,$f_cols);
99 die "Mismatched column count in rel for $table => $f_table"
102 for(my $i = 0 ; $i < @cols; $i++) {
103 $cond->{$f_cols[$i]} = $cols[$i];
105 eval { $self->_make_cond_rel( $table, $f_table, $cond ) };
108 eval { $self->_make_simple_rel( $table, $f_table, $cols ) };
111 warn qq/\# belongs_to_many failed "$@"\n\n/
112 if $@ && $self->debug;
119 my $dbh = $self->schema->storage->dbh;
120 my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
123 while ( my $row = $sth->fetchrow_hashref ) {
124 next unless lc( $row->{type} ) eq 'table';
125 push @tables, $row->{tbl_name};
131 my ( $self, $table ) = @_;
134 my $dbh = $self->schema->storage->dbh;
135 my $sth = $dbh->prepare("PRAGMA table_info('$table')");
138 while ( my $row = $sth->fetchrow_hashref ) {
139 push @columns, $row->{name};
143 # find primary key. so complex ;-(
144 $sth = $dbh->prepare(<<'SQL');
145 SELECT sql FROM sqlite_master WHERE tbl_name = ?
147 $sth->execute($table);
148 my ($sql) = $sth->fetchrow_array;
150 my ($primary) = $sql =~ m/
151 (?:\(|\,) # either a ( to start the definition or a , for next
152 \s* # maybe some whitespace
154 [^,]* # anything but the end or a ',' for next column
162 my ($pks) = $sql =~ m/PRIMARY\s+KEY\s*\(\s*([^)]+)\s*\)/i;
163 @pks = split( m/\s*\,\s*/, $pks ) if $pks;
165 return ( \@columns, \@pks );
170 L<DBIx::Schema::Class::Loader>