The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Name

Object::Relation::Schema::DB::SQLite - Object::Relation SQLite data store schema generation

Synopsis

  use Object::Relation::Schema;
  my $kbs = Object::Relation::Schema->new;
  $kbs->generate_to_file($file_name);

Description

This module generates and outputs to a file the schema information necessary to create a SQLite data store for a Object::Relation application. See Object::Relation::Schema and Object::Relation::Schema::DB for more information.

Instance Interface

Instance Methods

column_type

  my $type = $kbs->column_type($attr);

Pass in a Object::Relation::Meta::Attribute::Schema object to get back the SQLite column type to be used for the attribute. The column types are optimized for the best correspondence between the attribute types and the types supported by SQLite (relatively speaking, of course, since SQLite's data types are actually dynamic).

index_for_attr

  my $index = $kbs->index_for_attr($class, $attr);

Returns the SQL that declares an SQL index. This implementation overrides that in Object::Relation::Schema::DB to remove the UNIQUE keyword from the declaration if the attribute in question is unique but not distinct. The difference is that a unique attribute is unique only relative to the state attribute. A unique attribute can have more than one instance of a given value as long as no more than one of them also has a state greater than -1. For SQLite, this is handled by triggers.

constraints_for_class

  my @constraint_sql = $kbs->constraints_for_class($class);

Returns the SQL statements to create all of the constraints for the class described by the Object::Relation::Meta::Class::Schema object passed as the sole argument.

The constraint statements returned may include one or more of the following:

  • A trigger to ensure that the value of a state attribute is a valid state.

  • "Once triggers", which prevent a value from being changed in a column after the first time it has been set to a non-NULL value.

  • "Boolean triggers", which ensures that the value of a boolean column is either 0 or 1.

  • A foreign key constraint from the primary key column to the table for a concrete parent class.

  • Foreign key constraints to the tables for contained (referenced) objects.

  • "Unique triggers," which ensure that the value of a unique column is indeed unique.

state_trigger

  my @state_triggers = $kbs->state_trigger($class);

Returns SQLite triggers to validate that the value of a state column in the table representing the contents of the class represented by the Object::Relation::Meta::Class::Schema object passed as the sole argument. If the class does not have a state attribute (because it inherits the state from a concrete parent class), state_trigger() will return an empty list.

Called by constraints_for_class().

numeric_triggers

  my @numeric_triggers = $kbs->numeric_triggers($class);

Returns the SQLite triggers to validate the values of any numeric type attributes (whole numbers or positive integers) in the class. If the class has no numeric attributes numeric_triggers() will return an empty list.

Called by constraints_for_class().

boolean_triggers

  my @boolean_triggers = $kbs->boolean_triggers($class);

Returns the SQLite triggers to validate the values of any boolean type attributes in the class are stored as either 0 or 1 (zero or one) in their columns in the database. If the class has no boolean attributes boolean_triggers() will return an empty list.

Called by constraints_for_class().

once_triggers_sql

  my $once_triggers_sql_body = $kbs->once_triggers_sql(
    $key, $attr, $table, $constraint,
  );

This method is called by once_triggers() to generate database specific rules, functions, triggers, etc., to ensure that a column, once set to a non-null value, can never be changed.

unique_triggers

  my @unique_triggers = $kbs->unique_triggers($class);

Returns the SQLite triggers to validate the values of any "unique" attributes in the table representing the contents of the class represented by the Object::Relation::Meta::Class::Schema object passed as the sole argument. If the class has no unique attributes unique_triggers() will return an empty list.

Called by constraints_for_class().

operator_triggers

  my @operator_triggers = $kbs->operator_triggers($class);

Returns SQLite triggers to validate that the value of a operator column in the table representing the contents of the class represented by the Object::Relation::Meta::Class::Schema object passed as the sole argument. If the class does not have a operator attribute (because it inherits the operator from a concrete parent class), operator_trigger() will return an empty list.

Called by constraints_for_class().

media_type_triggers

  my @media_type_triggers = $kbs->media_type_triggers($class);

Returns SQLite triggers to validate that the value of a media_type column in the table representing the contents of the class represented by the Object::Relation::Meta::Class::Schema object passed as the sole argument. If the class does not have a media_type attribute (because it inherits the media_type from a concrete parent class), media_type_trigger() will return an empty list.

Called by constraints_for_class().

attribute_triggers

  my @attribute_triggers = $kbs->attribute_triggers($class);

Returns SQLite triggers to validate that the value of a attribute column in the table representing the contents of the class represented by the Object::Relation::Meta::Class::Schema object passed as the sole argument. If the class does not have a attribute attribute (because it inherits the attribute from a concrete parent class), attribute_trigger() will return an empty list.

Called by constraints_for_class().

version_triggers

  my @version_triggers = $kbs->version_triggers($class);

Returns SQLite triggers to validate that the value of a version column in the table representing the contents of the class represented by the Object::Relation::Meta::Class::Schema object passed as the sole argument. If the class does not have a version attribute (because it inherits the version from a concrete parent class), version_trigger() will return an empty list.

Called by constraints_for_class().

gtin_triggers

  my @gtin_triggers = $kbs->gtin_triggers($class);

Returns SQLite triggers to validate that the value of a gtin column in the table representing the contents of the class represented by the Object::Relation::Meta::Class::Schema object passed as the sole argument. If the class does not have a gtin attribute (because it inherits the gtin from a concrete parent class), gtin_trigger() will return an empty list.

Called by constraints_for_class().

insert_for_class

  my $insert_trigger_sql = $kbs->insert_for_class($class);

Returns an SQLite trigger that manages INSERT statements executed against the view for the class. The trigger ensures that the INSERT statement updates the table for the class as well as any parent classes. Extended and mediated objects are also inserted or updated as appropriate, but other contained objects are ignored, and should be inserted or updated separately.

update_for_class

  my $update_trigger_sql = $kbs->update_for_class($class);

Returns an SQLite trigger that manages UPDATE statements executed against the view for the class. The trigger ensures that the UPDATE statement updates the table for the class as well as any parent classes. Extended and mediated objects are also updated, but other contained objects are ignored, and should be updated separately.

delete_for_class

  my $delete_rule_sql = $kbs->delete_for_class($class);

Returns an SQLite rule that manages DELETE statements executed against the view for the class. Deletes simply pass through to the underlying table for the class; parent object records are left in tact.

extras_for_class

  my @sql = $kbs->extras_for_class($class)

Returns a list of any extra SQL statements that need to be executed in order to adequately represent a class in the database. By default, there are none, so this method returns none. But subclasses may override it to provide extra functionality.

Copyright and License

Copyright (c) 2004-2006 Kineticode, Inc. <info@obj_relode.com>

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.