package com.github.davidmoten.rx.jdbc;

import static org.junit.Assert.assertEquals;

import java.sql.SQLException;

import org.junit.Test;

import rx.functions.Action1;

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

public class DatabaseExampleTest {

    @Test
    public void testCreateAndUseAnInMemoryDatabase() throws SQLException {
        // create an h2 in-memory database that does not get dropped when all
        // connections closed
        Database db = Database.from("jdbc:h2:mem:demo1;DB_CLOSE_DELAY=-1");
        db.update(
                "create table person (name varchar(50) primary key, score int not null,dob date, registered timestamp)")
                .count().toBlocking().single();

        assertEquals(
                2,
                db.update("insert into person(name,score) values(?,?)")
                        .parameters("FRED", 21, "JOE", 34).execute());

        // 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();
    }

}

@@ -148,25 +148,83 @@
  rx.
-functions.Action1
+Observable;%0Aimport rx.functions.Action1;%0Aimport rx.observers.TestSubscriber
 ;%0A%0Ai
@@ -1490,10 +1490,723 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testParameterOperatorBackpressure() %7B%0A        // use composition to find the first person alphabetically with%0A        // a score less than the person with the last name alphabetically%0A        // whose name is not XAVIER. Two threads and connections will be used.%0A%0A        Database db = DatabaseCreator.db();%0A        TestSubscriber%3CString%3E ts = TestSubscriber.create(0);%0A        Observable.just(%22FRED%22, %22FRED%22).repeat()%0A                .lift(db.select(%22select name from person where name = ?%22)%0A                        .parameterOperator().getAs(String.class))%0A                .subscribe(ts);%0A        ts.requestMore(1);%0A        ts.assertValueCount(1);%0A        ts.unsubscribe();%0A    %7D%0A    %0A
 %7D%0A

add failing test