Skip to content

Commit

Permalink
Added SerializableBaseEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinAlpert committed Jan 12, 2020
1 parent 2929cbf commit 69fd69f
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 114 deletions.
22 changes: 0 additions & 22 deletions .github/workflows/deploy-maven.yml

This file was deleted.

108 changes: 108 additions & 0 deletions .github/workflows/post-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: Post-push actions

# Run workflow only on commits to `master`
on:
push:
branches:
- master

jobs:
maven_deploy:
name: Deploy to Maven
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@v1

- uses: actions/setup-java@v1
with:
java-version: '13'

- name: Release to Central Repository
uses: samuelmeuli/action-maven-publish@v0.1.0
with:
gpg_private_key: ${{ secrets.gpg_private_key }}
gpg_passphrase: ${{ secrets.gpg_passphrase }}
nexus_username: ${{ secrets.nexus_username }}
nexus_password: ${{ secrets.nexus_password }}

github_deploy:
name: Deploy to GitHub Packages
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@v1

- name: Setup JDK 13
uses: actions/setup-java@v1
with:
java-version: '13'

- name: Release to GitHub Package Registry
env:
github_token: ${{ secrets.GITHUB_TOKEN }}
gpg_private_key: ${{ secrets.gpg_private_key }}
gpg_passphrase: ${{ secrets.gpg_passphrase }}
run: |
mkdir ~/.m2
echo "${gpg_private_key}" | gpg --batch --import
echo "<settings><servers><server><id>github</id><username>CollinAlpert</username><password>${github_token}</password></server></servers></settings>" > ~/.m2/settings.xml
mvn clean deploy -B -e -Dmaven.wagon.http.pool=false -DaltDeploymentRepository=github::default::https://maven.pkg.github.com/CollinAlpert/TestMaven -Dgpg.passphrase=${gpg_passphrase}
github_release:
name: Create GitHub release
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@v1

- name: Setup JDK 13
uses: actions/setup-java@v1
with:
java-version: '13'

- name: Build project
run: mvn -B clean package

- name: Get project infos
id: get-project-infos
run: echo "::set-output name=maven_version::$(mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q)" && echo "::set-output name=maven_artifactId::$(mvn -Dexec.executable='echo' -Dexec.args='${project.artifactId}' --non-recursive exec:exec -q)"

- name: Create Release
id: create_release
uses: actions/create-release@v1.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.get-project-infos.outputs.maven_version }}
release_name: ${{ steps.get-project-infos.outputs.maven_version }}

- name: Upload JAR asset
uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./target/${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}.jar
asset_name: ${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}.jar
asset_content_type: application/java-archive

- name: Upload JavaDoc asset
uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./target/${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}-javadoc.jar
asset_name: ${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}-javadoc.jar
asset_content_type: application/java-archive

- name: Upload Sources asset
uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./target/${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}-sources.jar
asset_name: ${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}-sources.jar
asset_content_type: application/java-archive
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Include the Maven artifact:
<dependency>
<groupId>com.github.collinalpert</groupId>
<artifactId>java2db</artifactId>
<version>5.2</version>
<version>5.2.1</version>
</dependency>
```
Or include the [JAR](https://github.com/CollinAlpert/Java2DB/releases/latest) in your project.
Expand Down Expand Up @@ -104,7 +104,7 @@ Every service *must* extend `BaseService`.
That's it! Now we can get data from the database using the services using simple methods like `getById` and so on.\
As you can see from the example, custom methods can be defined in the respective service using the `getSingle` or `getMultiple` methods provided by the `BaseService`.

The last thing you need to do is give Java2DB access to your database. Set the static variables `HOST`, `DATABASE`, `USERNAME`, `PASSWORD` and optionally `PORT` of the `DBConnection` class to achieve possibility of connection.
The last thing you need to do is give Java2DB access to your database. Set the static variables `HOST`, `DATABASE`, `USERNAME`, `PASSWORD`, `TIMEOUT` (in seconds) and optionally `PORT` of the `DBConnection` class to achieve possibility of connection.

## Features

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.collinalpert</groupId>
<artifactId>java2db</artifactId>
<version>5.2</version>
<version>5.2.1</version>
<packaging>jar</packaging>

<name>Java2DB</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.collinalpert.java2db.database;

import com.github.collinalpert.java2db.exceptions.ConnectionFailedException;
import com.github.collinalpert.java2db.modules.LoggingModule;
import com.mysql.cj.exceptions.CJCommunicationsException;
import com.mysql.cj.jdbc.exceptions.CommunicationsException;

Expand All @@ -14,15 +13,9 @@

/**
* @author Collin Alpert
* @see <a href="https://github.com/CollinAlpert/APIs/blob/master/de/collin/DBConnection.java">GitHub</a>
*/
public class DBConnection implements Closeable {

/**
* The logger used to log queries and messages to the console.
*/
private static final LoggingModule logger;

/**
* Specifies the hostname/ip address of the database.
*/
Expand All @@ -49,25 +42,26 @@ public class DBConnection implements Closeable {
*/
public static int PORT = 3306;

/**
* Specifies the login timeout to the database in seconds.
*/
public static int TIMEOUT = 5;

/**
* Constant which determines if the queries generated by Java2DB will be logged in the console.
*/
public static boolean LOG_QUERIES = true;

static {
DriverManager.setLoginTimeout(5);
logger = LoggingModule.getInstance();
}

private Connection connection;
private boolean isConnectionValid;

public DBConnection() {
try {
var connectionString = String.format("jdbc:mysql://%s:%d/%s?serverTimezone=UTC", HOST, PORT, DATABASE);
var connectionString = String.format("jdbc:mysql://%s:%d/%s?rewriteBatchedStatements=true", HOST, PORT, DATABASE);
Class.forName("com.mysql.cj.jdbc.Driver");
System.setProperty("user", USERNAME);
System.setProperty("password", PASSWORD);
DriverManager.setLoginTimeout(TIMEOUT);
connection = DriverManager.getConnection(connectionString, System.getProperties());
isConnectionValid = true;
} catch (CJCommunicationsException | CommunicationsException e) {
Expand Down Expand Up @@ -97,10 +91,11 @@ public boolean isValid() {
* @throws SQLException if the query is malformed or cannot be executed.
*/
public ResultSet execute(String query) throws SQLException {
Statement statement = this.connection.createStatement();
logger.log(query);
var statement = this.connection.createStatement();
log(query);
var set = statement.executeQuery(query);
statement.closeOnCompletion();

return set;
}

Expand All @@ -118,7 +113,7 @@ public ResultSet execute(String query, Object... params) throws SQLException {
statement.setObject(i + 1, params[i]);
}

logger.log(query);
log(query);
var set = statement.executeQuery();
statement.closeOnCompletion();
return set;
Expand All @@ -133,7 +128,7 @@ public ResultSet execute(String query, Object... params) throws SQLException {
*/
public long update(String query) throws SQLException {
var statement = this.connection.createStatement();
logger.log(query);
log(query);
statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
return updateHelper(statement);
}
Expand All @@ -152,7 +147,7 @@ public long update(String query, Object... params) throws SQLException {
statement.setObject(i + 1, params[i]);
}

logger.log(query);
log(query);
statement.executeUpdate();
return updateHelper(statement);
}
Expand Down Expand Up @@ -198,4 +193,15 @@ public void close() {
this.isConnectionValid = false;
}
}

/**
* Prints queries to the console, while considering the {@link DBConnection#LOG_QUERIES} constant.
*
* @param text The message to print.
*/
private void log(String text) {
if (DBConnection.LOG_QUERIES) {
System.out.println(text);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.github.collinalpert.java2db.entities;

import java.io.Serializable;

/**
* @author Collin Alpert
*/
public class SerializableBaseEntity extends BaseEntity implements Serializable {

private long id;

public long getId() {
return id;
}

/**
* This setter only exists for frameworks like Spring, where a form needs to set this id.
* It is <b>greatly</b> discouraged from using this setter directly and it's effects will not be considered with any of the CRUD operations.
*
* @param id The id of the entity.
*/
@Deprecated
public void setId(long id) {
this.id = id;
}

@Override
public String toString() {
return "Id: " + this.id;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof SerializableBaseEntity)) {
return false;
}

SerializableBaseEntity that = (SerializableBaseEntity) o;

return this.id == that.id;
}

@Override
public int hashCode() {
return (int) (this.id ^ (this.id >>> 32));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,6 @@ public void invalidateCaches() {
public void invalidateCache(String name) {
listCache.getValue().invalidate(name);
streamCache.getValue().invalidate(name);
arrayCache.getValue().invalidate(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public final PaginationResult<T> orderBy(SqlFunction<T, ?>... orderFunctions) {
public final PaginationResult<T> orderBy(OrderTypes orderType, SqlFunction<T, ?>... orderFunctions) {
this.orderFunctions = orderFunctions;
this.orderType = orderType;

return this;
}
}

0 comments on commit 69fd69f

Please sign in to comment.