jood

Java object oriented database library

License

License

GroupId

GroupId

com.nikialeksey
ArtifactId

ArtifactId

jood
Last Version

Last Version

3.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

jood
Java object oriented database library
Project URL

Project URL

https://github.com/nikialeksey/jood
Source Code Management

Source Code Management

https://github.com/nikialeksey/jood

Download jood

How to add to project

<!-- https://jarcasting.com/artifacts/com.nikialeksey/jood/ -->
<dependency>
    <groupId>com.nikialeksey</groupId>
    <artifactId>jood</artifactId>
    <version>3.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.nikialeksey/jood/
implementation 'com.nikialeksey:jood:3.1.1'
// https://jarcasting.com/artifacts/com.nikialeksey/jood/
implementation ("com.nikialeksey:jood:3.1.1")
'com.nikialeksey:jood:jar:3.1.1'
<dependency org="com.nikialeksey" name="jood" rev="3.1.1">
  <artifact name="jood" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.nikialeksey', module='jood', version='3.1.1')
)
libraryDependencies += "com.nikialeksey" % "jood" % "3.1.1"
[com.nikialeksey/jood "3.1.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.cactoos : cactoos jar 0.49

Project Modules

There are no modules declared in this project.

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
)

Versions

Version
3.1.1
3.1.0
3.0.0
2.0.0
1.4.2
1.4.1
1.4.0
1.3.0
1.2.2
1.2.1
1.2.0
1.1.0
1.0.0
0.0.1