SQLibrary
SQLibrary 1.0.0 Beta:
A Bukkit / Spigot developer's solution to SQL
SQLibrary is a SQL Library with many many tools to connect to / creating SQL databases and files.With easy an easy to use connection API and QueryBuilder
Implementing SQLibrary dependency to your plugin
onEnable Function
if(!Bukkit.getPluginManager().isPluginEnabled("SQLibrary")) { Bukkit.getLogger().log( Level.SEVERE, "Plugin was unable to start | SQLibrary is not installed" ); this.setEnabled(false); return; }
Add to plugin.yml
soft-depend: [SQLibrary]
Connecting to a SQL Database
Connecting via MySQL
SQLDatabase database = null; try { database = new MySQL("hostname", "port", "database", "username", "password"); database.openConnection(); } catch (SQLException | ClassNotFoundException e) { Bukkit.getLogger.log(Level.SEVERE, "Unable to connect to MySQL database", e); return; }
Connecting via SQLite
SQLDatabase database = null; try { database = new SQLite(new File("path to database file")); database.openConnection(); } catch (SQLException | ClassNotFoundException e) { Bukkit.getLogger.log(Level.SEVERE, "Unable to connect to SQLite database", e); return; }
QueryBuilder Examples
SQL Table Builder
In this example we will be creating a player statistics table that holds player kills, deaths and gold
/* Create a new TableBuilder */ TableBuilder builder = QueryBuilder.createTable("player_stats"); builder.createIfNotExists(); /* Create a new Column Builder | new ColumnBuilder(String columnName, ColumnType type, new ColumnMeta(size)) */ ColumnBuilder columnBuilder = new ColumnBuilder("player_id", ColumnType.VARCHAR, new ColumnMeta(25).primaryKey().notNull()); /* Append additional columns to the Column Builder */ columnBuilder.appendColumn("player_kills", ColumnType.INT, new ColumnMeta(10).notNull().defaultValue(0)); columnBuilder.appendColumn("player_deaths", ColumnType.INT, new ColumnMeta(10).notNull().defaultValue(0)); columnBuilder.appendColumn("player_gold", ColumnType.INT, new ColumnMeta(10).notNull().defaultValue(500)); /* Set the Column Builder */ builder.columns(columnBuilder); /* Execute the SQL Update */ database.executeUpdate(builder.build());
SQL Select Builder
/* Create a new Select Builder */ SelectBuilder selectBuilder = QueryBuilder.selectAll("player_stats"); /* OR */ SelectBuilder selectBuilder = QueryBuilder.select("player_stats", {"player_kills", "player_deaths", "player_gold"}); /* Create a new Condition Builder to add to our SelectBuilder */ ConditionBuilder conditionBuilder = new ConditionBuillder(Condition.fieldEquals("player_id", "SOME PLAYER ID") selectBuilder.where(conditionBuilder); /* Execute the query and retrieve a Result Set */ ResultSet set = database.executeQuery(selectBuilder.build()); /* Process the Result set */ if(set.next) { /* Process Player Data */ } else { /* No player data in database */ }
SQL Update Builder
/* Create our UpdateBuilder */ UpdateBuilder builder = QueryBuilder.update("player_stats"); /* Set the Values we wish to update, In this example I am going to update the player kills by +1 and set the player gold to 2000 */ builder.values(new Value[] { /* SQLibrary will automatically format " ' " around Strings. Casting a String to a Object will allow you to set a un-formatted value */ new Value("player_kills", (Object) "`player_kills` + 1"); new Value("player_gold", 1000); }); /* Create a new Condition Builder */ ConditionBuilder conditionBuilder = new ConditionBuilder(Condition.fieldEquals("player_id", "SOME PLAYER ID")); /* Add another condition to the builder for an example */ conditionBuilder.and(Condition.fieldEqualOrLessThan("player_gold", 1000)); /* Add the ConditionBuilder and Execute the Update */ builder.where(conditionBuilder); database.executeUpdate(builder.build());
SQL Insert Builder
/* Create our InsertBuilder */ InsertBuilder builder = QueryBuilder.insertInto("player_stats"); /* Set the Values we want to insert */ builder.values(new Value[] { new Value("player_id", "SOME PLAYER ID"); new Value("player_kills", 12); new Value("player_deaths", 0); new Value("player_gold", 6000); }); /* Execute the Update */ database.executeUpdate(builder.build());
Does this accept putting objects (blobs) into the database as well or will i have to do that manually?
@Xayges
I created this plugin because I am a private developer and use SQL for most of my data storage projects. So I uploaded it here so anyone I have developed plugins for can get this resource here or on spigot.
It also gives the best of both worlds by allowing other developers to use my library if they really want to.
As for the difference, I am not sure I was unaware that there was another plugin by the same name. Although from a quick glance at your link and on spigot, the other developer has deprecated the project and will no longer be updating it so I may look into implementing the functions of his older library so plugins that used to hook into the other SQibrary can hook into this one instead without any errors.
What's the difference between this plugin and the other SQ Library? Does this plugin completely replace the other one? Will plugins that depend on the other SQ Library work with only this one installed?
The other SQ Library: http://dev.bukkit.org/bukkit-plugins/sqlibrary/