| 1 | package Decl::Semantics::Table; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use base qw(Decl::Node); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Text::ParseWords; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Iterator::Simple qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Decl::Semantics::Table - implements a table in a database. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 VERSION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Version 0.01 | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 13 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | When working with databases, it is always true that we make certain assumptions about its structure (what tables it has, what fields they have). | ||||||||||||||||||||||||||||
| 28 | The C<table> tag is how we define what in a table we intend to use, and how we expect it to be formatted. When developing a system from scratch, the | ||||||||||||||||||||||||||||
| 29 | C<table> tag can even create the table directly, and if we continue to give it authoritative status, it can also modify the existing tables to meet | ||||||||||||||||||||||||||||
| 30 | the specifications of the script. It does so by generating SQL CREATE and ALTER TABLE statements and running them against its database. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | A set of tables can also be told to generate a full SQL schema without talking to the database directly. This is the default if no database handle | ||||||||||||||||||||||||||||
| 33 | is defined. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | The semantics of the C<table> tag, as you'll not be surprised to hear, correspond closely to SQL semantics. There are just a few differences, and as usual, | ||||||||||||||||||||||||||||
| 36 | you don't have to use them if you don't want to, but I find them useful. Note that at the moment, these semantics are a subset of full SQL - don't expect to do your | ||||||||||||||||||||||||||||
| 37 | DBA work through the C<table> tag. The point of this exercise right now is to provide more of a quick sketch tool that generates simple SQL that can be refined as | ||||||||||||||||||||||||||||
| 38 | needed; my own SQL work is pretty superficial, so to do a better job, I'll eventually have to put a lot more work into refining the semantics of database management. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head2 Basic types | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Each column in a table has a type. I'm arbitrarily calling these types int, float, char, text, bool, and date (the last being a timestamp). | ||||||||||||||||||||||||||||
| 43 | You can, of course, use anything else you want as a type, and the C<table> tag will assume it's SQL and pass it through, for better or worse; this | ||||||||||||||||||||||||||||
| 44 | allows you to use anything specifically defined for your own database. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | A field specification in the C<table> tag is backwards from SQL - the type comes first. I'm really not doing this to mess with your head; tags are better | ||||||||||||||||||||||||||||
| 47 | suited for expression of type, so this matches C::Decl semantics better. If you really hate it, use the C<sql> subtag - this passes whatever it sees through as SQL | ||||||||||||||||||||||||||||
| 48 | without trying to get cute at all. It doesn't even try to parse it, actually, except to strip out the first word as the field name. So this will work fine: | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | table mytable | ||||||||||||||||||||||||||||
| 51 | sql key integer not null | ||||||||||||||||||||||||||||
| 52 | sql field varchar(200) not null default 'George' | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | This is your best bet when you start to offload checks to the database instead of just hacking something together. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | As always with Decl, the idea is to make it easy to slap something together while making it possible to be careful later. Nowhere is this | ||||||||||||||||||||||||||||
| 57 | attitude more evident than here in my glossing over the vast and troubled territory that is SQL. Did you know the 'S' is for 'standard'? Have you ever seen a more | ||||||||||||||||||||||||||||
| 58 | ironic acronym? | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | One more "basic" type: a C<key> is always an integer that autoincrements. You can have a character key by saying e.g. C<char myfield (key)>; a character key is simply | ||||||||||||||||||||||||||||
| 61 | declared PRIMARY KEY. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 Structural types | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | To represent relationships between tables, I'm using C<ref <tablename>> (defines a field named after the table with the same type as the table's key), | ||||||||||||||||||||||||||||
| 66 | C<ref field <tablename>> (in case you want to name it something else; maybe you have two such fields, for example), and "list". The list actually defines | ||||||||||||||||||||||||||||
| 67 | a subtable, and there are two variants: C<list <tablename>>/C<list field <tablename>> (defines an n-to-n relationship to the other table by means of an | ||||||||||||||||||||||||||||
| 68 | anonymous linking table), and a multilined variant: | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | list <field> | ||||||||||||||||||||||||||||
| 71 | int field1 | ||||||||||||||||||||||||||||
| 72 | int field2 | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | This actually creates an new table called <table>_<field> and gives it those two fields, plus the key of the master table. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | All of this makes it simpler and quicker to set up a normalized database and build queries against it that can be called from code. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 Data dictionary | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | The data dictionary is a quick and easy way to define new "types" - a title may be standardized throughout your database as a char (100), for example. So: | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | data-dictionary | ||||||||||||||||||||||||||||
| 83 | char title (100) | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Now we can use the title as a field type anywhere: | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | table | ||||||||||||||||||||||||||||
| 88 | title | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | or | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | table | ||||||||||||||||||||||||||||
| 93 | title | ||||||||||||||||||||||||||||
| 94 | title subtitle | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | If a field is not named, the type name will also be used as the default field name. (This seems pretty reasonable.) | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 FUTURE POSSIBILITIES | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head2 Variant SQL data dictionaries | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | The tags used in the data dictionary are C<always standard SQL>, whatever that might mean for your own database. C<If you need to work with multiple databases> | ||||||||||||||||||||||||||||
| 103 | then you can define database-specific data dictionaries like this: | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | data-dictionary (msaccess) | ||||||||||||||||||||||||||||
| 106 | char title (100) | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | and so on. If you leave one data dictionary unadorned with a database type, then it will serve as the default for any fields that don't have to be defined | ||||||||||||||||||||||||||||
| 109 | differently between the different databases. I wrestled with this setup, but I think it's the cleanest way to represent these things - plus it gives the added | ||||||||||||||||||||||||||||
| 110 | benefit that if you move from database A to database B, you can simply take your data dictionary and work down the list deciding which datatypes correspond to | ||||||||||||||||||||||||||||
| 111 | what in the new regime, then use the new data dictionary with no other format changes. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | How often will this come up, though? No idea. I just worry, that's all. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | At any rate, you can think of the special definitions for generic datatypes defined by this code as a default data dictionary. Any of those types may be overridden. | ||||||||||||||||||||||||||||
| 116 | Clear? Of course it is. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 More refined DBA facilities | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Defining indices would be nice, wouldn't it? | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 Building a spec based on existing SQL table definitions | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | This would be useful for introspection as well. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 FUNCTIONS DEFINED | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head2 defines(), tags_defined() | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub defines { ('table', 'data-dictionary'); } | ||||||||||||||||||||||||||
|
Subroutine
defines: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 12 | ▶ | sub tags_defined { Decl->new_data(<<EOF); } | ||||||||||||||||||||||||||
|
Subroutine
tags_defined: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | table (body=vanilla) | ||||||||||||||||||||||||||||
| 134 | data-dictionary (body=vanilla) | ||||||||||||||||||||||||||||
| 135 | EOF | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head2 build_payload, build_table, build_ddict | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | We connect to the default database - unless we are actually in a database object, or given a specific database by name. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | We first write SQL to define the table, and query the database to see what structure it thinks that table has (if it has that table), and of | ||||||||||||||||||||||||||||
| 142 | course, we do the same for any subtables. If there's a mismatch, we generate SQL to alter the table(s). | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | If we have authority, we then execute any SQL already generated. There should probably be some kind of workflow step to allow this authority to | ||||||||||||||||||||||||||||
| 145 | be delegated or deferred, but man, that's a can of worms that can be opened another day. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | The C<table> tag thus doesn't actually I<have> a payload per se. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub build_payload { | ||||||||||||||||||||||||||
|
Subroutine
build_payload: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | 0 | $self->{dictionary} = $self->find_context('data-dictionary'); | |||||||||||||||||||||||||||
| 153 | 0 | foreach ($self->nodes) { | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | $_->build if $_->can('build'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | return $self->build_table(@_) if $self->is('table'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | $self->build_ddict(@_); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | sub build_table { | ||||||||||||||||||||||||||
|
Subroutine
build_table: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | $self->{tables} = []; | |||||||||||||||||||||||||||
| 165 | 0 | $self->{table_data} = {}; | |||||||||||||||||||||||||||
| 166 | 0 | push @{$self->{tables}}, $self->name; | |||||||||||||||||||||||||||
| 167 | 0 | foreach my $l ($self->nodes()) { | |||||||||||||||||||||||||||
| 168 | 0 | ▶ | if ($l->is('query')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | # Not handling at the moment | ||||||||||||||||||||||||||||
| 170 | } else { | ||||||||||||||||||||||||||||
| 171 | # Anything else is either a list/link or a field. | ||||||||||||||||||||||||||||
| 172 | 0 | my ($fname, $def) = $self->sql_single_field($self->name, $l); | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | $self->{key} = $fname if ($def->{key}) and not $self->{key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$def->{"key"} and not $self->{"key"}
MC/DC0%0/2
$def->{"key"} and not $self->{"key"}
$def->{"key"}
not $self->{"key"}
Truth table0%0/3
A and not(B)
A= $def->{"key"}B= not $self->{"key"}
| |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | if (not $self->{key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | $self->{key} = $self->default_key($self->name); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | my $database = $self->find_context('database'); | |||||||||||||||||||||||||||
| 181 | 0 | my $dbtype = ''; | |||||||||||||||||||||||||||
| 182 | 0 | ▶ | $dbtype = $database->{database_type} if defined $database; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | my $db = undef; | |||||||||||||||||||||||||||
| 184 | 0 | ▶ | $db = $database if defined $database and $database->parameter('tables') eq 'active'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $database and $database->parameter("tables") eq "active"
MC/DC0%0/2
defined $database and $database->parameter("tables") eq "active"
defined $database
$database->parameter("tables") eq "active"
Truth table0%0/3
A and B
A= defined $databaseB= $database->parameter("tables") eq "active"
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | $self->{sql} = join ("\n", map { $self->sql_single_table($_, $dbtype, $db) } @{$self->{tables}}); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head2 Helper functions sql_single_table() and sql_single_field | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | These functions just spin out some SQL based on our data structures. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub sql_single_table { | ||||||||||||||||||||||||||
|
Subroutine
sql_single_table: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 0 | my ($self, $table, $dbtype, $db) = @_; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | my $table_info; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if (defined $db) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | $table_info = $db->table_info($table); | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | my @fields = map { | ||||||||||||||||||||||||||||
| 207 | 0 | my $fd = $self->{table_data}->{$table}->{fielddata}->{$_}; | |||||||||||||||||||||||||||
| 208 | 0 | ▶ | "$_ " . $fd->{type} . ($fd->{size} eq '' ? '' : ' (' . $fd->{size} . ")") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | } @{$self->{table_data}->{$table}->{fields}}; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 0 | my $sql = "create table $table (\n " . | |||||||||||||||||||||||||||
| 212 | join (",\n ", @fields) . | ||||||||||||||||||||||||||||
| 213 | "\n);\n"; | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if (defined $db and not defined $table_info) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $db and not defined $table_info
MC/DC0%0/2
defined $db and not defined $table_info
defined $db
not defined $table_info
Truth table0%0/3
A and not(B)
A= defined $dbB= not defined $table_info
| |||||||||||||||||||||||||||||
| 216 | 0 | print "Creating table $table\n"; | |||||||||||||||||||||||||||
| 217 | 0 | $db->dbh->do($sql); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 0 | return $sql; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 0 | ▶ | sub sql_single_field { | ||||||||||||||||||||||||||
|
Subroutine
sql_single_field: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||||||||
| 224 | 0 | my ($self, $table, $field) = @_; | |||||||||||||||||||||||||||
| 225 | 0 | ▶ | if ($field->is('list')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 0 | ▶ | if ($field->nodes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | # Subtable. | ||||||||||||||||||||||||||||
| 228 | 0 | ▶ | my $tname = $field->name || 'list'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$field->name || 'list'
MC/DC0%0/1
$field->name || 'list'
$field->name
Truth table0%0/2
A
A= $field->name
| |||||||||||||||||||||||||||||
| 229 | 0 | my $subtable = $table . '_' . $tname; | |||||||||||||||||||||||||||
| 230 | 0 | push @{$self->{tables}}, $subtable; | |||||||||||||||||||||||||||
| 231 | 0 | my $key = $self->get_table_key($table); | |||||||||||||||||||||||||||
| 232 | 0 | my $keydef = $self->get_table_field($table, $key); | |||||||||||||||||||||||||||
| 233 | my $def = { | ||||||||||||||||||||||||||||
| 234 | type => $keydef->{type}, | ||||||||||||||||||||||||||||
| 235 | size => $keydef->{size}, | ||||||||||||||||||||||||||||
| 236 | 0 | key => 0, | |||||||||||||||||||||||||||
| 237 | }; | ||||||||||||||||||||||||||||
| 238 | 0 | my $parent_key = 'ref_' . $key; | |||||||||||||||||||||||||||
| 239 | 0 | $self->add_field($subtable, $parent_key, $def); | |||||||||||||||||||||||||||
| 240 | 0 | $self->{tabledata}->{$subtable}->{parent_key} = $parent_key; | |||||||||||||||||||||||||||
| 241 | 0 | foreach ($field->nodes) { | |||||||||||||||||||||||||||
| 242 | 0 | $self->sql_single_field($subtable, $_); | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | } else { | ||||||||||||||||||||||||||||
| 245 | 0 | my @names = $field->names; | |||||||||||||||||||||||||||
| 246 | 0 | my $tname; | |||||||||||||||||||||||||||
| 247 | 0 | ▶ | if (@names == 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 248 | # error | ||||||||||||||||||||||||||||
| 249 | 0 | next; | |||||||||||||||||||||||||||
| 250 | } elsif (@names == 1) { | ||||||||||||||||||||||||||||
| 251 | 0 | $tname = $names[0]; | |||||||||||||||||||||||||||
| 252 | } else { | ||||||||||||||||||||||||||||
| 253 | 0 | $tname = $names[1]; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | 0 | push @{$self->{tables}}, $table . '_link_' . $tname; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | } else { | ||||||||||||||||||||||||||||
| 258 | 0 | ▶ | my $fname = $field->name || $field->tag; | ||||||||||||||||||||||||||
|
Condition0%0/3
$field->name || $field->tag
MC/DC0%0/2
$field->name || $field->tag
$field->name
$field->tag
Truth table0%0/3
A || B
A= $field->nameB= $field->tag
| |||||||||||||||||||||||||||||
| 259 | 0 | my $type = $field->tag; | |||||||||||||||||||||||||||
| 260 | 0 | ▶ | my $size = $field->parameter_n(0) || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$field->parameter_n(0) || ''
MC/DC0%0/1
$field->parameter_n(0) || ''
$field->parameter_n(0)
Truth table0%0/2
A
A= $field->parameter_n(0)
| |||||||||||||||||||||||||||||
| 261 | 0 | ▶ | my $key = $field->is('key') || $field->parameter('key') || 0; | ||||||||||||||||||||||||||
|
Condition0%0/5
$field->is('key') || $field->parameter('key') || 0
$field->is("key") or $field->parameter("key")
MC/DC0%0/2
$field->is('key') || $field->parameter('key') || 0
$field->is("key")
$field->parameter("key")
Truth table0%0/3
A or B
A= $field->is("key")B= $field->parameter("key")
| |||||||||||||||||||||||||||||
| 262 | 0 | ▶ | if ($size eq 'key') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 0 | ▶ | $size = $field->parameter_n(1) || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$field->parameter_n(1) || ''
MC/DC0%0/1
$field->parameter_n(1) || ''
$field->parameter_n(1)
Truth table0%0/2
A
A= $field->parameter_n(1)
| |||||||||||||||||||||||||||||
| 264 | 0 | $key = 1; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | 0 | ▶ | if (defined $self->{dictionary}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 0 | my $dict = $self->{dictionary}->dictionary_lookup($type); | |||||||||||||||||||||||||||
| 268 | 0 | ▶ | if (defined $dict) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 0 | $type = $dict->{type}; | |||||||||||||||||||||||||||
| 270 | 0 | ▶ | $size = $dict->{size} unless $size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 0 | ▶ | $key = $dict->{key} unless $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | 0 | my $def = { | |||||||||||||||||||||||||||
| 275 | type => $type, | ||||||||||||||||||||||||||||
| 276 | size => $size, | ||||||||||||||||||||||||||||
| 277 | key => $key, | ||||||||||||||||||||||||||||
| 278 | }; | ||||||||||||||||||||||||||||
| 279 | 0 | $self->add_field($table, $fname, $def); | |||||||||||||||||||||||||||
| 280 | 0 | return ($fname, $def); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | sub build_ddict { | ||||||||||||||||||||||||||
|
Subroutine
build_ddict: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 285 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 286 | 0 | $self->{tables} = ['dictionary']; | |||||||||||||||||||||||||||
| 287 | 0 | $self->{table_data}->{dictionary}->{fields} = []; | |||||||||||||||||||||||||||
| 288 | 0 | $self->{table_data}->{dictionary}->{fielddata} = {}; | |||||||||||||||||||||||||||
| 289 | 0 | foreach my $l ($self->nodes()) { | |||||||||||||||||||||||||||
| 290 | 0 | $self->sql_single_field('dictionary', $l); | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =head2 dictionary_lookup | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | This is called by a table on its dictionary to see if the dictionary knows about a given field. If the dictionary doesn't know, and if there | ||||||||||||||||||||||||||||
| 297 | is a higher-level data dictionary, then it gets called, and so on. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =cut | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 0 | ▶ | sub dictionary_lookup { | ||||||||||||||||||||||||||
|
Subroutine
dictionary_lookup: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 302 | 0 | my ($self, $field) = @_; | |||||||||||||||||||||||||||
| 303 | 0 | my $possible = $self->{table_data}->{dictionary}->{fielddata}->{$field}; | |||||||||||||||||||||||||||
| 304 | 0 | ▶ | return $possible if defined $possible; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 0 | ▶ | if (defined $self->{dictionary}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 0 | return $self->{dictionary}->dictionary_lookup($field); | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | 0 | return; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =head2 default_key, add_default_key, get_table_key, get_table_field, add_field | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | Table access functions. | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =cut | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 0 | ▶ | sub default_key { | ||||||||||||||||||||||||||
|
Subroutine
default_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 318 | 0 | my ($self, $table) = @_; | |||||||||||||||||||||||||||
| 319 | 0 | $table . '_id'; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | 0 | ▶ | sub add_default_key { | ||||||||||||||||||||||||||
|
Subroutine
add_default_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 322 | 0 | my ($self, $table) = @_; | |||||||||||||||||||||||||||
| 323 | 0 | my $key = $self->default_key($table); | |||||||||||||||||||||||||||
| 324 | 0 | unshift @{$self->{table_data}->{$table}->{fields}}, $key; | |||||||||||||||||||||||||||
| 325 | 0 | $self->{table_data}->{$table}->{fielddata}->{$key} = { | |||||||||||||||||||||||||||
| 326 | type => 'int', | ||||||||||||||||||||||||||||
| 327 | size => 'size', | ||||||||||||||||||||||||||||
| 328 | key => 1 | ||||||||||||||||||||||||||||
| 329 | }; | ||||||||||||||||||||||||||||
| 330 | 0 | return $key; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | 0 | ▶ | sub get_table_key { | ||||||||||||||||||||||||||
|
Subroutine
get_table_key: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 333 | 0 | my ($self, $table) = @_; | |||||||||||||||||||||||||||
| 334 | 0 | foreach (@{$self->{table_data}->{$table}->{fields}}) { | |||||||||||||||||||||||||||
| 335 | 0 | ▶ | return $_ if $self->{table_data}->{$table}->{fielddata}->{$_}->{key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | 0 | $self->add_default_key($table); | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | 0 | ▶ | sub get_table_field { | ||||||||||||||||||||||||||
|
Subroutine
get_table_field: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 340 | 0 | my ($self, $table, $field) = @_; | |||||||||||||||||||||||||||
| 341 | 0 | $self->{table_data}->{$table}->{fielddata}->{$field}; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 0 | ▶ | sub add_field { | ||||||||||||||||||||||||||
|
Subroutine
add_field: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 345 | 0 | my ($self, $table, $field, $def) = @_; | |||||||||||||||||||||||||||
| 346 | 0 | push @{$self->{table_data}->{$table}->{fields}}, $field; | |||||||||||||||||||||||||||
| 347 | 0 | $self->{table_data}->{$table}->{fielddata}->{$field} = $def; | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | Michael Roberts, C<< <michael at vivtek.com> >> | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =head1 BUGS | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | Please report any bugs or feature requests to C<bug-decl at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 358 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Decl>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 359 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | Copyright 2010 Michael Roberts. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 366 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 367 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =cut | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 13 | 1; # End of Decl::Semantics::Table | |||||||||||||||||||||||||||