Skip to content

Commit 0392453

Browse files
authored
Merge pull request #67 from zoobc/39/transaction-send-money
39/transaction send money
2 parents 920b250 + 725d0d4 commit 0392453

File tree

8 files changed

+516
-344
lines changed

8 files changed

+516
-344
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ linters-settings:
1616
gofmt:
1717
simplify: true
1818
gocyclo:
19-
min-complexity: 12
19+
min-complexity: 14
2020
maligned:
2121
suggest-new: true
2222
dupl:

common/query/accountQuery.go

+17
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ type (
1616

1717
AccountQueryInterface interface {
1818
GetAccountByID(accountID []byte) (str string, args []interface{})
19+
GetAccountByIDs(ids [][]byte) (str string, args [][]byte)
1920
InsertAccount(account *model.Account) (str string, args []interface{})
2021
ExtractModel(account *model.Account) []interface{}
2122
BuildModel(accounts []*model.Account, rows *sql.Rows) []*model.Account
23+
GetTableName() string
2224
}
2325
)
2426

@@ -36,6 +38,16 @@ func (aq *AccountQuery) GetAccountByID(accountID []byte) (str string, args []int
3638
[]interface{}{accountID}
3739
}
3840

41+
// GetAccountByIDs return query string to get accounts by multiple IDs
42+
func (aq *AccountQuery) GetAccountByIDs(ids [][]byte) (str string, args [][]byte) {
43+
return fmt.Sprintf(
44+
"SELECT %s FROM %s WHERE id in (%s)",
45+
strings.Join(aq.Fields, ","),
46+
aq.TableName,
47+
fmt.Sprintf("? %s", strings.Repeat(",?", len(ids)-1)),
48+
), args
49+
}
50+
3951
func (aq *AccountQuery) InsertAccount(account *model.Account) (str string, args []interface{}) {
4052
return fmt.Sprintf(
4153
"INSERT INTO %s (%s) VALUES(%s)",
@@ -66,3 +78,8 @@ func (*AccountQuery) BuildModel(accounts []*model.Account, rows *sql.Rows) []*mo
6678
}
6779
return accounts
6880
}
81+
82+
// GetTableName is func to get account table name
83+
func (aq *AccountQuery) GetTableName() string {
84+
return aq.TableName
85+
}

common/query/executor.go

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type (
1010
ExecutorInterface interface {
1111
Execute(string) (sql.Result, error)
1212
ExecuteSelect(query string, args ...interface{}) (*sql.Rows, error)
13+
ExecuteSelectRow(query string, args ...interface{}) *sql.Row
1314
ExecuteTransactions(queries []string) ([]sql.Result, error)
1415
ExecuteStatement(query string, args ...interface{}) (sql.Result, error)
1516
ExecuteTransactionStatements(queries map[*string][]interface{}) ([]sql.Result, error)
@@ -84,6 +85,15 @@ func (qe *Executor) ExecuteSelect(query string, args ...interface{}) (*sql.Rows,
8485
return rows, nil
8586
}
8687

88+
/*
89+
ExecuteSelectRow execute with select method that if you want to get `sql.Row` (single).
90+
This function is necessary if you want to processing the row,
91+
otherwise you can use `Execute` or `ExecuteTransactions`
92+
*/
93+
func (qe *Executor) ExecuteSelectRow(query string, args ...interface{}) *sql.Row {
94+
return qe.Db.QueryRow(query, args...)
95+
}
96+
8797
// ExecuteTransactions execute list of queries in transaction
8898
// will return error in case one or more of the query fail
8999
func (qe *Executor) ExecuteTransactions(queries []string) ([]sql.Result, error) {

common/query/executor_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,54 @@ func TestExecutor_ExecuteTransactionStatements(t *testing.T) {
295295
}
296296
})
297297
}
298+
299+
func TestExecutor_ExecuteSelectRow(t *testing.T) {
300+
type (
301+
fields struct {
302+
Db *sql.DB
303+
}
304+
args struct {
305+
query string
306+
args []interface{}
307+
}
308+
)
309+
db, mock, err := sqlmock.New()
310+
if err != nil {
311+
t.Error("failed while opening database connection")
312+
}
313+
mock.ExpectQuery(regexp.QuoteMeta(`
314+
ELECT () FROM account
315+
WHERE id = ? AND name = ? limit 1
316+
`)).WithArgs(1, 2).WillReturnRows(sqlmock.NewRows([]string{
317+
"field",
318+
}).AddRow(1))
319+
tests := []struct {
320+
name string
321+
fields fields
322+
args args
323+
}{
324+
{
325+
name: "wantSuccess",
326+
fields: fields{
327+
Db: db,
328+
},
329+
args: args{
330+
query: "SELECT () FROM account WHERE id = ? AND name = ? limit 1",
331+
args: []interface{}{
332+
1, 2,
333+
},
334+
},
335+
},
336+
}
337+
for _, tt := range tests {
338+
t.Run(tt.name, func(t *testing.T) {
339+
qe := &Executor{
340+
Db: tt.fields.Db,
341+
}
342+
_ = qe.ExecuteSelectRow(tt.args.query, tt.args.args...)
343+
if err := mock.ExpectationsWereMet(); err != nil {
344+
t.Errorf("Executor.ExecuteSelectRow() = %v", err)
345+
}
346+
})
347+
}
348+
}

common/transaction/doc.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
Package transaction include:
3+
- Validate
4+
- ApplyConfirmed
5+
- ApplyUnconfirmed
6+
*/
7+
package transaction

0 commit comments

Comments
 (0)