DBIx-QuickORM

 view release on metacpan or  search on metacpan

lib/DBIx/QuickORM/Dialect.pm  view on Meta::CPAN


=cut

sub dsn {
    my $self_or_class = shift;
    my ($db) = @_;

    my $driver = $db->dbi_driver // $self_or_class->dbi_driver;
    load_class($driver) or croak "Could not load '$driver': $@";
    my $dsn_driver = $driver;
    $dsn_driver =~ s/^DBD:://;

    my $db_name = $db->db_name;
    my $dbname_field = $self_or_class->dsn_dbname_field($driver);
    my $dsn = "dbi:${dsn_driver}:${dbname_field}=${db_name};";

    if (my $socket = $db->socket) {
        $dsn .= $self_or_class->dsn_socket_field($driver) . "=$socket";
    }
    elsif (my $host = $db->host) {
        $dsn .= "host=$host;";
        if (my $port = $db->port) {
            $dsn .= "port=$port;";
        }
    }
    else {
        croak "Cannot construct dsn without a host or socket";
    }

    return $dsn;
}

=pod

=item $sql = $dialect->upsert_statement($pk)

Returns the SQL fragment implementing an upsert keyed on the given primary
key columns. Column names are quoted so reserved-word or mixed-case columns
work.

=cut

sub upsert_statement {
    my $self = shift;
    my ($pk) = @_;
    my $dbh = $self->dbh;
    return "ON CONFLICT(" . join(", " => map { $dbh->quote_identifier($_) } @$pk) . ") DO UPDATE SET";
}

###############################################################################
# {{{ Schema Builder Code
###############################################################################

=pod

=item $schema = $dialect->build_schema_from_db(%params)

Introspects the live database and returns a L<DBIx::QuickORM::Schema>.
Requires an C<autofill> object. After all tables are built it runs the
autofill C<tables> hook with the complete name-to-table hashref under the
C<tables> key, giving callbacks one place to inspect or adjust the full set.

=cut

sub build_schema_from_db {
    my $self = shift;
    my %params = @_;

    croak "No autofill object provided" unless $params{autofill};

    my $dbh = $self->dbh;

    my $tables = $self->build_tables_from_db(%params);

    $params{autofill}->hook(tables => {tables => $tables});

    return DBIx::QuickORM::Schema->new(
        tables    => $tables,
        row_class => $params{row_class},
    );
}

=pod

=item $tables = $dialect->build_tables_from_db(%params)

=item ($pk, $unique, $links) = $dialect->build_table_keys_from_db($table, %params)

=item $columns = $dialect->build_columns_from_db($table, %params)

=item $indexes = $dialect->build_indexes_from_db($table, %params)

Per-table introspection helpers. Stubs; subclasses override.

=cut

sub build_tables_from_db     { my $self = shift; confess "Not Implemented" }
sub build_table_keys_from_db { my $self = shift; confess "Not Implemented" }
sub build_columns_from_db    { my $self = shift; confess "Not Implemented" }
sub build_indexes_from_db    { my $self = shift; confess "Not Implemented" }

=pod

=back

=cut

###############################################################################
# }}} Schema Builder Code
###############################################################################

1;

__END__

=head1 SOURCE

The source code repository for DBIx::QuickORM can be found at
L<https://github.com/exodist/DBIx-QuickORM>.

=head1 MAINTAINERS



( run in 0.452 second using v1.01-cache-2.11-cpan-140bd7fdf52 )