Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added DBIC related classes

  • Loading branch information...
commit affc2e8674be56421ad20f51e6112e2252d8f3bf 1 parent 4197ad3
@dann authored
View
8 angelos-core/Makefile.PL
@@ -78,9 +78,11 @@ requires(
'JSON::XS' => 0,
'Term::ReadLine' => 0,
- # Components
- 'Template' => '2.20',
- 'DBIx::Class::Schema' => '0.08012',
+ # Components (remove from core)
+ 'Template' => '2.20',
+ 'DBIx::Class::Schema' => '0.08012',
+ 'DBIx::Class::AsFdat' => 0,
+ 'DBIx::Class::UTF8Columns' => 0,
# Controller plugins
'Time::HiRes' => 0,
View
61 angelos-core/lib/Angelos/ORM/DBIC/AutoInflateDateTime.pm
@@ -0,0 +1,61 @@
+package Angelos::ORM::DBIC::AutoInflateDateTime;
+use strict;
+use warnings;
+use UNIVERSAL::require;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
+
+sub _datetime {
+ my $self = shift;
+ DateTime->require or die "DateTime is required";
+ return 'DateTime';
+}
+
+sub _datetime_parser {
+ my $self = shift;
+ if (my $parser = $self->__datetime_parser) {
+ return $parser;
+ }
+ my $parser = $self->result_source->storage->datetime_parser(@_);
+ return $self->__datetime_parser($parser);
+}
+
+sub datetime_column {
+ my $class = shift;
+ my $col = shift;
+
+ $class->inflate_column($col, {
+ inflate => sub {
+ my ($value, $obj) = @_;
+ return if $value eq '0000-00-00 00:00:00'; # XXX care for mysql return value
+ my $dt = $obj->_datetime_parser->parse_datetime($value);
+ return $dt ? $class->datetime->from_object(object => $dt) : undef;
+ },
+ deflate => sub {
+ my ($value, $obj) = @_;
+ $obj->_datetime_parser->format_datetime($value);
+ },
+ });
+}
+
+sub date_column {
+ my $class = shift;
+ my $col = shift;
+
+ $class->inflate_column($col, {
+ inflate => sub {
+ my ($value, $obj) = @_;
+ return if $value eq '0000-00-00'; #XXX care for mysql return value
+ my $dt = $obj->_datetime_parser->parse_date($value);
+ return $dt ? $class->datetime->from_object(object => $dt) : undef;
+ },
+ deflate => sub {
+ my ($value, $obj) = @_;
+ $obj->_datetime_parser->format_date($value);
+ },
+ });
+}
+
+1;
View
25 angelos-core/lib/Angelos/ORM/DBIC/AutoStoreDateTime.pm
@@ -0,0 +1,25 @@
+package Angelos::ORM::DBIC::AutoStoreDateTime;
+use strict;
+use warnings;
+use base 'DBIx::Class';
+
+# steal from MoFedge::Data::DBIC::AutoStoreDataTime
+sub insert {
+ my $self = shift;
+
+ $self->store_column( 'created_on', $self->datetime->now )
+ if $self->result_source->has_column('created_on');
+
+ $self->next::method(@_);
+}
+
+sub update {
+ my $self = shift;
+
+ $self->updated_on( $self->datetime->now )
+ if $self->result_source->has_column('updated_on');
+
+ $self->next::method(@_);
+}
+
+1;
View
55 angelos-core/lib/Angelos/ORM/DBIC/Schema.pm
@@ -0,0 +1,55 @@
+package Angelos::ORM::DBIC::Schema;
+use strict;
+use warnings;
+use base qw/DBIx::Class::Schema/;
+use Switch;
+
+__PACKAGE__->load_components(qw/
+ +Angelos::ORM::DBIC::AutoInflateDateTime
+ +Angelos::ORM::DBIC::AutoStoreDateTime
+ AsFdat
+ UTF8Columns
+ PK::Auto
+ Core
+/);
+
+
+__PACKAGE__->mk_classdata('master_schema');
+__PACKAGE__->mk_classdata('slave_schema');
+
+sub register_column {
+ my ($class, $column) = @_;
+
+ $class->next::method($column);
+
+ switch ($column) {
+ case /^created_on$/ { $class->datetime_column($column) }
+ case /^updated_on$/ { $class->datetime_column($column) }
+ case /_on$/ { $class->date_column($column) }
+ case /_at$/ { $class->datetime_column($column) }
+ }
+}
+
+sub master {
+ my $class = shift;
+ my $connect_info
+ = $class->config->database->{'master'}->{'connect_info'};
+ $class->master_schema($class->connect( @{$connect_info} )) unless $class->master_schema;
+ $class->master_schema;
+}
+
+sub slave {
+ my $class = shift;
+ return unless $class->config->{'database'};
+ return unless $class->config->{'database'}->{'slave'};
+ my $connect_info
+ = $class->config->{'database'}->{'slave'}->{'connect_info'};
+ $class->slave_schema($class->connect( @{$connect_info} )) unless $class->slave_schema;
+ $class->slave_schema;
+}
+
+sub config {
+ die 'sub class must implement config method';
+}
+
+1;
View
2  angelos-core/lib/Angelos/Script/Command/Db.pm
@@ -51,7 +51,7 @@ sub generate_schema {
my $connect_info = $self->_get_connect_info($app_class);
make_schema_at(
"${app_class}::Schema",
- { components => [ 'ResultSetManager', 'UTF8Columns' ],
+ { components => [ ],
dump_directory => dir( 'lib' ),
really_erase_my_files => $erase,
debug => 1,
View
43 angelos-core/lib/Angelos/Service/Role/DBIC.pm
@@ -2,7 +2,6 @@ package Angelos::Service::Role::DBIC;
use Angelos::Role;
use List::MoreUtils;
-
has 'resultset_moniker' => (
is => 'rw',
required => 1,
@@ -19,11 +18,17 @@ has 'cache' => (
required => 1
);
+has 'primary_key' => (
+ is => 'rw',
+ required => 1,
+ default => 'id',
+);
+
sub find {
my ( $self, $id ) = @_;
my $cache_key = $self->cache_key($id);
- my $obj = $self->get_from_cache($cache_key);
+ my $obj = $self->get_from_cache($cache_key);
if ( !$obj ) {
$obj = $self->_resultset->find($id);
if ($obj) {
@@ -34,8 +39,8 @@ sub find {
}
sub get_from_cache {
- my ($self, $cache_key) = @_;
- my $obj = $self->cache->get($cache_key);
+ my ( $self, $cache_key ) = @_;
+ my $obj = $self->cache->get($cache_key);
$obj;
}
@@ -46,25 +51,26 @@ sub get_multi_from_cache {
}
sub find_multi {
- my($self, @ids) = @_;
+ my ( $self, @ids ) = @_;
- my %id2key = map { $_ => $self->cache_key($_) } grep { defined } @ids;
- my $got = $self->get_multi_from_cache(values %id2key);
+ my %id2key = map { $_ => $self->cache_key($_) } grep {defined} @ids;
+ my $got = $self->get_multi_from_cache( values %id2key );
## If we got back all of the objects from the cache, return immediately.
- if (List::MoreUtils::all {defined $_} values %$got) {
+ if ( List::MoreUtils::all { defined $_ } values %$got ) {
my @objs = values %{$got};
return \@objs;
}
## Otherwise, look through the list of IDs to see what we're missing,
## and fall back to the backend to look up those objects.
- my($i, @got, @need, %need2got) = (0);
+ my ( $i, @got, @need, %need2got ) = (0);
for my $id (@ids) {
- if (defined $id && (my $obj = $got->{ $id2key{$id} })) {
+ if ( defined $id && ( my $obj = $got->{ $id2key{$id} } ) ) {
push @got, $obj;
- } else {
- push @got, undef;
+ }
+ else {
+ push @got, undef;
push @need, $id;
$need2got{$#need} = $i;
}
@@ -74,7 +80,7 @@ sub find_multi {
if (@need) {
for my $id (@need) {
my $obj = $self->find($id);
- $got[ $need2got{$i++} ] = $obj;
+ $got[ $need2got{ $i++ } ] = $obj;
}
}
@@ -124,6 +130,17 @@ sub first {
sub update {
my ( $self, $args ) = @_;
+
+ my $pk = $self->primary_key();
+ my $rs = $self->resultset();
+ my $key = delete $args->{$pk};
+ my $row = $self->find($key);
+ if ($row) {
+ while ( my ( $field, $value ) = each %$args ) {
+ $row->$field($value);
+ }
+ $row->update;
+ }
$self->_resultset->update($args);
}
View
15 angelos-core/lib/Angelos/View/TT.pm
@@ -33,13 +33,24 @@ has 'TIMER' => (
isa => 'Str',
);
+has 'EVAL_PERL' => (
+ is => 'rw',
+ default => 0,
+);
+
+has 'ENCODING' => (
+ is => 'rw',
+ default => 'utf8',
+);
+
sub _build_engine {
my $self = shift;
my $include_path ||= $self->INCLUDE_PATH;
$include_path ||= $self->root;
my $config = {
- EVAL_PERL => 0,
+ EVAL_PERL => $self->EVAL_PERL,
+ ENCODING=> $self->ENCODING,
INCLUDE_PATH => $include_path,
};
@@ -54,7 +65,7 @@ sub _render {
= "Couldn't render template "
. $c->stash->{template} . ": "
. $self->engine->error;
- $c->logger->error( $error );
+ $c->logger->error($error);
}
$out;
}
View
23 angelos-core/t/App/TestApp/lib/TestApp/Schema.pm
@@ -1,30 +1,9 @@
package TestApp::Schema;
use Mouse;
use TestApp::Config;
-extends qw(DBIx::Class::Schema);
+extends qw(Angelos::ORM::DBIC::Schema);
__PACKAGE__->load_classes();
-our $master_schema;
-our $slave_schema;
-
-sub master {
- my $class = shift;
- my $connect_info
- = $class->config->database->{'master'}->{'connect_info'};
- $master_schema ||= $class->connect( @{$connect_info} );
- $master_schema;
-}
-
-sub slave {
- my $class = shift;
- return unless $class->config->{'database'};
- return unless $class->config->{'database'}->{'slave'};
- my $connect_info
- = $class->config->{'database'}->{'slave'}->{'connect_info'};
- $slave_schema ||= $class->connect( @{$connect_info} );
- $slave_schema;
-}
-
sub config {
TestApp::Config->instance;
}
Please sign in to comment.
Something went wrong with that request. Please try again.