Code Monkey home page Code Monkey logo

jdbj's Introduction

This library is snapshot in 2015 of how I felt we should be thinking about application SQL queries. Not intended or suited for production use.


JDBJ is a small jdbc fluent interface for capturing query intent before query execution.

Primary Features

  • Named Parameters (No Positional Parameters, ever)
  • Collections bindable (not during batch execution)
  • Null-Safe fetching of (Boolean|Byte|Double|Float|Integer|Long|Short)
  • Lambda interface for bindings, transactions
  • Fetch-forward read-only cursors, always
  • Script Execution, with parameters
  • Comprehensive test suites for latest postgres, h2, derby, SQLite, MySql

Sample Code

Insert inside a transaction:

//INSERT some students
final List<NewStudent> newStudents = Arrays.asList(
        new NewStudent("Ada", "Lovelace", new BigDecimal("4.00")),
        new NewStudent("Haskell", "Curry", new BigDecimal("4.00"))
);

//NewStudent.INSERT is the resource which contains our sql statement
final ExecuteInsert<Long> insert = JDBJ.resource("student_insert.sql")
        .insert(rs->rs.getLong(1));

//db is a javax.sql.DataSource
List<Long> generatedKeys = JDBJ.transaction(connection -> {
    final List<Long> keys = new ArrayList<>();
    for (NewStudent newStudent : newStudents) {
        keys.addAll(insert.bindValues(newStudent::bindings).execute(connection));
    }
    return keys;
}).execute(db);

Select to list:

//setup query object
final MapQuery<Student> studentsByIds = JDBJ.resource("student_by_ids_limit.sql")
        .query()
        .bindLong(":limit", 10L)
        .bindLongs(":ids", generatedKeys)
        .map(Student::from);

//get as list
final ExecuteQuery<List<Student>> listQuery = studentsByIds
        .toList();
System.out.println(listQuery.execute(db));

Select to stream:

//get as stream
final StreamQuery<Student> streamQuery = studentsByIds
        .toStream();
try (Stream<Student> stream = streamQuery.execute(db)) {
    stream.forEach(System.out::println);
}

Examples

A full set of examples is being developed in the examples-branch, but here are some quick examples maintained in the test directory:

jdbj's People

Contributors

randyp avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.