Sueta
DA

DatabaseLib

3.2.0

Bukkit and BungeeCord library for asynchronous execution of SQL queries

·Plugin

Bukkit and BungeeCord library for asynchronous execution of SQL queries

Are you tired of syncing results of asynchronously executed methods back to the server thread? Do you want to easily configure your connection pools? Then this library is for you!

All tasks you submit using this library are executed asynchronously. After a task completes (either normally or by throwing an exception), its callback method is executed in the server thread (Bukkit plugins) or in one of the plugin threads (Bungee plugins). This library uses HikariCP as the main connection pool. More information on GitHub .

This library is also available as a BungeeCord plugin .

Dependencies :

Example:
1. Create a repository by extending PluginSqlTaskSubmitter :

public   final   class  UserRepository  extends  PluginSqlTaskSubmitter  {  
     public  UserRepository  (  JavaPlugin plugin  )   {   super   (  plugin  )   ;   }  

     public   void  getUser  (  UUID uuid, BiConsumer  <  User, Throwable  >  callback  )   {  
          String   query  =   "SELECT * FROM `users` WHERE `uuid` = ?"   ;  
         // you can submit different types of tasks  
        submitSqlPreparedStatementTask  (  query, preparedStatement  ->   {  
             // executed asynchronously  
            preparedStatement.  setString   (   1  , uuid.  toString   (   )   )   ;  
              ResultSet   rs  =  preparedStatement.  executeQuery   (   )   ;  
             return  rs.  next   (   )  
                     ?   new  User  (  uuid, rs.  getString   (   "email"   )   )  
                     :   null   ;  
         }  , callback  )   ;  
     }  
 }

2. Use your repository:

final   class  ExamplePlugin  extends  JavaPlugin  {  
     private  UserRepository userRepository  ;  

    @Override 
     public   void  onEnable  (   )   {  
        userRepository  =   new  UserRepository  (   this   )   ;  
        userRepository.  getUser   (  UUID.  fromString   (   "..."   )  ,  (  user, throwable  )   ->   {  
             // executed in the server-thread or a plugin-thread.  
             if   (  throwable  !=   null   )   {  
                  System   .  err  .  println   (   "exception: "   +  throwable  )   ;  
             }   else   {  
                  System   .  out  .  println   (   "result: "   +  user  )   ;  
             }  
         }   )   ;  
     }  
 }

Similar projects

Plugin →