SearchBuilder/SchemaGenerator.pm

filter: Statementstmt 29.5% 18 / 61 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 45.4% 5 / 11 Podpod 25.0% 1 / 4 totaltotal 25.0% 24 / 96 CC 9 SCAR 28.0
CC
9
Cov
23%
AddModel
30.0
_DBSchemaTableFromModel
30.0
new
2.0
CRAP 16.5
« SearchBuilder/Record.pm ↑ Summary SearchBuilder/Handle/ODBC.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package DBIx::SearchBuilder::SchemaGenerator;
5  
6 1 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use DBIx::DBSchema;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Class::ReturnValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Public accessors
11 1 __PACKAGE__->mk_accessors(qw(handle));
12 # Internal accessors: do not use from outside class
13 1 __PACKAGE__->mk_accessors(qw(_db_schema));
14  
15 =head2 new HANDLE
16  
17 Creates a new C<DBIx::SearchBuilder::SchemaGenerator> object. The single
18 required argument is a C<DBIx::SearchBuilder::Handle>.
19  
20 =cut
21  
22 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
23 0 my $class = shift;
24 0 my $handle = shift;
25 0 my $self = $class->SUPER::new();
26  
27 0 $self->handle($handle);
28  
29 0 my $schema = DBIx::DBSchema->new;
30 0 $self->_db_schema($schema);
31  
32 0 return $self;
33 }
34  
35 =for public_doc AddModel MODEL
36  
37 Adds a new model class to the SchemaGenerator. Model should either be an object
38 of a subclass of C<DBIx::SearchBuilder::Record>, or the name of such a subclass; in the
39 latter case, C<AddModel> will instantiate an object of the subclass.
40  
41 The model must define the instance methods C<Schema> and C<Table>.
42  
43 Returns true if the model was added successfully; returns a false C<Class::ReturnValue> error
44 otherwise.
45  
46 =cut
47  
48 0 ▶sub AddModel {
Subroutine AddModel: not called CC=5 SCAR=34.0
Pod undocumented
49 0 my $self = shift;
50 0 my $model = shift;
51  
52 # $model could either be a (presumably unfilled) object of a subclass of
53 # DBIx::SearchBuilder::Record, or it could be the name of such a subclass.
54  
55 0 ▶ unless (ref $model and UNIVERSAL::isa($model, 'DBIx::SearchBuilder::Record')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $model and UNIVERSAL::isa($model, "DBIx::SearchBuilder::Record")) 0 0 0
Condition0%0/3
ref $model and UNIVERSAL::isa($model, "DBIx::SearchBuilder::Record")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $model and UNIVERSAL::isa($model, "DBIx::SearchBuilder::Record")
ref $model UNIVERSAL::isa($model, "DBIx::SearchBuilder::Record")
Truth table0%0/3
A and B
A= ref $modelB= UNIVERSAL::isa($model, "DBIx::SearchBuilder::Record")
ABresult
0X0
100
111
56 0 my $new_model;
57 0 eval { $new_model = $model->new; };
58  
59 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
60 0 return $self->_error("Error making new object from $model: $@");
61 }
62  
63 0 ▶ return $self->_error("Didn't get a DBIx::SearchBuilder::Record from $model, got $new_model")
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($new_model, "DBIx::SearchBuilder::Record") 0 0 0
64 unless UNIVERSAL::isa($new_model, 'DBIx::SearchBuilder::Record');
65  
66 0 $model = $new_model;
67 }
68  
69 0 my $table_obj = $self->_DBSchemaTableFromModel($model);
70  
71 0 $self->_db_schema->addtable($table_obj);
72  
73 0 1;
74 }
75  
76 =for public_doc CreateTableSQLStatements
77  
78 Returns a list of SQL statements (as strings) to create tables for all of
79 the models added to the SchemaGenerator.
80  
81 =cut
82  
83 0 ▶sub CreateTableSQLStatements {
Subroutine CreateTableSQLStatements: not called CC=1 SCAR=6.9
Pod undocumented
84 0 my $self = shift;
85 # The sort here is to make it predictable, so that we can write tests.
86 0 return sort $self->_db_schema->sql($self->handle->dbh);
87 }
88  
89 =for public_doc CreateTableSQLText
90  
91 Returns a string containing a sequence of SQL statements to create tables for
92 all of the models added to the SchemaGenerator.
93  
94 =cut
95  
96 0 ▶sub CreateTableSQLText {
Subroutine CreateTableSQLText: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my $self = shift;
98  
99 0 return join "\n", map { "$_ ;\n" } $self->CreateTableSQLStatements;
100 }
101  
102 =for private_doc _DBSchemaTableFromModel MODEL
103  
104 Takes an object of a subclass of DBIx::SearchBuilder::Record; returns a new
105 C<DBIx::DBSchema::Table> object corresponding to the model.
106  
107 =cut
108  
109 0 ▶sub _DBSchemaTableFromModel {
Subroutine _DBSchemaTableFromModel: not called CC=5 SCAR=34.0
110 0 my $self = shift;
111 0 my $model = shift;
112  
113 0 my $table_name = $model->Table;
114 0 my $schema = $model->Schema;
115  
116 0 my $primary = "id"; # TODO allow override
117 0 my $primary_col = DBIx::DBSchema::Column->new({
118 name => $primary,
119 type => 'serial',
120 null => 'NOT NULL',
121 });
122  
123 0 my @cols = ($primary_col);
124  
125 # The sort here is to make it predictable, so that we can write tests.
126 0 for my $field (sort keys %$schema) {
127 # Skip foreign keys
128  
129 0 ▶ next if defined $schema->{$field}->{'REFERENCES'} and defined $schema->{$field}->{'KEY'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema->{$field}{"REFERENCES"} and defined $schema->{$field}{"KEY"} 0 0 0
Condition0%0/3
defined $schema->{$field}{"REFERENCES"} and defined $schema->{$field}{"KEY"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $schema->{$field}{"REFERENCES"} and defined $schema->{$field}{"KEY"}
defined $schema->{$field}{"REFERENCES"} defined $schema->{$field}{"KEY"}
Truth table0%0/3
A and B
A= defined $schema->{$field}{"REFERENCES"}B= defined $schema->{$field}{"KEY"}
ABresult
0X0
100
111
130  
131 # TODO XXX FIXME
132 # In lieu of real reference support, make references just integers
133 0 ▶ $schema->{$field}{'TYPE'} = 'integer' if $schema->{$field}{'REFERENCES'};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{$field}{"REFERENCES"} 0 0 0
134  
135 push @cols, DBIx::DBSchema::Column->new({
136 name => $field,
137 type => $schema->{$field}{'TYPE'},
138 null => 'NULL',
139 0 default => $schema->{$field}{'DEFAULT'},
140 });
141 }
142  
143 0 my $table = DBIx::DBSchema::Table->new({
144 name => $table_name,
145 primary_key => $primary,
146 columns => \@cols,
147 });
148  
149 0 return $table;
150 }
151  
152 =for private_doc _error STRING
153  
154 Takes in a string and returns it as a Class::ReturnValue error object.
155  
156 =cut
157  
158 0 ▶sub _error {
Subroutine _error: not called CC=1 SCAR=6.9
159 0 my $self = shift;
160 0 my $message = shift;
161  
162 0 my $ret = Class::ReturnValue->new;
163 0 $ret->as_error(errno => 1, message => $message);
164 0 return $ret->return_value;
165 }
166  
167  
168 1 1; # Magic true value required at end of module
169  
170 __END__
« SearchBuilder/Record.pm ↑ Summary SearchBuilder/Handle/ODBC.pm »