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