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