You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+76-21Lines changed: 76 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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:
3
3
#### Simplified Database Operations
4
4
- 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).
5
6
#### Compatibility and Flexibility
6
7
- 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.
7
8
#### Performance Optimizations
@@ -18,6 +19,18 @@ Some use cases that we optimize the performance:
18
19
## Some advantage features
19
20
#### Decimal
20
21
- 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.
21
34
#### Query Template (SQL Mapper)
22
35
- My batis for GOLANG.
23
36
- 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:
55
68
</mapper>
56
69
```
57
70
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.
62
81
- You can look at the sample at [go-sql-composite-key](https://github.com/go-tutorials/go-sql-composite-key).
63
82
- In this sample, the company_users has 2 primary keys: company_id and user_id
64
83
- 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:
- <b>Conclusion</b>: TheRepository 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 GOSDK 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. KeyConcepts of Pagination:
97
+
- PageSize: 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
+
- PageNumber: 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 LIMIT10OFFSET20).
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 SecureInputHandling
111
+
- See the above output, you can see we mapJSON field name to database column name: username with username, dateOfBirth with date_of_birth
112
+
73
113
#### SearchRepository
74
114
The flow for search/paging:
75
115
- Build the dynamic query
76
116
- Build the paging query from dynamic query (it specified forOracle, Postgres, MySQL, MSSQL, SQLite)
77
117
- Query data and map to array of struct
78
118
- Build the count query
79
119
- 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 StreamWriter](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 StreamInserter](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 StreamUpdater](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.
- Sample is at [go-sql-sample](https://github.com/source-code-template/go-sql-sample).
135
+
#### ActionLog
136
+
- SaveActionLog with dynamic database design
137
+
#### PasscodeAdapter
138
+
139
+
## Detailed samples of benefits
140
+
80
141
#### Dynamic query builder
81
142
- 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.
82
143
<table><thead><tr><td>
@@ -374,21 +435,15 @@ func (s *UserUseCase) Create(
374
435
```
375
436
</td></tr></tbody></table>
376
437
377
-
#### For batch job
378
-
- Inserter
379
-
- Updater
380
-
- Writer
381
-
- StreamInserter
382
-
- StreamUpdater
383
-
- StreamWriter
384
-
- BatchInserter
385
-
- BatchUpdater
386
-
- BatchWriter
387
-
#### HealthCheck
388
-
#### PasscodeAdapter
389
-
#### ActionLog
390
-
- SaveActionLog with dynamic database design
391
-
#### FieldLoader
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 MybatisforGO 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).
392
447
## Installation
393
448
Please make sure to initialize a Go module before installing core-go/sql:
0 commit comments