Skip to content

Repository files navigation

sqlava logo

CodeFactor

SQLava

Streamline MySQL interactions with SQLava, the beginner-friendly query builder for Java!

Effortlessly construct complex queries with its intuitive syntax, designed for all skill levels

✨ Key Features:

  • Intuitive Query Building: Craft elegant queries with ease, even without extensive SQL knowledge.
  • Asynchronous Excellence: Experience lightning-fast performance with asynchronous handling of connections and queries.
  • Beginner-Friendly: Dive in without hesitation, thanks to its clear and concise syntax.
  • Seamless Integration: Effortlessly incorporate SQLava into your Java projects.

Installation

If you're using maven or gradle as a dependency manager then just follow the steps below. Otherwise, just download the jar and add it as an artifact dependency.

Adding the repository

Maven
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Gradle
repositories {
mavenCentral()
maven {
url 'https://jitpack.io'
}
}

Implementing the dependency

Maven
<dependency>
<groupId>com.github.Cobeine</groupId>
<artifactId>SQLava</artifactId>
<version>{VERSION}</version>
</dependency>
Gradle
dependencies {
implementation 'com.github.Cobeine:SQLava:{VERSION}'//Please note that my library is dependant on Hikrai 5.0.1 or above//So, make sure to implement HikariCP in your project.
}

Examples

Connection Examples:

publicclassTest {
/** * Connection example * for driver classes @see me.cobeine.sqlava.connection.presets.MysqlJDBCDriverPresets; */privateMySQLConnectionconnection;
publicvoidexamples() {
varurl = JdbcUrlBuilder.newBuilder()
.host("host")
.port(3306)
.setAuto_reconnect(true)
.database("database")
.build();
CredentialsRecordmysql_creds = CredentialsRecord.builder()
.add(BasicMySQLCredentials.USERNAME,"username")
.add(BasicMySQLCredentials.PASSWORD,"password")
.add(BasicMySQLCredentials.DATABASE,"database")
.add(BasicMySQLCredentials.PORT,3306)
.add(BasicMySQLCredentials.POOL_SIZE,8)
.add(BasicMySQLCredentials.JDBC_URL, url)
.build();
connection = newMySQLConnection(mysql_creds);
connection.connect(result -> {
if (result.getException().isPresent()) {
result.getException().get().printStackTrace();
return;
}
//successfully connectedconnection.getTableCommands().createTable(newExampleTable(),tableResult -> {
if (tableResult.getException().isPresent()) {
tableResult.getException().get().printStackTrace();
return;
}
//successfully created table
});
});
PreparedQueryquery = connection.prepareStatement("SELECT * FROM example WHERE uuid=test");
query = connection.prepareStatement(Query.select("example").where("uuid", "test"));
query.executeQuery();
query.executeQueryAsync(result -> {
if (result.getException().isPresent()) {
result.getException().get().printStackTrace();
return;
}
//successfully executed queryresult.getResult().ifPresent(resultSet -> {
//...
});
});
Queryupdate = Query.update("example").set("uuid","test").where("id",1).and("uuid","test2");
update = Query.update("example").set("uuid").where("id").where("uuid");
connection.prepareStatement(update)
.setParameter(1,"test")
.setParameter(2,1)
.setParameter(3,"test2").executeUpdateAsync(result -> {
if (result.getException().isPresent()) {
result.getException().get().printStackTrace();
return;
}
//successfully executed query 
});
}
voidmethods(){
connection.connect();
connection.getLogger();
connection.getPool();
connection.getCredentialsRecord();
connection.closeConnection();
connection.prepareStatement(); // with args;JdbcUrlBuilder.newBuilder()
.host("host")
.port(3306)
.database("test")
.setAuto_reconnect(true)
.build();
}
}

Table Example

publicclassExampleextendsTable {
publicExample() {
super("example");
addColumns(
Column.of("id", ColumnType.INT).settings(ColumnSettings.AUTO_INCREMENT, ColumnSettings.UNIQUE),
Column.of("uuid", ColumnType.TEXT).settings(ColumnSettings.NOT_NULL, ColumnSettings.UNIQUE).defaultValue("none"),
Column.of("name", ColumnType.VARCHAR).size(64).settings(ColumnSettings.NOT_NULL, ColumnSettings.UNIQUE).defaultValue("none"),
Column.of("rank", ColumnType.VARCHAR).size(64).settings(ColumnSettings.NOT_NULL).defaultValue("DEFAULT")
);
setPrimaryKey("id");
}
}

Query examples

publicclassExamples {
publicvoidexamples() {
Tabletable = newTable();
connection.getTableCommands().createTable(table);
connection.prepareStatement(
Query.select("test").where("id").and("uuid"))
.setParameter(1, 5)
.setParameter(2, UUID.randomUUID())
.executeQueryAsync(result ->
result.executeIfPresent(resultSet -> {
resultSet.next(); // or hasNext depending on youintid = resultSet.getInt("id"); //examples
}).orElse(exception -> {
if (exception != null)
exception.printStackTrace();
}).apply()
);
//orQueryselectQuery = Query.select("table").where("uuid").and("id");
PreparedQueryquery = selectQuery.prepareStatement(connection);
query.setParameter(1, UUID.randomUUID()).setParameter(2, 1);
try (ResultSetset = query.executeQuery()) {
set.getInt("id");//etc
} catch (SQLExceptione) {
e.printStackTrace();
}
}
}

Contributing

Before contributing to this project, please note the following:

- Test your changes: Ensure that any changes you make to the project have been thoroughly tested before submitting them.
- Provide documentation: If you are adding new features or functionality, consider providing clear and concise documentation to help other contributors understand how to use them.
- Follow established standards: Adhere to established coding standards and best practices to ensure that your contributions are consistent with the rest of the project.
- Submit changes appropriately: Follow the project's preferred method of submitting changes, whether that is through pull requests, commits, or another method specified by the project owner.

Thanks for reading & reviewing my project!

Special thanks to @AkramLZ for inspiration!

About

Streamline MySQL interactions with SQLava, a beginner-friendly query builder for Java.

Resources

Stars

10 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages