Giter Site home page Giter Site logo

nikialeksey / jood Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 1.0 132 KB

Object oriented jdbc wrapper

License: MIT License

Java 100.00%
jdbc object-oriented library java java-8 java8 java-library sql database sql-database migrations sql-queries sql-migrations eo-principles elegantobjects elegant-objects

jood's Introduction

Jood

Elegant Objects Respected Here

nullfree status staticfree status allpublic status setterfree status nomultiplereturn status

Lib version Build Status codecov

License: MIT

logo

What is it?

Jood is object oriented sql-database library written in Java.

Getting started

Gradle

implementation 'com.nikialeksey:jood:x.y.z'

Maven

<dependency>
  <groupId>com.nikialeksey</groupId>
  <artifactId>jood</artifactId>
  <version>x.y.z</version>
</dependency>

Where x.y.z actual version from Lib version

Connection

Connection connection = DriverManager.getConnection(...);
Db db = new JdDb(() -> connection);

For example, connect to sqlite in-memory:

Connection connection = DriverManager.getConnection(
    "jdbc:sqlite::memory:"
);
Db db = new JdDb(() -> connection);

Data source

DataSource ds = ...;
Db db = new JdDb(ds);

For example, connect to h2 in-memory through the c3p0 pooled data source:

ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setJdbcUrl("jdbc:h2:mem:db_name");
Db db = new JdDb(ds);

Simple queries

Db db = ...;
db.write(new JdSql("CREATE TABLE a (n INTEGER NOT NULL)"));
db.write(new JdSql("INSERT INTO a(n) VALUES(5)"));
try (
    QueryResult qr = db.read(new JdSql("SELECT * FROM a"))
) {
    ResultSet rs = qr.rs();
    while (rs.next()) {
        String n = rs.getString("n");
    }
}

Query arguments

db.write(
    new JdSql(
        "INSERT INTO a(n) VALUES(?)",
        new IntArg(5)
    )
);

Transactions

Db db = ...;
db.run(() -> { // all changes inside will be commited after successfull execution
    db.run(() -> { // transactions could be inner
        for (int i = 0; i < 1000; i++) {
            db.write(new JdSql("INSERT INTO a(n) VALUES(1)"));
        }
    });
    for (int i = 0; i < 3000; i++) {
        db.write(new JdSql("INSERT INTO a(n) VALUES(2)"));
    }
});

Migrations

Db origin = ...;
Db db = new MigrationsDb(
    origin,
    new JdMigrations(
        new Migration() {
            @Override
            public int number() {
                return 0; // migration index
            }

            @Override
            public void execute(final Db db) throws JbException {
                db.write(
                    new JdSql(
                        "CREATE TABLE user (name TEXT NOT NULL)"
                    )
                );
            }
        },
        new Migration() {
            @Override
            public int number() {
                return 1; // migration index
            }

            @Override
            public void execute(final Db db) throws JbException {
                db.write(
                    new JdSql(
                        "ALTER TABLE user " +
                            "ADD lastname TEXT NOT NULL DEFAULT ''"
                    )
                );
            }
        }  
    ),
    2 // db version number
)

jood's People

Contributors

nikialeksey avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

alexrogalskiy

jood's Issues

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.