Skip to content

Commit d1deaee

Browse files
committed
refactoring
1 parent 51e9b2d commit d1deaee

File tree

6 files changed

+153
-170
lines changed

6 files changed

+153
-170
lines changed

buffer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (b *buffer) fill(need int) (err error) {
3737
b.idx = 0
3838
b.length = 0
3939

40-
n := 0
40+
var n int
4141
for b.length < need {
4242
n, err = b.rd.Read(b.buf[b.length:])
4343
b.length += n

connection.go

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
type mysqlConn struct {
2020
cfg *config
21-
flags ClientFlag
21+
flags clientFlag
2222
charset byte
2323
cipher []byte
2424
netConn net.Conn
@@ -87,7 +87,7 @@ func (mc *mysqlConn) Begin() (driver.Tx, error) {
8787
}
8888

8989
func (mc *mysqlConn) Close() (err error) {
90-
mc.writeCommandPacket(COM_QUIT)
90+
mc.writeCommandPacket(comQuit)
9191
mc.cfg = nil
9292
mc.buf = nil
9393
mc.netConn.Close()
@@ -97,7 +97,7 @@ func (mc *mysqlConn) Close() (err error) {
9797

9898
func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
9999
// Send command
100-
err := mc.writeCommandPacketStr(COM_STMT_PREPARE, query)
100+
err := mc.writeCommandPacketStr(comStmtPrepare, query)
101101
if err != nil {
102102
return nil, err
103103
}
@@ -124,37 +124,30 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
124124
return stmt, err
125125
}
126126

127-
func (mc *mysqlConn) Exec(query string, args []driver.Value) (_ driver.Result, err error) {
128-
if len(args) > 0 { // with args, must use prepared stmt
129-
var res driver.Result
130-
var stmt driver.Stmt
131-
stmt, err = mc.Prepare(query)
132-
if err == nil {
133-
res, err = stmt.Exec(args)
134-
if err == nil {
135-
return res, stmt.Close()
136-
}
137-
}
138-
} else { // no args, fastpath
127+
func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
128+
if len(args) == 0 { // no args, fastpath
139129
mc.affectedRows = 0
140130
mc.insertId = 0
141131

142-
err = mc.exec(query)
132+
err := mc.exec(query)
143133
if err == nil {
144134
return &mysqlResult{
145135
affectedRows: int64(mc.affectedRows),
146136
insertId: int64(mc.insertId),
147137
}, err
148138
}
139+
return nil, err
149140
}
150-
return nil, err
141+
142+
// with args, must use prepared stmt
143+
return nil, driver.ErrSkip
151144

152145
}
153146

154147
// Internal function to execute commands
155148
func (mc *mysqlConn) exec(query string) (err error) {
156149
// Send command
157-
err = mc.writeCommandPacketStr(COM_QUERY, query)
150+
err = mc.writeCommandPacketStr(comQuery, query)
158151
if err != nil {
159152
return
160153
}
@@ -174,28 +167,16 @@ func (mc *mysqlConn) exec(query string) (err error) {
174167
return
175168
}
176169

177-
func (mc *mysqlConn) Query(query string, args []driver.Value) (_ driver.Rows, err error) {
178-
if len(args) > 0 { // with args, must use prepared stmt
179-
var rows driver.Rows
180-
var stmt driver.Stmt
181-
stmt, err = mc.Prepare(query)
182-
if err == nil {
183-
rows, err = stmt.Query(args)
184-
if err == nil {
185-
return rows, stmt.Close()
186-
}
187-
}
188-
return
189-
} else { // no args, fastpath
190-
var rows *mysqlRows
170+
func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) {
171+
if len(args) == 0 { // no args, fastpath
191172
// Send command
192-
err = mc.writeCommandPacketStr(COM_QUERY, query)
173+
err := mc.writeCommandPacketStr(comQuery, query)
193174
if err == nil {
194175
// Read Result
195176
var resLen int
196177
resLen, err = mc.readResultSetHeaderPacket()
197178
if err == nil {
198-
rows = &mysqlRows{mc, false, nil, false}
179+
rows := &mysqlRows{mc, false, nil, false}
199180

200181
if resLen > 0 {
201182
// Columns
@@ -204,7 +185,10 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (_ driver.Rows, er
204185
return rows, err
205186
}
206187
}
188+
189+
return nil, err
207190
}
208191

209-
return nil, err
192+
// with args, must use prepared stmt
193+
return nil, driver.ErrSkip
210194
}

const.go

Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -10,119 +10,119 @@
1010
package mysql
1111

1212
const (
13-
MIN_PROTOCOL_VERSION byte = 10
14-
//MAX_PACKET_SIZE = 1<<24 - 1
15-
TIME_FORMAT = "2006-01-02 15:04:05"
13+
minProtocolVersion byte = 10
14+
//maxPacketSize = 1<<24 - 1
15+
timeFormat = "2006-01-02 15:04:05"
1616
)
1717

1818
// MySQL constants documentation:
1919
// http://dev.mysql.com/doc/internals/en/client-server-protocol.html
2020

21-
type ClientFlag uint32
21+
type clientFlag uint32
2222

2323
const (
24-
CLIENT_LONG_PASSWORD ClientFlag = 1 << iota
25-
CLIENT_FOUND_ROWS
26-
CLIENT_LONG_FLAG
27-
CLIENT_CONNECT_WITH_DB
28-
CLIENT_NO_SCHEMA
29-
CLIENT_COMPRESS
30-
CLIENT_ODBC
31-
CLIENT_LOCAL_FILES
32-
CLIENT_IGNORE_SPACE
33-
CLIENT_PROTOCOL_41
34-
CLIENT_INTERACTIVE
35-
CLIENT_SSL
36-
CLIENT_IGNORE_SIGPIPE
37-
CLIENT_TRANSACTIONS
38-
CLIENT_RESERVED
39-
CLIENT_SECURE_CONN
40-
CLIENT_MULTI_STATEMENTS
41-
CLIENT_MULTI_RESULTS
24+
clientLongPassword clientFlag = 1 << iota
25+
clientFoundRows
26+
clientLongFlag
27+
clientConnectWithDB
28+
clientNoSchema
29+
clientCompress
30+
clientODBC
31+
clientLocalFiles
32+
clientIgnoreSpace
33+
clientProtocol41
34+
clientInteractive
35+
clientSSL
36+
clientIgnoreSIGPIPE
37+
clientTransactions
38+
clientReserved
39+
clientSecureConn
40+
clientMultiStatements
41+
clientMultiResults
4242
)
4343

4444
type commandType byte
4545

4646
const (
47-
COM_QUIT commandType = iota + 1
48-
COM_INIT_DB
49-
COM_QUERY
50-
COM_FIELD_LIST
51-
COM_CREATE_DB
52-
COM_DROP_DB
53-
COM_REFRESH
54-
COM_SHUTDOWN
55-
COM_STATISTICS
56-
COM_PROCESS_INFO
57-
COM_CONNECT
58-
COM_PROCESS_KILL
59-
COM_DEBUG
60-
COM_PING
61-
COM_TIME
62-
COM_DELAYED_INSERT
63-
COM_CHANGE_USER
64-
COM_BINLOG_DUMP
65-
COM_TABLE_DUMP
66-
COM_CONNECT_OUT
67-
COM_REGISTER_SLAVE
68-
COM_STMT_PREPARE
69-
COM_STMT_EXECUTE
70-
COM_STMT_SEND_LONG_DATA
71-
COM_STMT_CLOSE
72-
COM_STMT_RESET
73-
COM_SET_OPTION
74-
COM_STMT_FETCH
47+
comQuit commandType = iota + 1
48+
comInitDB
49+
comQuery
50+
comFieldList
51+
comCreateDB
52+
comDropDB
53+
comRefresh
54+
comShutdown
55+
comStatistics
56+
comProcessInfo
57+
comConnect
58+
comProcessKill
59+
comDebug
60+
comPing
61+
comTime
62+
comDelayedInsert
63+
comChangeUser
64+
comBinlogDump
65+
comTableDump
66+
comConnectOut
67+
comRegiserSlave
68+
comStmtPrepare
69+
comStmtExecute
70+
comStmtSendLongData
71+
comStmtClose
72+
comStmtReset
73+
comSetOption
74+
comStmtFetch
7575
)
7676

7777
const (
78-
FIELD_TYPE_DECIMAL byte = iota
79-
FIELD_TYPE_TINY
80-
FIELD_TYPE_SHORT
81-
FIELD_TYPE_LONG
82-
FIELD_TYPE_FLOAT
83-
FIELD_TYPE_DOUBLE
84-
FIELD_TYPE_NULL
85-
FIELD_TYPE_TIMESTAMP
86-
FIELD_TYPE_LONGLONG
87-
FIELD_TYPE_INT24
88-
FIELD_TYPE_DATE
89-
FIELD_TYPE_TIME
90-
FIELD_TYPE_DATETIME
91-
FIELD_TYPE_YEAR
92-
FIELD_TYPE_NEWDATE
93-
FIELD_TYPE_VARCHAR
94-
FIELD_TYPE_BIT
78+
fieldTypeDecimal byte = iota
79+
fieldTypeTiny
80+
fieldTypeShort
81+
fieldTypeLong
82+
fieldTypeFloat
83+
fieldTypeDouble
84+
fieldTypeNULL
85+
fieldTypeTimestamp
86+
fieldTypeLongLong
87+
fieldTypeInt24
88+
fieldTypeDate
89+
fieldTypeTime
90+
fieldTypeDateTime
91+
fieldTypeYear
92+
fieldTypeNewDate
93+
fieldTypeVarChar
94+
fieldTypeBit
9595
)
9696
const (
97-
FIELD_TYPE_NEWDECIMAL byte = iota + 0xf6
98-
FIELD_TYPE_ENUM
99-
FIELD_TYPE_SET
100-
FIELD_TYPE_TINY_BLOB
101-
FIELD_TYPE_MEDIUM_BLOB
102-
FIELD_TYPE_LONG_BLOB
103-
FIELD_TYPE_BLOB
104-
FIELD_TYPE_VAR_STRING
105-
FIELD_TYPE_STRING
106-
FIELD_TYPE_GEOMETRY
97+
fieldTypeNewDecimal byte = iota + 0xf6
98+
fieldTypeEnum
99+
fieldTypeSet
100+
fieldTypeTinyBLOB
101+
fieldTypeMediumBLOB
102+
fieldTypeLongBLOB
103+
fieldTypeBLOB
104+
fieldTypeVarString
105+
fieldTypeString
106+
fieldTypeGeometry
107107
)
108108

109-
type FieldFlag uint16
109+
type fieldFlag uint16
110110

111111
const (
112-
FLAG_NOT_NULL FieldFlag = 1 << iota
113-
FLAG_PRI_KEY
114-
FLAG_UNIQUE_KEY
115-
FLAG_MULTIPLE_KEY
116-
FLAG_BLOB
117-
FLAG_UNSIGNED
118-
FLAG_ZEROFILL
119-
FLAG_BINARY
120-
FLAG_ENUM
121-
FLAG_AUTO_INCREMENT
122-
FLAG_TIMESTAMP
123-
FLAG_SET
124-
FLAG_UNKNOWN_1
125-
FLAG_UNKNOWN_2
126-
FLAG_UNKNOWN_3
127-
FLAG_UNKNOWN_4
112+
flagNotNULL fieldFlag = 1 << iota
113+
flagPriKey
114+
flagUniqueKey
115+
flagMultipleKey
116+
flagBLOB
117+
flagUnsigned
118+
flagZeroFill
119+
flagBinary
120+
flagEnum
121+
flagAutoIncrement
122+
flagTimestamp
123+
flagSet
124+
flagUnknown1
125+
flagUnknown2
126+
flagUnknown3
127+
flagUnknown4
128128
)

0 commit comments

Comments
 (0)