Skip to content

Commit 198e547

Browse files
committed
Add apis
1 parent 0fea664 commit 198e547

File tree

4 files changed

+735
-1
lines changed

4 files changed

+735
-1
lines changed

en/api/database.md

+264
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
# Database API Reference
2+
3+
## Repository Interface
4+
5+
The `Repository` interface provides methods for executing database operations.
6+
7+
### Interface Definition
8+
9+
```java
10+
public interface Repository {
11+
// Query methods
12+
List<Row> query(String sql, Object... parameters) throws SQLException;
13+
Row queryOne(String sql, Object... parameters) throws SQLException;
14+
15+
// Update methods
16+
int update(String sql, Object... parameters) throws SQLException;
17+
int[] batchUpdate(String sql, List<Object[]> parametersList) throws SQLException;
18+
19+
// Execute methods
20+
boolean execute(String sql, Object... parameters) throws SQLException;
21+
22+
// Transaction methods
23+
void begin() throws SQLException;
24+
void commit() throws SQLException;
25+
void rollback() throws SQLException;
26+
27+
// Connection management
28+
Connection getConnection() throws SQLException;
29+
void close() throws SQLException;
30+
}
31+
```
32+
33+
## DatabaseOperator
34+
35+
The `DatabaseOperator` class provides a convenient way to perform database operations.
36+
37+
### Class Definition
38+
39+
```java
40+
public class DatabaseOperator implements AutoCloseable {
41+
// Constructors
42+
public DatabaseOperator();
43+
public DatabaseOperator(String database);
44+
public DatabaseOperator(Connection connection);
45+
46+
// Query methods
47+
public ResultSet query(String sql) throws SQLException;
48+
public ResultSet executeQuery(PreparedStatement statement) throws SQLException;
49+
50+
// Update methods
51+
public int update(String sql) throws SQLException;
52+
public int executeUpdate(PreparedStatement statement) throws SQLException;
53+
54+
// Execute methods
55+
public boolean execute(String sql) throws SQLException;
56+
public boolean execute(PreparedStatement statement) throws SQLException;
57+
58+
// Prepared statement
59+
public PreparedStatement preparedStatement(String sql, Object[] parameters) throws SQLException;
60+
61+
// Transaction methods
62+
public Savepoint beginTransaction() throws SQLException;
63+
public void commitTransaction() throws SQLException;
64+
public void rollbackTransaction() throws SQLException;
65+
public void rollbackTransaction(Savepoint savepoint) throws SQLException;
66+
public Savepoint createSavepoint(String name) throws SQLException;
67+
public void releaseSavepoint(Savepoint savepoint) throws SQLException;
68+
public boolean isInTransaction() throws SQLException;
69+
70+
// SQL injection protection
71+
public void enableSafeCheck();
72+
public void disableSafeCheck();
73+
74+
// Resource management
75+
public void close() throws SQLException;
76+
}
77+
```
78+
79+
## AbstractData
80+
81+
The `AbstractData` class provides a base class for object-relational mapping.
82+
83+
### Class Definition
84+
85+
```java
86+
public abstract class AbstractData {
87+
// CRUD operations
88+
public void append() throws ApplicationException;
89+
public void update() throws ApplicationException;
90+
public void delete() throws ApplicationException;
91+
public void save() throws ApplicationException;
92+
93+
// Query operations
94+
public void findOneById() throws ApplicationException;
95+
public <T extends AbstractData> List<T> findAll() throws ApplicationException;
96+
public <T extends AbstractData> List<T> findWhere(String condition, Object... parameters) throws ApplicationException;
97+
public <T extends AbstractData> T findOne(String condition, Object... parameters) throws ApplicationException;
98+
99+
// Count operations
100+
public long count() throws ApplicationException;
101+
public long countWhere(String condition, Object... parameters) throws ApplicationException;
102+
103+
// Utility methods
104+
public String getTableName();
105+
public String getIdentifierName();
106+
public Object getIdentifierValue();
107+
public void setIdentifierValue(Object value);
108+
}
109+
```
110+
111+
## Row Interface
112+
113+
The `Row` interface provides methods for accessing data from query results.
114+
115+
### Interface Definition
116+
117+
```java
118+
public interface Row {
119+
// Get methods
120+
String getString(String columnName);
121+
int getInt(String columnName);
122+
long getLong(String columnName);
123+
double getDouble(String columnName);
124+
boolean getBoolean(String columnName);
125+
Date getDate(String columnName);
126+
Time getTime(String columnName);
127+
Timestamp getTimestamp(String columnName);
128+
Object getObject(String columnName);
129+
130+
// Check methods
131+
boolean isNull(String columnName);
132+
boolean hasColumn(String columnName);
133+
134+
// Column information
135+
Set<String> getColumnNames();
136+
}
137+
```
138+
139+
## Type Enum
140+
141+
The `Type` enum provides factory methods for creating repositories for different database types.
142+
143+
### Enum Definition
144+
145+
```java
146+
public enum Type {
147+
MySQL,
148+
SQLite,
149+
H2,
150+
MSSQL,
151+
PostgreSQL,
152+
Oracle;
153+
154+
public Repository createRepository();
155+
}
156+
```
157+
158+
## Example Usage
159+
160+
### Basic Query
161+
162+
```java
163+
try (DatabaseOperator operator = new DatabaseOperator()) {
164+
ResultSet results = operator.query("SELECT * FROM users WHERE id = 1");
165+
166+
if (results.next()) {
167+
String name = results.getString("name");
168+
String email = results.getString("email");
169+
System.out.println("User: " + name + " (" + email + ")");
170+
}
171+
}
172+
```
173+
174+
### Parameterized Query
175+
176+
```java
177+
try (DatabaseOperator operator = new DatabaseOperator()) {
178+
PreparedStatement stmt = operator.preparedStatement(
179+
"SELECT * FROM users WHERE email = ?",
180+
new Object[]{"[email protected]"}
181+
);
182+
183+
ResultSet results = operator.executeQuery(stmt);
184+
185+
while (results.next()) {
186+
int id = results.getInt("id");
187+
String name = results.getString("name");
188+
System.out.println("User ID: " + id + ", Name: " + name);
189+
}
190+
}
191+
```
192+
193+
### Transaction
194+
195+
```java
196+
try (DatabaseOperator operator = new DatabaseOperator()) {
197+
operator.beginTransaction();
198+
199+
try {
200+
operator.update("UPDATE accounts SET balance = balance - 100 WHERE id = 1");
201+
operator.update("UPDATE accounts SET balance = balance + 100 WHERE id = 2");
202+
203+
operator.commitTransaction();
204+
} catch (Exception e) {
205+
operator.rollbackTransaction();
206+
throw e;
207+
}
208+
}
209+
```
210+
211+
### Object-Relational Mapping
212+
213+
```java
214+
// Define a model class
215+
public class User extends AbstractData {
216+
private int id;
217+
private String name;
218+
private String email;
219+
220+
// Getters and setters
221+
// ...
222+
}
223+
224+
// Create a new user
225+
User user = new User();
226+
user.setName("John Doe");
227+
user.setEmail("[email protected]");
228+
user.append();
229+
230+
// Find a user by ID
231+
User foundUser = new User();
232+
foundUser.setId(1);
233+
foundUser.findOneById();
234+
235+
// Update a user
236+
foundUser.setName("Jane Doe");
237+
foundUser.update();
238+
239+
// Delete a user
240+
foundUser.delete();
241+
242+
// Find all users
243+
List<User> allUsers = new User().findAll();
244+
245+
// Find users with a condition
246+
List<User> filteredUsers = new User().findWhere("name LIKE ?", "%Doe%");
247+
```
248+
249+
## Best Practices
250+
251+
1. **Resource Management**: Always use try-with-resources to ensure proper closure of database resources.
252+
253+
2. **Parameterized Queries**: Use parameterized queries to prevent SQL injection.
254+
255+
3. **Transactions**: Use transactions for operations that require atomicity.
256+
257+
4. **Error Handling**: Implement proper error handling for database operations.
258+
259+
5. **Connection Pooling**: Configure appropriate connection pool settings for your application's needs.
260+
261+
## Related APIs
262+
263+
- [Configuration API](configuration.md)
264+
- [Application API](application.md)

en/database.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public String getBook(Integer id, Request request, Response response) {
515515
book.setId(id);
516516

517517
// Find the book by ID
518-
book.find();
518+
book.findOneById();
519519

520520
// Set content type to JSON
521521
response.headers().add(Header.CONTENT_TYPE.set("application/json"));

0 commit comments

Comments
 (0)