new dev release
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / SQLite.pm
CommitLineData
996be9ee 1package DBIx::Class::Schema::Loader::DBI::SQLite;
2
3use strict;
4use warnings;
5use base qw/DBIx::Class::Schema::Loader::DBI/;
fa994d3c 6use Carp::Clan qw/^DBIx::Class/;
996be9ee 7use Text::Balanced qw( extract_bracketed );
25328cc4 8use Class::C3;
996be9ee 9
0a701ff3 10our $VERSION = '0.04999_12';
32f784fc 11
996be9ee 12=head1 NAME
13
14DBIx::Class::Schema::Loader::DBI::SQLite - DBIx::Class::Schema::Loader::DBI SQLite Implementation.
15
16=head1 SYNOPSIS
17
18 package My::Schema;
19 use base qw/DBIx::Class::Schema::Loader/;
20
59cfa251 21 __PACKAGE__->loader_options( debug => 1 );
996be9ee 22
23 1;
24
25=head1 DESCRIPTION
26
27See L<DBIx::Class::Schema::Loader::Base>.
28
3b7f80f9 29=head1 METHODS
30
31=head2 rescan
32
33SQLite will fail all further commands on a connection if the
34underlying schema has been modified. Therefore, any runtime
35changes requiring C<rescan> also require us to re-connect
36to the database. The C<rescan> method here handles that
37reconnection for you, but beware that this must occur for
38any other open sqlite connections as well.
39
996be9ee 40=cut
41
3b7f80f9 42sub rescan {
43 my ($self, $schema) = @_;
44
45 $schema->storage->disconnect if $schema->storage;
46 $self->next::method($schema);
47}
48
996be9ee 49# XXX this really needs a re-factor
50sub _sqlite_parse_table {
51 my ($self, $table) = @_;
52
53 my @rels;
54 my @uniqs;
78b7ccaa 55 my %auto_inc;
996be9ee 56
57 my $dbh = $self->schema->storage->dbh;
5223f24a 58 my $sth = $self->{_cache}->{sqlite_master}
59 ||= $dbh->prepare(q{SELECT sql FROM sqlite_master WHERE tbl_name = ?});
996be9ee 60
61 $sth->execute($table);
62 my ($sql) = $sth->fetchrow_array;
63 $sth->finish;
64
65 # Cut "CREATE TABLE ( )" blabla...
d83f0539 66 $sql =~ /^[\w\s']+\((.*)\)$/si;
996be9ee 67 my $cols = $1;
68
69 # strip single-line comments
70 $cols =~ s/\-\-.*\n/\n/g;
71
72 # temporarily replace any commas inside parens,
73 # so we don't incorrectly split on them below
74 my $cols_no_bracketed_commas = $cols;
75 while ( my $extracted =
76 ( extract_bracketed( $cols, "()", "[^(]*" ) )[0] )
77 {
78 my $replacement = $extracted;
79 $replacement =~ s/,/--comma--/g;
80 $replacement =~ s/^\(//;
81 $replacement =~ s/\)$//;
82 $cols_no_bracketed_commas =~ s/$extracted/$replacement/m;
83 }
84
85 # Split column definitions
86 for my $col ( split /,/, $cols_no_bracketed_commas ) {
87
88 # put the paren-bracketed commas back, to help
89 # find multi-col fks below
90 $col =~ s/\-\-comma\-\-/,/g;
91
92 $col =~ s/^\s*FOREIGN\s+KEY\s*//i;
93
94 # Strip punctuations around key and table names
95 $col =~ s/[\[\]'"]/ /g;
96 $col =~ s/^\s+//gs;
97
98 # Grab reference
99 chomp $col;
100
7e6d4a22 101 if($col =~ /^(.*)\s+UNIQUE/i) {
996be9ee 102 my $colname = $1;
103 $colname =~ s/\s+.*$//;
104 push(@uniqs, [ "${colname}_unique" => [ lc $colname ] ]);
105 }
7e6d4a22 106 elsif($col =~/^\s*UNIQUE\s*\(\s*(.*)\)/i) {
996be9ee 107 my $cols = $1;
108 $cols =~ s/\s+$//;
109 my @cols = map { lc } split(/\s*,\s*/, $cols);
110 my $name = join(q{_}, @cols) . '_unique';
111 push(@uniqs, [ $name => \@cols ]);
112 }
113
78b7ccaa 114 if ($col =~ /AUTOINCREMENT/i) {
115 $col =~ /^(\S+)/;
116 $auto_inc{lc $1} = 1;
117 }
118
f8c39323 119 next if $col !~ /^(.*\S)\s+REFERENCES\s+(\w+) (?: \s* \( (.*) \) )? /ix;
996be9ee 120
121 my ($cols, $f_table, $f_cols) = ($1, $2, $3);
122
123 if($cols =~ /^\(/) { # Table-level
124 $cols =~ s/^\(\s*//;
125 $cols =~ s/\s*\)$//;
126 }
127 else { # Inline
128 $cols =~ s/\s+.*$//;
129 }
130
131 my @cols = map { s/\s*//g; lc $_ } split(/\s*,\s*/,$cols);
132 my $rcols;
133 if($f_cols) {
134 my @f_cols = map { s/\s*//g; lc $_ } split(/\s*,\s*/,$f_cols);
25328cc4 135 croak "Mismatched column count in rel for $table => $f_table"
996be9ee 136 if @cols != @f_cols;
137 $rcols = \@f_cols;
138 }
139 push(@rels, {
140 local_columns => \@cols,
141 remote_columns => $rcols,
142 remote_table => $f_table,
143 });
144 }
145
78b7ccaa 146 return { rels => \@rels, uniqs => \@uniqs, auto_inc => \%auto_inc };
147}
148
a8df0345 149sub _extra_column_info {
78b7ccaa 150 my ($self, $table, $col_name, $sth, $col_num) = @_;
632cb26f 151 ($table, $col_name) = @{$table}{qw/TABLE_NAME COLUMN_NAME/} if ref $table;
a8df0345 152 my %extra_info;
153
78b7ccaa 154 $self->{_sqlite_parse_data}->{$table} ||=
155 $self->_sqlite_parse_table($table);
156
a8df0345 157 if ($self->{_sqlite_parse_data}->{$table}->{auto_inc}->{$col_name}) {
158 $extra_info{is_auto_increment} = 1;
159 }
160
161 return \%extra_info;
996be9ee 162}
163
164sub _table_fk_info {
165 my ($self, $table) = @_;
166
167 $self->{_sqlite_parse_data}->{$table} ||=
168 $self->_sqlite_parse_table($table);
169
170 return $self->{_sqlite_parse_data}->{$table}->{rels};
171}
172
173sub _table_uniq_info {
174 my ($self, $table) = @_;
175
176 $self->{_sqlite_parse_data}->{$table} ||=
177 $self->_sqlite_parse_table($table);
178
179 return $self->{_sqlite_parse_data}->{$table}->{uniqs};
180}
181
182sub _tables_list {
183 my $self = shift;
5223f24a 184
996be9ee 185 my $dbh = $self->schema->storage->dbh;
5223f24a 186 my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
996be9ee 187 $sth->execute;
188 my @tables;
189 while ( my $row = $sth->fetchrow_hashref ) {
190 next unless lc( $row->{type} ) eq 'table';
522ee84e 191 next if $row->{tbl_name} =~ /^sqlite_/;
996be9ee 192 push @tables, $row->{tbl_name};
193 }
3b7f80f9 194 $sth->finish;
996be9ee 195 return @tables;
196}
197
198=head1 SEE ALSO
199
200L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
201L<DBIx::Class::Schema::Loader::DBI>
202
be80bba7 203=head1 AUTHOR
204
205See L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
206
207=head1 LICENSE
208
209This library is free software; you can redistribute it and/or modify it under
210the same terms as Perl itself.
211
996be9ee 212=cut
213
2141;