package com.github.davidmoten.rx.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

import com.github.davidmoten.rx.jdbc.annotations.Column;

import rx.functions.Action1;

public class DatabaseExampleTest {

    @Test
    public void testCreateAndUseAnInMemoryDatabase() throws SQLException {
        // little trick for an in-memory h2 database so that closing a
        // connection to it doesn't throw the db away
        Connection con = Database.from("jdbc:h2:mem:demo1").getConnectionProvider().get();
        Database db = Database.from(con);
        db.update(
                "create table person (name varchar(50) primary key, score int not null,dob date, registered timestamp)")
                .count().toBlocking().single();

        db.update("insert into person(name,score) values(?,?)").parameters("FRED", 21).count()
                .toBlocking().single();

        // use java 8 lambdas if you have them !
        db.select("select name, score from person").autoMap(Person.class)
                .forEach(new Action1<Person>() {
                    @Override
                    public void call(Person person) {
                        System.out.println(person.name() + " has score " + person.score());
                    }
                });
        db.close();
    }

    static interface Person {
        @Column
        String name();

        @Column
        int score();
    }

}

@@ -37,36 +37,8 @@
 c;%0A%0A
-import java.sql.Connection;%0A
 impo
@@ -63,16 +63,16 @@
 eption;%0A
+
 %0Aimport 
@@ -312,28 +312,21 @@
  // 
-little trick for
+create
  an 
+h2 
 in-m
@@ -331,19 +331,16 @@
 -memory 
-h2 
 database
@@ -344,210 +344,146 @@
 ase 
-so 
 that 
-closing a%0A        // connection to it doesn't throw the db away%0A        Connection con = Database.from(%22jdbc:h2:mem:demo1%22).getConnectionProvider().get();%0A        Database db = Database.from(con
+does not get dropped when all%0A        // connections closed%0A        Database db = Database.from(%22jdbc:h2:mem:demo1;DB_CLOSE_DELAY=-1%22
 );%0A 

add example