[LIB] DatabaseFactory - Easy MySQL and SQLite system with converters!

Discussion in 'Resources' started by lenis0012, Jul 5, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    lenis0012

    Hi again!
    I am here today with a factory to create databases in an easy way.

    Description
    I decided i would release my own database system i used in PvpLevels to the public.
    The advantage of this is the easy functions, no sql queries are required, but you can still use them.
    And it has an easy DatabaseConfigBuilder that supports config sections and custom info.
    It also has inbuilt Converter classes which you can use for UUID conversion or any other conversion of your like.

    Download
    GitHub: https://github.com/lenis0012/DatabaseFactory
    (Maven and javadocs comming soon, for now simply download the source and put it in your plugin)

    How to use
    First of all, you want to construct your factory:
    Code:java
    1. DatabaseFactory factory = new DatabaseFactory(myPlugin);
    2. factory.generateConfigSection(); //Default config section

    Now you need to configure it.
    You can use DatabaseConfigBuilder for this.
    The easiest way is to construct it using a ConfigurationSection and a SQLite backup file, this will use your config to check if mysql is enabled, if not it defaults to the sqlite file you speciafied.
    Code:java
    1. ConfigurationSection section = getConfig().getConfigurationSection("MySQL");
    2. File sqliteFile = new File(getDataFolder(), "database.db");
    3. DatabaseConfigBuilder config = new DatabaseConfigBuilder(section, sqliteFile);
    4. Database database = factory.getDatabase(config);
    5. database.connect(); //Its time for the real work

    You now have a working database, but without tabled.
    So first we need to create a table.
    I recomment to make these static final fields in a Tables.java class.
    So that you can use it everywhere:
    Code:java
    1. public static final Table MY_TABLE = new Table("NAME", "key VARCHAR(160),value INT";

    Now we need to register the table, and then you can use it:
    Code:java
    1. database.registerTable(Tables.MY_TABLE);
    2. database.set(Tables.MY_TABLE, "my funny key", 500);
    3. int myFunnyKey = database.get(Tables.MY_TABLE, "key", "value", "my funny key"); //Get value from MY_TABLE where key = my funny key.
     
  2. Offline

    Quaro

    lenis0012 Shouldn't connection has to be closed after execution?
     
  3. Offline

    lenis0012

    You could.
    You can do database.close();
     
  4. Offline

    Mr360zack

  5. Offline

    IDragonfire

    Why people don't use the persistence layer?
     
  6. Offline

    lenis0012

    I do, but it doesn't support JDBC SQLite believe
     
Thread Status:
Not open for further replies.

Share This Page