NAME
Class::Maker::Extension::Schema::Tangram - creates Tangram schema from a class hierarchy
SYNOPSIS
use Class::Maker;
use Class::Maker::Examples;
use Class::Maker::Extension::Schema::Tangram qw(schema);
my $class_schema = schema( 'User' );
my $schema = Tangram::Relational->schema( { classes => $class_schema,normalize => sub { $_[0] =~ s/::/_/; $_[0] } } );
my $dbh = DBI->connect( ) or die;
{
my $aref_result = $dbh->selectcol_arrayref( q{SHOW TABLES} ) or die $DBI::errstr;
my %tables;
@tables{ @$aref_result } = 1;
Tangram::Relational->deploy( $schema, $dbh ) unless exists $tables{'tangram'};
}
# To delete all tangram tables of this schema
#
# Tangram::Relational->retreat( $schema, $dbh );
@ENV{ qw(DBI_DSN DBI_USER DBI_PASS) } = ( 'DBI:mysql:localhost:tangram' );
my $storage = Tangram::Relational->connect( $schema, @ENV{ qw(DBI_DSN DBI_USER DBI_PASS) }, { dbh => $dbh } ) or die;
my $tbl = $storage->remote( 'Human::Group' );
my ($group) = $storage->select( $tbl, $tbl->{name} eq 'dbadmin' );
unless( $group )
{
$group = new Human::Group( -name => 'dbadmin', -desc => 'database administrators' );
print Dumper $group;
$storage->insert( $group );
}
and so forth...
DESCRIPTION
Class::Maker::Extension::Schema::Tangram uses reflection to get the appropriate information about a tree of classes and then to convert this into a "schema" which can be deployed to Tangram (object persistance).
schema( $oref )
Determines the "Tangram::Schema" representation of a "class tree" including the complete inhereted objects.
Constructing Tangram Schema WHEN WE HAVE TO DEPLOY (first time registering persistance).
schema() scans recursivle through the inheritance tree and creates all parent schemas also (Cave: You should
configure tangram also via the "persistance =>" key in your class.
For comulative schema (incl. "User"`s parent "Human" class) ,+ the non-inheritated "Human::Group" Class::Maker::
schema( 'User' , 'Human::Group' );
For single schema:
User->schema(); #(incl. "User"`s parent "Human" class)
or
UserGroup->schema; # no isa, no parent class schema`s !
$Class::Maker::Extension::Schema::Tangram::mappings
This is a hash which is used to map the Class::Maker attribute types to tangram types. While the first key is determing whether the attribute type value was an ARRAY ( => [qw(one two)] ) or a HASH ( => { father => 'Human' } ). Here is the default mapping table:
{ ARRAY => { hash => 'flat_hash',
array => 'flat_array',
}
}
EXAMPLE
Reflex
# Human $VAR1 = { 'configure' => { 'dtor' => 'delete', 'ctor' => 'new' }, 'public' => { 'string' => [ 'coutrycode', 'postalcode', 'firstname', 'lastname', 'sex', 'eye_color', 'hair_color', 'occupation', 'city', 'region', 'street', 'fax' ], 'int' => [ 'age' ], 'hash' => [ 'contacts', 'telefon' ], 'array' => [ 'nicknames', 'friends' ], 'time' => [ 'birth', 'driverslicense', 'dead' ] } };
# User $VAR1 = { 'isa' => [ 'Human' ], 'version' => '0.01', 'public' => { 'string' => [ 'email', 'lastlog', 'registered' ], 'real' => [ 'konto' ], 'int' => [ 'logins' ], 'array' => { 'cars' => 'Vehicle', 'friends' => 'User' }, 'ref' => { 'group' => 'Human::Group' } } };
# Vehicle
$VAR1 = { 'public' => { 'string' => [ 'model' ], 'int' => [ 'wheels' ] } };
#Human::Group
$VAR1 = { 'public' => { 'string' => [ 'name', 'desc' ] } };
Result
$VAR1 = [ [ 'Vehicle', { 'fields' => { 'string' => [ 'model' ], 'int' => [ 'wheels' ] } }, 'Human::Group', { 'fields' => { 'string' => [ 'name', 'desc' ] } }, 'Human', { 'fields' => { 'string' => [ 'coutrycode', 'postalcode', 'firstname', 'lastname', 'sex', 'eye_color', 'hair_color', 'occupation', 'city', 'region', 'street', 'fax' ], 'flat_array' => [ 'nicknames', 'friends' ], 'int' => [ 'age' ], 'flat_hash' => [ 'contacts', 'telefon' ], 'time' => [ 'birth', 'driverslicense', 'dead' ] } }, 'User', { 'bases' => [ 'Human' ], 'fields' => { 'string' => [ 'email', 'lastlog', 'registered' ], 'real' => [ 'konto' ], 'int' => [ 'logins' ], 'array' => { 'cars' => 'Vehicle', 'friends' => 'User' }, 'ref' => { 'group' => 'Human::Group' } } } ] ];