/[cvs]/nfo/perl/libs/Data/Storage/Schema/Abstract.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Storage/Schema/Abstract.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations)
Sun Nov 17 07:12:06 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.4: +12 -3 lines
+ extended ::getProperties with argument "want_transactions"

1 ###############################################
2 #
3 # $Id: Abstract.pm,v 1.4 2002/11/15 13:21:39 joko Exp $
4 #
5 # $Log: Abstract.pm,v $
6 # 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 # Revision 1.3 2002/11/09 01:27:23 joko
10 # + dependency of Class::Tangram now is 1.13 at minimum
11 #
12 # Revision 1.2 2002/10/25 11:45:10 joko
13 # + enhanced robustness
14 # + more logging for debug-levels
15 #
16 # Revision 1.1 2002/10/17 00:07:14 joko
17 # + new
18 #
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
50 $logger->debug( __PACKAGE__ . "->new" );
51
52 #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
60 #$logger->debug( __PACKAGE__ . "->new( @args_all )" );
61
62 my $self = { %args };
63 bless $self, $class;
64
65 #use Data::Dumper; print Dumper($self);
66
67 # 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 if ($ctversion lt 1.13) {
83 print "Version of \"Class::Tangram\": ", $ctversion, "\n";
84 print "minimum required version : ", "1.13", "\n";
85 #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 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 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 if (!$self->{invocant}->can('getFilename')) {
136 $logger->error( __PACKAGE__ . "->_doinit: Package $self->{invocant} doesn't contain method 'getFilename'" );
137 return;
138 }
139 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 my $schema_properties = {};
147 if ($self->{invocant}->can('getProperties')) {
148 # 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 }
152
153 # build argument to be passed to "Tangram::Schema->new(...)"
154 my @classes;
155 map {
156 $logger->debug( __PACKAGE__ . "->_doinit: Initializing Class $_" );
157 # 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 # 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 # create a new Tangram schema object
177 # 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 return 1;
181
182 =pod
183 # ======================================
184 # schema template
185 $self->{schema} = Tangram::Schema->new({
186
187 classes => [
188 # misc
189 'Person' => $Person::schema,
190 ],
191
192 make_object => sub { ... },
193 set_id => sub { ... }
194 get_id => sub { ... }
195 normalize => sub { ... },
196
197 control => '...'
198
199 sql => { ... },
200
201 });
202 # ======================================
203 =cut
204
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 #print "schema is not yet initialized!", "\n";
219 #exit;
220 $logger->error( __PACKAGE__ . "->getSchema: Schema is not yet initialized!" );
221 return;
222 }
223 return $self->{schema};
224 }
225
226
227 1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed