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