Skip to content

Commit

Permalink
doc: add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
VarusHsu committed Feb 18, 2024
1 parent 238fe71 commit 112973e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

const (
// for colorful terminal print
green = "\033[32m"
red = "\033[31m"
blue = "\033[34m"
Expand Down Expand Up @@ -57,6 +58,11 @@ type Database interface {
// Initiate a DELETE FROM statement
DeleteFrom(table Table) deleteWithTable

// Begin Start a new transaction and returning a Transaction object.
// the DDL operations using the returned Transaction object will
// regard as one time transaction.
// User must manually call Commit() or Rollback() to end the transaction,
// after that, more DDL operations or TCL will return error.
Begin() (Transaction, error)
}

Expand All @@ -78,7 +84,10 @@ func (d *database) SetLogger(logger func(sql string, durationNano int64)) {
d.logger = logger
}

// defaultLogger is sqlingo default logger,
// which print log to stderr and regard executing time gt 100ms as slow sql.
func defaultLogger(sql string, durationNano int64) {
// for finding code position, try once is enough
once.Do(func() {
// $GOPATH/pkg/mod/github.com/lqs/[email protected]/database.go
_, file, _, _ := runtime.Caller(0)
Expand All @@ -99,6 +108,7 @@ func defaultLogger(sql string, durationNano int64) {
}
}

// convert durationNano (int64) to time.Duration
du := time.Duration(durationNano)
// todo shouldn't append ';' here
if !strings.HasSuffix(sql, ";") {
Expand All @@ -113,6 +123,7 @@ func defaultLogger(sql string, durationNano int64) {
},
" ")

// print to stderr
fmt.Fprintln(os.Stderr, blue+line1+reset)
if du < 100*time.Millisecond {
fmt.Fprintf(os.Stderr, "%s%s%s\n", green, sql, reset)
Expand Down
4 changes: 4 additions & 0 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func (s deleteStatus) Execute() (sql.Result, error) {
if err != nil {
return nil, err
}

// use transaction if it exists, otherwise use database.
// this is because when s.scope.Transaction is not nil,
// it must be built by transaction.
if s.scope.Transaction != nil {
return s.scope.Transaction.Execute(sqlString)
}
Expand Down
1 change: 1 addition & 0 deletions expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (e expression) GetTable() Table {
}

type scope struct {
// Transaction should be nil if without transaction begin
Transaction *transaction
Database *database
Tables []Table
Expand Down
5 changes: 5 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func (s selectStatus) RightJoin(table Table) selectWithJoin {
return s.join("RIGHT ", table)
}

// NaturalJoin joins the table using the NATURAL keyword.
// it automatically matches the columns in the two tables that have the same name.
// it not be needed but be provided for completeness.
func (s selectStatus) NaturalJoin(table Table) selectWithJoinOn {
base := activeSelectBase(&s)
base.scope.lastJoin = &join{
Expand Down Expand Up @@ -479,6 +482,8 @@ func (s selectBase) buildSelectBase(sb *strings.Builder) error {
sb.WriteString(join.prefix)
sb.WriteString("JOIN ")
sb.WriteString(join.table.GetSQL(s.scope))
// cause on isn't a required part of join when using natural join,
// so move it to if statement
if join.on != nil {
onSql, err := join.on.GetSQL(s.scope)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

// Transaction is the interface of a transaction with underlying sql.Tx object.
// It provides methods to execute DDL and TCL operations.
type Transaction interface {
GetTx() *sql.Tx
Query(sql string) (Cursor, error)
Expand Down Expand Up @@ -67,12 +68,18 @@ func (d *database) BeginTx(ctx context.Context, opts *sql.TxOptions, f func(tx T
return nil
}

// Begin starts a new transaction and returning a Transaction object.
// the DDL operations using the returned Transaction object will
// regard as one time transaction.
// User must manually call Commit() or Rollback() to end the transaction,
// after that, more DDL operations or TCL will return error.
func (d *database) Begin() (Transaction, error) {
var err error
tx, err := d.db.Begin()
if err != nil {
return nil, err
}
// copy extra to transaction
t := &transaction{
tx: tx,
logger: d.logger,
Expand Down

0 comments on commit 112973e

Please sign in to comment.