2
Ora2Pg - Oracle to PostgreSQL database schema converter
6
$ENV{ORACLE_HOME} = '/usr/local/oracle/oracle816';
13
# Init the database connection
14
my $dbsrc = 'dbi:Oracle:host=testdb.samse.fr;sid=TEST;port=1521';
15
my $dbuser = 'system';
16
my $dbpwd = 'manager';
18
# Create an instance of the Ora2Pg perl module
19
my $schema = new Ora2Pg (
20
datasource => $dbsrc, # Database DBD datasource
21
user => $dbuser, # Database user
22
password => $dbpwd, # Database password
30
# Create the POSTGRESQL representation of all objects in the database
31
$schema->export_schema("output.sql");
35
or if you only want to extract some tables:
37
# Create an instance of the Ora2Pg perl module
38
my @tables = ('tab1', 'tab2', 'tab3');
39
my $schema = new Ora2Pg (
40
datasource => $dbsrc, # Database DBD datasource
41
user => $dbuser, # Database user
42
password => $dbpwd, # Database password
44
or # Tables to extract
45
tables => [('tab1','tab2')],
46
debug => 1 # To show somethings when running
49
or if you only want to extract the 10 first tables:
51
# Create an instance of the Ora2Pg perl module
52
my $schema = new Ora2Pg (
53
datasource => $dbsrc, # Database DBD datasource
54
user => $dbuser, # Database user
55
password => $dbpwd, # Database password
56
max => 10 # 10 first tables to extract
59
or if you only want to extract tables 10 to 20:
61
# Create an instance of the Ora2Pg perl module
62
my $schema = new Ora2Pg (
63
datasource => $dbsrc, # Database DBD datasource
64
user => $dbuser, # Database user
65
password => $dbpwd, # Database password
66
min => 10, # Begin extraction at indice 10
67
max => 20 # End extraction at indice 20
70
To choose a particular Oracle schema to export just set the following
71
option to your schema name:
75
This schema definition can also be needed when you want to export data.
76
If export failed and complain that the table doesn't exists use this to
77
prefix the table name by the schema name.
79
If you want to use PostgreSQL 7.3 schema support activate the init
80
option 'export_schema' set to 1. Default is no schema export
82
To know at which indices tables can be found during extraction use the
87
To extract all views set the type option as follow:
91
To extract all grants set the type option as follow:
95
To extract all sequences set the type option as follow:
99
To extract all triggers set the type option as follow:
103
To extract all functions set the type option as follow:
107
To extract all procedures set the type option as follow:
111
To extract all packages and body set the type option as follow:
115
Default is table extraction
119
To extract all data from table extraction as INSERT statement use:
123
To extract all data from table extraction as COPY statement use:
127
and data_limit => n to specify the max tuples to return. If you set this
128
options to 0 or nothing, no limitation are used. Additional option
129
'table', 'min' and 'max' can also be used.
131
When use of COPY or DATA you can export data by calling method:
133
$schema->export_data("output.sql");
135
Data are dumped to the given filename or to STDOUT with no argument. You
136
can also send these data directly to a PostgreSQL backend using the
139
$schema->send_to_pgdb($destdatasrc,$destuser,$destpasswd);
141
In this case you must call export_data() without argument after the call
142
to method send_to_pgdb().
144
If you set type to COPY and you want to dump data directly to a PG
145
database, you must call method send_to_pgdb but data will not be sent
146
via DBD::Pg but they will be load to the database using the psql
147
command. Calling this method is istill required to be able to extract
148
database name, hostname and port information. Edit the $PSQL variable to
149
match the path of your psql command (nothing to edit if psql is in your
153
Ora2Pg is a perl OO module used to export an Oracle database schema to a
154
PostgreSQL compatible schema.
156
It simply connect to your Oracle database, extract its structure and
157
generate a SQL script that you can load into your PostgreSQL database.
159
I'm not a Oracle DBA so I don't really know something about its internal
160
structure so you may find some incorrect things. Please tell me what is
161
wrong and what can be better.
163
It currently dump the database schema (tables, views, sequences,
164
indexes, grants), with primary, unique and foreign keys into PostgreSQL
165
syntax without editing the SQL code generated.
167
It now can dump Oracle data into PostgreSQL DB as online process. You
168
can choose what columns can be exported for each table.
170
Functions, procedures and triggers PL/SQL code generated must be
171
reviewed to match the PostgreSQL syntax. Some usefull recommandation on
172
porting Oracle to PostgreSQL can be found at
173
http://techdocs.postgresql.org/ under the "Converting from other
174
Databases to PostgreSQL" Oracle part. I just notice one thing more is
175
that the trunc() function in Oracle is the same for number or date so be
176
carefull when porting to PostgreSQL to use trunc() for number and
177
date_trunc() for date.
180
The goal of the Ora2Pg perl module is to cover all part needed to export
181
an Oracle database to a PostgreSQL database without other thing that
182
provide the connection parameters to the Oracle database.
184
Features must include:
186
- Database schema export (tables, views, sequences, indexes),
187
with unique, primary and foreign key.
188
- Grants/privileges export by user and group.
189
- Table selection (by name and max table) export.
190
- Export Oracle schema to PostgreSQL 7.3 schema.
191
- Predefined functions/triggers/procedures/packages export.
193
- Sql query converter (todo)
195
My knowledge regarding database is really poor especially for Oracle so
196
contribution is welcome.
199
You just need the DBI, DBD::Pg and DBD::Oracle perl module to be
205
Creates a new Ora2Pg object.
207
Supported options are:
209
- datasource : DBD datasource (required)
210
- user : DBD user (optional with public access)
211
- password : DBD password (optional with public access)
212
- schema : Oracle internal schema to extract
213
- type : Type of data to extract, can be TABLE,VIEW,GRANT,SEQUENCE,
214
TRIGGER,FUNCTION,PROCEDURE,DATA,COPY,PACKAGE
215
- debug : Print the current state of the parsing
216
- export_schema : Export Oracle schema to PostgreSQL 7.3 schema
217
- tables : Extract only the given tables (arrayref)
218
- showtableid : Display only the table indice during extraction
219
- min : Indice to begin extraction. Default to 0
220
- max : Indice to end extraction. Default to 0 mean no limits
221
- data_limit : Number max of tuples to return during data extraction (default 0 no limit)
223
Attempt that this list should grow a little more because all
224
initialization is done by this way.
228
Print SQL data output to a filename or to STDOUT if no file is given.
230
Must be used only if type option is set to DATA or COPY =cut
232
sub export_data { my ($self, $outfile) = @_;
234
$self->_get_sql_data($outfile);
239
Print SQL conversion output to a filename or simply return these data if
242
send_to_pgdb DEST_DATASRC DEST_USER DEST_PASSWD
244
Open a DB handle to a PostgreSQL database
246
modify_struct TABLE_NAME ARRAYOF_FIELDNAME
248
Modify a table structure during export. Only given fieldname will be
254
Initialize a Ora2Pg object instance with a connexion to the Oracle
259
This function is used to retrieve all privilege information.
261
It extract all Oracle's ROLES to convert them as Postgres groups and
262
search all users associated to these roles.
264
Set the main hash $self->{groups}. Set the main hash $self->{grantss}.
268
This function is used to retrieve all sequences information.
270
Set the main hash $self->{sequences}.
274
This function is used to retrieve all triggers information.
276
Set the main hash $self->{triggers}.
280
This function is used to retrieve all functions information.
282
Set the main hash $self->{functions}.
286
This function is used to retrieve all packages information.
288
Set the main hash $self->{packages}.
292
This function is used to retrieve all table information.
294
Set the main hash of the database structure $self->{tables}. Keys are
295
the names of all tables retrieved from the current database. Each table
296
information compose an array associated to the table_info key as array
297
reference. In other way:
299
$self->{tables}{$class_name}{table_info} = [(OWNER,TYPE)];
301
DBI TYPE can be TABLE, VIEW, SYSTEM TABLE, GLOBAL TEMPORARY, LOCAL
302
TEMPORARY, ALIAS, SYNONYM or a data source specific type identifier.
303
This only extract TABLE type.
305
It also get the following informations in the DBI object to affect the
306
main hash of the database structure :
308
$self->{tables}{$class_name}{field_name} = $sth->{NAME};
309
$self->{tables}{$class_name}{field_type} = $sth->{TYPE};
311
It also call these other private subroutine to affect the main hash of
312
the database structure :
314
@{$self->{tables}{$class_name}{column_info}} = $self->_column_info($class_name, $owner);
315
@{$self->{tables}{$class_name}{primary_key}} = $self->_primary_key($class_name, $owner);
316
@{$self->{tables}{$class_name}{unique_key}} = $self->_unique_key($class_name, $owner);
317
@{$self->{tables}{$class_name}{foreign_key}} = $self->_foreign_key($class_name, $owner);
321
This function is used to retrieve all views information.
323
Set the main hash of the views definition $self->{views}. Keys are the
324
names of all views retrieved from the current database values are the
325
text definition of the views.
327
It then set the main hash as follow:
329
# Definition of the view
330
$self->{views}{$table}{text} = $view_infos{$table};
334
Returns a string containing the entire SQL Schema definition compatible
339
This function implements a Oracle-native data extraction.
341
Return a list of array reference containing the data
343
_sql_type INTERNAL_TYPE LENGTH PRECISION SCALE
345
This function return the PostgreSQL datatype corresponding to the Oracle
348
_column_info TABLE OWNER
350
This function implements a Oracle-native column information.
352
Return a list of array reference containing the following informations
353
for each column the given a table
355
[( column name, column type, column length, nullable column, default
358
_primary_key TABLE OWNER
360
This function implements a Oracle-native primary key column information.
362
Return a list of all column name defined as primary key for the given
365
_unique_key TABLE OWNER
367
This function implements a Oracle-native unique key column information.
369
Return a list of all column name defined as unique key for the given
372
_foreign_key TABLE OWNER
374
This function implements a Oracle-native foreign key reference
377
Return a list of hash of hash of array reference. Ouuf! Nothing very
378
difficult. The first hash is composed of all foreign key name. The
379
second hash just have two key known as 'local' and remote' corresponding
380
to the local table where the foreign key is defined and the remote table
383
The foreign key name is composed as follow:
385
'local_table_name->remote_table_name'
387
Foreign key data consist in two array representing at the same indice
388
the local field and the remote field where the first one refer to the
389
second. Just like this:
391
@{$link{$fkey_name}{local}} = @local_columns;
392
@{$link{$fkey_name}{remote}} = @remote_columns;
396
This function implements a Oracle-native users information.
398
Return a hash of all users as an array.
402
This function implements a Oracle-native roles information.
404
Return a hash of all groups (roles) as an array of associated users.
408
This function implements a Oracle-native user privilege information.
410
Return a hash of all tables grants as an array of associated users.
412
_get_indexes TABLE OWNER
414
This function implements a Oracle-native indexes information.
416
Return hash of array containing all unique index and a hash of array of
417
all indexes name which are not primary keys for the given table.
421
This function implements a Oracle-native sequences information.
423
Return a hash of array of sequence name with MIN_VALUE, MAX_VALUE,
424
INCREMENT and LAST_NUMBER for the given table.
428
This function implements a Oracle-native views information.
430
Return a hash of view name with the SQL query it is based on.
434
This function implements a Oracle-native column information.
436
Return a list of array reference containing the following informations
437
for each alias of the given view
439
[( column name, column id )]
443
This function implements a Oracle-native triggers information.
445
Return an array of refarray of all triggers informations
449
This function implements a Oracle-native functions information.
451
Return a hash of all function name with their PLSQL code
455
This function implements a Oracle-native packages information.
457
Return a hash of all function name with their PLSQL code
461
This function retrieve all Oracle-native tables information.
463
Return a handle to a DB query statement
466
Gilles Darold <gilles@darold.net>
469
Copyright (c) 2001 Gilles Darold - All rights reserved.
471
This program is free software; you can redistribute it and/or modify it
472
under the same terms as Perl itself.
475
This perl module is in the same state as my knowledge regarding
476
database, it can move and not be compatible with older version so I will
477
do my best to give you official support for Ora2Pg. Your volontee to
478
help construct it and your contribution are welcome.
481
the DBI manpage, the DBD::Oracle manpage, the DBD::Pg manpage
484
Thanks to Jason Servetar who decided me to implement data extraction.