Skip to content

Commit 63b0584

Browse files
committed
Update readme
1 parent fbd3078 commit 63b0584

1 file changed

Lines changed: 76 additions & 21 deletions

File tree

README.md

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# sql
2-
- It is the library to work with "database/sql" of GO SDK to offer some advantages:
1+
# SQL
2+
It is the library to work with "database/sql" of GO SDK to offer some advantages:
33
#### Simplified Database Operations
44
- Simplify common database operations, such as CRUD (Create, Read, Update, Delete) operations, transactions, and batch processing, by providing high-level abstractions and utilities.
5+
- The sample is at [go-sql-sample](https://github.com/source-code-template/go-sql-sample).
56
#### Compatibility and Flexibility
67
- It is compatible with various SQL databases such as [Postgres](github.com/lib/pq), [My SQL](github.com/go-sql-driver/mysql), [MS SQL](https://github.com/denisenkom/go-mssqldb), [Oracle](https://github.com/godror/godror), [SQLite](https://github.com/mattn/go-sqlite3), offer flexibility in terms of database driver selection, and query building capabilities.
78
#### Performance Optimizations
@@ -18,6 +19,18 @@ Some use cases that we optimize the performance:
1819
## Some advantage features
1920
#### Decimal
2021
- Support decimal, which is useful for currency
22+
#### Query Builders
23+
- Utilities to build dynamic SQL queries programmatically.
24+
- Support for common SQL operations (SELECT, INSERT, UPDATE, DELETE).
25+
- Support insert or update (upsert) operations, support Oracle, Postgres, My SQL, MS SQL, SQLite
26+
#### Data Mapping:
27+
- Functions to map SQL rows to Go structs.
28+
- Benefits:
29+
- Simplifies the process of converting database rows into Go objects.
30+
- Reduces repetitive code and potential errors in manual data mapping.
31+
- Enhances code readability and maintainability
32+
#### Transaction Management:
33+
- Support for database transactions, including commit and rollback.
2134
#### Query Template (SQL Mapper)
2235
- My batis for GOLANG.
2336
- Project sample is at [go-admin](https://github.com/project-samples/go-admin). Mybatis file is here [query.xml](https://github.com/project-samples/go-admin/blob/main/configs/query.xml)
@@ -55,10 +68,16 @@ Some use cases that we optimize the performance:
5568
</mapper>
5669
```
5770

58-
#### Generic Repository (CRUD repository)
59-
- It is like [CrudRepository](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html) of Spring, which promotes rapid development and consistency across applications.
60-
- While it provides many advantages, such as reducing boilerplate code and ensuring transactional integrity, it also offers flexibility and control over complex queries, because it uses "database/sql" at GO SDK level.
61-
- Especially, it also supports composite primary key.
71+
#### Generic CRUD Repository
72+
[Repository](https://github.com/core-go/sql/blob/main/repository/repository.go) is like [CrudRepository](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html) of Spring, it provides these advantages:
73+
- <b>Simplicity</b>: provides a set of standard CRUD (Create, Read, Update, Delete) operations out of the box, reducing the amount of boilerplate code developers need to write.
74+
- Especially, it provides "Save" method, to build an insert or update statement, specified for Oracle, MySQL, MS SQL, Postgres, SQLite.
75+
- <b>Consistency</b>: By using Repository, the code follows a consistent pattern for data access across the application, making it easier to understand and maintain.
76+
- <b>Rapid Development</b>: reducing boilerplate code and ensuring transactional integrity.
77+
- <b>Flexibility</b>: offers flexibility and control over complex queries, because it uses "database/sql" at GO SDK level.
78+
- <b>Type Safety</b>: being a generic interface, it provides type-safe access to the entity objects, reducing the chances of runtime errors.
79+
- <b>Learning Curve</b>: it supports utilities at GO SDK level. So, a developer who works with "database/sql" at GO SDK can quickly understand and use it.
80+
- <b>Composite primary key</b>: it supports composite primary key.
6281
- You can look at the sample at [go-sql-composite-key](https://github.com/go-tutorials/go-sql-composite-key).
6382
- In this sample, the company_users has 2 primary keys: company_id and user_id
6483
- You can define a GO struct, which contains 2 fields: CompanyId and UserId
@@ -70,13 +89,55 @@ Some use cases that we optimize the performance:
7089
UserId string `json:"userId" gorm:"column:user_id;primary_key"`
7190
}
7291
```
92+
- <b>Conclusion</b>: The Repository offers a straightforward way to implement basic CRUD operations, promoting rapid development and consistency across applications. While it provides many advantages, such as reducing boilerplate code and ensuring transactional integrity, it also it also offers flexibility and control over complex queries, because it uses "database/sql" at GO SDK level.
93+
- <b>Samples</b>: The sample is at [go-sql-generic-sample](https://github.com/source-code-template/go-sql-generic-sample). The composite key sample is at [go-sql-composite-key](https://github.com/go-tutorials/go-sql-composite-key).
94+
#### Filtering, Pagination and Sorting
95+
- <b>Filtering</b> is the process of narrowing down a dataset based on specific criteria or conditions. This allows users to refine the results to match their needs, making it easier to find relevant data.
96+
- <b>Pagination</b> is the process of dividing a large dataset into smaller pages. Key Concepts of Pagination:
97+
- Page Size: The number of items displayed on each page.
98+
- Example: If you have 100 items and a page size of 10, there will be 10 pages in total.
99+
- Page Number: The current page being viewed.
100+
- Example: If you are on page 3 with a page size of 10, items 21 to 30 will be displayed.
101+
- Offset and Limit:
102+
- Offset: The number of items to skip before starting to collect the result set.
103+
- Limit: The maximum number of items to return.
104+
- Example: For page 3 with a page size of 10, the offset would be 20, and the limit would be 10 (SELECT * FROM items LIMIT 10 OFFSET 20).
105+
- <b>Sorting</b>: build a dynamic SQL with sorting:
106+
- Build multi-column sorting based on dynamic parameters:
107+
- Input: sort=phone,-id,username,-dateOfBirth
108+
- Output: order by phone, id desc, username, date_of_birth desc
109+
- You can define your own format, and inject your own function to map
110+
- Safe and Secure Input Handling
111+
- See the above output, you can see we map JSON field name to database column name: username with username, dateOfBirth with date_of_birth
112+
73113
#### Search Repository
74114
The flow for search/paging:
75115
- Build the dynamic query
76116
- Build the paging query from dynamic query (it specified for Oracle, Postgres, My SQL, MS SQL, SQLite)
77117
- Query data and map to array of struct
78118
- Build the count query
79119
- Count the total records for paging
120+
121+
#### For batch job
122+
- [SQL Writer](https://github.com/core-go/sql/blob/main/writer/writer.go): to insert or update data
123+
- [SQL Inserter](https://github.com/core-go/sql/blob/main/writer/inserter.go): to insert data
124+
- [SQL Updater](https://github.com/core-go/sql/blob/main/writer/updater.go): to update data
125+
- [SQL Stream Writer](https://github.com/core-go/sql/blob/main/writer/stream_writer.go): to insert or update data. When you write data, it keeps the data in the buffer, it does not write data. It just writes data when flush.
126+
- [SQL Stream Inserter](https://github.com/core-go/sql/blob/main/writer/stream_inserter.go): to insert data. When you write data, it keeps the data in the buffer, it does not write data. It just writes data when flush. Especially, we build 1 single SQL statement to improve the performance.
127+
- [SQL Stream Updater](https://github.com/core-go/sql/blob/main/writer/stream_updater.go): to update data. When you write data, it keeps the data in the buffer, it does not write data. It just writes data when flush.
128+
- [Batch Inserter](https://github.com/core-go/sql/blob/main/batch/batch_inserter.go): to insert a batch of records. It builds a single SQL statement to improve the performance, specified for Oracle, Postgres, My SQL, MS SQL, SQLite.
129+
- [Batch Updater](https://github.com/core-go/sql/blob/main/batch/batch_updater.go)
130+
- [Batch Writer](https://github.com/core-go/sql/blob/main/batch/batch_writer.go)
131+
132+
#### Health Check
133+
- Monitors the health of database connections
134+
- Sample is at [go-sql-sample](https://github.com/source-code-template/go-sql-sample).
135+
#### Action Log
136+
- Save Action Log with dynamic database design
137+
#### Passcode Adapter
138+
139+
## Detailed samples of benefits
140+
80141
#### Dynamic query builder
81142
- Look at this sample [user](https://github.com/source-code-template/go-sql-sample/blob/main/internal/user/user.go), you can see it automatically build a dynamic query for serach.
82143
<table><thead><tr><td>
@@ -374,21 +435,15 @@ func (s *UserUseCase) Create(
374435
```
375436
</td></tr></tbody></table>
376437

377-
#### For batch job
378-
- Inserter
379-
- Updater
380-
- Writer
381-
- StreamInserter
382-
- StreamUpdater
383-
- StreamWriter
384-
- BatchInserter
385-
- BatchUpdater
386-
- BatchWriter
387-
#### Health Check
388-
#### Passcode Adapter
389-
#### Action Log
390-
- Save Action Log with dynamic database design
391-
#### Field Loader
438+
## Summary of Samples
439+
- Utilities to simplify common database operations: the sample is at [go-sql-sample](https://github.com/source-code-template/go-sql-sample).
440+
- The sample of generic CRUD repository is at [go-sql-generic-sample](https://github.com/source-code-template/go-sql-generic-sample).
441+
- The composite key sample is at [go-sql-composite-key](https://github.com/go-tutorials/go-sql-composite-key).
442+
- The sample of dynamic query builder is at [user](https://github.com/source-code-template/go-sql-sample/blob/main/internal/user/user.go)
443+
- The sample of Mybatis for GO is at [go-admin](https://github.com/project-samples/go-admin). Mybatis file is at [query.xml](https://github.com/project-samples/go-admin/blob/main/configs/query.xml).
444+
#### Batch processing samples
445+
- The sample of export data is at [go-sql-export](https://github.com/project-samples/go-sql-export).
446+
- The sample of import data is at [go-sql-import](https://github.com/project-samples/go-sql-import).
392447
## Installation
393448
Please make sure to initialize a Go module before installing core-go/sql:
394449

0 commit comments

Comments
 (0)