ORM/Auto.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 55 CC 4 SCAR 24.0
CC
4
Cov
0%
orm
20.0
DESTROY
2.0
BEGIN
2.0
CRAP 11.0
1 #
2 # DESCRIPTION
3 # ORM::Auto - is module for auto create all nedded ORM classes
4 # for connected to database.
5 #
6 # PerlORM - Object relational mapper (ORM) for Perl. PerlORM is Perl
7 # library that implements object-relational mapping. Its features are
8 # much similar to those of Java's Hibernate library, but interface is
9 # much different and easier to use.
10 #
11 # AUTHOR
12 # Nickolay A. Briginetc <nick_briginetc@sourceforge.net>
13 #
14 # COPYRIGHT
15 # Copyright (C) 2005-2006 Nickolay A. Briginetc & Alexey V. Akimov
16 #
17 # This library is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU Lesser General Public
19 # License as published by the Free Software Foundation; either
20 # version 2.1 of the License, or (at your option) any later version.
21 #
22 # This library is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 # Lesser General Public License for more details.
26 #
27 # You should have received a copy of the GNU Lesser General Public
28 # License along with this library; if not, write to the Free Software
29 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #
31 #package ORMAuto;
32 package ORM::Auto;
33
34 use strict;
35
36 use vars qw(@ISA @EXPORT $__BASES);
37
38 @ISA = ('Exporter');
39
40 @EXPORT = qw(orm);
41
42 $__BASES = {};
43
44 use Carp;
45
46 sub orm {
47 my $config_name = shift;
48 my $callpkg = scalar caller;
49
50 # åÓÌÉ ÎÅ ÕËÁÚÁÔØ ÉÍÑ ËÏÎÆÉÇÕÒÁÃÉÉ ÔÏ ÂÕÄÅÔ ÉÓÐÏÌØÚÏ×ÁÎÏ ÉÍÑ ×ÙÚ×Á×ÛÅÇÏ ÐÁËÅÔÁ,
51 # Ó ÎÅÂÏÌØÛÉÍ ÄÏ×ÅÓËÏÍ :)
52 my $base = $config_name || $callpkg.'_ORM';
53
54 if (not exists $__BASES->{$base}) {
55 # ÐÏÐÙÔËÁ ×ÙÚ×ÁÔØ ÓÕÝÅÓÔ×ÕÀÝÉÊ ÍÏÄÕÌØ
56 eval "use $base;";
57
58 # ÅÓÌÉ ÎÅ ÐÏÌÕÞÉÌÏÓØ ÔÏ ÓÏÚÄÁÅÍ ÎÏ×ÙÊ package Ó ÎÅÏÂÈÏÄÉÍÙÍÉ Ó×ÏÊÓÔ×ÁÍÉ.
59 my $eval = qq(package $base; use base 'ORM'; );
60
61
62 # Á ÚÄÅÓØ ÉÄÅÔ ×ÎÕÔÒÅÎÎÉÊ eval, ÞÔÏ ×ÏÚÍÏÖÎÏ ÐÌÏÈÏ ÓËÁÖÅÔÓÑ ÎÁ ÐÒÏÉÚ×ÏÄÉÔÅÌØÎÏÓÔÉ
63 # ÎÏ ÄÏÂÁ×ÌÑÅÔ ÎÅÍÅÒÑÎÏ ÆÕÎËÃÉÏÎÁÌØÎÏÓÔÉ.
64 # ËÌÁÓÓ ÔÁÂÌÉÃÙ ÓÏÚÄÁÅÔÓÑ ÎÁ ÌÅÔÕ ÉÚ ÉÍÅÎÉ ×ÙÚ×ÁÎÎÏÊ ÆÕÎËÃÉÉ
65 # É ÅÓÌÉ ÔÁËÁÑ ÔÁÂÌÉÃÁ ÓÕÝÅÓÔ×ÕÅÔ ÔÏ ÂÕÄÅÔ ×ÐÏÌÎÅ ÁÄÅË×ÁÔÎÏ ÐÏÄËÌÀÞÅÎÁ
66 # É ÍÏÖÅÔ ÉÓÐÏÌØÚÏ×ÁÔØÓÑ ËÁË ÏÂßÅËÔ ORM
67 $eval .= q(our $__TABLES = {};);
68 $eval .= q(sub AUTOLOAD { shift; use vars qw($AUTOLOAD););
69 $eval .= q(return $__TABLES->{$AUTOLOAD} if exists $__TABLES->{$AUTOLOAD};);
70 $eval .= q(eval "use $AUTOLOAD;";);
71 $eval .= q(my $eval = "package $AUTOLOAD;";);
72 $eval .= q($eval .= "use ORM::Base ').$base.q('";);
73 $eval .= q($eval .= ", qw(@_)" if @_;);
74 $eval .= q($eval .= "; ";);
75 $eval .= q(eval $eval if $@;);
76 $eval .= q(warn("$@") if $@;);
77 $eval .= q(eval "do $AUTOLOAD;";);
78 $eval .= q($__TABLES->{$AUTOLOAD} = $AUTOLOAD;);
79 $eval .= q(return $AUTOLOAD;});
80
81 eval $eval if $@;
82 croak("$@") if $@;
83
84 eval "do $base";
85
86 $__BASES->{$base} = $base;
87 }
88
89 return $base
90
91 }
92
93 sub DESTROY {
94 my $self = shift;
95 }
96
97 =head1 NAME
98
99 ORM::Auto - is module for auto create all nedded ORM classes
100 for connected to database.
101
102 =head1 SYNOPSIS
103
104 use ORM::Auto;
105 use ORM::Error;
106 use ORM::Db::DBI::SQLite;
107
108 my $error = ORM::Error->new;
109
110 orm->_init(
111 prefer_lazy_load => 0,
112 emulate_foreign_keys => 1,
113 default_cache_size => 200,
114 error => $error,
115 db => ORM::Db::DBI::SQLite->new
116 (database => 'data/main.db')
117 );
118 die $error->text if $error->any;
119
120 unless (orm->sessions(table => 't_sessions')->find_id(id => 1) {
121 print "Created new session with id: ".orm->sessions->new->id;
122 }
123
124 OR for multi connected
125
126 #Backup books
127 use ORM::Auto;
128 use ORM::Error;
129 use ORM::Db::DBI::SQLite;
130 use ORM::Db::DBI::MySQL;
131
132 my $error = ORM::Error->new;
133
134 # Connect to original base
135 orm->_init(
136 error => $error,
137 db => ORM::Db::DBI::MySQL->new(
138 host => 'localhost',
139 port => '3306',
140 database => 'books',
141 user => 'book_man',
142 password => 'super_password'
143 )
144 );
145 die $error->text if $error->any;
146
147 # create the backup base as one file
148 orm('new_base')->_init(
149 error => $error,
150 db => ORM::Db::DBI::SQLite->new
151 (database => 'backup_'.time.'.db')
152 );
153 die $error->text if $error->any;
154
155 # Select the books with setting interested
156 my $orm_books = orm->book(table => 'book')->find(
157 filter => (orm->book->M->interested == 1),
158 error => $error,
159 return_res => 1 );
160
161 # Write intersted books in backup base
162 while (my $orm = $orm_books->next) {
163 orm('new_base')->book(table => 'books')->new(prop => {
164 author => $orm->author,
165 title => $orm->title,
166 isbn => $orm->isbn,
167 published => $orm->date_of_publish
168 }
169 )
170 }
171 }
172
173
174 =head1 DESCRIPTION
175
176 This module allowed not created on disk, modules for each table in base
177 Also and main module for base is not need. All created on fly :)
178
179
180 =item orm([config_name])
181
182 return the primary class for connected to database
183 config_name - any allowed perl identifier for different connection to bases.
184 as default <caller_name> + '_ORM'.
185
186 Example:
187 package MyApp::books;
188 use ORM::Auto;
189
190 print orm; # printing MyApp::books_ORM
191
192 However, if module config_name is exists, connected with it.
193
194 =item orm->table([parameters])
195
196 return the table class for access to data
197 parameters allowed only once, in first use. In future is not need.
198
199 =head1 SEE ALSO
200
201 L<ORM>
202
203 L<ORM::Tutorial>
204
205 L<ORM::Error>
206
207 L<ORM::ResultSet>
208
209 L<ORM::Broken>
210
211 L<ORM::Db>
212
213 L<ORM::Order>
214
215 L<ORM::Metaprop>
216
217 L<ORM::DbLog>
218
219 http://perlorm.sourceforge.net/
220
221 =head1 AUTHOR
222
223 Nickolay A. Briginetc
224
225 =head1 COPYRIGHT AND LICENSE
226
227 Copyright (C) 2006 Nickolay A. Briginetc & Alexey V. Akimov
228
229 This library is free software; you can redistribute it and/or
230 modify it under the terms of the GNU Lesser General Public
231 License as published by the Free Software Foundation; either
232 version 2.1 of the License, or (at your option) any later version.
233
234 This library is distributed in the hope that it will be useful,
235 but WITHOUT ANY WARRANTY; without even the implied warranty of
236 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
237 Lesser General Public License for more details.
238
239 You should have received a copy of the GNU Lesser General Public
240 License along with this library; if not, write to the Free Software
241 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
242
243 =cut
244
245 1;