Skip to content

4_Functions

DavidAllendorf edited this page Jun 17, 2025 · 4 revisions

Initialisation

Standalone Initialisation:

   new MineDb(new Logger, new File("SOME_PATH"));

Initialisation in a Minecraft Plugin Example:

    private final MineDb api;

    @Override
    public void onEnable() {
        this.getServer().getPluginManager().registerEvents(this, this);
        try {
            this.api = new MineDb(getLogger(), getDataFolder());
            this.api.loadSchema();
            doSomething(this.api); //Example
        } catch (Exception e) {
            log.severe("Database couldn't be initialized");
        }
    }

    @Override
    public void onDisable() {
       this.api.close();
    }

Initialisation with Name:

   new MineDb(new Logger, new File("SOME_PATH"), "custom.db");

loadSchema

Standard Schema:

   MineDb.loadSchema(); //initialized MineDb!

Custom Schema:

   MineDb.loadSchema("custom.json"); //initialized MineDb!

Insert

   InsertRow row = new InsertRow();
   row.put("uuid", "1");
   row.put("name", "Test_1");
   row.put("progress", "10");
   ReturnSet returnValSimple = api.insert(TEST_TABLE,row);
   //returnValSimple.getChangedRows();

Insert Multi

   InsertMultiRows multiRows = new InsertMultiRows();
   InsertRow row1 = new InsertRow();
   row1.put("uuid", "2");
   row1.put("name", "Test_2");
   row1.put("progress", "20");
   multiRows.add(row1);

   InsertRow row2 = new InsertRow();
   row2.put("uuid", "3");
   row2.put("name", "Test_3");
   row2.put("progress", "30");
   multiRows.add(row2);

   ReturnSet returnVal = api.insert(TEST_TABLE,multiRows);
   //returnVal.getChangedRows();

Update

   UpdateRow updateData = new UpdateRow();
   updateData.put("uuid", "33");
   updateData.put("name", "Test_3_Updated");
   updateData.put("progress", "30.3");
   ReturnSet returnVal = api.update(TEST_TABLE, updateData, cond("uuid = 3"));
   //returnVal.getChangedRows()

Delete

   ReturnSet returnVal = api.delete(TEST_TABLE, cond("uuid = 2"));
   //returnVal.getChangedRows();

Select

   ReturnSet rs =  api.select(TEST_TABLE, col("uuid","name", "progress"), cond("uuid > 3"), sort("uuid desc"));
   //rs.getColumn(0,"uuid");
   //rs.getColumn(1,"name")

Select-Aggregate-Function

When using aggregate Functions(AVG, COUNT, MAX, MIN, SUM) you have to create Aliases ("column" as "alias") to use them later

   ReturnSet rs =  api.select(TEST_TABLE, col("sum(progress) as sum_progress"));
   return rs.getColumn(0, "sum_progress").equalsIgnoreCase("100.3");

createTable

"Expert" Function. You shouldn use this normally:

   TableConfig conf = new TableConfig("tableName");
   conf.addColumn("uuid", TableConfig.ColTypes.INTEGER);
   conf.addColumn("name", TableConfig.ColTypes.TEXT);
   conf.addColumn("progress", TableConfig.ColTypes.REAL);
   conf.setPrimary("uuid");
   api.createTable(conf);

deleteTable

"Expert" Function. You shouldn use this normally:

   api.deleteTable("tableName");

Clone this wiki locally