1 package DBIx::Class::Schema::Loader::SQLite;
4 use base qw/DBIx::Class::Schema::Loader::Generic/;
5 use Text::Balanced qw( extract_bracketed );
10 DBIx::Class::Schema::Loader::SQLite - DBIx::Class::Schema::Loader SQLite Implementation.
14 use DBIx::Class::Schema::Loader;
16 # $loader is a DBIx::Class::Schema::Loader::SQLite
17 my $loader = DBIx::Class::Schema::Loader->new(
18 dsn => "dbi:SQLite:dbname=/path/to/dbfile",
23 See L<DBIx::Class::Schema::Loader>.
27 sub _loader_db_classes {
28 return qw/DBIx::Class::PK::Auto::SQLite/;
31 # XXX this really needs a re-factor
32 sub _loader_relationships {
34 foreach my $table ( $class->tables ) {
36 my $dbh = $class->storage->dbh;
37 my $sth = $dbh->prepare(<<"");
38 SELECT sql FROM sqlite_master WHERE tbl_name = ?
40 $sth->execute($table);
41 my ($sql) = $sth->fetchrow_array;
44 # Cut "CREATE TABLE ( )" blabla...
45 $sql =~ /^[\w\s]+\((.*)\)$/si;
48 # strip single-line comments
49 $cols =~ s/\-\-.*\n/\n/g;
51 # temporarily replace any commas inside parens,
52 # so we don't incorrectly split on them below
53 my $cols_no_bracketed_commas = $cols;
54 while ( my $extracted =
55 ( extract_bracketed( $cols, "()", "[^(]*" ) )[0] )
57 my $replacement = $extracted;
58 $replacement =~ s/,/--comma--/g;
59 $replacement =~ s/^\(//;
60 $replacement =~ s/\)$//;
61 $cols_no_bracketed_commas =~ s/$extracted/$replacement/m;
64 # Split column definitions
65 for my $col ( split /,/, $cols_no_bracketed_commas ) {
67 # put the paren-bracketed commas back, to help
68 # find multi-col fks below
69 $col =~ s/\-\-comma\-\-/,/g;
71 $col =~ s/^\s*FOREIGN\s+KEY\s*//i;
73 # Strip punctuations around key and table names
74 $col =~ s/[\[\]'"]/ /g;
79 if( $col =~ /^(.*)\s+REFERENCES\s+(\w+) (?: \s* \( (.*) \) )? /ix ) {
81 my ($cols, $f_table, $f_cols) = ($1, $2, $3);
83 if($cols =~ /^\(/) { # Table-level
94 my @cols = map { s/\s*//g; $_ } split(/\s*,\s*/,$cols);
95 my @f_cols = map { s/\s*//g; $_ } split(/\s*,\s*/,$f_cols);
96 die "Mismatched column count in rel for $table => $f_table"
99 for(my $i = 0 ; $i < @cols; $i++) {
100 $cond->{$f_cols[$i]} = $cols[$i];
107 eval { $class->_loader_make_relations( $table, $f_table, $cond ) };
108 warn qq/\# belongs_to_many failed "$@"\n\n/
109 if $@ && $class->_loader_debug;
117 my $dbh = $class->storage->dbh;
118 my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
121 while ( my $row = $sth->fetchrow_hashref ) {
122 next unless lc( $row->{type} ) eq 'table';
123 push @tables, $row->{tbl_name};
128 sub _loader_table_info {
129 my ( $class, $table ) = @_;
132 my $dbh = $class->storage->dbh;
133 my $sth = $dbh->prepare("PRAGMA table_info('$table')");
136 while ( my $row = $sth->fetchrow_hashref ) {
137 push @columns, $row->{name};
141 # find primary key. so complex ;-(
142 $sth = $dbh->prepare(<<'SQL');
143 SELECT sql FROM sqlite_master WHERE tbl_name = ?
145 $sth->execute($table);
146 my ($sql) = $sth->fetchrow_array;
148 my ($primary) = $sql =~ m/
149 (?:\(|\,) # either a ( to start the definition or a , for next
150 \s* # maybe some whitespace
152 [^,]* # anything but the end or a ',' for next column
160 my ($pks) = $sql =~ m/PRIMARY\s+KEY\s*\(\s*([^)]+)\s*\)/i;
161 @pks = split( m/\s*\,\s*/, $pks ) if $pks;
163 return ( \@columns, \@pks );
168 L<DBIx::Schema::Class::Loader>