DBIx::ORM::Declarative

Perl extension for object-oriented database access
Download

DBIx::ORM::Declarative Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Jim Schneider
  • Publisher web site:
  • http://search.cpan.org/~jschneid/

DBIx::ORM::Declarative Tags


DBIx::ORM::Declarative Description

Perl extension for object-oriented database access DBIx::ORM::Declarative is a Perl module for object-oriented database access.SYNOPSIS # In, e.g., MyModel.pm package MyModel; use DBIx::ORM::Declarative ( { schema => $name, limit_clause => 'LIMIT %count% OFFSET %offset%', tables => , unique => , ... ], columns => , }, ... ], joins => , }, ... ], }, ... ); 1; # In the application class my $db = MyModel->new(handle => $dbh); # Add a table on the fly $db->table(table => $name, primary => , ...); # Use a table my $tab = $db->table_name; my @res = $tab->search(, ... ); my $ent = $tab->create($key => $value, ...); $ent->column_name($value); $ent->commit; $ent->delete; $tab->delete(, ...); my $len = $tab->size; print "Table $name now has $len rows ";ABSTRACTDBIx::ORM::Declarative encapsulates the creation of database table classes at compile time. You declare the properties of table and row classes with data structures passed to the DBIx::ORM::Declarative module when the module is used. You can also add table classes on the fly. Best of all, you don't need to know a thing about SQL to create or use the classes.The DBIx::ORM::Declarative class encapsulates common database operations on tables and provides an object-oriented interface to them. It provides a simple way of constructing an object-oriented database framework. In particular, the user doesn't need to create base classes and subclasses - the DBIx::ORM::Declarative class takes care of all that for you. No SQL knowledge is needed for most databases, not even for joins.The class is customized at compile time by presenting a list of schema declarations in the use declaration for the module. This is accomplished by creating a family of new, related classes on the fly. The base class of this family is called the schema class. As a convenience, this schema class is added as a base class of the class where DBIx::ORM::Declarative was used; thus, you don't need create DBIx::ORM::Declarative objects directly, and you can create subclasses that extend DBIx::ORM::Declarative without having to know the intimate details of how the classes work.The schema declarations are hash references, each of which must have the keys schema (which declares the name to be used to bind a particular database declaration to an object) and tables (which declares the details of the tables themselves). The key limit_clause, if present, declares a substitution pattern for use with limiting searches. By default, it's LIMIT %offset%,%count%, which is suitable for MySQL. The example above is suitable for PostgreSQL. The key joins, if present, declares relations between tables. More on it later.The value corresponding to the tables key is a reference to an array of hash references, each of which describes the particulars of a single table. The value corresponding to the table_aliases key is a hash reference, where each key is a table's alias, and the corresponding value is the name of the base table. The value corresponding to the joins key is a reference to an array of hash references, each of which describes the particulars of a constructed equijoin.The table hashes each support the keys table (which declares the name of the table, as used by SQL), alias (which declares the name of the table as used by Perl, if needed), primary (which declares the components of the primary key), unique (which declares the components of other unique keys), and columns (which declares the name and constraints of columns). Additionally, if your table has a primary key consisting of a single column, you can provide a stand-in for the primary key in the case where you provide a null value for the primary key on creation. The table hash keys for_null_primary and select_null_primary control this. You can also create a virtual table as join between two or more tables using the join_clause hash key, and you can search on aggregates by providing a group_by hash key.If the table name is not a valid Perl identifier, an alias should be given. This will let you use derived methods to access the table. You can provide an alias for table name even if the SQL table name is a valid Perl identifier. If an alias is provided, the table will be accessed with that name.The primary key declaration is a reference to an array of strings, each of which is the SQL name of a column that makes up part of the primary key. This portion of the table declaration is optional.The unique keys declaration is a reference to an array of array refs, each of which contains a list of strings that declare the SQL names of the columns that make up the unique key. It is not necessary to replicate the primary key declaration in the unique keys declaration - it will be copied over as needed.The for_null_primary and select_null_primary allow you to use somewhat arcane features of various databases to derive primary key values. The value corresponding to the for_null_primary key is the literal expression to be used in place of a NULL for a missing or undefined primary key column. For example, if you are using an Oracle database, and using the sequence SEQKEY for your primary key value, you would use SEQKEY.NEXTVAL. If this key is not present, NULL primary key values won't be included in affected INSERT statements.The value of the select_null_primary key provides a literal SQL SELECT command to fetch the primary key value generated by the for_null_primary. For the Oracle example, the appropriate value would be: SELECT SEQKEY.CURRVAL FROM DUALIf you are using MySQL with an auto increment primary key, you would not need to set a value for for_null_primary, and you would use the following for select_null_primary: SELECT LAST_INSERT_ID()The join_clause key lets you define derived tables by performing a join. The value is a literal string that pastes two or more tables together. For example, if you have tables named EMPLOYEE, ADDRESS, and PAYROLL, where EMPLOYEE has an ADDRESS_ID column corresponding to ADDRESS's primary key, and PAYROLL has an EMPLOYEE_ID column, the appropriate join clause, table clause, and alias clause would be something like this: table => 'PAYROLL', alias => 'PAYROLL_EMPLOYEE_ADDRESS', join_clause => 'JOIN EMPLOYEE USING (EMPLOYEE_ID) JOIN ADDRESS ' . 'USING (ADDRESS_ID)',This construction requires the use of aliases for table and column names. For a more flexible alternative, see NEW JOIN SYNTAX, below.The columns declaration is a reference to an array of hash refs, each of which declares a single column. The valid keys are name (which provides the SQL name for the column), alias (which provides the Perl name for the column, if necessary), type (which is one of number, string, nullablenumber, or nullablestring), matches (which is a regular expression that should match whatever is in the column), and constraint (which is a reference to a function or the name of a method used to validate the value of the column).If the column name is not a valid Perl identifier, an alias should be given, so that derived methods can be used on returned row objects.Type checking upon setting a column's value will be done in the order constraint, matches, type - if the constraint key is present, the corresponding code reference or method will be called to validate any new column value, or else if the matches key is present, the corresponding regular expression will be used to vallidate any new column value, or else if the type key is present, the type will be validated. Note that a type of nullablestring implies no validation at all.The constraint function is called as a method on the table or row object, and it's given the proposed new value and column names as arguments. For example, if you're attempting to set the col1 column on a row to the value no surrender, the validation code would be run as: $ent->$validate('no surrender', 'col1');Any columns in a primary key declaration that aren't in a columns declaration are added to the end of the columns declaration in the order found in the primary key declaration, with a type of string. Any columns in a unique key declaration that aren't in a columns or primary key declaration are added after that, with a type of nullablestring.The group_by key lets you define searches on tables using aggregated results. For example, if you have a PAYROLL table with an EMPLOYEE_ID, CHECK_DT, and CHECK_AMT table, and you want to be able to get the total amount an employee has been paid in a given period of time, you'd need a table declaration like this: table => 'PAYROLL', alias => 'payroll_total_pay', group_by => 'EMPLOYEE_ID', columns => , Requirements: · Perl


DBIx::ORM::Declarative Related Software