Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

FlatDB Action Status

An easy to use library for flat-file databases.

Features

  • Create tables using Java classes.
  • Auto-update tables when new fields are added in the Java class.
  • Easily connect to your database file.

Requirements

Java 8+

Dependencies

FlatDB comes with H2 Driver. You can exclude it in your build tool if you wish to use a different driver.


Getting Started

Add FlatDB as dependency.

Gradle

repositories {
  maven { url 'https://jitpack.io' }
}
dependencies {
  compile 'com.github.cyr1en:flatdb:LATEST_VERSION'
}

Maven

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>
<dependency>
  <groupId>com.github.cyr1en</groupId>
  <artifactId>flatdb</artifactId>
  <version>LATEST_VERSION</version>
</dependency>

Using FlatDB

Creating a Table

Let's first create a table that we will pass down to the DatabaseBuilder.

@Table(nameOverride = "table")
public class TestTable {
  @Column(autoIncrement = true) int id;
  @Column(primaryKey = true) UUID uuid;
  @Column String name;
  @Column String lastName;
}

The class above will get translated to:

id uuid name lastname

Connecting to Database

Now let's configure our connection to the database using the DatabaseBuilder class. Here, we're setting the path in which the database is located. If the database file doesn't exist, it will be generated. We're also setting the prefix of the tables to be "test_". Lastly, we now add the table that we created above.

DatabaseBuilder builder = new DatabaseBuilder()
        .setPath(System.getProperty("user.dir") + "/testDb/flatDB")
        .setDatabasePrefix("test_")
        .appendTable(TestTable.class);
try {
  Database db = builder.build();
} catch (SQLException e) {
  e.printStackTrace();
}

After that, it's pretty much a generic JDBC experience. Except the Database#executeQuery() function returns an Optional.

To do a query

Optional<ResutlSet> result = db.executeQuery("SELECT * FROM 'test_table' WHERE name = 'someName'");
result.ifPresent(rs -> System.out.println("Hey I'm present"));

Additional Configurations

Defining Custom Type Maps.

FlatDB automatically converts the data types of a field into an SQL type equivalent. However, by default, FlatDB doesn't have all the data types mapped. To solve this, we can define our own type conversions in the TypeMap class where we need to provide a java class, and an SQLTypePair(Types.SOME_TYPE, "default value").

Using DatabaseBuilder
DatabaseBuilder builder = new DatabaseBuilder();
builder.addCustomType(UUID.class, SQLTypePair.of(Types.VARCHAR, "null"));
Using TypeMap
//The last parameter indicates if we should override the definition for UUID.class
TypeMap.addCustomType(UUID.class, SQLTypePair.of(Types.VARCHAR, "null"), true);

To see more examples: Click here

Process Tables in Runtime

In cases where new classes are loaded in runtime, and additional tables need to be processed. The TableProcessor class allows us to do so.

Database database; //assuming database is already initialized.
TableProcessor tableProcessor = new TableProcessor(database);
tableProcessor.process(SomeClass.class);

Disclaimer

DBTablePrinter by hturon is embedded in FlatDB to allow users to easily print their ResultSets. See DBTablePrinter in this project.

About

An easy to use library for flat-file databases.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages