1 |
############################################### |
## ------------------------------------------------------------------------ |
2 |
# |
## $Id$ |
3 |
# $Id$ |
## ------------------------------------------------------------------------ |
4 |
# |
## $Log$ |
5 |
# $Log$ |
## Revision 1.7 2002/12/16 06:45:07 joko |
6 |
# Revision 1.1 2002/10/17 00:07:14 joko |
## + injecting property 'guid' into each conrete class when initializing the schema |
7 |
# + new |
## |
8 |
# |
## Revision 1.6 2002/11/17 07:21:47 jonen |
9 |
# |
## + using Class::Tangram::import_schema() again for proper usage of its helper functions |
10 |
############################################### |
## |
11 |
|
## Revision 1.5 2002/11/17 07:12:06 joko |
12 |
|
## + extended ::getProperties with argument "want_transactions" |
13 |
|
## |
14 |
|
## Revision 1.4 2002/11/15 13:21:39 joko |
15 |
|
## + added capability to propagate global schema options from schema module to tangram schema |
16 |
|
## |
17 |
|
## Revision 1.3 2002/11/09 01:27:23 joko |
18 |
|
## + dependency of Class::Tangram now is 1.13 at minimum |
19 |
|
## |
20 |
|
## Revision 1.2 2002/10/25 11:45:10 joko |
21 |
|
## + enhanced robustness |
22 |
|
## + more logging for debug-levels |
23 |
|
## |
24 |
|
## Revision 1.1 2002/10/17 00:07:14 joko |
25 |
|
## + new |
26 |
|
## ------------------------------------------------------------------------ |
27 |
|
|
28 |
# ==================================== |
|
29 |
package Data::Storage::Schema::Abstract; |
package Data::Storage::Schema::Abstract; |
30 |
|
|
31 |
use strict; |
use strict; |
32 |
use warnings; |
use warnings; |
|
|
|
|
use Tangram; |
|
|
use Class::Tangram; |
|
|
|
|
|
use Tangram::RawDate; |
|
|
use Tangram::RawTime; |
|
|
use Tangram::RawDateTime; |
|
33 |
|
|
34 |
use Tangram::FlatArray; |
use Tangram; |
35 |
use Tangram::FlatHash; |
use Class::Tangram; |
36 |
use Tangram::PerlDump; |
|
37 |
|
use Tangram::RawDate; |
38 |
# get logger instance |
use Tangram::RawTime; |
39 |
my $logger = Log::Dispatch::Config->instance; |
use Tangram::RawDateTime; |
40 |
|
|
41 |
|
use Tangram::FlatArray; |
42 |
|
use Tangram::FlatHash; |
43 |
|
use Tangram::PerlDump; |
44 |
|
|
45 |
|
|
46 |
|
use Data::Dumper; |
47 |
|
|
48 |
|
# get logger instance |
49 |
|
my $logger = Log::Dispatch::Config->instance; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
# ======== object constructor - %args to $self ======== |
# ======== object constructor - %args to $self ======== |
56 |
my $class = ref($invocant) || $invocant; |
my $class = ref($invocant) || $invocant; |
57 |
my @args = (); |
my @args = (); |
58 |
@_ && (@args = @_); |
@_ && (@args = @_); |
59 |
#$logger->debug( __PACKAGE__ . "->new(@args)" ); |
|
60 |
|
$logger->debug( __PACKAGE__ . "->new" ); |
61 |
|
|
62 |
#my $self = { @_ }; |
#my $self = { @_ }; |
63 |
|
|
64 |
# merge all entries from all hashes in args-array to single array (key, val, key, val) |
# merge all entries from all hashes in args-array to single array (key, val, key, val) |
66 |
map { push(@args_all, %{$_}); } @args; |
map { push(@args_all, %{$_}); } @args; |
67 |
# make hash from array |
# make hash from array |
68 |
my %args = @args_all; |
my %args = @args_all; |
69 |
|
|
70 |
|
#$logger->debug( __PACKAGE__ . "->new( @args_all )" ); |
71 |
|
|
72 |
my $self = { %args }; |
my $self = { %args }; |
73 |
bless $self, $class; |
bless $self, $class; |
74 |
|
|
75 |
|
#use Data::Dumper; print Dumper($self); |
76 |
|
|
77 |
# remember as which Class we are instantiated |
# remember as which Class we are instantiated |
78 |
$self->{invocant} = $invocant; |
$self->{invocant} = $invocant; |
79 |
|
|
89 |
my $ctversion = $Class::Tangram::VERSION; |
my $ctversion = $Class::Tangram::VERSION; |
90 |
#print "ctversion: ", $ctversion, "\n"; |
#print "ctversion: ", $ctversion, "\n"; |
91 |
#if ($ctversion ne 1.04) { |
#if ($ctversion ne 1.04) { |
92 |
if ($ctversion ne 1.12) { |
if ($ctversion lt 1.13) { |
93 |
print "Version of \"Class::Tangram\": ", $ctversion, "\n"; |
print "Version of \"Class::Tangram\": ", $ctversion, "\n"; |
94 |
print "absolutely required version: ", "1.12", "\n"; |
print "minimum required version : ", "1.13", "\n"; |
95 |
#print "quitting!!! (please install older version (1.04) of \"Class::Tangram\"", "\n"; |
#print "quitting!!! (please install older version (1.04) of \"Class::Tangram\"", "\n"; |
96 |
exit; |
exit; |
97 |
} |
} |
102 |
my $filename = shift; |
my $filename = shift; |
103 |
my $exclude_pattern = shift; |
my $exclude_pattern = shift; |
104 |
$logger->debug( __PACKAGE__ . "->_parsePackageFile( ... )" ); |
$logger->debug( __PACKAGE__ . "->_parsePackageFile( ... )" ); |
105 |
|
if (!$filename) { |
106 |
|
$logger->error( __PACKAGE__ . "->_parsePackageFile: you must specify a filename" ); |
107 |
|
return; |
108 |
|
} |
109 |
|
if (! -e $filename) { |
110 |
|
$logger->error( __PACKAGE__ . "->_parsePackageFile: could not find $filename" ); |
111 |
|
} |
112 |
my @packages = (); |
my @packages = (); |
113 |
open(FH, '<' . $filename); |
open(FH, '<' . $filename); |
114 |
while (<FH>) { |
while (<FH>) { |
142 |
# parse complete package to build list of classes |
# parse complete package to build list of classes |
143 |
if ($#{$classlist} == -1) { |
if ($#{$classlist} == -1) { |
144 |
$logger->debug( "_doinit() Initializing all Classes from " . $self->{invocant} ); |
$logger->debug( "_doinit() Initializing all Classes from " . $self->{invocant} ); |
145 |
my $perl_packagename = $self->{invocant}; |
if (!$self->{invocant}->can('getFilename')) { |
146 |
|
$logger->error( __PACKAGE__ . "->_doinit: Package $self->{invocant} doesn't contain method 'getFilename'" ); |
147 |
|
return; |
148 |
|
} |
149 |
my $perl_packagefile = eval('return ' . $self->{invocant} . '::getFilename();'); |
my $perl_packagefile = eval('return ' . $self->{invocant} . '::getFilename();'); |
150 |
my $packages = $self->_parsePackageNamesFromFile($perl_packagefile, $self->{invocant}); |
my $packages = $self->_parsePackageNamesFromFile($perl_packagefile, $self->{invocant}); |
151 |
# TODO: |
# TODO: |
153 |
$classlist = $packages; |
$classlist = $packages; |
154 |
} |
} |
155 |
|
|
156 |
|
my $schema_properties = {}; |
157 |
|
if ($self->{invocant}->can('getProperties')) { |
158 |
|
# TODO: rework this! call by ref and/or do oo |
159 |
|
#$schema_properties = eval('return ' . $self->{invocant} . '::getProperties();'); |
160 |
|
$schema_properties = eval('return ' . $self->{invocant} . '::getProperties($self->{want_transactions});'); |
161 |
|
} |
162 |
|
|
163 |
|
# build temporary index to look up class by classname |
164 |
|
my $classindex = {}; |
165 |
|
|
166 |
# build argument to be passed to "Tangram::Schema->new(...)" |
# build argument to be passed to "Tangram::Schema->new(...)" |
167 |
my @classes; |
my @classes; |
168 |
map { |
map { |
169 |
$logger->debug( __PACKAGE__ . "->_doinit() Initializing Class $_" ); |
$logger->debug( __PACKAGE__ . "->_doinit: Initializing Class $_" ); |
170 |
|
Class::Tangram::import_schema($_); |
171 |
# classname |
# classname |
172 |
push @classes, $_; |
push @classes, $_; |
173 |
# schema |
# schema |
174 |
my $schema; |
my $schema; |
175 |
my $evalstr = '$schema = $' . $_ . '::schema;'; |
my $evalstr = '$schema = $' . $_ . '::schema;'; |
176 |
my $res = eval($evalstr); |
my $res = eval($evalstr); |
|
use Data::Dumper; |
|
177 |
push @classes, $schema; |
push @classes, $schema; |
178 |
|
$classindex->{$_} = $schema; |
179 |
} @{$classlist}; |
} @{$classlist}; |
180 |
|
|
181 |
|
# build up the schema as a hash |
182 |
|
my $schema = { |
183 |
|
classes => \@classes, |
184 |
|
}; |
185 |
|
|
186 |
|
# merge schema properties |
187 |
|
foreach (keys %$schema_properties) { |
188 |
|
$schema->{$_} = $schema_properties->{$_}; |
189 |
|
} |
190 |
|
|
191 |
|
#print Dumper($schema); |
192 |
|
|
193 |
|
# patch guid into each concrete (non-abstract) object |
194 |
|
# TODO: refactor this code into function: _injectProperty('guid') |
195 |
|
foreach (@{$schema->{classes}}) { |
196 |
|
my $ref = ref $_; |
197 |
|
next if !$ref || $ref ne 'HASH'; |
198 |
|
next if $_->{abstract}; |
199 |
|
|
200 |
|
#print Dumper($_); |
201 |
|
|
202 |
|
if (!$_->{fields}->{string}) { |
203 |
|
$_->{fields}->{string}->{guid} = undef; |
204 |
|
|
205 |
|
} elsif (ref $_->{fields}->{string} eq 'ARRAY') { |
206 |
|
push @{$_->{fields}->{string}}, 'guid'; |
207 |
|
|
208 |
|
} elsif (ref $_->{fields}->{string} eq 'HASH') { |
209 |
|
$_->{fields}->{string}->{guid} = undef; |
210 |
|
|
211 |
|
} |
212 |
|
} |
213 |
|
|
214 |
# create a new Tangram schema object |
# create a new Tangram schema object |
215 |
# these classes must occour in the same order used at a previous setup |
# a) these classes must occour in the same order used at a previous setup or |
216 |
$self->{schema} = Tangram::Schema->new({ classes => \@classes }); |
# b) better use class ids! |
217 |
|
$self->{schema} = Tangram::Schema->new($schema); |
218 |
return 1; |
return 1; |
219 |
|
|
220 |
|
=pod |
221 |
# ====================================== |
# ====================================== |
222 |
# template |
# schema template |
223 |
$self->{schema} = Tangram::Schema->new({ |
$self->{schema} = Tangram::Schema->new({ |
224 |
|
|
225 |
classes => [ |
classes => [ |
|
|
|
226 |
# misc |
# misc |
227 |
'SystemEvent' => $SystemEvent::schema, |
'Person' => $Person::schema, |
|
|
|
228 |
], |
], |
229 |
|
|
230 |
|
make_object => sub { ... }, |
231 |
|
set_id => sub { ... } |
232 |
|
get_id => sub { ... } |
233 |
|
normalize => sub { ... }, |
234 |
|
|
235 |
|
control => '...' |
236 |
|
|
237 |
|
sql => { ... }, |
238 |
|
|
239 |
}); |
}); |
240 |
# ====================================== |
# ====================================== |
241 |
|
=cut |
242 |
|
|
243 |
} |
} |
244 |
|
|
253 |
sub getSchema { |
sub getSchema { |
254 |
my $self = shift; |
my $self = shift; |
255 |
if (!$self->{schema}) { |
if (!$self->{schema}) { |
256 |
print "schema is not yet initialized!", "\n"; |
#print "schema is not yet initialized!", "\n"; |
257 |
exit; |
#exit; |
258 |
|
$logger->error( __PACKAGE__ . "->getSchema: Schema is not yet initialized!" ); |
259 |
|
return; |
260 |
} |
} |
261 |
return $self->{schema}; |
return $self->{schema}; |
262 |
} |
} |