Skip to content

Commit 6e68d7f

Browse files
committed
Improve README.md
1 parent 56c277b commit 6e68d7f

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

README.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,151 @@
11
# ForestDatabase
2+
3+
ForestDatabase is a Java library for SpigotMC projects which
4+
allows automated SQL queries generation using custom entity annotations. The library supports database system PostgreSQL.
5+
6+
## Table of contents
7+
8+
* [Getting started](#getting-started)
9+
* [Setting up the API](#setting-up-the-api)
10+
* [Annotations](#annotations)
11+
* [Accessing the database](#accessing-the-database)
12+
* [License](#license)
13+
14+
## Getting started
15+
16+
[![badge](https://jitpack.io/v/ForestTechMC/ForestDatabase.svg)](https://jitpack.io/#ForestTechMC/ForestDatabase)
17+
18+
Replace **VERSION** with the version you want to use. The latest is always recommended.
19+
20+
<details>
21+
<summary>Maven</summary>
22+
23+
```xml
24+
<repositories>
25+
<repository>
26+
<id>jitpack.io</id>
27+
<url>https://jitpack.io</url>
28+
</repository>
29+
</repositories>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>com.github.ForestTechMC</groupId>
34+
<artifactId>ForestDatabase</artifactId>
35+
<version>1.0.8</version>
36+
</dependency>
37+
</dependencies>
38+
```
39+
</details>
40+
41+
<details>
42+
<summary>Gradle</summary>
43+
44+
```groovy
45+
allprojects {
46+
repositories {
47+
maven { url 'https://jitpack.io' }
48+
}
49+
}
50+
51+
dependencies {
52+
implementation 'com.github.ForestTechMC:ForestDatabase:VERSION'
53+
}
54+
```
55+
</details>
56+
57+
## Setting up the API
58+
59+
To use ForestDatabase, its API needs to be initialized first. It is recommended to do so in `onEnable` method.
60+
61+
```java
62+
@Override
63+
public void onEnable() {
64+
DatabaseAPI databaseAPI = new DatabaseAPI(this);
65+
databaseAPI.setup();
66+
}
67+
```
68+
69+
After initial setup, database object needs to be registered to the API.
70+
71+
```java
72+
HikariDatabase hikariDatabase = new HikariDatabase(
73+
"localhost:5432",
74+
"my_database",
75+
"username",
76+
"password");
77+
78+
databaseAPI.addDatabase("database_id", hikariDatabase);
79+
```
80+
81+
All connections shall be closed using `databaseAPI#closeAll()` call.
82+
83+
## Annotations
84+
85+
To make entity be recognized by the ForestDatabase, it needs to be annotated with special annotations and must include empty constructor.
86+
87+
Each entity needs to be annotated with `@DatabaseEntity` annotation. Each field which shall be recognized by ForestDatabase shall include `@Column` annotation.
88+
89+
```java
90+
import cz.foresttech.database.annotation.*;
91+
92+
@DatabaseEntity
93+
public class Car {
94+
95+
@Column
96+
@PrimaryKey
97+
@AutoIncrement
98+
private int id;
99+
100+
@Column
101+
@Text(customLength = 50)
102+
private String brandName;
103+
104+
@Column
105+
private double price;
106+
107+
@Column
108+
@NullableColumn
109+
@Text
110+
private String description;
111+
112+
// Will be ignored by ForestDatabase
113+
private boolean internalValue;
114+
115+
public Car() {
116+
}
117+
118+
/* .. getters, setter, another constructors ... */
119+
}
120+
```
121+
122+
ForestDatabase will automatically convert class names and field names to snake case, if custom name is not used.
123+
124+
```java
125+
@DatabaseEntity(table="custom_table_name")
126+
@Column(key="custom_column_name")
127+
```
128+
129+
## Accessing the database
130+
131+
To access the database and load/edit the data, you can use direct methods on DatabaseAPI instance.
132+
133+
```java
134+
// Creates the table (if not exist)
135+
databaseAPI.createTable("database_id", Car.class);
136+
137+
// Inserts the object data into the database
138+
Car car = new Car();
139+
databaseAPI.insertOrUpdate("database_id", car);
140+
141+
// Removes the object from the database asynchronously
142+
databaseAPI.deleteAsync("database_id", car);
143+
144+
// Retrieves all objects from the database
145+
databaseAPI.findAll("database_id", Car.class).forEach(car -> {
146+
// ... do stuff
147+
});
148+
```
149+
150+
## License
151+
ForestDatabase is licensed under the permissive MIT license. Please see [`LICENSE.txt`](https://github.com/ForestTechMC/ForestRedisAPI/blob/master/LICENSE.txt) for more information.

src/main/java/cz/foresttech/database/DatabaseEntityConvertor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cz.foresttech.database.processor.DatabaseValueProcessor;
55

66
import java.lang.reflect.Field;
7+
import java.lang.reflect.Modifier;
78
import java.sql.Timestamp;
89
import java.util.ArrayList;
910
import java.util.Arrays;
@@ -238,6 +239,9 @@ private <T> String processDeleteConditionScript(Class<T> clazz, T object) throws
238239
StringBuilder values = new StringBuilder();
239240

240241
for (Field field : getDeclaredFields(clazz)) {
242+
if (Modifier.isStatic(field.getModifiers())) {
243+
continue;
244+
}
241245
field.setAccessible(true);
242246

243247
boolean isPrimaryKey = field.isAnnotationPresent(PrimaryKey.class);
@@ -267,6 +271,9 @@ private <T> String getValuesFromField(Class<T> clazz, T object) throws IllegalAc
267271
StringBuilder values = new StringBuilder();
268272

269273
for (Field field : getDeclaredFields(clazz)) {
274+
if (Modifier.isStatic(field.getModifiers())) {
275+
continue;
276+
}
270277
field.setAccessible(true);
271278
Column column = field.getAnnotation(Column.class);
272279
if (column == null) continue;
@@ -287,6 +294,9 @@ private <T> String getColumnsFromField(Class<T> clazz) {
287294
StringBuilder keys = new StringBuilder();
288295

289296
for (Field field : getDeclaredFields(clazz)) {
297+
if (Modifier.isStatic(field.getModifiers())) {
298+
continue;
299+
}
290300
field.setAccessible(true);
291301
Column column = field.getAnnotation(Column.class);
292302
if (column == null) continue;

0 commit comments

Comments
 (0)