29
DBDI for Perl6

Perl6 DBDI YAPC::EU 201008

Embed Size (px)

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

Page 1: Perl6 DBDI YAPC::EU 201008

DBDIfor Perl6

Page 2: Perl6 DBDI YAPC::EU 201008

In

2007I said...

Page 3: Perl6 DBDI YAPC::EU 201008

Database interfaces for open source languages suck

Page 4: Perl6 DBDI YAPC::EU 201008

They’re all limited

They’re all different

They’re all duplicatingdevelopment effort

Page 5: Perl6 DBDI YAPC::EU 201008

We need

a common database driver API

for Perl 6 and Parrot

Page 6: Perl6 DBDI YAPC::EU 201008

But what?

Page 7: Perl6 DBDI YAPC::EU 201008

Mature, Stable, Functional,Object Oriented

Well documented, with a test suite

Well known to a wide user base

Well known to driver developers

Page 8: Perl6 DBDI YAPC::EU 201008

JDBCNo, not Java, just the JDBC API

The class and method names,the semantics

the documentationthe test suite

Page 9: Perl6 DBDI YAPC::EU 201008

Actually, not plain JDBC

Sanitize the worst influences of Java

Easier to use for dynamic languages

Page 10: Perl6 DBDI YAPC::EU 201008

DBD

DBI

Database Client Library

Your Code DBI v2 API

DBDI API

Page 11: Perl6 DBDI YAPC::EU 201008

DBD

DBI

Database Client Library

Your Code DBI v2 API

DBDI API

NO JDBC FOR THIS!

USE JDBC

FOR THIS

Page 12: Perl6 DBDI YAPC::EU 201008

How?

Page 13: Perl6 DBDI YAPC::EU 201008

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

Page 14: Perl6 DBDI YAPC::EU 201008

$ 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

Page 15: Perl6 DBDI YAPC::EU 201008

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;

...}

Page 16: Perl6 DBDI YAPC::EU 201008

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

...}

Page 17: Perl6 DBDI YAPC::EU 201008

2009

Page 18: Perl6 DBDI YAPC::EU 201008

MiniDBI (formerly “FakeDBI”)

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

“hopefully be obsoleted ASAP”

has PostgreSQL and MySQL drivers

Page 19: Perl6 DBDI YAPC::EU 201008

2010

Page 20: Perl6 DBDI YAPC::EU 201008

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

Page 21: Perl6 DBDI YAPC::EU 201008

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

Page 22: Perl6 DBDI YAPC::EU 201008

# --- 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($_) }, [email protected]; say @row.join(", "); }}

Page 23: Perl6 DBDI YAPC::EU 201008

MiniDBI Drivers

DB Client Libs

Your Code

MiniDBI

DBDI Drivers

DBDI

BARE

SKELETON

BASIC BUT

FUNCTIONAL

CURRENT SITUATION

Page 24: Perl6 DBDI YAPC::EU 201008

Next steps?

Page 25: Perl6 DBDI YAPC::EU 201008

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.

Page 26: Perl6 DBDI YAPC::EU 201008

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

Page 27: Perl6 DBDI YAPC::EU 201008

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

Page 28: Perl6 DBDI YAPC::EU 201008

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

Page 29: Perl6 DBDI YAPC::EU 201008

Interested?

Come and join in!

[email protected]

#dbdi on irc.freenode.org