/[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.6 - (show annotations)
Sun Nov 17 07:21:47 2002 UTC (21 years, 7 months ago) by jonen
Branch: MAIN
Changes since 1.5: +5 -1 lines
+ using Class::Tangram::import_schema() again for proper usage of its helper functions

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

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