1 |
joko |
1.1 |
############################################### |
2 |
|
|
# |
3 |
joko |
1.4 |
# $Id: Abstract.pm,v 1.3 2002/11/09 01:27:23 joko Exp $ |
4 |
joko |
1.2 |
# |
5 |
|
|
# $Log: Abstract.pm,v $ |
6 |
joko |
1.4 |
# Revision 1.3 2002/11/09 01:27:23 joko |
7 |
|
|
# + dependency of Class::Tangram now is 1.13 at minimum |
8 |
|
|
# |
9 |
joko |
1.3 |
# Revision 1.2 2002/10/25 11:45:10 joko |
10 |
|
|
# + enhanced robustness |
11 |
|
|
# + more logging for debug-levels |
12 |
|
|
# |
13 |
joko |
1.2 |
# Revision 1.1 2002/10/17 00:07:14 joko |
14 |
|
|
# + new |
15 |
joko |
1.1 |
# |
16 |
|
|
# |
17 |
|
|
############################################### |
18 |
|
|
|
19 |
|
|
# ==================================== |
20 |
|
|
package Data::Storage::Schema::Abstract; |
21 |
|
|
|
22 |
|
|
use strict; |
23 |
|
|
use warnings; |
24 |
|
|
|
25 |
|
|
use Tangram; |
26 |
|
|
use Class::Tangram; |
27 |
|
|
|
28 |
|
|
use Tangram::RawDate; |
29 |
|
|
use Tangram::RawTime; |
30 |
|
|
use Tangram::RawDateTime; |
31 |
|
|
|
32 |
|
|
use Tangram::FlatArray; |
33 |
|
|
use Tangram::FlatHash; |
34 |
|
|
use Tangram::PerlDump; |
35 |
|
|
|
36 |
|
|
# get logger instance |
37 |
|
|
my $logger = Log::Dispatch::Config->instance; |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
# ======== object constructor - %args to $self ======== |
41 |
|
|
sub new { |
42 |
|
|
my $invocant = shift; |
43 |
|
|
my $class = ref($invocant) || $invocant; |
44 |
|
|
my @args = (); |
45 |
|
|
@_ && (@args = @_); |
46 |
joko |
1.2 |
|
47 |
joko |
1.1 |
#my $self = { @_ }; |
48 |
|
|
|
49 |
|
|
# merge all entries from all hashes in args-array to single array (key, val, key, val) |
50 |
|
|
my @args_all = (); |
51 |
|
|
map { push(@args_all, %{$_}); } @args; |
52 |
|
|
# make hash from array |
53 |
|
|
my %args = @args_all; |
54 |
joko |
1.2 |
|
55 |
|
|
$logger->debug( __PACKAGE__ . "->new( @args_all )" ); |
56 |
joko |
1.1 |
|
57 |
|
|
my $self = { %args }; |
58 |
|
|
bless $self, $class; |
59 |
|
|
|
60 |
|
|
# remember as which Class we are instantiated |
61 |
|
|
$self->{invocant} = $invocant; |
62 |
|
|
|
63 |
|
|
$self->_checkClassTangram(); |
64 |
|
|
$self->_init(); |
65 |
|
|
|
66 |
|
|
return $self; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
sub _checkClassTangram { |
70 |
|
|
my $self = shift; |
71 |
|
|
# check version of "Class::Tangram" |
72 |
|
|
my $ctversion = $Class::Tangram::VERSION; |
73 |
|
|
#print "ctversion: ", $ctversion, "\n"; |
74 |
|
|
#if ($ctversion ne 1.04) { |
75 |
joko |
1.3 |
if ($ctversion lt 1.13) { |
76 |
joko |
1.1 |
print "Version of \"Class::Tangram\": ", $ctversion, "\n"; |
77 |
joko |
1.3 |
print "minimum required version : ", "1.13", "\n"; |
78 |
joko |
1.1 |
#print "quitting!!! (please install older version (1.04) of \"Class::Tangram\"", "\n"; |
79 |
|
|
exit; |
80 |
|
|
} |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
sub _parsePackageNamesFromFile { |
84 |
|
|
my $self = shift; |
85 |
|
|
my $filename = shift; |
86 |
|
|
my $exclude_pattern = shift; |
87 |
|
|
$logger->debug( __PACKAGE__ . "->_parsePackageFile( ... )" ); |
88 |
joko |
1.2 |
if (!$filename) { |
89 |
|
|
$logger->error( __PACKAGE__ . "->_parsePackageFile: you must specify a filename" ); |
90 |
|
|
return; |
91 |
|
|
} |
92 |
|
|
if (! -e $filename) { |
93 |
|
|
$logger->error( __PACKAGE__ . "->_parsePackageFile: could not find $filename" ); |
94 |
|
|
} |
95 |
joko |
1.1 |
my @packages = (); |
96 |
|
|
open(FH, '<' . $filename); |
97 |
|
|
while (<FH>) { |
98 |
|
|
chomp(); |
99 |
|
|
my $good = 1; |
100 |
|
|
$exclude_pattern && ($good = !m/$exclude_pattern/); |
101 |
|
|
push @packages, $1 if m/^package (.+?);/ && $good; |
102 |
|
|
} |
103 |
|
|
close(FH); |
104 |
|
|
return \@packages; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
sub _doinit { |
108 |
|
|
my $self = shift; |
109 |
|
|
|
110 |
|
|
$logger->debug( __PACKAGE__ . "->_doinit()" ); |
111 |
|
|
|
112 |
|
|
# WHAT? |
113 |
|
|
# imports classes to Tangram-Schema in this style, but in an abstracted way: |
114 |
|
|
# Class::Tangram::import_schema("SystemEvent"); |
115 |
|
|
# TODO: |
116 |
|
|
# move this code to Data::Storage::Schema::Tangram |
117 |
|
|
|
118 |
|
|
# TODO: |
119 |
|
|
# two cases: |
120 |
|
|
# 1. use a given list of strings as to-be-used-object-names |
121 |
|
|
# 2. parse the content of the given package and use all declared Perl Packages as objects |
122 |
|
|
|
123 |
|
|
my $classlist = $self->{EXPORT_OBJECTS}; |
124 |
|
|
|
125 |
|
|
# parse complete package to build list of classes |
126 |
|
|
if ($#{$classlist} == -1) { |
127 |
|
|
$logger->debug( "_doinit() Initializing all Classes from " . $self->{invocant} ); |
128 |
joko |
1.2 |
if (!$self->{invocant}->can('getFilename')) { |
129 |
|
|
$logger->error( __PACKAGE__ . "->_doinit: Package $self->{invocant} doesn't contain method 'getFilename'" ); |
130 |
|
|
return; |
131 |
|
|
} |
132 |
joko |
1.1 |
my $perl_packagefile = eval('return ' . $self->{invocant} . '::getFilename();'); |
133 |
|
|
my $packages = $self->_parsePackageNamesFromFile($perl_packagefile, $self->{invocant}); |
134 |
|
|
# TODO: |
135 |
|
|
# cache list so this routine isn't called on every startup/request |
136 |
|
|
$classlist = $packages; |
137 |
|
|
} |
138 |
|
|
|
139 |
joko |
1.4 |
my $schema_properties = {}; |
140 |
|
|
if ($self->{invocant}->can('getProperties')) { |
141 |
|
|
$schema_properties = eval('return ' . $self->{invocant} . '::getProperties();'); |
142 |
|
|
} |
143 |
|
|
|
144 |
joko |
1.1 |
# build argument to be passed to "Tangram::Schema->new(...)" |
145 |
|
|
my @classes; |
146 |
|
|
map { |
147 |
joko |
1.2 |
$logger->debug( __PACKAGE__ . "->_doinit: Initializing Class $_" ); |
148 |
joko |
1.1 |
# classname |
149 |
|
|
push @classes, $_; |
150 |
|
|
# schema |
151 |
|
|
my $schema; |
152 |
|
|
my $evalstr = '$schema = $' . $_ . '::schema;'; |
153 |
|
|
my $res = eval($evalstr); |
154 |
|
|
push @classes, $schema; |
155 |
|
|
} @{$classlist}; |
156 |
|
|
|
157 |
joko |
1.4 |
# build up the schema as a hash |
158 |
|
|
my $schema = { |
159 |
|
|
classes => \@classes, |
160 |
|
|
}; |
161 |
|
|
|
162 |
|
|
# merge schema properties |
163 |
|
|
foreach (keys %$schema_properties) { |
164 |
|
|
$schema->{$_} = $schema_properties->{$_}; |
165 |
|
|
} |
166 |
|
|
|
167 |
joko |
1.1 |
# create a new Tangram schema object |
168 |
joko |
1.4 |
# a) these classes must occour in the same order used at a previous setup or |
169 |
|
|
# b) better use class ids! |
170 |
|
|
$self->{schema} = Tangram::Schema->new($schema); |
171 |
joko |
1.1 |
return 1; |
172 |
|
|
|
173 |
joko |
1.4 |
=pod |
174 |
joko |
1.1 |
# ====================================== |
175 |
joko |
1.4 |
# schema template |
176 |
joko |
1.1 |
$self->{schema} = Tangram::Schema->new({ |
177 |
joko |
1.4 |
|
178 |
joko |
1.1 |
classes => [ |
179 |
|
|
# misc |
180 |
joko |
1.4 |
'Person' => $Person::schema, |
181 |
joko |
1.1 |
], |
182 |
joko |
1.4 |
|
183 |
|
|
make_object => sub { ... }, |
184 |
|
|
set_id => sub { ... } |
185 |
|
|
get_id => sub { ... } |
186 |
|
|
normalize => sub { ... }, |
187 |
|
|
|
188 |
|
|
control => '...' |
189 |
|
|
|
190 |
|
|
sql => { ... }, |
191 |
|
|
|
192 |
joko |
1.1 |
}); |
193 |
|
|
# ====================================== |
194 |
joko |
1.4 |
=cut |
195 |
joko |
1.1 |
|
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
sub _init { |
199 |
|
|
my $self = shift; |
200 |
|
|
if (!$self->{schema}) { |
201 |
|
|
$self->_doinit(); |
202 |
|
|
} |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
# return schema |
206 |
|
|
sub getSchema { |
207 |
|
|
my $self = shift; |
208 |
|
|
if (!$self->{schema}) { |
209 |
joko |
1.2 |
#print "schema is not yet initialized!", "\n"; |
210 |
|
|
#exit; |
211 |
|
|
$logger->error( __PACKAGE__ . "->getSchema: Schema is not yet initialized!" ); |
212 |
|
|
return; |
213 |
joko |
1.1 |
} |
214 |
|
|
return $self->{schema}; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
|
218 |
|
|
1; |