annotate Lib/Deployment/CDBI.pm @ 30:dd4d72600c69

ORM in works
author Sergey
date Tue, 03 Nov 2009 16:31:47 +0300
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 package Deployment::CDBI;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 use DBI;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5 use Schema::DataSource;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 use Schema::DataSource::CDBIBuilder;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8 our @ISA = qw(Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 DeclareProperty DataSchemaFile => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 DeclareProperty DataSourceDir => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13 DeclareProperty DSNamespace => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 DeclareProperty DBConnection => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 DeclareProperty DBTraitsClass => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 my ($this,%args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 $this->{$DataSchemaFile} = $args{'DataSchemaFile'} or die new Exception('A data shema file is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 $this->{$DataSourceDir} = $args{'DataSourceDir'} or die new Exception('A directory for a data source is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 $this->{$DSNamespace} = $args{'DSNamespace'} || 'DataSource';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 $this->{$DBTraitsClass} = $args{'DBTraitsClass'} or die new Exception('A DBTraitsClass is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 $this->{$DBConnection} = $args{'DBConnection'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 sub Update {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 my ($this) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 my $prefix = $this->{$DSNamespace}.'::';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 my $schemaDS = new Schema::DataSource(DataSourceBuilder => new Schema::DataSource::CDBIBuilder);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 $schemaDS->BuildSchema($this->{$DataSchemaFile});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36 my $schemaDB = $schemaDS->DataSourceBuilder->BuildDBSchema();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 (my $fname = $this->{$DSNamespace} ) =~ s/::/\//g;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 $schemaDS->DataSourceBuilder->WriteModules($this->{$DataSourceDir}.$fname.'.pm',$prefix);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40 if ($this->{$DBConnection}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 my $dbh = DBI->connect(@{$this->{$DBConnection}}) or die new Exception('Failed to connect to the database',@{$this->{$DBConnection}});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 my $SchemaSource;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 if (UNIVERSAL::can($this->{$DBTraitsClass},'GetMetaTable')) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 $SchemaSource = new Deployment::CDBI::SQLSchemeSource (MetaTable => $this->{$DBTraitsClass}->GetMetaTable($dbh));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 die new Exception("Can't get meta table");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 my $schemaDBOld = $SchemaSource->ReadSchema($schemaDB->Name);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 my $updater = $this->{$DBTraitsClass}->new(SrcSchema => $schemaDBOld, DstSchema => $schemaDB);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 $updater->UpdateSchema();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 $dbh->do($_) or die new Exception('Failed to execute the sql statement', $_) foreach $updater->Handler->Sql;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 $SchemaSource->SaveSchema($schemaDB);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 $schemaDBOld->Dispose;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 $schemaDB->Dispose;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 package Deployment::CDBI::SQLSchemeSource;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 use Data::Dumper;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 use MIME::Base64;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 use Storable qw(nstore_fd fd_retrieve);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69 our @ISA = qw(Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 DeclareProperty MetaTable => ACCESS_NONE;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 sub ReadSchema {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 my ($this,$name) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78 my $schema = decode_base64($this->{$MetaTable}->ReadProperty("db_schema_$name"));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 if ($schema) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 open my $hvar,"<",\$schema or die new Exception("Failed to create a handle to the variable");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 return fd_retrieve($hvar);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 return new Schema::DB(Name => $name, Version => 0);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 sub SaveSchema {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88 my ($this,$schema) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90 my $name = $schema->Name;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 my $data;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94 open my $hvar,">",\$data or die new Exception("Failed to create a handle to the variable");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95 nstore_fd($schema,$hvar);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98 $this->{$MetaTable}->SetProperty("db_schema_$name",encode_base64($data));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 1;