1 |
joko |
1.1 |
############################################### |
2 |
|
|
# |
3 |
|
|
# $Id$ |
4 |
|
|
# |
5 |
|
|
# $Log$ |
6 |
|
|
# |
7 |
|
|
############################################### |
8 |
|
|
|
9 |
|
|
# ==================================== |
10 |
|
|
package Data::Storage::Schema::Abstract; |
11 |
|
|
|
12 |
|
|
use strict; |
13 |
|
|
use warnings; |
14 |
|
|
|
15 |
|
|
use Tangram; |
16 |
|
|
use Class::Tangram; |
17 |
|
|
|
18 |
|
|
use Tangram::RawDate; |
19 |
|
|
use Tangram::RawTime; |
20 |
|
|
use Tangram::RawDateTime; |
21 |
|
|
|
22 |
|
|
use Tangram::FlatArray; |
23 |
|
|
use Tangram::FlatHash; |
24 |
|
|
use Tangram::PerlDump; |
25 |
|
|
|
26 |
|
|
# get logger instance |
27 |
|
|
my $logger = Log::Dispatch::Config->instance; |
28 |
|
|
|
29 |
|
|
|
30 |
|
|
# ======== object constructor - %args to $self ======== |
31 |
|
|
sub new { |
32 |
|
|
my $invocant = shift; |
33 |
|
|
my $class = ref($invocant) || $invocant; |
34 |
|
|
my @args = (); |
35 |
|
|
@_ && (@args = @_); |
36 |
|
|
#$logger->debug( __PACKAGE__ . "->new(@args)" ); |
37 |
|
|
#my $self = { @_ }; |
38 |
|
|
|
39 |
|
|
# merge all entries from all hashes in args-array to single array (key, val, key, val) |
40 |
|
|
my @args_all = (); |
41 |
|
|
map { push(@args_all, %{$_}); } @args; |
42 |
|
|
# make hash from array |
43 |
|
|
my %args = @args_all; |
44 |
|
|
|
45 |
|
|
my $self = { %args }; |
46 |
|
|
bless $self, $class; |
47 |
|
|
|
48 |
|
|
# remember as which Class we are instantiated |
49 |
|
|
$self->{invocant} = $invocant; |
50 |
|
|
|
51 |
|
|
$self->_checkClassTangram(); |
52 |
|
|
$self->_init(); |
53 |
|
|
|
54 |
|
|
return $self; |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
sub _checkClassTangram { |
58 |
|
|
my $self = shift; |
59 |
|
|
# check version of "Class::Tangram" |
60 |
|
|
my $ctversion = $Class::Tangram::VERSION; |
61 |
|
|
#print "ctversion: ", $ctversion, "\n"; |
62 |
|
|
#if ($ctversion ne 1.04) { |
63 |
|
|
if ($ctversion ne 1.12) { |
64 |
|
|
print "Version of \"Class::Tangram\": ", $ctversion, "\n"; |
65 |
|
|
print "absolutely required version: ", "1.12", "\n"; |
66 |
|
|
#print "quitting!!! (please install older version (1.04) of \"Class::Tangram\"", "\n"; |
67 |
|
|
exit; |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
sub _parsePackageNamesFromFile { |
72 |
|
|
my $self = shift; |
73 |
|
|
my $filename = shift; |
74 |
|
|
my $exclude_pattern = shift; |
75 |
|
|
$logger->debug( __PACKAGE__ . "->_parsePackageFile( ... )" ); |
76 |
|
|
my @packages = (); |
77 |
|
|
open(FH, '<' . $filename); |
78 |
|
|
while (<FH>) { |
79 |
|
|
chomp(); |
80 |
|
|
my $good = 1; |
81 |
|
|
$exclude_pattern && ($good = !m/$exclude_pattern/); |
82 |
|
|
push @packages, $1 if m/^package (.+?);/ && $good; |
83 |
|
|
} |
84 |
|
|
close(FH); |
85 |
|
|
return \@packages; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
sub _doinit { |
89 |
|
|
my $self = shift; |
90 |
|
|
|
91 |
|
|
$logger->debug( __PACKAGE__ . "->_doinit()" ); |
92 |
|
|
|
93 |
|
|
# WHAT? |
94 |
|
|
# imports classes to Tangram-Schema in this style, but in an abstracted way: |
95 |
|
|
# Class::Tangram::import_schema("SystemEvent"); |
96 |
|
|
# TODO: |
97 |
|
|
# move this code to Data::Storage::Schema::Tangram |
98 |
|
|
|
99 |
|
|
# TODO: |
100 |
|
|
# two cases: |
101 |
|
|
# 1. use a given list of strings as to-be-used-object-names |
102 |
|
|
# 2. parse the content of the given package and use all declared Perl Packages as objects |
103 |
|
|
|
104 |
|
|
my $classlist = $self->{EXPORT_OBJECTS}; |
105 |
|
|
|
106 |
|
|
# parse complete package to build list of classes |
107 |
|
|
if ($#{$classlist} == -1) { |
108 |
|
|
$logger->debug( "_doinit() Initializing all Classes from " . $self->{invocant} ); |
109 |
|
|
my $perl_packagename = $self->{invocant}; |
110 |
|
|
my $perl_packagefile = eval('return ' . $self->{invocant} . '::getFilename();'); |
111 |
|
|
my $packages = $self->_parsePackageNamesFromFile($perl_packagefile, $self->{invocant}); |
112 |
|
|
# TODO: |
113 |
|
|
# cache list so this routine isn't called on every startup/request |
114 |
|
|
$classlist = $packages; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
# build argument to be passed to "Tangram::Schema->new(...)" |
118 |
|
|
my @classes; |
119 |
|
|
map { |
120 |
|
|
$logger->debug( __PACKAGE__ . "->_doinit() Initializing Class $_" ); |
121 |
|
|
# classname |
122 |
|
|
push @classes, $_; |
123 |
|
|
# schema |
124 |
|
|
my $schema; |
125 |
|
|
my $evalstr = '$schema = $' . $_ . '::schema;'; |
126 |
|
|
my $res = eval($evalstr); |
127 |
|
|
use Data::Dumper; |
128 |
|
|
push @classes, $schema; |
129 |
|
|
} @{$classlist}; |
130 |
|
|
|
131 |
|
|
# create a new Tangram schema object |
132 |
|
|
# these classes must occour in the same order used at a previous setup |
133 |
|
|
$self->{schema} = Tangram::Schema->new({ classes => \@classes }); |
134 |
|
|
return 1; |
135 |
|
|
|
136 |
|
|
# ====================================== |
137 |
|
|
# template |
138 |
|
|
$self->{schema} = Tangram::Schema->new({ |
139 |
|
|
classes => [ |
140 |
|
|
|
141 |
|
|
# misc |
142 |
|
|
'SystemEvent' => $SystemEvent::schema, |
143 |
|
|
|
144 |
|
|
], |
145 |
|
|
}); |
146 |
|
|
# ====================================== |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
sub _init { |
152 |
|
|
my $self = shift; |
153 |
|
|
if (!$self->{schema}) { |
154 |
|
|
$self->_doinit(); |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
# return schema |
159 |
|
|
sub getSchema { |
160 |
|
|
my $self = shift; |
161 |
|
|
if (!$self->{schema}) { |
162 |
|
|
print "schema is not yet initialized!", "\n"; |
163 |
|
|
exit; |
164 |
|
|
} |
165 |
|
|
return $self->{schema}; |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
|
169 |
|
|
1; |