Ambrosia

 view release on metacpan or  search on metacpan

share/Managers/buildXml.pm  view on Meta::CPAN

{
    $_[0]->__tableIdInc = 1;
}

sub prepare
{
    my $self = shift;
    Context->repository->set( schema_list => [] );
    storage->foreach(\&processDataSource, $self);

    my $path_to_app = config->ProjectPath;
    if ( $path_to_app )
    {
        $path_to_app =~ s{/[^/]+$}{};
    }
    else
    {
        $path_to_app = '';
    }

    my $projectName = config()->ID;
    my $message = <<MESSAGE;

#######################################################################
#
#   ADL file ${projectName}.xml has been created successfully.
#
#   Now you can additionally edit ${projectName}.xml and run:
#   ambrosia -c ${projectName}.conf -d ${projectName}.xml -a xml2app
#
#######################################################################

MESSAGE

    chomp(my $hostname = `hostname`);
    Context->repository->set( config => {
            name     => config->ID,
            label    => config->Label,
            charset  => lc(config->Charset || 'utf-8'),
            hostname => config->hostname || $hostname,

            ServerName => config->ServerName,
            ServerPort => config->ServerPort,

            ProjectPath => abs_path($path_to_app),
            PerlLibPath => join(' ', map {abs_path($_)} split /\s+/, config->PerlLibPath),
        } );
    Context->repository->set( Message => $message );
}

sub getDataSource
{
    my $t = shift;
    my $sn = shift;
    if ( ref config->data_source->{$t} eq 'ARRAY' )
    {
        foreach ( @{config->data_source->{$t}} )
        {
            return $_ if $_->{source_name} eq $sn;
        }
        throw Ambrosia::error::Exception::BadParams "Error: cannot find in config data_source source_name=$sn in type = $t";
    }
    elsif( config->data_source->{$t}->{source_name} eq $sn )
    {
        return config->data_source->{$t};
    }
    else
    {
        throw Ambrosia::error::Exception::BadParams "Error: cannot find in config data_source source_name=$sn in type = $t";
    }
}

sub processDataSource
{
    my $driver = shift;
    my $type = shift;
    my $source_name = shift;
    my $self = shift;

    my $schema_list = Context->repository->get( 'schema_list' );
    my $schema = {type => $type,
                  ($driver->catalog ? (catalog => $driver->catalog) : ()),
                  schema => $driver->schema,
                  tables => [],
                  config => {} };
    push @$schema_list, $schema;

    my $ds = getDataSource($type, $source_name);

    $schema->{config} = {
        db_engine   => $ds->{engine_name},
        db_source   => $source_name,
        db_params   => $ds->{engine_params},
        db_user     => $ds->{user},
        db_password => $ds->{password},
        db_charset  => (config->data_source_info->{$type}->{$source_name}->{charset} || 'utf8'),
    };

    my $tables = table_info($driver);
    my %hTables = ();

    my %foreign_keys = ();
    foreach ( @{foreign_key_info($driver)} )
    {
        push @{$foreign_keys{$_->{pktable_name}}}, {
            fktable_name => $_->{fktable_name},
            pkcolumn_name => $_->{pkcolumn_name},
            fkcolumn_name => $_->{fkcolumn_name},
            key_seq       => $_->{key_seq},
        };
    }

    foreach my $t ( @$tables )
    {
        my %primary_keys = ();
        foreach ( @{primary_key_info($driver, $t->{table_name})} )
        {
            $primary_keys{$_->{column_name}} = $_->{key_seq};
        }
        my $hasPK = scalar keys %primary_keys;

        my %table = (tId => $self->__tableIdInc++);
        $table{type} = uc $t->{table_type};
        $table{name} = $t->{table_name};
        if ( my $has_one = $foreign_keys{$table{name}} )
        {
            $table{has_one} = $has_one;
        }



( run in 0.951 second using v1.01-cache-2.11-cpan-ceb78f64989 )