comparison Lib/IMPL/SQL/Schema/Traits/MysqlFormatter.pm @ 269:dacfe7c0311a

SQL schema updated (unstable)
author sergey
date Thu, 24 Jan 2013 20:00:27 +0400
parents
children
comparison
equal deleted inserted replaced
267:bbc0da7ef90e 269:dacfe7c0311a
1 package IMPL::SQL::Schema::Traits::MysqlFormatter;
2 use strict;
3
4 sub quote{
5 my $self = shift;
6
7 if (wantarray) {
8 return map { my $str=$_; $str=~ s/'/''/g; "'$str'"; } @_;
9 } else {
10 return join '',map { my $str=$_; $str=~ s/'/''/g; "'$str'"; } @_;
11 }
12 }
13
14 sub quote_names {
15 my $self = shift;
16
17 if (wantarray) {
18 return map { my $str=$_; $str=~ s/`/``/g; "`$str`"; } @_;
19 } else {
20 return join '',map { my $str=$_; $str=~ s/`/``/g; "`$str`"; } @_;
21 }
22 }
23
24 sub FormatTypeName {
25 my ($self,$type) = @_;
26
27 }
28
29 sub FormatValue {
30 my ($self,$value,$type) = @_;
31 }
32
33 sub FormatColumn {
34 my ($self, $column) = @_;
35
36 my @parts = (
37 $self->quote_names($column->{name}),
38 $self->FormatTypeName($column->{type}),
39 $column->isNullable ? 'NULL' : 'NOT NULL'
40 );
41
42 push @parts, $self->FormatValue( $column->{defaultValue}, $column->{type} )
43 if $column->{defaultValue};
44
45 push @parts, 'AUTO_INCREMENT'
46 if $column->{tag} and $column->{tag}->{auto_increment};
47
48 return join ' ', @parts;
49 }
50
51 sub FormatCreateTable {
52 my ($self,$op) = @_;
53
54 my $table = $op->table;
55
56 my @lines;
57
58 push @lines, "CREATE TABLE (";
59
60 push @lines, map { " " . $self->FormatColumn($_) } @{$table->{columns}}
61 if $table->{columns};
62
63 push @lines, ");";
64
65 return join "\n",@lines;
66 }
67
68 sub FormatDropTable {
69 my ($self,$op) = @_;
70
71 return join ' ',
72 'DROP TABLE',
73 $self->quote_names($op->{tableName}),
74 ';';
75 }
76
77 sub FormatRenameTable {
78 my ($self,$op) = @_;
79
80 return join ' ',
81 'ALTER TABLE',
82 $self->quote_names($op->{tableName}),
83 'RENAME TO',
84 $self->quote_names($op->{tableNewName}),
85 ';';
86 }
87
88 sub FormatAlterTableAddColumn {
89 my ($this,$op,$schema) = @_;
90
91 my @parts;
92
93 $schema->GetTable($op->{tableName})
94 }
95
96 1;