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

NAME

DBIx::Table::TestDataGenerator::TableProbe - defines roles of DBMS (meta)data handlers

DESCRIPTION

This class is used internally. For each DBMS to be supported by DBIx::Table::TestDataGenerator, a class must be implemented which impersonates all the roles defined in the current class TableProbe. Note that in the following, we often abbreviate "foreign key" as "fkey".

SUBROUTINES/METHODS IMPLEMENTED BY TABLEPROBE ITSELF

dbh

Read-only accessor for a DBI database handle.

database

Read-only accessor for a database name.

schema

Read-only accessor for a database schema name.

table

Read-only accessor for the name of the table in which the test data will be created.

insert_statement

Argument: A reference to an array containing the column names of the target table.

Returns a parameterized insert statement for the target table involving the passed in column names.

num_records

Returns the number of records in the target table.

Arguments:

  • $colname_array_ref Reference to an array of column names of the target table.

  • $col_width_array_ref Reference to an array of integers

Minimalistic printing of a table's contents to STDOUT for debugging purposes. Prints the values for the column in $colname_array_ref in columns whose width is defined by $col_width_array_ref.

SUBROUTINES/METHODS TO BE IMPLEMENTED BY CLASSES IMPERSONATING THE TABLEPROBE ROLE

column_names

Returns a reference to an array of the column names of the target table in no particular order.

num_roots

Arguments:

  • $pkey_col: Name of primary key column

  • $parent_pkey_col: Name of a column in the target table referencing the column $pkey_col by a foreign key constraint

Returns the number of roots in the target table in case a foreign key reference exists linking the referencing column $parent_pkey_col to the primary key column $pkey_col. A record is considered a node if either $pkey_col = $parent_pkey_col or $parent_pkey_col = NULL.

seed

Argument: random number seed $seed.

Seeds the random number with the integer $seed to allow for reproducible runs. In cases such as SQLite, the random number generation cannot be seeded, so the method will do nothing in these cases.

random_record

  • $table: Table name

  • $colname_list: Reference to an array containing a subset of the column names of table $table.

Determines a random record from table $table and returns a reference to a hash where the keys are the column names in list $colname_list and the values the values of these columns in the selected record.

get_incrementor ($type, $max)

Arguments:

  • $type: data type of the current DBMS applicable to table columns

  • $max: value based on which the incrementor will determine the next "incremented" value

Returns an anonymous function for incrementing the values of a column of data type $type starting at a value to be determined by the current "maximum" $max. In case of a numeric data type, $max will be just the current maximum, but in case of strings, we have decided to pass the maximum length since there is no natural ordering available. E.g. Perl using per default another order than the lexicographic order employed by Oracle. In our default implementations, for string data types we add values for the current column at 'A...A0', where A is repeated $max times and increase the appended integer in each step. This should be made more flexible in future versions.

get_type_preference_for_incrementing

Arguments: none

We must decide which of the column values of a record to be added will be changed in case of a uniqueness constraint. This method returns a reference to an array listing the supported data types. The order of the data types defines which column in such a unique constraint will get preference over others based on its data type.

unique_columns_with_max

Argument $get_pkey_columns: used for its falsey or truthy value

In case $get_pkey_columns is false, this method returns a hash reference of the following structure:

  {
      UNIQUE_CONSTR_1 =>
      {
        DATA_TYPE_1 => [ [ COL_NAME_1, MAX_VAL_1 ], ..., [COL_NAME_N, MAX_VAL_N] ],
        DATA_TYPE_2 => [ [ COL_NAME_N+1, MAX_VAL_N+1 ], ..., [COL_NAME_M, MAX_VAL_M] ],
        ...
      }
      UNIQUE_CONSTR_2 => {...}
    ...
  }

Here, the keys of the base hash are the names of all uniqueness constraints. For each such constraint, the value of the base hash is another hash having as values all the data types used for columns in the constraint and as values an array reference where each element is a pair (column_name, max_value) where column_name runs over all column names in the constraint and max_value is the corresponding current maximum value. (Please note the comment in the description of get_incrementor on how we currently determine this maximum in case of string data types.)

In case $get_pkey_columns is true, the corresponding information is returned for the primary key constraint, in particular the base hash has only one key as there may be only one primary key constraint:

  {
      PRIMARY_KEY_NAME =>
      {
        DATA_TYPE_1 => [ [ COL_NAME_1, MAX_VAL_1 ], ..., [COL_NAME_N, MAX_VAL_N] ],
        DATA_TYPE_2 => [ [ COL_NAME_N+1, MAX_VAL_N+1 ], ..., [COL_NAME_M, MAX_VAL_M] ],
        ...
      }
  }

fkey_name_to_fkey_table

Arguments: none

Returns a hash where the keys are the names of the foreign keys on the target table and the values the names of the corresponding referenced tables.

fkey_referenced_cols_to_referencing_cols

Arguments: none

Returns a reference to a dictionary having as keys the fkey names and for each key as value a dictionary where the keys are the names of the referenced column names and the values the names of the corresponding referencing column names.

fkey_referenced_cols

Arguments: none

Returns a reference to a hash having the fkey names as keys and a comma-separated list of the column names of the referenced columns of the fkey as values.

get_self_reference

Arguments: none

If there is an fkey defining a self-reference, its name and the name of the referencing column are returned in a two-element array reference, otherwise undef is returned.

selfref_tree

Arguments:

  • $key_col: primary key column name of a one-column primary key

  • $parent_refkey_col: name of another column

Suppose we have a self-reference in the target table, i.e. a one-column foreign key pointing to the primary key of the target table. In this case, selfref_tree returns a tree defined by the parent-child relation the self-reference defines. $key_col is the name of the primary key column and $parent_refkey_col the name of the column containing the reference to the parent record.

AUTHOR

Jos\x{00E9} Diaz Seng, <josediazseng at gmx.de>

LICENSE AND COPYRIGHT

Copyright 2012 Jos\x{00E9} Diaz Seng.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.