MySQL lock and unlock tables statement.
This commit is contained in:
parent
ee4897a1e2
commit
a3ae52c43c
5 changed files with 128 additions and 18 deletions
|
|
@ -15,6 +15,7 @@ const (
|
||||||
DeleteStatementType StatementType = "DELETE"
|
DeleteStatementType StatementType = "DELETE"
|
||||||
SetStatementType StatementType = "SET"
|
SetStatementType StatementType = "SET"
|
||||||
LockStatementType StatementType = "LOCK"
|
LockStatementType StatementType = "LOCK"
|
||||||
|
UnLockStatementType StatementType = "UNLOCK"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Serializer interface {
|
type Serializer interface {
|
||||||
|
|
|
||||||
54
mysql/lock_statement.go
Normal file
54
mysql/lock_statement.go
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import "github.com/go-jet/jet/internal/jet"
|
||||||
|
|
||||||
|
type LockStatement interface {
|
||||||
|
Statement
|
||||||
|
READ() Statement
|
||||||
|
WRITE() Statement
|
||||||
|
}
|
||||||
|
|
||||||
|
func LOCK(tables ...jet.SerializerTable) LockStatement {
|
||||||
|
newLock := &lockStatementImpl{
|
||||||
|
Lock: jet.ClauseStatementBegin{Name: "LOCK TABLES", Tables: tables},
|
||||||
|
Read: jet.ClauseOptional{Name: "READ"},
|
||||||
|
Write: jet.ClauseOptional{Name: "WRITE"},
|
||||||
|
}
|
||||||
|
|
||||||
|
newLock.StatementImpl = jet.NewStatementImpl(Dialect, jet.LockStatementType, newLock, &newLock.Lock, &newLock.Read, &newLock.Write)
|
||||||
|
|
||||||
|
return newLock
|
||||||
|
}
|
||||||
|
|
||||||
|
type lockStatementImpl struct {
|
||||||
|
jet.StatementImpl
|
||||||
|
|
||||||
|
Lock jet.ClauseStatementBegin
|
||||||
|
Read jet.ClauseOptional
|
||||||
|
Write jet.ClauseOptional
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lockStatementImpl) READ() Statement {
|
||||||
|
l.Read.Show = true
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lockStatementImpl) WRITE() Statement {
|
||||||
|
l.Write.Show = true
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func UNLOCK_TABLES() Statement {
|
||||||
|
newUnlock := &unlockStatementImpl{
|
||||||
|
Unlock: jet.ClauseStatementBegin{Name: "UNLOCK TABLES"},
|
||||||
|
}
|
||||||
|
|
||||||
|
newUnlock.StatementImpl = jet.NewStatementImpl(Dialect, jet.UnLockStatementType, newUnlock, &newUnlock.Unlock)
|
||||||
|
|
||||||
|
return newUnlock
|
||||||
|
}
|
||||||
|
|
||||||
|
type unlockStatementImpl struct {
|
||||||
|
jet.StatementImpl
|
||||||
|
Unlock jet.ClauseStatementBegin
|
||||||
|
}
|
||||||
21
mysql/lock_statement_test.go
Normal file
21
mysql/lock_statement_test.go
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestLockRead(t *testing.T) {
|
||||||
|
assertStatement(t, table2.LOCK().READ(), `
|
||||||
|
LOCK TABLES db.table2 READ;
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLockWrite(t *testing.T) {
|
||||||
|
assertStatement(t, table2.LOCK().WRITE(), `
|
||||||
|
LOCK TABLES db.table2 WRITE;
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUNLOCK_TABLES(t *testing.T) {
|
||||||
|
assertStatement(t, UNLOCK_TABLES(), `
|
||||||
|
UNLOCK TABLES;
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
@ -2,12 +2,6 @@ package mysql
|
||||||
|
|
||||||
import "github.com/go-jet/jet/internal/jet"
|
import "github.com/go-jet/jet/internal/jet"
|
||||||
|
|
||||||
//type Table jet.Table
|
|
||||||
//
|
|
||||||
//func NewTable(schemaName, name string, columns ...jet.Column) Table {
|
|
||||||
// return jet.NewTable(Dialect, schemaName, name, columns...)
|
|
||||||
//}
|
|
||||||
|
|
||||||
type Table interface {
|
type Table interface {
|
||||||
jet.SerializerTable
|
jet.SerializerTable
|
||||||
readableTable
|
readableTable
|
||||||
|
|
@ -15,9 +9,7 @@ type Table interface {
|
||||||
INSERT(columns ...jet.Column) InsertStatement
|
INSERT(columns ...jet.Column) InsertStatement
|
||||||
UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement
|
UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement
|
||||||
DELETE() DeleteStatement
|
DELETE() DeleteStatement
|
||||||
//LOCK() LockStatement
|
LOCK() LockStatement
|
||||||
|
|
||||||
//As(alias string)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type readableTable interface {
|
type readableTable interface {
|
||||||
|
|
@ -94,21 +86,21 @@ type tableImpl struct {
|
||||||
parent Table
|
parent Table
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *tableImpl) INSERT(columns ...jet.Column) InsertStatement {
|
func (t *tableImpl) INSERT(columns ...jet.Column) InsertStatement {
|
||||||
return newInsertStatement(w.parent, jet.UnwidColumnList(columns))
|
return newInsertStatement(t.parent, jet.UnwidColumnList(columns))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *tableImpl) UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement {
|
func (t *tableImpl) UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement {
|
||||||
return newUpdateStatement(w.parent, jet.UnwindColumns(column, columns...))
|
return newUpdateStatement(t.parent, jet.UnwindColumns(column, columns...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *tableImpl) DELETE() DeleteStatement {
|
func (t *tableImpl) DELETE() DeleteStatement {
|
||||||
return newDeleteStatement(w.parent)
|
return newDeleteStatement(t.parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (w *tableInterfaceImpl) LOCK() LockStatement {
|
func (t *tableImpl) LOCK() LockStatement {
|
||||||
// return LOCK(w.parent)
|
return LOCK(t.parent)
|
||||||
//}
|
}
|
||||||
|
|
||||||
type joinTable2 struct {
|
type joinTable2 struct {
|
||||||
tableImpl
|
tableImpl
|
||||||
|
|
|
||||||
42
tests/mysql/lock_test.go
Normal file
42
tests/mysql/lock_test.go
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-jet/jet/internal/testutils"
|
||||||
|
. "github.com/go-jet/jet/mysql"
|
||||||
|
. "github.com/go-jet/jet/tests/.gentestdata/mysql/dvds/table"
|
||||||
|
"gotest.tools/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLockRead(t *testing.T) {
|
||||||
|
query := Customer.LOCK().READ()
|
||||||
|
|
||||||
|
testutils.AssertStatementSql(t, query, `
|
||||||
|
LOCK TABLES dvds.customer READ;
|
||||||
|
`)
|
||||||
|
|
||||||
|
_, err := query.Exec(db)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLockWrite(t *testing.T) {
|
||||||
|
query := Customer.LOCK().WRITE()
|
||||||
|
|
||||||
|
testutils.AssertStatementSql(t, query, `
|
||||||
|
LOCK TABLES dvds.customer WRITE;
|
||||||
|
`)
|
||||||
|
|
||||||
|
_, err := query.Exec(db)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnlockTables(t *testing.T) {
|
||||||
|
query := UNLOCK_TABLES()
|
||||||
|
|
||||||
|
testutils.AssertStatementSql(t, query, `
|
||||||
|
UNLOCK TABLES;
|
||||||
|
`)
|
||||||
|
|
||||||
|
_, err := query.Exec(db)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue