DBSchema::Table(3pm)

πŸ’… DBSchema::Table πŸ’…

okay bestie, sit DOWN, because today we're talking about DBIx::DBSchema::Table, straight outta the User Contributed Perl Documentation (yes, the 3pm section, we're fancy here). it's a perl module, it's got tables, it's got ✨main character energy✨, and by the end of this page you're gonna understand every single bit of it. no cap.


✨ NAME ✨

DBIx::DBSchema::Table β€” Table objects. that's it. that's the whole name. iconic brevity. πŸŽ€


🌈 SYNOPSIS 🌈

here's the vibe, in code, exactly as written, don't come for me:

use DBIx::DBSchema::Table;

#new style (preferred), pass a hashref of parameters
$table = new DBIx::DBSchema::Table (
  {
    name         => "table_name",
    primary_key  => "primary_key",
    columns      => \@dbix_dbschema_column_objects,
    #deprecated# unique      => $dbix_dbschema_colgroup_unique_object,
    #deprecated# 'index'     => $dbix_dbschema_colgroup_index_object,
    indices      => \@dbix_dbschema_index_objects,
    foreign_keys => \@dbix_dbschema_foreign_key_objects,
  }
);

#old style (VERY deprecated)
$table = new DBIx::DBSchema::Table (
  "table_name",
  "primary_key",
  $dbix_dbschema_colgroup_unique_object,
  $dbix_dbschema_colgroup_index_object,
  @dbix_dbschema_column_objects,
);

$table->addcolumn ( $dbix_dbschema_column_object );

$table_name = $table->name;
$table->name("table_name");

$primary_key = $table->primary_key;
$table->primary_key("primary_key");

#deprecated# $dbix_dbschema_colgroup_unique_object = $table->unique;
#deprecated# $table->unique( $dbix_dbschema__colgroup_unique_object );

#deprecated# $dbix_dbschema_colgroup_index_object = $table->index;
#deprecated# $table->index( $dbix_dbschema_colgroup_index_object );

%indices = $table->indices;
$dbix_dbschema_index_object = $indices{'index_name'};
@all_index_names = keys %indices;
@all_dbix_dbschema_index_objects = values %indices;

@column_names = $table->columns;

$dbix_dbschema_column_object = $table->column("column");

#preferred
@sql_statements = $table->sql_create_table( $dbh );
@sql_statements = $table->sql_create_table( $datasrc, $username, $password );

#possible problems
@sql_statements = $table->sql_create_table( $datasrc );
@sql_statements = $table->sql_create_table;

so yes: new style = pass a hashref, that's the preferred one, do that one. old style? "VERY deprecated". like, the original authors literally wrote VERY in caps. respect the caps. πŸ’€


πŸ¦‹ DESCRIPTION πŸ¦‹

DBIx::DBSchema::Table objects represent a single database table. one table, one object. it's giving monogamy. 🫢


πŸ‘‘ METHODS πŸ‘‘

new HASHREF

Creates a new DBIx::DBSchema::Table object. the preferred usage is to pass a hash reference of named parameters, like this:

{
  name          => TABLE_NAME,
  primary_key   => PRIMARY_KEY,
  columns       => COLUMNS,
  indices       => INDICES,
  local_options => OPTIONS,
}

and here's what all those lil keywords mean:

  • TABLE_NAME β€” the name of the table. groundbreaking, i know. πŸ’–
  • PRIMARY_KEY β€” the primary key. it may be empty, so don't panic if it is.
  • COLUMNS β€” a reference to an array of DBIx::DBSchema::Column objects (go peek at DBIx::DBSchema::Column).
  • INDICES β€” a reference to an array of DBIx::DBSchema::Index objects (see DBIx::DBSchema::Index), or a hash reference of index names (keys) and DBIx::DBSchema::Index objects (values).
  • FOREIGN_KEYS β€” a reference to an array of DBIx::DBSchema::ForeignKey objects (see DBIx::DBSchema::ForeignKey).
  • OPTIONS β€” a scalar of database-specific table options, like "WITHOUT OIDS" for Pg or "TYPE=InnoDB" for mysql. yes bestie, dialect-specific little accessories. we love a coordinating piece. πŸ’…

new_odbc DATABASE_HANDLE TABLE_NAME

Creates a new DBIx::DBSchema::Table object from the supplied DBI database handle for the specified table. this uses the experimental DBI type_info method to create a table with standard (ODBC) SQL column types that most closely correspond to any non-portable column types. use this if you wanna import a schema you plan to use with a bunch of different database engines! 🌈

although primary key and (unique) index information will only be imported from databases that have DBIx::DBSchema::DBD drivers (currently MySQL and PostgreSQL), import of column names and attributes should work for any database. note the should is italicized in spirit, okay, tread lightly. πŸ™ˆ

Note: the _odbc refers to the column types used and nothing else β€” you do not have to have ODBC installed or connect to the database via ODBC. ok that's such a relief, breathe.

new_native DATABASE_HANDLE TABLE_NAME

Creates a new DBIx::DBSchema::Table object from the supplied DBI database handle for the specified table. this one uses database-native methods to read the schema, and it will preserve any non-portable column types. but listen: this method is only available if there's a DBIx::DBSchema::DBD for the corresponding database engine (currently, MySQL and PostgreSQL). no driver, no dice. 🎲

addcolumn COLUMN

Adds this DBIx::DBSchema::Column object. that's the whole thing. short queen. ✨

delcolumn COLUMN_NAME

Deletes this column. returns false if no column of this name was found to remove, true otherwise. so like, it tells you if it actually found something to yeet. πŸ—‘οΈ

name [ TABLE_NAME ]

Returns or sets the table name. getter-setter combo, we stan a dual threat. πŸ‘‘

local_options [ OPTIONS ]

Returns or sets the database-specific table options string.

primary_key [ PRIMARY_KEY ]

Returns or sets the primary key.

columns

Returns a list consisting of the names of all columns. just the names, bestie, not the whole objects. keep your expectations realistic. πŸ“

column COLUMN_NAME

Returns the column object (see DBIx::DBSchema::Column) for the specified COLUMN_NAME.

indices

Returns a list of key-value pairs suitable for assigning to a hash. keys are index names, and values are index objects (see DBIx::DBSchema::Index). so you can literally just do %indices = $table->indices; and go off. πŸ”₯

unique_singles

Meet exciting and unique singles using this method! 😭 i'm not even kidding, that's what the original says, and honestly? iconic. this method returns a list of column names that are indexed with their own, unique, non-compound (that's the "single" part) indices. they're single and ready to mingle. πŸ’–

sql_create_table [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ]

Returns a list of SQL statements to create this table.

the data source can be specified by passing an open DBI database handle, or by passing the DBI data source name, username and password.

although the username and password are optional, it is best to call this method with a database handle or data source including a valid username and password β€” a DBI connection will be opened and the quoting and type mapping will be more reliable. so like, technically optional, but do it anyway. trust. 🫢

if passed a DBI data source (or handle) such as DBI:mysql:database, it'll use MySQL- or PostgreSQL-specific syntax. non-standard syntax for other engines (if applicable) may also be supported in the future. *fingers crossed* 🀞

sql_add_constraints [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ]

Returns a list of SQL statements to add constraints (foreign keys) to this table.

the data source can be specified by passing an open DBI database handle, or by passing the DBI data source name, username and password.

although the username and password are optional, it is best to call this method with a database handle or data source including a valid username and password β€” a DBI connection will be opened and the quoting and type mapping will be more reliable.

if passed a DBI data source (or handle) such as DBI:mysql:database, it'll use MySQL- or PostgreSQL-specific syntax. non-standard syntax for other engines (if applicable) may also be supported in the future. same disclaimer, same energy. ⭐

sql_alter_table PROTOTYPE_TABLE, [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ]

Returns a list of SQL statements to alter this table so that it is identical to the provided table, which is also a DBIx::DBSchema::Table object. glow-up time for your schema. πŸ’…

the data source can be specified by passing an open DBI database handle, or by passing the DBI data source name, username and password.

although the username and password are optional, it is best to call this method with a database handle or data source including a valid username and password β€” a DBI connection will be opened and used to check the database version as well as for more reliable quoting and type mapping. note that the database connection will be used passively, not to actually run the CREATE statements. it's just there to look pretty and check things, it's not doing the deed. πŸ‘€

if passed a DBI data source (or handle) such as DBI:mysql:database or DBI:Pg:dbname=database, it'll use syntax specific to that database engine. currently supported databases are MySQL and PostgreSQL.

if not passed a data source (or handle), or if there is no driver for the specified database, it will attempt to use generic SQL syntax. an improviser. 🌸

sql_alter_constraints PROTOTYPE_TABLE, [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ]

Returns a list of SQL statements to alter this table's constraints (foreign keys) so that they are identical to the provided table, which is also a DBIx::DBSchema::Table object.

the data source can be specified by passing an open DBI database handle, or by passing the DBI data source name, username and password.

although the username and password are optional, it is best to call this method with a database handle or data source including a valid username and password β€” a DBI connection will be opened and used to check the database version as well as for more reliable quoting and type mapping. note that the database connection will be used passively, not to actually run the CREATE statements.

if passed a DBI data source (or handle) such as DBI:mysql:database or DBI:Pg:dbname=database, it'll use syntax specific to that database engine. currently supported databases are MySQL and PostgreSQL.

if not passed a data source (or handle), or if there is no driver for the specified database, it will attempt to use generic SQL syntax.

foreign_keys_sql

foreign_keys

Returns a list of foreign keys (DBIx::DBSchema::ForeignKey objects). yeah, these two get listed together in the original β€” foreign_keys_sql and foreign_keys β€” and the description is exactly what it is. πŸ¦‹


πŸŽ€ AUTHOR πŸŽ€

Ivan Kohler <ivan-dbix-dbschema@420.am>

thanks to Mark Ethan Trostler <mark@zzo.com> for a patch to allow tables with no indices. king behavior, honestly. πŸ‘‘


Copyright (c) 2000-2007 Ivan Kohler Copyright (c) 2000 Mail Abuse Prevention System LLC Copyright (c) 2007-2013 Freeside Internet Services, Inc. All rights reserved.

this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. slay, legally. ✨


πŸ’€ BUGS πŸ’€

okay bestie, here's the tea, and it's lukewarm but honest:

  • sql_create_table() has database-specific foo that probably ought to be abstracted into the DBIx::DBSchema::DBD:: modules (or no? it doesn't anymore?). 🫠
  • sql_alter_table() also has database-specific foo that ought to be abstracted into the DBIx::DBSchema::DBD:: modules.
  • sql_create_table() may change or destroy the object's data. if you need to use the object after sql_create_table, make a copy beforehand. not me warning you like your mom before a party β€” MAKE. A. COPY. 😭
fr fr, that one's not a vibe, that one's a genuine hazard. protect your data, bestie. πŸ›‘οΈ
  • some of the logic in new_odbc might be better abstracted into Column.pm etc.
  • add methods to get and set specific indices, by name? (like column COLUMN_NAME)
  • should the indices method be a setter, not just a getter?

🌟 SEE ALSO 🌟

DBIx::DBSchema, DBIx::DBSchema::Column, DBI, DBIx::DBSchema::Index, DBIx::DBSchema::FoeignKey


and that's the whole page, bestie!! 🫢 this one's riding on perl v5.36.0 and it was dated 2022-09-10. go forth, build tables, make copies before you sql_create_table, and remember β€” meet exciting and unique singles responsibly. πŸ’–βœ¨πŸŒΈ

πŸ“¦ where this page comes from