« blib/lib/blx/xsdsql/xml/base.pm
↑ Summary
blib/lib/blx/xsdsql/schema_repository/sql/generic/cursor.pm »
| 1 | package blx::xsdsql::generator::sql::generic::handle::insert_dictionary; | ||
| 2 | use strict; # use strict is for PBP | ||
| 3 | use Filter::Include; | ||
| 4 | include blx::xsdsql::include; | ||
| 5 | #line 6 | ||
| 6 | use Scalar::Util qw(looks_like_number); | ||
| 7 | |||
| 8 | use base(qw(blx::xsdsql::generator::sql::generic::handle)); | ||
| 9 | |||
| 10 | sub _get_create_prefix { | ||
| 11 | my ($self,%params)=@_; | ||
| 12 | return "insert into "; | ||
| 13 | } | ||
| 14 | |||
| 15 | |||
| 16 | sub _get_columns_string_list { | ||
| 17 | my ($self,$columns,%params)=@_; | ||
| 18 | return '('.join(',',map { $_->get_sql_name } @$columns).')'; | ||
| 19 | } | ||
| 20 | |||
| 21 | |||
| 22 | sub _get_begin_value_constant { | ||
| 23 | my ($self,%params)=@_; | ||
| 24 | return " values ("; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub _manip_value { #manip values from input data | ||
| 28 | my ($self,$col,$v,%params)=@_; | ||
| 29 | $v=$self->{BINDING}->get_attrs_value(qw(DB_CONN))->quote($v) | ||
| 30 | unless defined $v && looks_like_number($v); | ||
| 31 | return $v; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _get_value_data { | ||
| 35 | my ($self,$columns,$data,%params)=@_; | ||
| 36 | |||
| 37 | return join(',',map { | ||
| 38 | my $name=$_->get_name; | ||
| 39 | affirm { exists $data->{$name} } "$name: column non defined in data - see the method get_dictionary_data of the correspondenting class"; | ||
| 40 | my $v=$self->_manip_value($_,$data->{$name}); | ||
| 41 | $v; | ||
| 42 | } @$columns | ||
| 43 | ); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _get_end_value_constant { | ||
| 47 | my ($self,%params)=@_; | ||
| 48 | return ")"; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | sub get_binding_objects { | ||
| 53 | my ($self,$schema,%params)=@_; | ||
| 54 | my $root_table=$schema->get_root_table; | ||
| 55 | return wantarray ? ( $root_table ) : [ $root_table ]; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _get_dictionary_columns { | ||
| 59 | my ($self,$table,%params)=@_; | ||
| 60 | return $table->get_columns(%params); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _get_table_data { | ||
| 64 | my ($self,$table,$type,%params)=@_; | ||
| 65 | return {} unless defined $table; | ||
| 66 | if (ref($table)=~/::table$/) { | ||
| 67 | for my $col($table->get_columns) { | ||
| 68 | $col->set_sql_type(EXTRA_TABLES => $self->{EXTRA_TABLES}); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | my $data=$table->get_dictionary_data($type,%params); | ||
| 72 | return $data; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _get_table_columns_data { | ||
| 76 | my ($self,$extra_tables,$table,$type,%params)=@_; | ||
| 77 | my $dic=$extra_tables->{$type}; | ||
| 78 | affirm { defined $dic } "'$type': wrong table type"; | ||
| 79 | my $data=$self->_get_table_data($table,$type,%params); | ||
| 80 | my $columns=$self->_get_dictionary_columns($dic); | ||
| 81 | return ($columns,$data); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _print_insert { | ||
| 85 | my ($self,$dic,$dic_columns,$data,%params)=@_; | ||
| 86 | my $d=ref($data) eq 'ARRAY' ? $data : [ $data ]; | ||
| 87 | for my $data(@$d) { | ||
| 88 | $self->{STREAMER}->put_line( | ||
| 89 | $self->_get_create_prefix | ||
| 90 | ,$dic->get_sql_name | ||
| 91 | ,$self->_get_columns_string_list($dic_columns) | ||
| 92 | ,$self->_get_begin_value_constant | ||
| 93 | ,$self->_get_value_data($dic_columns,$data) | ||
| 94 | ,$self->_get_end_value_constant | ||
| 95 | ,$dic->command_terminator | ||
| 96 | ,"\n" | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | $self->{STREAMER}->put_line if ref($data) eq 'ARRAY'; | ||
| 100 | return $self; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _insert_catalog { | ||
| 104 | my ($self,$extra_tables,%params)=@_; | ||
| 105 | my ($dic_columns,$data)=$self->_get_table_columns_data($extra_tables,undef,'CATALOG_DICTIONARY'); | ||
| 106 | |||
| 107 | for my $k(qw(CATALOG_NAME OUTPUT_NAMESPACE DB_NAMESPACE)) { | ||
| 108 | affirm { defined $params{$k} } "$k: param not set"; | ||
| 109 | $data->{lc($k)}=$params{$k}; | ||
| 110 | } | ||
| 111 | my $dic=$extra_tables->{CATALOG_DICTIONARY}; | ||
| 112 | return $self->_print_insert($dic,$dic_columns,$data,%params); | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _insert_schema { | ||
| 116 | my ($self,$extra_tables,%params)=@_; | ||
| 117 | my $schema=$params{SCHEMA}; | ||
| 118 | affirm { defined $schema } "param SCHEMA not set"; | ||
| 119 | my ($dic_columns,$data)=$self->_get_table_columns_data($extra_tables,$schema,'SCHEMA_DICTIONARY',%params); | ||
| 120 | $data->{is_root_schema}=$params{ROOT_SCHEMA} ? 1 : 0; | ||
| 121 | $data->{location}=$params{LOCATION} unless $params{ROOT_SCHEMA}; | ||
| 122 | my $dic=$extra_tables->{SCHEMA_DICTIONARY}; | ||
| 123 | return $self->_print_insert($dic,$dic_columns,$data,%params); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _insert_table { | ||
| 127 | my ($self,$extra_tables,$table,%params)=@_; | ||
| 128 | my ($dic_columns,$data)=$self->_get_table_columns_data($extra_tables,$table,'TABLE_DICTIONARY',%params); | ||
| 129 | my $dic=$extra_tables->{TABLE_DICTIONARY}; | ||
| 130 | return $self->_print_insert($dic,$dic_columns,$data,%params); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _insert_columns { | ||
| 134 | my ($self,$extra_tables,$table,%params)=@_; | ||
| 135 | my ($dic_columns,$data)=$self->_get_table_columns_data($extra_tables,$table,'COLUMN_DICTIONARY',%params); | ||
| 136 | my $dic=$extra_tables->{COLUMN_DICTIONARY}; | ||
| 137 | return $self->_print_insert($dic,$dic_columns,$data,%params); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _insert_relation_tables { | ||
| 141 | my ($self,$extra_tables,$table,%params)=@_; | ||
| 142 | my $dic=$extra_tables->{RELATION_TABLE_DICTIONARY}; | ||
| 143 | my ($dic_columns,$data)=$self->_get_table_columns_data($extra_tables,$table,'RELATION_TABLE_DICTIONARY',%params); | ||
| 144 | return $self->_print_insert($dic,$dic_columns,$data,%params); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub _insert_relation_schemas { | ||
| 148 | my ($self,$extra_tables,%params)=@_; | ||
| 149 | my $dic=$extra_tables->{RELATION_SCHEMA_DICTIONARY}; | ||
| 150 | my ($dic_columns,$data)=$self->_get_table_columns_data($extra_tables,undef,'RELATION_SCHEMA_DICTIONARY',%params); | ||
| 151 | |||
| 152 | my %d=( | ||
| 153 | parent_schema_code => $params{PARENT_SCHEMA_CODE} | ||
| 154 | ,child_sequence => $params{SCHEMA_CHILD_SEQ} | ||
| 155 | ,child_schema_code => $params{SCHEMA_CODE} | ||
| 156 | ,parent_namespace => $params{NAMESPACE} | ||
| 157 | ,child_location => $params{LOCATION} | ||
| 158 | ,catalog_name => $params{CATALOG_NAME} | ||
| 159 | ); | ||
| 160 | |||
| 161 | for my $k(keys %d) { | ||
| 162 | $data->{$k}=$d{$k} | ||
| 163 | } | ||
| 164 | |||
| 165 | return $self->_print_insert($dic,$dic_columns,$data,%params); | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | sub first_pass { | ||
| 170 | my ($self,%params)=@_; | ||
| 171 | my $extra_tables=$self->{EXTRA_TABLES}->factory_extra_tables; | ||
| 172 | $self->_insert_catalog($extra_tables,%params); | ||
| 173 | return $self->_insert_schema($extra_tables,%params,ROOT_SCHEMA => 1); | ||
| 174 | } | ||
| 175 | |||
| 176 | sub table_header { | ||
| 177 | my ($self,$table,%params)=@_; | ||
| 178 | my $schema=$params{SCHEMA}; | ||
| 179 | my $extra_tables=$self->{EXTRA_TABLES}->factory_extra_tables; | ||
| 180 | $self->_insert_table($extra_tables,$table,%params); | ||
| 181 | $self->_insert_columns($extra_tables,$table,%params); | ||
| 182 | return $self->_insert_relation_tables($extra_tables,$table,%params); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub relation_schema { | ||
| 186 | my ($self,%params)=@_; | ||
| 187 | |||
| 188 | for my $k(qw(PARENT_SCHEMA_CODE SCHEMA_CODE SCHEMA_CHILD_SEQ CATALOG_NAME SCHEMA LOCATION NAMESPACE)) { | ||
| 189 | affirm { defined $params{$k} } "$k: param not set"; | ||
| 190 | } | ||
| 191 | |||
| 192 | my $extra_tables=$self->{EXTRA_TABLES}->factory_extra_tables; | ||
| 193 | $self->_insert_schema($extra_tables,%params,ROOT_SCHEMA => undef); | ||
| 194 | return $self->_insert_relation_schemas($extra_tables,%params); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub last_pass { | ||
| 198 | my ($self,%params)=@_; | ||
| 199 | unless ($params{NO_EMIT_COMMENTS}) { | ||
| 200 | my $schema=$params{SCHEMA}; | ||
| 201 | $self->{STREAMER}->put_line($schema->get_root_table->comment(' end of insert dictionary ')); | ||
| 202 | } | ||
| 203 | return $self; | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; | ||
| 207 | |||
| 208 | __END__ |