| 1 | package Daje::Workflow::GenerateSQL::Script::Fields; | ||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Daje::Workflow::GenerateSQL::Base::Common', -base, -signatures; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Syntax::Keyword::Match qw(match); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # GenerateSQL::Sql::Table::Fields -- create the fields part of a create table script | ||||||||||
| 7 | # | ||||||||||
| 8 | # SYNOPSIS | ||||||||||
| 9 | # ======== | ||||||||||
| 10 | # my $json = from_json( qq ( | ||||||||||
| 11 | # { | ||||||||||
| 12 | # "fields": { | ||||||||||
| 13 | # "userid": "varchar", | ||||||||||
| 14 | # "username": "varchar", | ||||||||||
| 15 | # "password": "varchar", | ||||||||||
| 16 | # "phone": "varchar", | ||||||||||
| 17 | # "active": "bigint", | ||||||||||
| 18 | # "support": "bigint", | ||||||||||
| 19 | # "is_admin": "bigint" | ||||||||||
| 20 | # } | ||||||||||
| 21 | # } | ||||||||||
| 22 | # )); | ||||||||||
| 23 | # | ||||||||||
| 24 | # my $template = GenerateSQL::Tools::Datasections->new( | ||||||||||
| 25 | # data_sections => "table,foregin_key,index" , | ||||||||||
| 26 | # source => 'GenerateSQL::Template::Templates' | ||||||||||
| 27 | # ); | ||||||||||
| 28 | # | ||||||||||
| 29 | # my $fields = GenerateSQL::Sql::Table::Fields->new( | ||||||||||
| 30 | # json => $json, | ||||||||||
| 31 | # template => $template, | ||||||||||
| 32 | # ); | ||||||||||
| 33 | # | ||||||||||
| 34 | # $fields->create_fields(); | ||||||||||
| 35 | # my $sql = $fields->sql; | ||||||||||
| 36 | # | ||||||||||
| 37 | # METHODS | ||||||||||
| 38 | # ======= | ||||||||||
| 39 | # create_fields() Create the SQL for field creation from JSON | ||||||||||
| 40 | # get_defaults($datatype) Get defaults part for the sql datatype | ||||||||||
| 41 | # | ||||||||||
| 42 | # | ||||||||||
| 43 | # | ||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | sub create_fields($self){ | ||||||||
|
Subroutine
create_fields: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 47 | 0 | my $field = ''; | |||||||||
| 48 | 0 | eval { | |||||||||
| 49 | 0 | my $fields = $self->json->{fields}; | |||||||||
| 50 | 0 | foreach my $key (sort keys %{$fields}) { | |||||||||
| 51 | 0 | $field .= $key . ' ' . $fields->{$key} . $self->get_defaults($fields->{$key}) . ','; | |||||||||
| 52 | } | ||||||||||
| 53 | }; | ||||||||||
| 54 | 0 | ▶ | $self->error->add_error($@) if defined $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | 0 | $self->set_sql($field); | |||||||||
| 57 | |||||||||||
| 58 | 0 | return ; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | sub get_defaults($self, $datatype) { | ||||||||
|
Subroutine
get_defaults: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||
| 62 | 0 | my $result = ""; | |||||||||
| 63 | 0 | ▶ | if (index($datatype,'(') > -1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 0 | $datatype = substr($datatype,0,index($datatype,'(')) | |||||||||
| 65 | } | ||||||||||
| 66 | match(lc($datatype) : eq) { | ||||||||||
| 67 | 0 | ▶ | case ('bigint') { $result = " not null default 0 \n"} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | ▶ | case ('smallint') { $result = " not null default 0 \n"} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | ▶ | case ('integer') { $result = " not null default 0 \n"} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | ▶ | case ('decimal') { $result = " not null default 0.0 \n"} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | ▶ | case ('numeric') { $result = " not null default 0.0 \n"} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | ▶ | case ('varchar') { $result = " not null default '' \n"} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | ▶ | case ('char') { $result = " not null default '' \n"} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | case ('text') { $result = " not null default '' \n"} | |||||||||
| 75 | 0 | default { $result = '' } | |||||||||
| 76 | } | ||||||||||
| 77 | 0 | ▶ | return $result; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 1 | 1; | |||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | |||||||||||
| 85 | |||||||||||
| 86 | #################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### | ||||||||||
| 87 | |||||||||||
| 88 | =head1 NAME | ||||||||||
| 89 | |||||||||||
| 90 | Daje::Workflow::GeneratSQL::Script::Fields | ||||||||||
| 91 | |||||||||||
| 92 | |||||||||||
| 93 | =head1 SYNOPSIS | ||||||||||
| 94 | |||||||||||
| 95 | my $json = from_json( qq ( | ||||||||||
| 96 | { | ||||||||||
| 97 | "fields": { | ||||||||||
| 98 | "userid": "varchar", | ||||||||||
| 99 | "username": "varchar", | ||||||||||
| 100 | "password": "varchar", | ||||||||||
| 101 | "phone": "varchar", | ||||||||||
| 102 | "active": "bigint", | ||||||||||
| 103 | "support": "bigint", | ||||||||||
| 104 | "is_admin": "bigint" | ||||||||||
| 105 | } | ||||||||||
| 106 | } | ||||||||||
| 107 | )); | ||||||||||
| 108 | |||||||||||
| 109 | my $template = GenerateSQL::Tools::Datasections->new( | ||||||||||
| 110 | data_sections => "table,foregin_key,index" , | ||||||||||
| 111 | source => 'GenerateSQL::Template::Templates' | ||||||||||
| 112 | ); | ||||||||||
| 113 | |||||||||||
| 114 | my $fields = GenerateSQL::Sql::Table::Fields->new( | ||||||||||
| 115 | json => $json, | ||||||||||
| 116 | template => $template, | ||||||||||
| 117 | ); | ||||||||||
| 118 | |||||||||||
| 119 | $fields->create_fields(); | ||||||||||
| 120 | my $sql = $fields->sql; | ||||||||||
| 121 | |||||||||||
| 122 | |||||||||||
| 123 | |||||||||||
| 124 | =head1 DESCRIPTION | ||||||||||
| 125 | |||||||||||
| 126 | GenerateSQL::Sql::Table::Fields -- create the fields part of a create table script | ||||||||||
| 127 | |||||||||||
| 128 | |||||||||||
| 129 | |||||||||||
| 130 | =head1 REQUIRES | ||||||||||
| 131 | |||||||||||
| 132 | L<Syntax::Keyword::Match> | ||||||||||
| 133 | |||||||||||
| 134 | L<Mojo::Base> | ||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | =head1 METHODS | ||||||||||
| 138 | |||||||||||
| 139 | create_fields() Create the SQL for field creation from JSON | ||||||||||
| 140 | get_defaults($datatype) Get defaults part for the sql datatype | ||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | |||||||||||
| 144 | |||||||||||
| 145 | |||||||||||
| 146 | =cut | ||||||||||
| 147 | |||||||||||