|
NAMEDBIx::Admin::CreateTable - Create and drop tables, primary indexes, and sequencesSynopsis#!/usr/bin/env perl use strict; use warnings; use DBI; use DBIx::Admin::CreateTable; # ---------------- my($dbh) = DBI -> connect(...); my($creator) = DBIx::Admin::CreateTable -> new(dbh => $dbh, verbose => 1); my($table_name) = 'test'; $creator -> drop_table($table_name); my($primary_key) = $creator -> generate_primary_key_sql($table_name); $creator -> create_table(<<SQL); create table $table_name ( id $primary_key, data varchar(255) ) SQL See also xt/author/fk.t in DBIx::Admin::TableInfo. Description"DBIx::Admin::CreateTable" is a pure Perl module.Database vendors supported: MySQL, Oracle, Postgres, SQLite. Assumptions:
Constructor and initializationnew(...) returns an object of type "DBIx::Admin::CreateTable".This is the class contructor. Usage: DBIx::Admin::CreateTable -> new(). This method takes a set of parameters. Only the dbh parameter is mandatory. For each parameter you wish to use, call new as new(param_1 => value_1, ...).
Method: create_table($sql, $arg)Returns '' (empty string) if successful and DBI errstr() if there is an error.$sql is the SQL to create the table. $arg is an optional hash ref of options per table. The keys are table names. The only sub-key at the moment is...
Usage with CGI::Session: my($creator) = DBIx::Admin::CreateTable -> new(dbh => $dbh, verbose => 1); my($table_name) = 'sessions'; my($type) = $creator -> db_vendor() eq 'ORACLE' ? 'long' : 'text'; $creator -> drop_table($table_name); $creator -> create_table(<<SQL, {$table_name => {no_sequence => 1} }); create table $table_name ( id char(32) primary key, a_session $type not null ) SQL Typical usage: my($creator) = DBIx::Admin::CreateTable -> new(dbh => $dbh, verbose => 1); my($table_name) = 'test'; my($primary_key) = $creator -> generate_primary_key_sql($table_name); $creator -> drop_table($table_name); $creator -> create_table(<<SQL); create table $table_name ( id $primary_key, data varchar(255) ) SQL The SQL generated by this call to create_table() is spelled-out in the (SQL) table below. Action: Method: create_table($table_name, $arg). Comment: Creation of tables and sequences. Sequence: See generate_primary_sequence_name($table_name). +----------|---------------------------------------------------+ | | Action for $$arg{$table_name} | | Vendor | {no_sequence => 0} | {no_sequence => 1} | +----------|------------------------------|--------------------+ | MySQL | Create table | Create table | +----------|------------------------------|--------------------+ | Oracle | Create sequence before table | Create table | +----------|------------------------------|--------------------+ | Postgres | Create sequence before table | Create table | +----------|------------------------------|--------------------+ | SQLite | Create table | Create table | +----------|------------------------------|--------------------+ SQL: Method: create_table($table_name, $arg). Comment: SQL generated. Sequence: See generate_primary_sequence_name($table_name). +----------|-------------------------------------------------------------------------------------+ | | SQL for $$arg{$table_name} | | Vendor | {no_sequence => 0} | {no_sequence => 1} | +----------|------------------------------------------|------------------------------------------+ | MySQL | create table $table_name | create table $table_name | | | (id integer primary key | (id integer auto_increment | | | auto_increment, | primary key, | | | data varchar(255) ) | data varchar(255) ) | +----------|------------------------------------------|------------------------------------------+ | Oracle | create sequence ${table_name}_id_seq & | | | | create table $table_name | create table $table_name | | | (id integer primary key, | (id integer primary key, | | | data varchar(255) ) | data varchar(255) ) | +----------|------------------------------------------|------------------------------------------+ | Postgres | create sequence ${table_name}_id_seq & | | | | create table $table_name | create table $table_name | | | (id integer primary key | (id integer primary key | | | default nextval("${table_name}_id_seq"), | default nextval("${table_name}_id_seq"), | | | data varchar(255) ) | data varchar(255) ) | +----------|------------------------------------------|------------------------------------------+ | SQLite | create table $table_name | create table $table_name | | | (id integer primary key | (id integer autoincrement | | | autoincrement, | primary key, | | | data varchar(255) ) | data varchar(255) ) | +----------|------------------------------------------|------------------------------------------+ Method: db_vendor()Returns an upper-case string identifying the database vendor.Return string: Method: db_vendor(db_vendor). Comment: Value returned. +----------|------------+ | Vendor | String | +----------|------------+ | MySQL | MYSQL | +----------|------------+ | Oracle | ORACLE | +----------|------------+ | Postgres | POSTGRESQL | +----------|------------+ | SQLite | SQLITE | +----------|------------+ Method: drop_table($table_name, $arg)Returns '' (empty string).$table_name is the name of the table to drop. $arg is an optional hash ref of options, the same as for "create_table()". Action: Method: drop_table($table_name, $arg). Comment: Deletion of tables and sequences. Sequence: See generate_primary_sequence_name($table_name). +----------|-------------------------------------------------+ | | Action for $$arg{$table_name} | | Vendor | {no_sequence => 0} | {no_sequence => 1} | +----------|----------------------------|--------------------+ | MySQL | Drop table | Drop table | +----------|----------------------------|--------------------+ | Oracle | Drop sequence before table | Drop table | +----------|----------------------------|--------------------+ | Postgres | Drop sequence after table | Drop table | +----------|----------------------------|--------------------+ | SQLite | Drop table | Drop table | +----------|----------------------------|--------------------+ SQL: Method: drop_table($table_name, $arg). Comment: SQL generated. Sequence: See generate_primary_sequence_name($table_name). +----------|---------------------------------------------------------------+ | | SQL for $$arg{$table_name} | | Vendor | {no_sequence => 0} | {no_sequence => 1} | +----------|--------------------------------------|------------------------+ | MySQL | drop table $table_name | drop table $table_name | +----------|--------------------------------------|------------------------+ | Oracle | drop sequence ${table_name}_id_seq & | | | | drop table $table_name | drop table $table_name | +----------|--------------------------------------|------------------------+ | Postgres | drop table $table_name & | drop table $table_name | | | drop sequence ${table_name}_id_seq | | +----------|--------------------------------------|------------------------+ | SQLite | drop table $table_name | drop table $table_name | +----------|--------------------------------------|------------------------+ Note: drop_table() turns off RaiseError so we do not error if the sequence and table being deleted do not exist. This is new in V 2.00. Method: generate_primary_index_name($table_name)Returns the name of the index corresponding to the primary key for the given table.The module does not call this method. SQL: Method: generate_primary_index_name($table_name). Comment: Generation of name of the index for the primary key. +----------|--------------------+ | Vendor | SQL | +----------|--------------------+ | MySQL | | +----------|--------------------+ | Oracle | | +----------|--------------------+ | Postgres | ${table_name}_pkey | +----------|--------------------+ | SQLite | | +----------|--------------------+ Method: generate_primary_key_sql($table_name)Returns partial SQL for declaring the primary key for the given table.See the Synopsis for how to use this method. SQL: Method: generate_primary_key_sql($table_name). Comment: Generation of partial SQL for primary key. Sequence: See generate_primary_sequence_name($table_name). +----------|-----------------------------------------------------+ | Vendor | SQL | +----------|-----------------------------------------------------+ | MySQL | integer primary key auto_increment | +----------|-----------------------------------------------------+ | Oracle | integer primary key | +----------|-----------------------------------------------------+ | Postgres | integer primary key default nextval($sequence_name) | +----------|-----------------------------------------------------+ | SQLite | integer primary key autoincrement | +----------|-----------------------------------------------------+ Method: generate_primary_sequence_name($table_name)Returns the name of the sequence used to populate the primary key of the given table.SQL: Method: generate_primary_sequence_name($table_name). Comment: Generation of name for sequence. +----------|----------------------+ | Vendor | SQL | +----------|----------------------+ | MySQL | | +----------|----------------------+ | Oracle | ${table_name}_id_seq | +----------|----------------------+ | Postgres | ${table_name}_id_seq | +----------|----------------------+ | SQLite | | +----------|----------------------+ Method: reset_all_sequences($arg)Returns nothing.Resets the primary key sequence for all tables, except those marked by $arg as not having a sequence. Note: This method only works if called against an object which knows the names of all tables and sequences. This means you must have called at least one of these, for each table:
$arg is an optional hash ref of options, the same as for "create_table()". Summary: Method: reset_all_sequences($arg). Comment: Reset all sequences. +----------|-------------------------------------------------------+ | Vendor | Action | +----------|-------------------------------------------------------+ | MySQL | Do nothing | +----------|-------------------------------------------------------+ | Oracle | Call reset_sequence($table_name, $arg) for all tables | +----------|-------------------------------------------------------+ | Postgres | Call reset_sequence($table_name, $arg) for all tables | +----------|-------------------------------------------------------+ | SQLite | Do nothing | +----------|-------------------------------------------------------+ Method: reset_sequence($table_name, $arg)Returns nothing.Resets the primary key sequence for the given table, except if it is marked by $arg as not having a sequence. $arg is an optional hash ref of options, the same as for "create_table()". Summary: Method: reset_sequence($table_name, $arg). Comment: Reset one sequence. Sequence: The value of the sequence is set to the number of records in the table. +----------|-----------------------------------------+ | | Action for $$arg{$table_name} | | Vendor | {no_sequence => 0} | {no_sequence => 1} | +----------|--------------------|--------------------+ | MySQL | Do nothing | Do nothing | +----------|--------------------|--------------------+ | Oracle | Set sequence value | Do nothing | +----------|--------------------|--------------------+ | Postgres | Set sequence value | Do nothing | +----------|--------------------|--------------------+ | SQLite | Do nothing | Do nothing | +----------|--------------------|--------------------+ FAQWhich versions of the servers did you test?Versions as at 2014-03-07 +----------|------------+ | Vendor | V | +----------|------------+ | MariaDB | 5.5.36 | +----------|------------+ | Oracle | 10.2.0.1.0 | (Not tested for years) +----------|------------+ | Postgres | 9.1.12 | +----------|------------+ | SQLite | 3.7.17 | +----------|------------+ Do all database servers accept the same 'create table' commands?No. You have been warned.References for 'Create table': MySQL <https://dev.mysql.com/doc/refman/5.7/en/create-table.html>. Postgres <http://www.postgresql.org/docs/9.3/interactive/sql-createtable.html>. SQLite <https://sqlite.org/lang_createtable.html>. Consider these: create table one ( id integer primary key autoincrement, data varchar(255) ) $engine create table two ( id integer primary key autoincrement, one_id integer not null, data varchar(255), foreign key(one_id) references one(id) ) $engine Putting the 'foreign key' clause at the end makes it a table constraint. Some database servers, e.g. MySQL and Postgres, allow you to attach it to a particular column, as explained next.
Do not forget this when using SQLite: $dbh -> do('pragma foreign_keys = on') if ($dsn =~ /SQLite/i); Do I include the name of an auto-populated column in an insert statement?Depends on the server. Some databases, e.g. Postgres, do not want the name of the primary key in the insert statement if the server is to generate a value for a column.SQL for insert: Comment: SQL for insertion of rows containing auto-populated values. Sequence: See generate_primary_sequence_name($table_name). +----------|-----------------------------------------------------------------------+ | Vendor | SQL | +----------|-----------------------------------------------------------------------+ | MySQL | insert into $table_name (data) values (?) | +----------|-----------------------------------------------------------------------+ | Oracle | insert into $table_name (id, data) values ($sequence_name.nextval, ?) | +----------|-----------------------------------------------------------------------+ | Postgres | insert into $table_name (data) values (?) | +----------|-----------------------------------------------------------------------+ | SQLite | insert into $table_name (id, data) values (undef, ?) | +----------|-----------------------------------------------------------------------+ Do I have to use a sequence to populate a primary key?Well, no, actually. See next question.How to I override the auto-populated value for a primary key column?By including the name and the value in the insert statement.SQL for insert: Comment: SQL for insertion of rows overriding auto-populated values. +----------|--------------------------------------------------+ | Vendor | SQL | +----------|--------------------------------------------------+ | MySQL | insert into $table_name (id, data) values (?, ?) | +----------|--------------------------------------------------+ | Oracle | insert into $table_name (id, data) values (?, ?) | +----------|--------------------------------------------------+ | Postgres | insert into $table_name (id, data) values (?, ?) | +----------|--------------------------------------------------+ | SQLite | insert into $table_name (id, data) values (?, ?) | +----------|--------------------------------------------------+ Are primary keys always not null and unique?Yes. All servers document primary key as meaning both non null and unique.See AlsoDBIx::Admin::DSNManager.DBIx::Admin::TableInfo. Version NumbersVersion numbers < 1.00 represent development versions. From 1.00 up, they are production versions.Repository<https://github.com/ronsavage/DBIx-Admin-CreateTable>SupportBugs should be reported via the CPAN bug tracker at<https://github.com/ronsavage/DBIx-Admin-CreateTable/issues> Author"DBIx::Admin::CreateTable" was written by Ron Savage <ron@savage.net.au> in 2006.<http://savage.net.au/> CopyrightAustralian copyright (c) 2006, Ron Savage. All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of the Artistic or the GPL licences, copies of which is available at: http://www.opensource.org/licenses/index.html
Visit the GSP FreeBSD Man Page Interface. |