BungeeSQL 1.0.0

A BungeeCord, asynchronous MySQL API

  1. rickydaan
    Contributors:
    Puddanator
    BungeeSQL, a leightweight SQL API for BungeeCord servers. No more hassling with JDBC connections, just push queries on the go!

    PLEASE NOTE! This plugin is an API, and will do nothing except for supporting plugins that require BungeeSQL!

    From here on, I would only recommend developers to read!

    The most important thing that you might notice is that there are two different classes available, the Database and the AsyncDatabase (com.techsdev.bungeesql.Database or .AsyncDatabase). The Database one is deprecated and is based upon synchronous requests, meaning everything will be paused until the server continues. The async database is based upon MySQLCallbacks (com.techsdev.bungeesql.MySQLCallback) which is an interface with one void: public void done(Object o);

    That being said, let's get started on how to use this! There are two different types of queries, the ExecuteUpdate and ExecuteQuery functions. The ExecuteUpdate is used for queries that update or insert anything in the database (INSERT INTO, UPDATE). The ExecuteQuery is used to make calls to the database (SELECT).

    ExecuteQuery returns a ResultSet, or calls MySQLCallback->done(ResultSet result);
    ExecuteUpdate
    returns an integer, or calls MySQLCallback->done(Integer result);

    To make a database (We will use an asynchronous database) we firstly make a constructor:
    Code (Text):
    try{
       // Could establish SQL database link
       // void AsyncDatabase(String username, String password, String ip, String port, String database, Plugin plugin );
       AsyncDatabase db = new AsyncDatabase("root", "", "127.0.0.1", "3306", "bungee", this);
    }catch(SQLException e){
       // Could not establish SQL database link (Wrong user/pass, database offline, eg
       e.printStackTrace();
    }
    We now have a database link. It's important that you store the database somewhere in your code where you can always retrieve it, as you should never need to make more than one AsyncDatabase instance per plugin (Unless you use multiple MySQL databases). In order to see if a table exists, we could use this:

    Code (Text):
    db.tableExists("", new MySQLCallback(){
       @Override
       public void done(Object o){
          if(o instanceof Exception){
             ((Exception)o).printStackTrace();
          }else{
              boolean exists = (boolean) o;
              if(exists==false){
                 String query = "CREATE TABLE blablablabla";
                 db.ExecuteUpdate(query, new MySQLCallback(){@Override public void done(Object o){}});
              }
          }
       }
    }
     
    You can see I checked if the Exception was an error, because if the code errors the "done" function will be called. That way you can do most without having to write complicated code. It is a bit longer, but alot easier.

    We can also easily check if an entry exists in the database, by doing this:
    Code (Text):
    db.entryExists("players", "UUID", proxiedplayer.getUniqueId().toString(), new MySQLCallback(){
     
    maxiz22 and Remy2402 like this.