Skip to content

Commit 7d5de3b

Browse files
committed
Revert "feat: transaction support powerful more"
This reverts commit cf1908f.
1 parent 1c7e60d commit 7d5de3b

8 files changed

Lines changed: 30 additions & 427 deletions

File tree

common.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -140,29 +140,6 @@ func commaOrderBys(scope scope, orderBys []OrderBy) (string, error) {
140140
}
141141

142142
func getCallerInfo(db database, retry bool) string {
143-
if !db.enableCallerInfo {
144-
return ""
145-
}
146-
extraInfo := ""
147-
if retry {
148-
extraInfo += " (retry)"
149-
}
150-
for i := 0; true; i++ {
151-
_, file, line, ok := runtime.Caller(i)
152-
if !ok {
153-
break
154-
}
155-
if file == "" || strings.Contains(file, "/sqlingo@v") {
156-
continue
157-
}
158-
segs := strings.Split(file, "/")
159-
name := segs[len(segs)-1]
160-
return fmt.Sprintf("/* %s:%d%s */ ", name, line, extraInfo)
161-
}
162-
return ""
163-
}
164-
165-
func getTxCallerInfo(db transaction, retry bool) string {
166143
if !db.enableCallerInfo {
167144
return ""
168145
}

database.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,11 @@ type Database interface {
5858
Update(table Table) updateWithSet
5959
// Initiate a DELETE FROM statement
6060
DeleteFrom(table Table) deleteWithTable
61+
}
6162

62-
// Begin Start a new transaction and returning a Transaction object.
63-
// the DDL operations using the returned Transaction object will
64-
// regard as one time transaction.
65-
// User must manually call Commit() or Rollback() to end the transaction,
66-
// after that, more DDL operations or TCL will return error.
67-
Begin() (Transaction, error)
63+
type txOrDB interface {
64+
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
65+
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
6866
}
6967

7068
var (
@@ -74,6 +72,7 @@ var (
7472

7573
type database struct {
7674
db *sql.DB
75+
tx *sql.Tx
7776
logger LoggerFunc
7877
dialect dialect
7978
retryPolicy func(error) bool
@@ -186,6 +185,13 @@ func (d database) GetDB() *sql.DB {
186185
return d.db
187186
}
188187

188+
func (d database) getTxOrDB() txOrDB {
189+
if d.tx != nil {
190+
return d.tx
191+
}
192+
return d.db
193+
}
194+
189195
func (d database) Query(sqlString string) (Cursor, error) {
190196
return d.QueryContext(context.Background(), sqlString)
191197
}
@@ -196,7 +202,7 @@ func (d database) QueryContext(ctx context.Context, sqlString string) (Cursor, e
196202
sqlStringWithCallerInfo := getCallerInfo(d, isRetry) + sqlString
197203
rows, err := d.queryContextOnce(ctx, sqlStringWithCallerInfo, isRetry)
198204
if err != nil {
199-
isRetry = d.retryPolicy != nil && d.retryPolicy(err)
205+
isRetry = d.tx == nil && d.retryPolicy != nil && d.retryPolicy(err)
200206
if isRetry {
201207
continue
202208
}
@@ -221,7 +227,7 @@ func (d database) queryContextOnce(ctx context.Context, sqlString string, retry
221227
interceptor := d.interceptor
222228
var rows *sql.Rows
223229
invoker := func(ctx context.Context, sql string) (err error) {
224-
rows, err = d.GetDB().QueryContext(ctx, sql)
230+
rows, err = d.getTxOrDB().QueryContext(ctx, sql)
225231
return
226232
}
227233

@@ -258,7 +264,7 @@ func (d database) ExecuteContext(ctx context.Context, sqlString string) (sql.Res
258264

259265
var result sql.Result
260266
invoker := func(ctx context.Context, sql string) (err error) {
261-
result, err = d.GetDB().ExecContext(ctx, sql)
267+
result, err = d.getTxOrDB().ExecContext(ctx, sql)
262268
return
263269
}
264270
var err error

expression.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,9 @@ func (e expression) GetTable() Table {
148148
}
149149

150150
type scope struct {
151-
// Transaction should be nil if without transaction begin
152-
Transaction *transaction
153-
Database *database
154-
Tables []Table
155-
lastJoin *join
151+
Database *database
152+
Tables []Table
153+
lastJoin *join
156154
}
157155

158156
func staticExpression(sql string, priority priority, isBool bool) expression {

field.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ func newField(table Table, fieldName string) actualField {
6161
expression: expression{
6262
builder: func(scope scope) (string, error) {
6363
dialect := dialectUnknown
64-
if scope.Transaction != nil {
65-
dialect = scope.Transaction.dialect
66-
} else if scope.Database != nil {
64+
if scope.Database != nil {
6765
dialect = scope.Database.dialect
6866
}
6967
if len(scope.Tables) != 1 || scope.lastJoin != nil || scope.Tables[0].GetName() != tableName {

select.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -610,17 +610,11 @@ func (s selectStatus) FetchCursor() (Cursor, error) {
610610
return nil, err
611611
}
612612

613-
var c Cursor
614-
if s.base.scope.Transaction != nil {
615-
c, err = s.base.scope.Transaction.QueryContext(s.ctx, sqlString)
616-
} else {
617-
c, err = s.base.scope.Database.QueryContext(s.ctx, sqlString)
618-
}
619-
613+
cursor, err := s.base.scope.Database.QueryContext(s.ctx, sqlString)
620614
if err != nil {
621615
return nil, err
622616
}
623-
return c, nil
617+
return cursor, nil
624618
}
625619

626620
func (s selectStatus) FetchFirst(dest ...interface{}) (ok bool, err error) {

table.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ func (t table) GetName() string {
2424
}
2525

2626
func (t table) GetSQL(scope scope) string {
27-
if scope.Transaction != nil {
28-
return t.sqlDialects[scope.Transaction.dialect]
29-
}
3027
return t.sqlDialects[scope.Database.dialect]
3128
}
3229

0 commit comments

Comments
 (0)