Commit | Line | Data |
996be9ee |
1 | package DBIx::Class::Schema::Loader::DBI::SQLite; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use base qw/DBIx::Class::Schema::Loader::DBI/; |
fa994d3c |
6 | use Carp::Clan qw/^DBIx::Class/; |
996be9ee |
7 | use Text::Balanced qw( extract_bracketed ); |
25328cc4 |
8 | use Class::C3; |
996be9ee |
9 | |
b327622b |
10 | our $VERSION = '0.04999_05'; |
32f784fc |
11 | |
996be9ee |
12 | =head1 NAME |
13 | |
14 | DBIx::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 | |
27 | See L<DBIx::Class::Schema::Loader::Base>. |
28 | |
3b7f80f9 |
29 | =head1 METHODS |
30 | |
31 | =head2 rescan |
32 | |
33 | SQLite will fail all further commands on a connection if the |
34 | underlying schema has been modified. Therefore, any runtime |
35 | changes requiring C<rescan> also require us to re-connect |
36 | to the database. The C<rescan> method here handles that |
37 | reconnection for you, but beware that this must occur for |
38 | any other open sqlite connections as well. |
39 | |
996be9ee |
40 | =cut |
41 | |
3b7f80f9 |
42 | sub 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 |
50 | sub _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 |
149 | sub _extra_column_info { |
78b7ccaa |
150 | my ($self, $table, $col_name, $sth, $col_num) = @_; |
a8df0345 |
151 | my %extra_info; |
152 | |
78b7ccaa |
153 | $self->{_sqlite_parse_data}->{$table} ||= |
154 | $self->_sqlite_parse_table($table); |
155 | |
a8df0345 |
156 | if ($self->{_sqlite_parse_data}->{$table}->{auto_inc}->{$col_name}) { |
157 | $extra_info{is_auto_increment} = 1; |
158 | } |
159 | |
160 | return \%extra_info; |
996be9ee |
161 | } |
162 | |
163 | sub _table_fk_info { |
164 | my ($self, $table) = @_; |
165 | |
166 | $self->{_sqlite_parse_data}->{$table} ||= |
167 | $self->_sqlite_parse_table($table); |
168 | |
169 | return $self->{_sqlite_parse_data}->{$table}->{rels}; |
170 | } |
171 | |
172 | sub _table_uniq_info { |
173 | my ($self, $table) = @_; |
174 | |
175 | $self->{_sqlite_parse_data}->{$table} ||= |
176 | $self->_sqlite_parse_table($table); |
177 | |
178 | return $self->{_sqlite_parse_data}->{$table}->{uniqs}; |
179 | } |
180 | |
181 | sub _tables_list { |
182 | my $self = shift; |
5223f24a |
183 | |
996be9ee |
184 | my $dbh = $self->schema->storage->dbh; |
5223f24a |
185 | my $sth = $dbh->prepare("SELECT * FROM sqlite_master"); |
996be9ee |
186 | $sth->execute; |
187 | my @tables; |
188 | while ( my $row = $sth->fetchrow_hashref ) { |
189 | next unless lc( $row->{type} ) eq 'table'; |
522ee84e |
190 | next if $row->{tbl_name} =~ /^sqlite_/; |
996be9ee |
191 | push @tables, $row->{tbl_name}; |
192 | } |
3b7f80f9 |
193 | $sth->finish; |
996be9ee |
194 | return @tables; |
195 | } |
196 | |
197 | =head1 SEE ALSO |
198 | |
199 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
200 | L<DBIx::Class::Schema::Loader::DBI> |
201 | |
202 | =cut |
203 | |
204 | 1; |