our
$VERSION
=
'1.59'
;
sub
parse {
my
(
$tr
,
$dbh
) =
@_
;
my
$schema
=
$tr
->schema;
my
$db_user
=
uc
$tr
->parser_args()->{db_user};
my
$sth
=
$dbh
->table_info(
undef
,
$db_user
,
'%'
,
'TABLE'
);
while
(
my
$table_info
=
$sth
->fetchrow_hashref(
'NAME_uc'
)) {
next
if
(
$table_info
->{TABLE_NAME} =~ /\$/);
my
$table
=
$schema
->add_table(
name
=>
$table_info
->{TABLE_NAME},
type
=>
$table_info
->{TABLE_TYPE},
);
my
$sth
;
$sth
=
$dbh
->column_info(
undef
,
$table_info
->{TABLE_SCHEM},
$table_info
->{TABLE_NAME},
'%'
);
while
(
my
$column
=
$sth
->fetchrow_hashref(
'NAME_uc'
)) {
my
$f
=
$table
->add_field(
name
=>
$column
->{COLUMN_NAME},
default_value
=>
$column
->{COLUMN_DEF},
data_type
=>
$column
->{TYPE_NAME},
order
=>
$column
->{ORDINAL_POSITION},
size
=>
$column
->{COLUMN_SIZE},
) ||
die
$table
->error;
$f
->is_nullable(
$column
->{NULLABLE} == 1 );
}
$sth
=
$dbh
->primary_key_info(
undef
,
$table_info
->{TABLE_SCHEM},
$table_info
->{TABLE_NAME},
);
while
(
my
$primary_key
=
$sth
->fetchrow_hashref(
'NAME_uc'
)) {
my
$f
=
$table
->get_field(
$primary_key
->{COLUMN_NAME} );
$f
->is_primary_key(1);
}
$sth
=
$dbh
->foreign_key_info(
undef
,
undef
,
undef
,
undef
,
$table_info
->{TABLE_SCHEM},
$table_info
->{TABLE_NAME},
);
my
$cons
= {};
while
(
my
$foreign_key
=
$sth
->fetchrow_hashref(
'NAME_uc'
)) {
my
$name
=
$foreign_key
->{FK_NAME};
$cons
->{
$name
}->{reference_table} =
$foreign_key
->{UK_TABLE_NAME};
push
@{
$cons
->{
$name
}->{fields} },
$foreign_key
->{FK_COLUMN_NAME};
push
@{
$cons
->{
$name
}->{reference_fields} },
$foreign_key
->{UK_COLUMN_NAME};
}
for
my
$name
(
keys
%$cons
) {
my
$c
=
$table
->add_constraint(
type
=> FOREIGN_KEY,
name
=>
$name
,
fields
=>
$cons
->{
$name
}->{fields},
reference_fields
=>
$cons
->{
$name
}->{reference_fields},
reference_table
=>
$cons
->{
$name
}->{reference_table},
) ||
die
$table
->error;
}
}
return
1;
}
1;