Skip to content

Commit fbd3078

Browse files
committed
update readme
1 parent 4cf159d commit fbd3078

File tree

4 files changed

+8
-40
lines changed

4 files changed

+8
-40
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Some use cases that we optimize the performance:
1313
- Props: Because it interacts with DB once, it has a better performance.
1414
#### Reduced Boilerplate Code
1515
- Reduce boilerplate code associated with database interactions, allowing developers to focus more on application logic rather than low-level database handling
16-
- In this [layer architecture sample](https://github.com/source-code-template/go-sql-layer-architecture-sample), you can see we can reduce a lot of source code at [data access layer](https://github.com/source-code-template/go-sql-layer-architecture-sample/blob/main/internal/user/repository/adapter/adapter.go), or you use [generic repository](https://github.com/core-go/sql/blob/main/adapter/adapter.go) to replace all repository source code.
16+
- In this [layer architecture sample](https://github.com/source-code-template/go-sql-sample), you can see we can reduce a lot of source code at [data access layer](https://github.com/source-code-template/go-sql-sample/blob/main/internal/user/repository/adapter/adapter.go), or you use [generic repository](https://github.com/core-go/sql/blob/main/adapter/adapter.go) to replace all repository source code.
1717

1818
## Some advantage features
1919
#### Decimal
@@ -78,7 +78,7 @@ The flow for search/paging:
7878
- Build the count query
7979
- Count the total records for paging
8080
#### Dynamic query builder
81-
- Look at this sample [user](https://github.com/source-code-template/go-sql-layer-architecture-sample/blob/main/internal/user/user.go), you can see it automatically build a dynamic query for serach.
81+
- 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.
8282
<table><thead><tr><td>
8383

8484
```go
@@ -331,7 +331,7 @@ func (s *userService) Create(
331331
```
332332
</td></tr></tbody></table>
333333

334-
- In another example, in [this service layer](https://github.com/source-code-template/go-sql-layer-architecture-sample/blob/main/internal/user/service/usecase.go) and [this data access layer](https://github.com/source-code-template/go-sql-layer-architecture-sample/blob/main/internal/user/repository/adapter/adapter.go), it simplifies transaction handling, at GO SDK level.
334+
- In another example, in [this service layer](https://github.com/source-code-template/go-sql-sample/blob/main/internal/user/service/usecase.go) and [this data access layer](https://github.com/source-code-template/go-sql-sample/blob/main/internal/user/repository/adapter/adapter.go), it simplifies transaction handling, at GO SDK level.
335335
<table><thead><tr><td>
336336

337337
[GO SDK Only](https://github.com/go-tutorials/go-sql-hexagonal-architecture-sample/blob/main/internal/user/service/user_service.go)

dao/writer.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,22 +218,6 @@ func (a *Writer[T]) Patch(ctx context.Context, model map[string]interface{}) (in
218218
}
219219
return rowsAffected, err
220220
}
221-
func handleDuplicate(db *sql.DB, err error) (int64, error) {
222-
x := err.Error()
223-
driver := q.GetDriver(db)
224-
if driver == q.DriverPostgres && strings.Contains(x, "pq: duplicate key value violates unique constraint") {
225-
return 0, nil
226-
} else if driver == q.DriverMysql && strings.Contains(x, "Error 1062: Duplicate entry") {
227-
return 0, nil //mysql Error 1062: Duplicate entry 'a-1' for key 'PRIMARY'
228-
} else if driver == q.DriverOracle && strings.Contains(x, "ORA-00001: unique constraint") {
229-
return 0, nil //mysql Error 1062: Duplicate entry 'a-1' for key 'PRIMARY'
230-
} else if driver == q.DriverMssql && strings.Contains(x, "Violation of PRIMARY KEY constraint") {
231-
return 0, nil //Violation of PRIMARY KEY constraint 'PK_aa'. Cannot insert duplicate key in object 'dbo.aa'. The duplicate key value is (b, 2).
232-
} else if driver == q.DriverSqlite3 && strings.Contains(x, "UNIQUE constraint failed") {
233-
return 0, nil
234-
}
235-
return 0, err
236-
}
237221

238222
func setVersion(vo reflect.Value, versionIndex int) bool {
239223
versionType := vo.Field(versionIndex).Type().String()

repository/repository.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func NewSqlRepositoryWithVersionAndArray[T any, K any](db *sql.DB, tableName str
2929
driver.Valuer
3030
sql.Scanner
3131
}, opts ...func(int) string) (*Repository[T, K], error) {
32-
adapter, err := NewSqlWriterWithVersionAndArray[*T](db, tableName, versionField, toArray, opts...)
32+
repo, err := NewSqlWriterWithVersionAndArray[*T](db, tableName, versionField, toArray, opts...)
3333
if err != nil {
3434
return nil, err
3535
}
@@ -43,7 +43,7 @@ func NewSqlRepositoryWithVersionAndArray[T any, K any](db *sql.DB, tableName str
4343
var k K
4444
kType := reflect.TypeOf(k)
4545
idMap := false
46-
if len(adapter.Keys) > 1 {
46+
if len(repo.Keys) > 1 {
4747
if kType.Kind() == reflect.Map {
4848
idMap = true
4949
} else if kType.Kind() != reflect.Struct {
@@ -55,8 +55,8 @@ func NewSqlRepositoryWithVersionAndArray[T any, K any](db *sql.DB, tableName str
5555
if err != nil {
5656
return nil, err
5757
}
58-
fields := q.BuildFieldsBySchema(adapter.Schema)
59-
return &Repository[T, K]{adapter, fieldsIndex, fields, idMap}, nil
58+
fields := q.BuildFieldsBySchema(repo.Schema)
59+
return &Repository[T, K]{repo, fieldsIndex, fields, idMap}, nil
6060
}
6161
func (a *Repository[T, K]) All(ctx context.Context) ([]T, error) {
6262
var objs []T

repository/writer.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dao
1+
package repository
22

33
import (
44
"context"
@@ -218,22 +218,6 @@ func (a *Writer[T]) Patch(ctx context.Context, model map[string]interface{}) (in
218218
}
219219
return rowsAffected, err
220220
}
221-
func handleDuplicate(db *sql.DB, err error) (int64, error) {
222-
x := err.Error()
223-
driver := q.GetDriver(db)
224-
if driver == q.DriverPostgres && strings.Contains(x, "pq: duplicate key value violates unique constraint") {
225-
return 0, nil
226-
} else if driver == q.DriverMysql && strings.Contains(x, "Error 1062: Duplicate entry") {
227-
return 0, nil //mysql Error 1062: Duplicate entry 'a-1' for key 'PRIMARY'
228-
} else if driver == q.DriverOracle && strings.Contains(x, "ORA-00001: unique constraint") {
229-
return 0, nil //mysql Error 1062: Duplicate entry 'a-1' for key 'PRIMARY'
230-
} else if driver == q.DriverMssql && strings.Contains(x, "Violation of PRIMARY KEY constraint") {
231-
return 0, nil //Violation of PRIMARY KEY constraint 'PK_aa'. Cannot insert duplicate key in object 'dbo.aa'. The duplicate key value is (b, 2).
232-
} else if driver == q.DriverSqlite3 && strings.Contains(x, "UNIQUE constraint failed") {
233-
return 0, nil
234-
}
235-
return 0, err
236-
}
237221

238222
func setVersion(vo reflect.Value, versionIndex int) bool {
239223
versionType := vo.Field(versionIndex).Type().String()

0 commit comments

Comments
 (0)