Perl6 DBDI YAPC::EU 201008

Preview:

DESCRIPTION

Slides of my Perl 6 DBDI (database interface) talk at YAPC::EU in August 2010. Please also see the fun screencast that includes a live demo of perl6 using a perl5 DBI driver: http://timbunce.blip.tv/file/3973550/

Citation preview

DBDIfor Perl6

In

2007I said...

Database interfaces for open source languages suck

They’re all limited

They’re all different

They’re all duplicatingdevelopment effort

We need

a common database driver API

for Perl 6 and Parrot

But what?

Mature, Stable, Functional,Object Oriented

Well documented, with a test suite

Well known to a wide user base

Well known to driver developers

JDBCNo, not Java, just the JDBC API

The class and method names,the semantics

the documentationthe test suite

Actually, not plain JDBC

Sanitize the worst influences of Java

Easier to use for dynamic languages

DBD

DBI

Database Client Library

Your Code DBI v2 API

DBDI API

DBD

DBI

Database Client Library

Your Code DBI v2 API

DBDI API

NO JDBC FOR THIS!

USE JDBC

FOR THIS

How?

java2perl6api

Generates Perl 6 Role modules that mirror the API of specified Java Classes

Originally a GSoC Projectby Phil Crow in 2007

http://github.com/timbunce/java2perl6

$ java2perl6api --add_types jdbclib-typemap --outdir jdbclib java.sql.DriverManagerperl6: Rakudo Perl 6, version 2010.07-39-gac8a2ae built on parrot 2.6.0 r48152loading java.sql.DriverManagerloading . java.sql.Connectionloading . . java.sql.Arrayloading . . . java.sql.ResultSetloading . . . . java.io.Readerloading . . . . java.sql.Dateloading . . . . java.sql.Refloading . . . . java.sql.ResultSetMetaDataloading . . . . . java.sql.Wrapperloading . . . . java.sql.RowIdloading . . . . java.sql.SQLWarningloading . . . . . java.sql.SQLExceptionloading . . . . java.sql.Statement...wrote jdbclib/java/lang/Enum.pm6 - class java.lang.Enumwrote jdbclib/java/sql/CallableStatement.pm6 - interface java.sql.CallableStatement...compiling jdbclib/java/sql/SQLException.pm6 - class java.sql.SQLExceptioncompiling jdbclib/java/sql/Statement.pm6 - interface java.sql.Statement...

Recurses into types used Maps Java types to Perl6

Compiles to .pir for validation and speed

public interface java.sql.Statement extends java.sql.Wrapper {

public static final int SUCCESS_NO_INFO; public static final int EXECUTE_FAILED;

public abstract boolean execute(java.lang.String)throws java.sql.SQLException;

public abstract boolean execute(java.lang.String, int)throws java.sql.SQLException;

...}

role java::sql::Statement does java::sql::Wrapper {

method SUCCESS_NO_INFO (--> Int) is export { ... } method EXECUTE_FAILED (--> Int) is export { ... }

multi method execute ( Str $v1, --> Bool ) { ... } # throws java.sql.SQLException

multi method execute ( Str $v1, Int $v2, --> Bool ) { ... } # throws java.sql.SQLException

...}

2009

MiniDBI (formerly “FakeDBI”)

“a tiny subset of DBI in Perl 6”started by Martin Berends

“hopefully be obsoleted ASAP”

has PostgreSQL and MySQL drivers

2010

DBDI

A database driver manager and driver implementation framework in Perl 6

Uses the roles generated by runningjava2perl6api java.sql.DriverManager

http://github.com/timbunce/DBDI

It Runs!

A working PostgreSQL driveris included in DBDI

It’s very minimal today (August 2010)but it only took a few hours to write

# --- DBDI usage example ---use v6;

use DBDI;use DBDI::pglibpq;

my $url = @*ARGS.shift || prompt "Enter a 'dbdi:driver:...' URL: ";

my $con = DBDI::DriverManager.getConnection($url, 'testuser', 'testpass');

while prompt 'SQL: ' -> $sql {

my $result = $con.createStatement.executeQuery($sql); my $meta = $result.getMetaData;

my @names = map { $meta.getColumnLabel($_) }, 1..$meta.getColumnCount; say @names.join(", ");

while ( $result.next ) { my @row = map { $result.getString($_) }, 1..@names.elems; say @row.join(", "); }}

MiniDBI Drivers

DB Client Libs

Your Code

MiniDBI

DBDI Drivers

DBDI

BARE

SKELETON

BASIC BUT

FUNCTIONAL

CURRENT SITUATION

Next steps?

Build up a class/role hirearchy borrowing from open source JDBC drivers

Separate driver-specific logic from driver-independant logic to make it easy to

implement drivers

Establish infrastructure for logging, exceptions etc.

MiniDBI Drivers

DB Client Libs

Your Code

MiniDBI DBDI

MiniDBI DBDI Driver

MiniDBI Tests

Driver

Framew

orkDBDI Drivers

TRANSITION

& TEST ENABLER

SUCK BRAINZ

FROM MiniDBI

DRIVERS INTO

DBDI DRIVERS

SOME POINT IN THE FUTURE

MiniDBI Drivers

DB Client Libs

Your Code

MiniDBI

DBDI Drivers

DBDI Driver

Framew

ork

Also ADD a DRIVER

to access Perl5 DBI

using blitzkost

SOME POINT IN THE FUTURE

DB Client Libs

Your Code

DBDI Drivers

DBDI

DBI v1/v2/v3/*D

riverFram

ework

JDBC Test Suite

Sufficient Magic

SOME POINT FURTHER IN THE FUTURE

Interested?

Come and join in!

dbdi-dev@perl.org

#dbdi on irc.freenode.org