DbLib - A library that bundles database drivers for simple use with your plugin

This is a discussion topic for the Ore project, DbLib. View the full project on Ore for downloads and more information.


DbLib

DbLib is a library, that bundles database drivers for simple use with your plugin.

Author SupremeMortal
Maintainer SupremeMortal
Note I’m not a maintainer of the plugin, but I can help if you have issues using it with PowerNukkit.
License GNU v3.0
Source code GitHub - Nukkit-coders/DbLib: DbLib a library for Nukkit, that include ORMLite and drivers for MySQL and SQLite
Issue Tracker Issues · Nukkit-coders/DbLib · GitHub
Compatiblity PowerNukkit 1.6.0.0-PN-SNAPSHOT
Requirements None

Features

Provides access to popular database engines: SQLite and MySQL;
Includes popular library ORMLite, that provides access to database without using SQL-queries;
Organizes universal data storage for all plugins, that uses DbLib. Server owner must configure DbLib once and all plugins that use DbLib will work fine!

How to use plugin (Server owners must read this)

If any plugin requires a DbLib you just need to download and install it. Usually, it will be enough. But if you going to use to MySQL or another SQLite file you need to configure plugin (Read at Config section)

Note for plugin developers

If you plugin requires DbLib, please add a direct depend to your plugin.yml, if the dependency is optional then add a softdepend as in the example bellow:

# If required
depend: 
 - DbLib
# If optional
softdepend:
 - DbLib

ORMLite is popular engine, that provides access to databases using ORM technique. Practically it means that you can read and save data into the database without writing SQL-queries.

DbLib also includes database drivers:
SQLite driver
MySQL driver
If you would not like to use ORMLite, you can use MySQL (or SQLite) drivers as usually, using JDBC provided by Java.

How to connect to universal DbLib storage

DbLib suggests using universal database (by default it is a file nukkit.db, located in server folder or MySQL database defined by the user) for all plugins. If DbLib installed on the server you don’t need to think about database drivers, URLs, names and passwords.

Here is example of creating new table (check API section for more info):

   <repositories>
       <repository>
           <id>nukkit-repo</id>
           <url>https://repo.nukkitx.com/snapshot</url>
       </repository>
   </repositories>

   <dependencies>
       <dependency>
           <groupId>ru.nukkit.dblib</groupId>
           <artifactId>DbLib</artifactId>
           <version>1.0-SNAPSHOT</version>
           <scope>provided</scope>
       </dependency>
   </dependencies>

Example:

  public boolean connectToDbLib(){
    if (this.getServer().getPluginManager().getPlugin("DbLib") == null){
      this.getLogger().info(TextFormat.RED+"DbLib plugin not found");
      return false;
    }
    connectionSource = DbLib.getConnectionSource();
    if (connectionSource == null) return false;
    try {
      passDao =  DaoManager.createDao(connectionSource, PasswordsTable.class);
      TableUtils.createTableIfNotExists(connectionSource, PasswordsTable.class);
    } catch (Exception e) {
      return false;
    }
    return true;
  }

But if you going to use any other database you must use this method:

DbLib.getConnectionSoruce(String url, String userName, String password)

A new version has been released for DbLib, it is available for download here.


SQLite/MySQL library for your plugins (Nukkit)