NAME

Podius::Persistence::Media - pure virtual superclass of persistence medias


SYNOPSIS

Must not be used in user applications, use subclassed Media package instead. Supposing Podius::Persistence::Media::Implementation is such subclass, the synopsis is:

  # This example is not up to date and will not work
  use Podius::Persistence::Media::Implementation;
  $pm = new Podius::Persistence::Media::Implementation(@parameters_to_init_pm);
  $pm->insert(
    'students',
    [1, 2],
    [{ name => 'Tom', age => '22' }, { name => 'Bob', age => '34' }]
  );
  my $name_of_first_student = $pm->retrieve('students', [1])->[0]->{name};
  $pm->delete('students', [1]);
  print "All existing student ids: (",
    join(", ", @{$pm->get_all_keys('students')}, ")\n";
  print "All existing tables: (",
    join(", ", @{$pm->get_all_tables()}, ")\n";


DESCRIPTION

This Persistence Media abstraction used by Beans Persistence Layer.

Table inheritance and single level transactions are supported.


REQUIREMENTS

the Exception manpage.


CONSTRUCTORS

new

description
Class constructor.

returns
New formed Media reference.


METHODS

begin

description
Starts transaction, which can be them submitted by submint method or aborted by rollback method.

parameters
None.

returns
None. Throws exception on failure.

commit

description
Commits transaction, started by begin method. All changes done during transaction to Persistence Media are commited.

parameters
None.

returns
None. Throws exception on failure.

rollback

description
Aborts transaction, started by begin method. All changes done during transaction to Persistence Media are restored.

parameters
None.

returns
None. Throws exception on failure.

insert

description
Inserts (new) given 'rows', referenced by 'keys' into 'table'.

parameters
  * table - name of table to insert data into.
If table is ARRAY ref - the last name in the array is taken as table name
and all rest are taken as supertable names and links in supertables to table
instance are created.
  * keys - array ref of row-keys to insert data into
  * revisions - array of row revisions
  * complex_rows - array of elements, each representing data of one row to save.
Some of the row data will be saved in specified additional tables, instead of
the main table. One complex_row is an array of the form:
  [
    [ [ field, ftype, value ]* ],
    [ add_table, [ [ kfield, kvalue ]* ],
      [ [ rfield, rvalue ]* ], [ [ lfield, [ lvalue* ] ]* ] ]*
  ]
returns
1 on success, otherwise exception is thrown.

update

description
Updates given 'rows', referenced by 'keys' inside 'table'.

parameters
  * table - name of table to update data in
  * keys - array ref of row-keys to insert data into
  * revisions - array of row revisions
  * complex_rows - array of elements, each representing data of one row to save.
Some of the row data will be saved in specified additional tables, instead of
the main table. One complex_row is an array of the form:
  [
    [ [ unmodified_field* ], [ unmodified_add_table_property* ] ],
    [ [ field, ftype, value ]* ],
    [ add_table, [ [ kfield, kvalue ]* ],
      [ [ rfield, rvalue ]* ], [ [ lfield, [ lvalue* ] ]* ] ]*
  ]
returns
1 on success, otherwise exception is thrown.

get_real_tables

description
Returns real tables for given keys and super table.

parameters
  * table - name of super table to look at
  * keys  - array ref of row-keys to look at
returns
Array of scalars (names of tables). On error, Exception::Podius::NoTableKeyError is thrown.

get_revisions

description
Gets the revision values for given table keys.

parameters
  * table - name of table to query
  * keys - array ref of row-keys to query
  * out_of_tx - optional boolean flag, work outside of the transaction
returns
Array ref of revisions on success, otherwise Exception::Podius::NoTableKeyError is thrown.

retrieve

description
Retrieves and returns the 'rows' inside table 'table', referenced by 'keys'.

parameters
  * table - name of real table to retrieve data from
  * table_data - array of the form:
  [
    [ [ field, ftype ]* ],
    [ add_table, [ ifield, [ cfield, cvalue ]* ],
      [ rfield* ], [ lfield* ] ]*
  ]
  * keys - array ref of row-keys to retrieve data from
  * out_of_tx - optional boolean flag, work outside of the transaction
returns
Array of complex_row. On failure exception is thrown. One complex_row (for one key) is an array of the form (according to table_data):
  [
    [ value* ],
    [ [ rvalue* ], [ [ lvalue* ]* ] ]*
  ]

If any table key does not exist, Exception::Podius::NoTableKeyError is thrown.

delete

description
Deletes rows, referenced by 'keys' from table 'table'.

parameters
  * table - name of table to delete from.
If table is ARRAY ref - the last name in the array is taken as table name
and all rest are taken as supertable names and links in supertables to table
instance are deleted.
  * table_data - array of the form:
  [
    [ add_table, [ ifield, [ cfield, cvalue ]* ] ]*
  ]
  * keys - array ref of row-keys to delete
returns
1 on success, otherwise exception is thrown.

trash

description
Restores rows (undo), referenced by 'keys' in table 'table'. This forgets all update changes done to rows pointed by keys.

Calling this method outside a transaction should have no effect.

parameters
The same as for delete method.

returns
1 on success, otherwise exception is thrown.

grep_changed_keys

description
This method could be used before update to determine conflicts. Returns a subset of keys for which the revision (timestamp) was changed.

parameters
  * table - name of table to delete from.
  * keys - array ref of row-keys to query.
  * revisions - array ref of original revisions to check against.
returns
Array ref of keys (the subset of given keys).

reset_high_key

description
Resets the high key for the given base table.

The exception must be thrown if there is still any data in the given table and all subtables.

parameters
  * table - name of table to reset high key for
  * optional value of the high key to set (default: -1)
returns
1 on success, otherwise exception is thrown.

get_unique_key

description
Evaluates unique key for given table. This is done by implementing high/low key approach. High key part is taken from high_key table (it is stored for every given table), low part grows from 1 to low_key_limit (1000).

When first time unique key is requested for given table, high key part is read, incremented and stored back. This high key part is used for assigning first low_key_limit new keys of given table, after that new high key is evaluated.

Resulting key is evaluated by this formula: high_key * low_key_limit + low_key.

parameters
table - name of table to get unique key for

returns
New unique (in the table) key. On failure exception is thrown.

force_unique_key

description
May be used to force unique key. This method should be called instead of get_unique_key in special situations when the unique key is known rather than to be auto-calculated. The effect is the high key may be increased to be good for the given key, so there are less conflicts in the future key assigning.

parameters
  * table name (or arrayref of table names)
  * unique key to force
returns
none

init_unique_keys

description
Forgets all keys, returned by get_unique_key for all tables. Next time get_unique_key must search for first unused key from start.

parameters
None.

If/when concurent access (directly or indirectly) to PM will be implemented, session id will be a parameter.

returns
none

set_inheritance_table_names

description
Sets a name of inheritance table and names of its fields.

parameters
Array of form:
  [
    table-name, [
      supertype-field-name,
      id-field-name,
      realtype-field-name,
    ]
  ]

returns
none

set_high_key_table_names

description
Sets a name of high-key table and names of its fields.

parameters
Array of form:
  [
    table-name, [
      supertype-field-name,
      lastvalue-field-name,
    ]
  ]

returns
none

set_key_field_name

description
Sets a name of key field in all data tables.

parameters
Scalar, like 'uid'.

returns
none

set_revision_field_name

description
Sets a name of revision field in all data tables.

parameters
Scalar, like 'uid'.

returns
none

get_all_keys

description
Returns all existing keys for given 'table'.

parameters
table - name of table to get unique key for

returns
Reference to array of scalars (all given 'table' keys).

get_all_tables

description
Returns all existing table names in database.

parameters
none

returns
Reference to array of scalars (all table names).

execute_query

description
Low level method, executes given query and returns result in perl data. Note, the method don't check anything, requested data is returned in whole, if it is too big, this can eat all your memory.

parameters
Scalar, representing query.

returns
Array of arrays (table values).