alter_drop_constraint drop constraint, not index
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Generator / DDL / SQLite.pm
CommitLineData
a1552a5a 1package SQL::Translator::Generator::DDL::SQLite;
2
22c0c10f 3=head1 NAME
d22073f1 4
22c0c10f 5SQL::Translator::Generator::DDL::SQLite - A Moo based SQLite DDL generation
6engine.
7
8=head1 DESCRIPTION
9
10I<documentation volunteers needed>
11
12=cut
33d693c3 13
a1552a5a 14use Moo;
a1552a5a 15
2d23c1e1 16has quote_chars => (is=>'ro', default=>sub { +[qw(" ")] } );
17
39bfaa8b 18with 'SQL::Translator::Generator::Role::Quote';
a1552a5a 19with 'SQL::Translator::Generator::Role::DDL';
20
39bfaa8b 21sub name_sep { q(.) }
a1552a5a 22
23sub _build_type_map {
24 +{
8224e2cf 25 set => 'varchar',
26 bytea => 'blob',
a1552a5a 27 }
28}
29
8224e2cf 30sub _build_sizeless_types {
31 +{
32 text => 1,
33 blob => 1,
34 }
35}
27f0e868 36sub _build_numeric_types {
37 +{
38 int => 1,
39 integer => 1,
40 tinyint => 1,
41 smallint => 1,
42 mediumint => 1,
43 bigint => 1,
44 'unsigned big int' => 1,
45 int2 => 1,
46 int8 => 1,
47 numeric => 1,
48 decimal => 1,
49 boolean => 1,
50 real => 1,
51 double => 1,
52 'double precision' => 1,
53 float => 1,
54 }
55}
a1552a5a 56
57sub _build_unquoted_defaults {
58 +{
59 NULL => 1,
60 'now()' => 1,
61 CURRENT_TIMESTAMP => 1,
62 }
63}
64
65sub nullable { () }
66
27df230e 67sub _ipk {
68 my ($self, $field) = @_;
69
70 my $pk = $field->table->primary_key;
71 my @pk_fields = $pk ? $pk->fields : ();
72
73 $field->is_primary_key && scalar @pk_fields == 1 &&
74 ( $field->data_type =~ /int(eger)?$/i
75 ||
76 ( $field->data_type =~ /^number?$/i && $field->size !~ /,/ ) )
77}
78
7f3f64d7 79sub field_autoinc {
80 my ($self, $field) = @_;
81
82 return (
83 (
84 ($field->extra->{auto_increment_type}||'') eq 'monotonic'
85 and
86 $self->_ipk($field)
87 and
88 $field->is_auto_increment
89 )
90 ? 'AUTOINCREMENT'
91 : ''
92 );
93}
94
a1552a5a 95sub field {
96 my ($self, $field) = @_;
97
7313cecb 98
a1552a5a 99 return join ' ',
100 $self->field_comments($field),
101 $self->field_name($field),
27df230e 102 ( $self->_ipk($field)
7313cecb 103 ? ( 'INTEGER PRIMARY KEY' )
a1552a5a 104 : ( $self->field_type($field) )
105 ),
7f3f64d7 106 ( $self->field_autoinc($field) || () ),
a1552a5a 107 $self->field_nullable($field),
ff6dc6d4 108 $self->field_default($field, {
109 NULL => 1,
110 'now()' => 1,
111 'CURRENT_TIMESTAMP' => 1,
112 }),
a1552a5a 113}
114
a1552a5a 1151;
116
22c0c10f 117=head1 AUTHORS
118
119See the included AUTHORS file:
120L<http://search.cpan.org/dist/SQL-Translator/AUTHORS>
121
122=head1 COPYRIGHT
123
124Copyright (c) 2012 the SQL::Translator L</AUTHORS> as listed above.
125
126=head1 LICENSE
127
128This code is free software and may be distributed under the same terms as Perl
129itself.
130
131=cut