1DBSchema::Table(3)    User Contributed Perl Documentation   DBSchema::Table(3)
2
3
4

NAME

6       DBIx::DBSchema::Table - Table objects
7

SYNOPSIS

9         use DBIx::DBSchema::Table;
10
11         #new style (preferred), pass a hashref of parameters
12         $table = new DBIx::DBSchema::Table (
13           {
14             name         => "table_name",
15             primary_key  => "primary_key",
16             columns      => \@dbix_dbschema_column_objects,
17             #deprecated# unique      => $dbix_dbschema_colgroup_unique_object,
18             #deprecated# 'index'     => $dbix_dbschema_colgroup_index_object,
19             indices      => \@dbix_dbschema_index_objects,
20             foreign_keys => \@dbix_dbschema_foreign_key_objects,
21           }
22         );
23
24         #old style (VERY deprecated)
25         $table = new DBIx::DBSchema::Table (
26           "table_name",
27           "primary_key",
28           $dbix_dbschema_colgroup_unique_object,
29           $dbix_dbschema_colgroup_index_object,
30           @dbix_dbschema_column_objects,
31         );
32
33         $table->addcolumn ( $dbix_dbschema_column_object );
34
35         $table_name = $table->name;
36         $table->name("table_name");
37
38         $primary_key = $table->primary_key;
39         $table->primary_key("primary_key");
40
41         #deprecated# $dbix_dbschema_colgroup_unique_object = $table->unique;
42         #deprecated# $table->unique( $dbix_dbschema__colgroup_unique_object );
43
44         #deprecated# $dbix_dbschema_colgroup_index_object = $table->index;
45         #deprecated# $table->index( $dbix_dbschema_colgroup_index_object );
46
47         %indices = $table->indices;
48         $dbix_dbschema_index_object = $indices{'index_name'};
49         @all_index_names = keys %indices;
50         @all_dbix_dbschema_index_objects = values %indices;
51
52         @column_names = $table->columns;
53
54         $dbix_dbschema_column_object = $table->column("column");
55
56         #preferred
57         @sql_statements = $table->sql_create_table( $dbh );
58         @sql_statements = $table->sql_create_table( $datasrc, $username, $password );
59
60         #possible problems
61         @sql_statements = $table->sql_create_table( $datasrc );
62         @sql_statements = $table->sql_create_table;
63

DESCRIPTION

65       DBIx::DBSchema::Table objects represent a single database table.
66

METHODS

68       new HASHREF
69           Creates a new DBIx::DBSchema::Table object.  The preferred usage is
70           to pass a hash reference of named parameters.
71
72             {
73               name          => TABLE_NAME,
74               primary_key   => PRIMARY_KEY,
75               columns       => COLUMNS,
76               indices       => INDICES,
77               local_options => OPTIONS,
78             }
79
80           TABLE_NAME is the name of the table.
81
82           PRIMARY_KEY is the primary key (may be empty).
83
84           COLUMNS is a reference to an array of DBIx::DBSchema::Column
85           objects (see DBIx::DBSchema::Column).
86
87           INDICES is a reference to an array of DBIx::DBSchema::Index objects
88           (see DBIx::DBSchema::Index), or a hash reference of index names
89           (keys) and DBIx::DBSchema::Index objects (values).
90
91           FOREIGN_KEYS is a references to an array of
92           DBIx::DBSchema::ForeignKey objects (see
93           DBIx::DBSchema::ForeignKey).
94
95           OPTIONS is a scalar of database-specific table options, such as
96           "WITHOUT OIDS" for Pg or "TYPE=InnoDB" for mysql.
97
98       new_odbc DATABASE_HANDLE TABLE_NAME
99           Creates a new DBIx::DBSchema::Table object from the supplied DBI
100           database handle for the specified table.  This uses the
101           experimental DBI type_info method to create a table with standard
102           (ODBC) SQL column types that most closely correspond to any non-
103           portable column types.   Use this to import a schema that you wish
104           to use with many different database engines.  Although primary key
105           and (unique) index information will only be imported from databases
106           with DBIx::DBSchema::DBD drivers (currently MySQL and PostgreSQL),
107           import of column names and attributes *should* work for any
108           database.
109
110           Note: the _odbc refers to the column types used and nothing else -
111           you do not have to have ODBC installed or connect to the database
112           via ODBC.
113
114       new_native DATABASE_HANDLE TABLE_NAME
115           Creates a new DBIx::DBSchema::Table object from the supplied DBI
116           database handle for the specified table.  This uses database-native
117           methods to read the schema, and will preserve any non-portable
118           column types.  The method is only available if there is a
119           DBIx::DBSchema::DBD for the corresponding database engine
120           (currently, MySQL and PostgreSQL).
121
122       addcolumn COLUMN
123           Adds this DBIx::DBSchema::Column object.
124
125       delcolumn COLUMN_NAME
126           Deletes this column.  Returns false if no column of this name was
127           found to remove, true otherwise.
128
129       name [ TABLE_NAME ]
130           Returns or sets the table name.
131
132       local_options [ OPTIONS ]
133           Returns or sets the database-specific table options string.
134
135       primary_key [ PRIMARY_KEY ]
136           Returns or sets the primary key.
137
138       columns
139           Returns a list consisting of the names of all columns.
140
141       column COLUMN_NAME
142           Returns the column object (see DBIx::DBSchema::Column) for the
143           specified COLUMN_NAME.
144
145       indices
146           Returns a list of key-value pairs suitable for assigning to a hash.
147           Keys are index names, and values are index objects (see
148           DBIx::DBSchema::Index).
149
150       unique_singles
151           Meet exciting and unique singles using this method!
152
153           This method returns a list of column names that are indexed with
154           their own, unique, non-compond (that's the "single" part) indices.
155
156       sql_create_table [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [
157       ATTR ] ] ]
158           Returns a list of SQL statments to create this table.
159
160           The data source can be specified by passing an open DBI database
161           handle, or by passing the DBI data source name, username and
162           password.
163
164           Although the username and password are optional, it is best to call
165           this method with a database handle or data source including a valid
166           username and password - a DBI connection will be opened and the
167           quoting and type mapping will be more reliable.
168
169           If passed a DBI data source (or handle) such as
170           `DBI:mysql:database', will use MySQL- or PostgreSQL-specific
171           syntax.  Non-standard syntax for other engines (if applicable) may
172           also be supported in the future.
173
174       sql_add_constraints [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD
175       [ ATTR ] ] ]
176           Returns a list of SQL statments to add constraints (foreign keys)
177           to this table.
178
179           The data source can be specified by passing an open DBI database
180           handle, or by passing the DBI data source name, username and
181           password.
182
183           Although the username and password are optional, it is best to call
184           this method with a database handle or data source including a valid
185           username and password - a DBI connection will be opened and the
186           quoting and type mapping will be more reliable.
187
188           If passed a DBI data source (or handle) such as
189           `DBI:mysql:database', will use MySQL- or PostgreSQL-specific
190           syntax.  Non-standard syntax for other engines (if applicable) may
191           also be supported in the future.
192
193       sql_alter_table PROTOTYPE_TABLE, [ DATABASE_HANDLE | DATA_SOURCE [
194       USERNAME PASSWORD [ ATTR ] ] ]
195           Returns a list of SQL statements to alter this table so that it is
196           identical to the provided table, also a DBIx::DBSchema::Table
197           object.
198
199           The data source can be specified by passing an open DBI database
200           handle, or by passing the DBI data source name, username and
201           password.
202
203           Although the username and password are optional, it is best to call
204           this method with a database handle or data source including a valid
205           username and password - a DBI connection will be opened and used to
206           check the database version as well as for more reliable quoting and
207           type mapping.  Note that the database connection will be used
208           passively, not to actually run the CREATE statements.
209
210           If passed a DBI data source (or handle) such as
211           `DBI:mysql:database' or `DBI:Pg:dbname=database', will use syntax
212           specific to that database engine.  Currently supported databases
213           are MySQL and PostgreSQL.
214
215           If not passed a data source (or handle), or if there is no driver
216           for the specified database, will attempt to use generic SQL syntax.
217
218       sql_alter_constraints PROTOTYPE_TABLE, [ DATABASE_HANDLE | DATA_SOURCE
219       [ USERNAME PASSWORD [ ATTR ] ] ]
220           Returns a list of SQL statements to alter this table's constraints
221           (foreign keys) so that they are identical to the provided table,
222           also a DBIx::DBSchema::Table object.
223
224           The data source can be specified by passing an open DBI database
225           handle, or by passing the DBI data source name, username and
226           password.
227
228           Although the username and password are optional, it is best to call
229           this method with a database handle or data source including a valid
230           username and password - a DBI connection will be opened and used to
231           check the database version as well as for more reliable quoting and
232           type mapping.  Note that the database connection will be used
233           passively, not to actually run the CREATE statements.
234
235           If passed a DBI data source (or handle) such as
236           `DBI:mysql:database' or `DBI:Pg:dbname=database', will use syntax
237           specific to that database engine.  Currently supported databases
238           are MySQL and PostgreSQL.
239
240           If not passed a data source (or handle), or if there is no driver
241           for the specified database, will attempt to use generic SQL syntax.
242
243       foreign_keys_sql
244       foreign_keys
245           Returns a list of foreign keys (DBIx::DBSchema::ForeignKey
246           objects).
247

AUTHOR

249       Ivan Kohler <ivan-dbix-dbschema@420.am>
250
251       Thanks to Mark Ethan Trostler <mark@zzo.com> for a patch to allow
252       tables with no indices.
253
255       Copyright (c) 2000-2007 Ivan Kohler Copyright (c) 2000 Mail Abuse
256       Prevention System LLC Copyright (c) 2007-2013 Freeside Internet
257       Services, Inc.  All rights reserved.  This program is free software;
258       you can redistribute it and/or modify it under the same terms as Perl
259       itself.
260

BUGS

262       sql_create_table() has database-specific foo that probably ought to be
263       abstracted into the DBIx::DBSchema::DBD:: modules (or no?  it doesn't
264       anymore?).
265
266       sql_alter_table() also has database-specific foo that ought to be
267       abstracted into the DBIx::DBSchema::DBD:: modules.
268
269       sql_create_table() may change or destroy the object's data.  If you
270       need to use the object after sql_create_table, make a copy beforehand.
271
272       Some of the logic in new_odbc might be better abstracted into Column.pm
273       etc.
274
275       Add methods to get and set specific indices, by name? (like column
276       COLUMN_NAME)
277
278       indices method should be a setter, not just a getter?
279

SEE ALSO

281       DBIx::DBSchema, DBIx::DBSchema::Column, DBI, DBIx::DBSchema::Index,
282       DBIx::DBSchema::FoeignKey
283
284
285
286perl v5.36.0                      2023-01-20                DBSchema::Table(3)
Impressum