Browse files

Gofmt and fixes to Go weekly.2011-12-06

  • Loading branch information...
1 parent 31d3444 commit ffe83c51916899b95fba97ff532db4ebcfa021c7 @ziutek committed Dec 7, 2011
Showing with 1,224 additions and 1,216 deletions.
  1. +3 −1 mysql/row.go
  2. +10 −10 native/addons.go
  3. +273 −276 native/bind_test.go
  4. +130 −130 native/binding.go
  5. +405 −394 native/codecs.go
  6. +95 −95 native/command.go
  7. +145 −145 native/consts.go
  8. +52 −52 native/init.go
  9. +2 −2 native/mysql.go
  10. +16 −16 native/native_test.go
  11. +1 −1 native/packet.go
  12. +3 −3 native/prepared.go
  13. +3 −5 native/result.go
  14. +86 −86 native/unsafe.go
View
4 mysql/row.go
@@ -111,7 +111,9 @@ func (tr Row) UintErr(nn int) (val uint, err error) {
case uint8:
val = uint(data)
case []byte:
- val, err = strconv.Atoui(string(data))
+ var v uint64
+ v, err = strconv.ParseUint(string(data), 0, strconv.IntSize)
+ val = uint(v)
case uint64:
if strconv.IntSize > 32 {
val = uint(data)
View
20 native/addons.go
@@ -1,17 +1,17 @@
package native
func NbinToNstr(nbin *[]byte) *string {
- if nbin == nil {
- return nil
- }
- str := string(*nbin)
- return &str
+ if nbin == nil {
+ return nil
+ }
+ str := string(*nbin)
+ return &str
}
func NstrToNbin(nstr *string) *[]byte {
- if nstr == nil {
- return nil
- }
- bin := []byte(*nstr)
- return &bin
+ if nstr == nil {
+ return nil
+ }
+ bin := []byte(*nstr)
+ return &bin
}
View
549 native/bind_test.go
@@ -1,310 +1,307 @@
package native
import (
- "testing"
- "bytes"
- "math"
- "reflect"
+ "bytes"
"github.com/ziutek/mymysql/mysql"
+ "math"
+ "reflect"
+ "testing"
)
var (
- Bytes = []byte("Ala ma Kota!")
- String = "ssss" //"A kot ma Alę!"
- blob = mysql.Blob{1, 2, 3}
- dateT = mysql.Datetime{Year: 2010, Month: 12, Day: 30, Hour: 17, Minute:21}
- tstamp = mysql.Timestamp{Year: 2001, Month: 2, Day: 3, Hour: 7, Minute:2}
- date = mysql.Date{Year: 2011, Month: 2, Day: 3}
- tim = -mysql.Time((5 * 24*3600 + 4 * 3600 + 3 * 60 + 2) * 1e9 + 1)
-
- pBytes *[]byte
- pString *string
- pBlob *mysql.Blob
- pDateT *mysql.Datetime
- pTstamp *mysql.Timestamp
- pDate *mysql.Date
- pTim *mysql.Time
-
- raw = mysql.Raw{MYSQL_TYPE_INT24, &[]byte{3, 2, 1}}
-
- Int8 = int8(1)
- Uint8 = uint8(2)
- Int16 = int16(3)
- Uint16 = uint16(4)
- Int32 = int32(5)
- Uint32 = uint32(6)
- Int64 = int64(0x7000100020003001)
- Uint64 = uint64(0xffff0000ffff0000)
- Int = int(7)
- Uint = uint(8)
-
- Float32 = float32(1e10)
- Float64 = 256e256
-
- pInt8 *int8
- pUint8 *uint8
- pInt16 *int16
- pUint16 *uint16
- pInt32 *int32
- pUint32 *uint32
- pInt64 *int64
- pUint64 *uint64
- pInt *int
- pUint *uint
- pFloat32 *float32
- pFloat64 *float64
+ Bytes = []byte("Ala ma Kota!")
+ String = "ssss" //"A kot ma Alę!"
+ blob = mysql.Blob{1, 2, 3}
+ dateT = mysql.Datetime{Year: 2010, Month: 12, Day: 30, Hour: 17, Minute: 21}
+ tstamp = mysql.Timestamp{Year: 2001, Month: 2, Day: 3, Hour: 7, Minute: 2}
+ date = mysql.Date{Year: 2011, Month: 2, Day: 3}
+ tim = -mysql.Time((5*24*3600+4*3600+3*60+2)*1e9 + 1)
+
+ pBytes *[]byte
+ pString *string
+ pBlob *mysql.Blob
+ pDateT *mysql.Datetime
+ pTstamp *mysql.Timestamp
+ pDate *mysql.Date
+ pTim *mysql.Time
+
+ raw = mysql.Raw{MYSQL_TYPE_INT24, &[]byte{3, 2, 1}}
+
+ Int8 = int8(1)
+ Uint8 = uint8(2)
+ Int16 = int16(3)
+ Uint16 = uint16(4)
+ Int32 = int32(5)
+ Uint32 = uint32(6)
+ Int64 = int64(0x7000100020003001)
+ Uint64 = uint64(0xffff0000ffff0000)
+ Int = int(7)
+ Uint = uint(8)
+
+ Float32 = float32(1e10)
+ Float64 = 256e256
+
+ pInt8 *int8
+ pUint8 *uint8
+ pInt16 *int16
+ pUint16 *uint16
+ pInt32 *int32
+ pUint32 *uint32
+ pInt64 *int64
+ pUint64 *uint64
+ pInt *int
+ pUint *uint
+ pFloat32 *float32
+ pFloat64 *float64
)
type BindTest struct {
- val interface{}
- typ uint16
- length int
+ val interface{}
+ typ uint16
+ length int
}
-var bindTests = []BindTest {
- BindTest{nil, MYSQL_TYPE_NULL, 0},
-
- BindTest{Bytes, MYSQL_TYPE_VAR_STRING, -1},
- BindTest{String, MYSQL_TYPE_STRING, -1},
- BindTest{blob, MYSQL_TYPE_BLOB, -1},
- BindTest{dateT, MYSQL_TYPE_DATETIME, -1},
- BindTest{tstamp, MYSQL_TYPE_TIMESTAMP, -1},
- BindTest{date, MYSQL_TYPE_DATE, -1},
- BindTest{tim, MYSQL_TYPE_TIME, -1},
-
- BindTest{&Bytes, MYSQL_TYPE_VAR_STRING, -1},
- BindTest{&String, MYSQL_TYPE_STRING, -1},
- BindTest{&blob, MYSQL_TYPE_BLOB, -1},
- BindTest{&dateT, MYSQL_TYPE_DATETIME, -1},
- BindTest{&tstamp, MYSQL_TYPE_TIMESTAMP, -1},
- BindTest{&date, MYSQL_TYPE_DATE, -1},
- BindTest{&tim, MYSQL_TYPE_TIME, -1},
-
- BindTest{pBytes, MYSQL_TYPE_VAR_STRING, -1},
- BindTest{pString, MYSQL_TYPE_STRING, -1},
- BindTest{pBlob, MYSQL_TYPE_BLOB, -1},
- BindTest{pDateT, MYSQL_TYPE_DATETIME, -1},
- BindTest{pTstamp, MYSQL_TYPE_TIMESTAMP, -1},
- BindTest{pDate, MYSQL_TYPE_DATE, -1},
- BindTest{pTim, MYSQL_TYPE_TIME, -1},
-
- BindTest{raw, MYSQL_TYPE_INT24, -1},
-
- BindTest{Int8, MYSQL_TYPE_TINY, 1},
- BindTest{Int16, MYSQL_TYPE_SHORT, 2},
- BindTest{Int32, MYSQL_TYPE_LONG, 4},
- BindTest{Int64, MYSQL_TYPE_LONGLONG, 8},
- BindTest{Int, MYSQL_TYPE_LONG, 4}, // Hack
-
- BindTest{&Int8, MYSQL_TYPE_TINY, 1},
- BindTest{&Int16, MYSQL_TYPE_SHORT, 2},
- BindTest{&Int32, MYSQL_TYPE_LONG, 4},
- BindTest{&Int64, MYSQL_TYPE_LONGLONG, 8},
- BindTest{&Int, MYSQL_TYPE_LONG, 4}, // Hack
-
- BindTest{pInt8, MYSQL_TYPE_TINY, 1},
- BindTest{pInt16, MYSQL_TYPE_SHORT, 2},
- BindTest{pInt32, MYSQL_TYPE_LONG, 4},
- BindTest{pInt64, MYSQL_TYPE_LONGLONG, 8},
- BindTest{pInt, MYSQL_TYPE_LONG, 4}, // Hack
-
- BindTest{Uint8, MYSQL_TYPE_TINY | MYSQL_UNSIGNED_MASK, 1},
- BindTest{Uint16, MYSQL_TYPE_SHORT | MYSQL_UNSIGNED_MASK, 2},
- BindTest{Uint32, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4},
- BindTest{Uint64, MYSQL_TYPE_LONGLONG | MYSQL_UNSIGNED_MASK, 8},
- BindTest{Uint, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4},//Hack
-
- BindTest{&Uint8, MYSQL_TYPE_TINY | MYSQL_UNSIGNED_MASK, 1},
- BindTest{&Uint16, MYSQL_TYPE_SHORT | MYSQL_UNSIGNED_MASK, 2},
- BindTest{&Uint32, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4},
- BindTest{&Uint64, MYSQL_TYPE_LONGLONG | MYSQL_UNSIGNED_MASK, 8},
- BindTest{&Uint, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4},//Hack
-
- BindTest{pUint8, MYSQL_TYPE_TINY | MYSQL_UNSIGNED_MASK, 1},
- BindTest{pUint16, MYSQL_TYPE_SHORT | MYSQL_UNSIGNED_MASK, 2},
- BindTest{pUint32, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4},
- BindTest{pUint64, MYSQL_TYPE_LONGLONG | MYSQL_UNSIGNED_MASK, 8},
- BindTest{pUint, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4},//Hack
-
- BindTest{Float32, MYSQL_TYPE_FLOAT, 4},
- BindTest{Float64, MYSQL_TYPE_DOUBLE, 8},
-
- BindTest{&Float32, MYSQL_TYPE_FLOAT, 4},
- BindTest{&Float64, MYSQL_TYPE_DOUBLE, 8},
+var bindTests = []BindTest{
+ BindTest{nil, MYSQL_TYPE_NULL, 0},
+
+ BindTest{Bytes, MYSQL_TYPE_VAR_STRING, -1},
+ BindTest{String, MYSQL_TYPE_STRING, -1},
+ BindTest{blob, MYSQL_TYPE_BLOB, -1},
+ BindTest{dateT, MYSQL_TYPE_DATETIME, -1},
+ BindTest{tstamp, MYSQL_TYPE_TIMESTAMP, -1},
+ BindTest{date, MYSQL_TYPE_DATE, -1},
+ BindTest{tim, MYSQL_TYPE_TIME, -1},
+
+ BindTest{&Bytes, MYSQL_TYPE_VAR_STRING, -1},
+ BindTest{&String, MYSQL_TYPE_STRING, -1},
+ BindTest{&blob, MYSQL_TYPE_BLOB, -1},
+ BindTest{&dateT, MYSQL_TYPE_DATETIME, -1},
+ BindTest{&tstamp, MYSQL_TYPE_TIMESTAMP, -1},
+ BindTest{&date, MYSQL_TYPE_DATE, -1},
+ BindTest{&tim, MYSQL_TYPE_TIME, -1},
+
+ BindTest{pBytes, MYSQL_TYPE_VAR_STRING, -1},
+ BindTest{pString, MYSQL_TYPE_STRING, -1},
+ BindTest{pBlob, MYSQL_TYPE_BLOB, -1},
+ BindTest{pDateT, MYSQL_TYPE_DATETIME, -1},
+ BindTest{pTstamp, MYSQL_TYPE_TIMESTAMP, -1},
+ BindTest{pDate, MYSQL_TYPE_DATE, -1},
+ BindTest{pTim, MYSQL_TYPE_TIME, -1},
+
+ BindTest{raw, MYSQL_TYPE_INT24, -1},
+
+ BindTest{Int8, MYSQL_TYPE_TINY, 1},
+ BindTest{Int16, MYSQL_TYPE_SHORT, 2},
+ BindTest{Int32, MYSQL_TYPE_LONG, 4},
+ BindTest{Int64, MYSQL_TYPE_LONGLONG, 8},
+ BindTest{Int, MYSQL_TYPE_LONG, 4}, // Hack
+
+ BindTest{&Int8, MYSQL_TYPE_TINY, 1},
+ BindTest{&Int16, MYSQL_TYPE_SHORT, 2},
+ BindTest{&Int32, MYSQL_TYPE_LONG, 4},
+ BindTest{&Int64, MYSQL_TYPE_LONGLONG, 8},
+ BindTest{&Int, MYSQL_TYPE_LONG, 4}, // Hack
+
+ BindTest{pInt8, MYSQL_TYPE_TINY, 1},
+ BindTest{pInt16, MYSQL_TYPE_SHORT, 2},
+ BindTest{pInt32, MYSQL_TYPE_LONG, 4},
+ BindTest{pInt64, MYSQL_TYPE_LONGLONG, 8},
+ BindTest{pInt, MYSQL_TYPE_LONG, 4}, // Hack
+
+ BindTest{Uint8, MYSQL_TYPE_TINY | MYSQL_UNSIGNED_MASK, 1},
+ BindTest{Uint16, MYSQL_TYPE_SHORT | MYSQL_UNSIGNED_MASK, 2},
+ BindTest{Uint32, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4},
+ BindTest{Uint64, MYSQL_TYPE_LONGLONG | MYSQL_UNSIGNED_MASK, 8},
+ BindTest{Uint, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4}, //Hack
+
+ BindTest{&Uint8, MYSQL_TYPE_TINY | MYSQL_UNSIGNED_MASK, 1},
+ BindTest{&Uint16, MYSQL_TYPE_SHORT | MYSQL_UNSIGNED_MASK, 2},
+ BindTest{&Uint32, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4},
+ BindTest{&Uint64, MYSQL_TYPE_LONGLONG | MYSQL_UNSIGNED_MASK, 8},
+ BindTest{&Uint, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4}, //Hack
+
+ BindTest{pUint8, MYSQL_TYPE_TINY | MYSQL_UNSIGNED_MASK, 1},
+ BindTest{pUint16, MYSQL_TYPE_SHORT | MYSQL_UNSIGNED_MASK, 2},
+ BindTest{pUint32, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4},
+ BindTest{pUint64, MYSQL_TYPE_LONGLONG | MYSQL_UNSIGNED_MASK, 8},
+ BindTest{pUint, MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK, 4}, //Hack
+
+ BindTest{Float32, MYSQL_TYPE_FLOAT, 4},
+ BindTest{Float64, MYSQL_TYPE_DOUBLE, 8},
+
+ BindTest{&Float32, MYSQL_TYPE_FLOAT, 4},
+ BindTest{&Float64, MYSQL_TYPE_DOUBLE, 8},
}
func makeAddressable(v reflect.Value) reflect.Value {
- if v.IsValid() {
- // Make an addresable value
- av := reflect.New(v.Type()).Elem()
- av.Set(v)
- v = av
- }
- return v
+ if v.IsValid() {
+ // Make an addresable value
+ av := reflect.New(v.Type()).Elem()
+ av.Set(v)
+ v = av
+ }
+ return v
}
func TestBind(t *testing.T) {
- for _, test := range bindTests {
- v := makeAddressable(reflect.ValueOf(test.val))
- val := bindValue(v)
- if val.typ != test.typ || val.length != test.length {
- t.Errorf(
- "Type: %s exp=0x%x res=0x%x Len: exp=%d res=%d",
- reflect.TypeOf(test.val), test.typ, val.typ, test.length,
- val.length,
- )
- }
- }
+ for _, test := range bindTests {
+ v := makeAddressable(reflect.ValueOf(test.val))
+ val := bindValue(v)
+ if val.typ != test.typ || val.length != test.length {
+ t.Errorf(
+ "Type: %s exp=0x%x res=0x%x Len: exp=%d res=%d",
+ reflect.TypeOf(test.val), test.typ, val.typ, test.length,
+ val.length,
+ )
+ }
+ }
}
-
type WriteTest struct {
- val interface{}
- exp []byte
+ val interface{}
+ exp []byte
}
var writeTest []WriteTest
+
func init() {
- b := make([]byte, 64 * 1024)
- for ii := range b {
- b[ii] = byte(ii)
- }
- blob = mysql.Blob(b)
-
- writeTest = []WriteTest{
- WriteTest{Bytes, append([]byte{byte(len(Bytes))}, Bytes...)},
- WriteTest{String, append([]byte{byte(len(String))}, []byte(String)...)},
- WriteTest{pBytes, nil},
- WriteTest{pString, nil},
- WriteTest {
- blob,
- append(
- append(
- []byte{253},
- EncodeU24(uint32(len(blob)))...
- ),
- []byte(blob)...,
- ),
- },
- WriteTest {
- dateT,
- []byte{
- 7, byte(dateT.Year), byte(dateT.Year >> 8), byte(dateT.Month),
- byte(dateT.Day), byte(dateT.Hour), byte(dateT.Minute),
- byte(dateT.Second),
- },
- },
- WriteTest {
- &dateT,
- []byte{
- 7, byte(dateT.Year), byte(dateT.Year >> 8), byte(dateT.Month),
- byte(dateT.Day), byte(dateT.Hour), byte(dateT.Minute),
- byte(dateT.Second),
- },
- },
- WriteTest {
- date,
- []byte{
- 4, byte(date.Year), byte(date.Year >> 8), byte(date.Month),
- byte(date.Day),
- },
- },
- WriteTest {
- &date,
- []byte{
- 4, byte(date.Year), byte(date.Year >> 8), byte(date.Month),
- byte(date.Day),
- },
- },
- WriteTest {
- tim,
- []byte{12, 1, 5, 0, 0, 0, 4, 3, 2, 1, 0, 0, 0},
- },
- WriteTest {
- &tim,
- []byte{12, 1, 5, 0, 0, 0, 4, 3, 2, 1, 0, 0, 0},
- },
- WriteTest{dateT, EncodeDatetime(&dateT)},
- WriteTest{&dateT, EncodeDatetime(&dateT)},
- WriteTest{pDateT, nil},
-
- WriteTest{tstamp, EncodeDatetime((*mysql.Datetime)(&tstamp))},
- WriteTest{&tstamp, EncodeDatetime((*mysql.Datetime)(&tstamp))},
- WriteTest{pTstamp, nil},
-
- WriteTest{date, EncodeDate(&date)},
- WriteTest{&date, EncodeDate(&date)},
- WriteTest{pDate, nil},
-
- WriteTest{tim, EncodeTime(&tim)},
- WriteTest{&tim, EncodeTime(&tim)},
- WriteTest{pTim, nil},
-
- WriteTest{Int, EncodeU32(uint32(Int))}, // Hack
- WriteTest{Int16, EncodeU16(uint16(Int16))},
- WriteTest{Int32, EncodeU32(uint32(Int32))},
- WriteTest{Int64, EncodeU64(uint64(Int64))},
-
- WriteTest{Int , EncodeU32(uint32(Int))}, // Hack
- WriteTest{Uint16, EncodeU16(Uint16)},
- WriteTest{Uint32, EncodeU32(Uint32)},
- WriteTest{Uint64, EncodeU64(Uint64)},
-
- WriteTest{&Int, EncodeU32(uint32(Int))}, // Hack
- WriteTest{&Int16, EncodeU16(uint16(Int16))},
- WriteTest{&Int32, EncodeU32(uint32(Int32))},
- WriteTest{&Int64, EncodeU64(uint64(Int64))},
-
- WriteTest{&Uint, EncodeU32(uint32(Uint))}, // Hack
- WriteTest{&Uint16, EncodeU16(Uint16)},
- WriteTest{&Uint32, EncodeU32(Uint32)},
- WriteTest{&Uint64, EncodeU64(Uint64)},
-
- WriteTest{pInt, nil},
- WriteTest{pInt16, nil},
- WriteTest{pInt32, nil},
- WriteTest{pInt64, nil},
-
- WriteTest{Float32, EncodeU32(math.Float32bits(Float32))},
- WriteTest{Float64, EncodeU64(math.Float64bits(Float64))},
-
- WriteTest{&Float32, EncodeU32(math.Float32bits(Float32))},
- WriteTest{&Float64, EncodeU64(math.Float64bits(Float64))},
-
- WriteTest{pFloat32, nil},
- WriteTest{pFloat64, nil},
- }
+ b := make([]byte, 64*1024)
+ for ii := range b {
+ b[ii] = byte(ii)
+ }
+ blob = mysql.Blob(b)
+
+ writeTest = []WriteTest{
+ WriteTest{Bytes, append([]byte{byte(len(Bytes))}, Bytes...)},
+ WriteTest{String, append([]byte{byte(len(String))}, []byte(String)...)},
+ WriteTest{pBytes, nil},
+ WriteTest{pString, nil},
+ WriteTest{
+ blob,
+ append(
+ append([]byte{253}, EncodeU24(uint32(len(blob)))...),
+ []byte(blob)...,
+ ),
+ },
+ WriteTest{
+ dateT,
+ []byte{
+ 7, byte(dateT.Year), byte(dateT.Year >> 8), byte(dateT.Month),
+ byte(dateT.Day), byte(dateT.Hour), byte(dateT.Minute),
+ byte(dateT.Second),
+ },
+ },
+ WriteTest{
+ &dateT,
+ []byte{
+ 7, byte(dateT.Year), byte(dateT.Year >> 8), byte(dateT.Month),
+ byte(dateT.Day), byte(dateT.Hour), byte(dateT.Minute),
+ byte(dateT.Second),
+ },
+ },
+ WriteTest{
+ date,
+ []byte{
+ 4, byte(date.Year), byte(date.Year >> 8), byte(date.Month),
+ byte(date.Day),
+ },
+ },
+ WriteTest{
+ &date,
+ []byte{
+ 4, byte(date.Year), byte(date.Year >> 8), byte(date.Month),
+ byte(date.Day),
+ },
+ },
+ WriteTest{
+ tim,
+ []byte{12, 1, 5, 0, 0, 0, 4, 3, 2, 1, 0, 0, 0},
+ },
+ WriteTest{
+ &tim,
+ []byte{12, 1, 5, 0, 0, 0, 4, 3, 2, 1, 0, 0, 0},
+ },
+ WriteTest{dateT, EncodeDatetime(&dateT)},
+ WriteTest{&dateT, EncodeDatetime(&dateT)},
+ WriteTest{pDateT, nil},
+
+ WriteTest{tstamp, EncodeDatetime((*mysql.Datetime)(&tstamp))},
+ WriteTest{&tstamp, EncodeDatetime((*mysql.Datetime)(&tstamp))},
+ WriteTest{pTstamp, nil},
+
+ WriteTest{date, EncodeDate(&date)},
+ WriteTest{&date, EncodeDate(&date)},
+ WriteTest{pDate, nil},
+
+ WriteTest{tim, EncodeTime(&tim)},
+ WriteTest{&tim, EncodeTime(&tim)},
+ WriteTest{pTim, nil},
+
+ WriteTest{Int, EncodeU32(uint32(Int))}, // Hack
+ WriteTest{Int16, EncodeU16(uint16(Int16))},
+ WriteTest{Int32, EncodeU32(uint32(Int32))},
+ WriteTest{Int64, EncodeU64(uint64(Int64))},
+
+ WriteTest{Int, EncodeU32(uint32(Int))}, // Hack
+ WriteTest{Uint16, EncodeU16(Uint16)},
+ WriteTest{Uint32, EncodeU32(Uint32)},
+ WriteTest{Uint64, EncodeU64(Uint64)},
+
+ WriteTest{&Int, EncodeU32(uint32(Int))}, // Hack
+ WriteTest{&Int16, EncodeU16(uint16(Int16))},
+ WriteTest{&Int32, EncodeU32(uint32(Int32))},
+ WriteTest{&Int64, EncodeU64(uint64(Int64))},
+
+ WriteTest{&Uint, EncodeU32(uint32(Uint))}, // Hack
+ WriteTest{&Uint16, EncodeU16(Uint16)},
+ WriteTest{&Uint32, EncodeU32(Uint32)},
+ WriteTest{&Uint64, EncodeU64(Uint64)},
+
+ WriteTest{pInt, nil},
+ WriteTest{pInt16, nil},
+ WriteTest{pInt32, nil},
+ WriteTest{pInt64, nil},
+
+ WriteTest{Float32, EncodeU32(math.Float32bits(Float32))},
+ WriteTest{Float64, EncodeU64(math.Float64bits(Float64))},
+
+ WriteTest{&Float32, EncodeU32(math.Float32bits(Float32))},
+ WriteTest{&Float64, EncodeU64(math.Float64bits(Float64))},
+
+ WriteTest{pFloat32, nil},
+ WriteTest{pFloat64, nil},
+ }
}
func TestWrite(t *testing.T) {
- buf := new(bytes.Buffer)
- for _, test := range writeTest {
- buf.Reset()
- v := makeAddressable(reflect.ValueOf(test.val))
- val := bindValue(v)
- writeValue(buf, val)
- if !bytes.Equal(buf.Bytes(), test.exp) || val.Len() != len(test.exp) {
- t.Errorf("%s - exp_len=%d res_len=%d exp: %v res: %v",
- reflect.TypeOf(test.val), len(test.exp), val.Len(),
- test.exp, buf.Bytes(),
- )
- }
- }
+ buf := new(bytes.Buffer)
+ for _, test := range writeTest {
+ buf.Reset()
+ v := makeAddressable(reflect.ValueOf(test.val))
+ val := bindValue(v)
+ writeValue(buf, val)
+ if !bytes.Equal(buf.Bytes(), test.exp) || val.Len() != len(test.exp) {
+ t.Errorf("%s - exp_len=%d res_len=%d exp: %v res: %v",
+ reflect.TypeOf(test.val), len(test.exp), val.Len(),
+ test.exp, buf.Bytes(),
+ )
+ }
+ }
}
func TestEscapeString(t *testing.T) {
- txt := " \000 \n \r \\ ' \" \032 "
- exp := ` \0 \n \r \\ \' \" \Z `
- out := escapeString(txt)
- if out != exp {
- t.Fatalf("escapeString: ret='%s' exp='%s'", out, exp)
- }
+ txt := " \000 \n \r \\ ' \" \032 "
+ exp := ` \0 \n \r \\ \' \" \Z `
+ out := escapeString(txt)
+ if out != exp {
+ t.Fatalf("escapeString: ret='%s' exp='%s'", out, exp)
+ }
}
func TestEscapeQuotes(t *testing.T) {
- txt := " '' '' ' ' ' "
- exp := ` '''' '''' '' '' '' `
- out := escapeQuotes(txt)
- if out != exp {
- t.Fatalf("escapeString: ret='%s' exp='%s'", out, exp)
- }
+ txt := " '' '' ' ' ' "
+ exp := ` '''' '''' '' '' '' `
+ out := escapeQuotes(txt)
+ if out != exp {
+ t.Fatalf("escapeString: ret='%s' exp='%s'", out, exp)
+ }
}
View
260 native/binding.go
@@ -1,140 +1,140 @@
package native
import (
- "reflect"
"github.com/ziutek/mymysql/mysql"
+ "reflect"
)
// val should be an addressable value
func bindValue(val reflect.Value) (out *paramValue) {
- if !val.IsValid() {
- return &paramValue{typ: MYSQL_TYPE_NULL}
- }
- typ := val.Type()
+ if !val.IsValid() {
+ return &paramValue{typ: MYSQL_TYPE_NULL}
+ }
+ typ := val.Type()
out = new(paramValue)
- if typ.Kind() == reflect.Ptr {
- // We have addressable pointer
- out.SetAddr(val.UnsafeAddr())
- // Dereference pointer for next operation on its value
- typ = typ.Elem()
- val = val.Elem()
- } else {
- // We have addressable value. Create a pointer to it
- pv := val.Addr()
- // This pointer is unaddressable so copy it and return an address
- ppv := reflect.New(pv.Type())
- ppv.Elem().Set(pv)
- out.SetAddr(ppv.Pointer())
- }
-
- // Obtain value type
- switch typ.Kind() {
- case reflect.String:
- out.typ = MYSQL_TYPE_STRING
- out.length = -1
- return
-
- case reflect.Int:
- out.typ = _INT_TYPE
- out.length = _SIZE_OF_INT
- return
-
- case reflect.Int8:
- out.typ = MYSQL_TYPE_TINY
- out.length = 1
- return
-
- case reflect.Int16:
- out.typ = MYSQL_TYPE_SHORT
- out.length = 2
- return
-
- case reflect.Int32:
- out.typ = MYSQL_TYPE_LONG
- out.length = 4
- return
-
- case reflect.Int64:
- if typ == mysql.TimeType {
- out.typ = MYSQL_TYPE_TIME
- out.length = -1
- return
- }
- out.typ = MYSQL_TYPE_LONGLONG
- out.length = 8
- return
-
- case reflect.Uint:
- out.typ = _INT_TYPE | MYSQL_UNSIGNED_MASK
- out.length = _SIZE_OF_INT
- return
-
- case reflect.Uint8:
- out.typ = MYSQL_TYPE_TINY | MYSQL_UNSIGNED_MASK
- out.length = 1
- return
-
- case reflect.Uint16:
- out.typ = MYSQL_TYPE_SHORT | MYSQL_UNSIGNED_MASK
- out.length = 2
- return
-
- case reflect.Uint32:
- out.typ = MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK
- out.length = 4
- return
-
- case reflect.Uint64:
- if typ == mysql.TimeType {
- out.typ = MYSQL_TYPE_TIME
- out.length = -1
- return
- }
- out.typ = MYSQL_TYPE_LONGLONG | MYSQL_UNSIGNED_MASK
- out.length = 8
- return
-
- case reflect.Float32:
- out.typ = MYSQL_TYPE_FLOAT
- out.length = 4
- return
-
- case reflect.Float64:
- out.typ = MYSQL_TYPE_DOUBLE
- out.length = 8
- return
-
- case reflect.Slice:
- out.length = -1
- if typ == mysql.BlobType {
- out.typ = MYSQL_TYPE_BLOB
- return
- }
- if typ.Elem().Kind() == reflect.Uint8 {
- out.typ = MYSQL_TYPE_VAR_STRING
- return
- }
-
- case reflect.Struct:
- out.length = -1
- if typ == mysql.DatetimeType {
- out.typ = MYSQL_TYPE_DATETIME
- return
- }
- if typ == mysql.DateType {
- out.typ = MYSQL_TYPE_DATE
- return
- }
- if typ == mysql.TimestampType {
- out.typ = MYSQL_TYPE_TIMESTAMP
- return
- }
- if typ == mysql.RawType {
- out.typ = val.FieldByName("Typ").Interface().(uint16)
- out.SetAddr(val.FieldByName("Val").Pointer())
- out.raw = true
- return
- }
- }
- panic(BIND_UNK_TYPE)
+ if typ.Kind() == reflect.Ptr {
+ // We have addressable pointer
+ out.SetAddr(val.UnsafeAddr())
+ // Dereference pointer for next operation on its value
+ typ = typ.Elem()
+ val = val.Elem()
+ } else {
+ // We have addressable value. Create a pointer to it
+ pv := val.Addr()
+ // This pointer is unaddressable so copy it and return an address
+ ppv := reflect.New(pv.Type())
+ ppv.Elem().Set(pv)
+ out.SetAddr(ppv.Pointer())
+ }
+
+ // Obtain value type
+ switch typ.Kind() {
+ case reflect.String:
+ out.typ = MYSQL_TYPE_STRING
+ out.length = -1
+ return
+
+ case reflect.Int:
+ out.typ = _INT_TYPE
+ out.length = _SIZE_OF_INT
+ return
+
+ case reflect.Int8:
+ out.typ = MYSQL_TYPE_TINY
+ out.length = 1
+ return
+
+ case reflect.Int16:
+ out.typ = MYSQL_TYPE_SHORT
+ out.length = 2
+ return
+
+ case reflect.Int32:
+ out.typ = MYSQL_TYPE_LONG
+ out.length = 4
+ return
+
+ case reflect.Int64:
+ if typ == mysql.TimeType {
+ out.typ = MYSQL_TYPE_TIME
+ out.length = -1
+ return
+ }
+ out.typ = MYSQL_TYPE_LONGLONG
+ out.length = 8
+ return
+
+ case reflect.Uint:
+ out.typ = _INT_TYPE | MYSQL_UNSIGNED_MASK
+ out.length = _SIZE_OF_INT
+ return
+
+ case reflect.Uint8:
+ out.typ = MYSQL_TYPE_TINY | MYSQL_UNSIGNED_MASK
+ out.length = 1
+ return
+
+ case reflect.Uint16:
+ out.typ = MYSQL_TYPE_SHORT | MYSQL_UNSIGNED_MASK
+ out.length = 2
+ return
+
+ case reflect.Uint32:
+ out.typ = MYSQL_TYPE_LONG | MYSQL_UNSIGNED_MASK
+ out.length = 4
+ return
+
+ case reflect.Uint64:
+ if typ == mysql.TimeType {
+ out.typ = MYSQL_TYPE_TIME
+ out.length = -1
+ return
+ }
+ out.typ = MYSQL_TYPE_LONGLONG | MYSQL_UNSIGNED_MASK
+ out.length = 8
+ return
+
+ case reflect.Float32:
+ out.typ = MYSQL_TYPE_FLOAT
+ out.length = 4
+ return
+
+ case reflect.Float64:
+ out.typ = MYSQL_TYPE_DOUBLE
+ out.length = 8
+ return
+
+ case reflect.Slice:
+ out.length = -1
+ if typ == mysql.BlobType {
+ out.typ = MYSQL_TYPE_BLOB
+ return
+ }
+ if typ.Elem().Kind() == reflect.Uint8 {
+ out.typ = MYSQL_TYPE_VAR_STRING
+ return
+ }
+
+ case reflect.Struct:
+ out.length = -1
+ if typ == mysql.DatetimeType {
+ out.typ = MYSQL_TYPE_DATETIME
+ return
+ }
+ if typ == mysql.DateType {
+ out.typ = MYSQL_TYPE_DATE
+ return
+ }
+ if typ == mysql.TimestampType {
+ out.typ = MYSQL_TYPE_TIMESTAMP
+ return
+ }
+ if typ == mysql.RawType {
+ out.typ = val.FieldByName("Typ").Interface().(uint16)
+ out.SetAddr(val.FieldByName("Val").Pointer())
+ out.raw = true
+ return
+ }
+ }
+ panic(BIND_UNK_TYPE)
}
View
799 native/codecs.go
@@ -1,567 +1,578 @@
package native
import (
- "io"
- "crypto/sha1"
- "bytes"
+ "bytes"
+ "crypto/sha1"
"github.com/ziutek/mymysql/mysql"
+ "io"
)
func DecodeU16(buf []byte) uint16 {
- return uint16(buf[1]) << 8 | uint16(buf[0])
+ return uint16(buf[1])<<8 | uint16(buf[0])
}
func readU16(rd io.Reader) uint16 {
- buf := make([]byte, 2)
- readFull(rd, buf)
- return DecodeU16(buf)
+ buf := make([]byte, 2)
+ readFull(rd, buf)
+ return DecodeU16(buf)
}
func DecodeU24(buf []byte) uint32 {
- return (uint32(buf[2]) << 8 | uint32(buf[1])) << 8 | uint32(buf[0])
+ return (uint32(buf[2])<<8|uint32(buf[1]))<<8 | uint32(buf[0])
@sougou
sougou added a note Apr 25, 2012

Shouldn't this be uint32(buf[2])<<16...? Same comment for 32, etc below. You could also use go's encoding/binary (LittleEndian?) instead.

@ziutek
Owner
ziutek added a note May 7, 2012

See parentheses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
}
func readU24(rd io.Reader) uint32 {
- buf := make([]byte, 3)
- readFull(rd, buf)
- return DecodeU24(buf)
+ buf := make([]byte, 3)
+ readFull(rd, buf)
+ return DecodeU24(buf)
}
func DecodeU32(buf []byte) uint32 {
- return ((uint32(buf[3]) << 8 | uint32(buf[2])) << 8 |
- uint32(buf[1])) << 8 | uint32(buf[0])
+ return ((uint32(buf[3])<<8|uint32(buf[2]))<<8|
+ uint32(buf[1]))<<8 | uint32(buf[0])
}
func readU32(rd io.Reader) uint32 {
- buf := make([]byte, 4)
- readFull(rd, buf)
- return DecodeU32(buf)
+ buf := make([]byte, 4)
+ readFull(rd, buf)
+ return DecodeU32(buf)
}
func DecodeU64(buf []byte) (rv uint64) {
- for ii, vv := range buf {
- rv |= uint64(vv) << uint(ii * 8)
- }
- return
+ for ii, vv := range buf {
+ rv |= uint64(vv) << uint(ii*8)
+ }
+ return
}
func readU64(rd io.Reader) (rv uint64) {
- buf := make([]byte, 8)
- readFull(rd, buf)
- return DecodeU64(buf)
+ buf := make([]byte, 8)
+ readFull(rd, buf)
+ return DecodeU64(buf)
}
func EncodeU16(val uint16) []byte {
- return []byte{byte(val), byte(val >> 8)}
+ return []byte{byte(val), byte(val >> 8)}
}
func writeU16(wr io.Writer, val uint16) {
- write(wr, EncodeU16(val))
+ write(wr, EncodeU16(val))
}
func EncodeU24(val uint32) []byte {
- return []byte{byte(val), byte(val >> 8), byte(val >> 16)}
+ return []byte{byte(val), byte(val >> 8), byte(val >> 16)}
}
func writeU24(wr io.Writer, val uint32) {
- write(wr, EncodeU24(val))
+ write(wr, EncodeU24(val))
}
func EncodeU32(val uint32) []byte {
- return []byte{byte(val), byte(val >> 8), byte(val >> 16), byte(val >> 24)}
+ return []byte{byte(val), byte(val >> 8), byte(val >> 16), byte(val >> 24)}
}
func writeU32(wr io.Writer, val uint32) {
- write(wr, EncodeU32(val))
+ write(wr, EncodeU32(val))
}
func EncodeU64(val uint64) []byte {
- buf := make([]byte, 8)
- for ii := range buf {
- buf[ii] = byte(val >> uint(ii * 8))
- }
- return buf
+ buf := make([]byte, 8)
+ for ii := range buf {
+ buf[ii] = byte(val >> uint(ii*8))
+ }
+ return buf
}
func writeU64(wr io.Writer, val uint64) {
- write(wr, EncodeU64(val))
+ write(wr, EncodeU64(val))
}
-
func readNu64(rd io.Reader) *uint64 {
- bb := readByte(rd)
- var val uint64
- switch bb {
- case 251:
- return nil
+ bb := readByte(rd)
+ var val uint64
+ switch bb {
+ case 251:
+ return nil
- case 252:
- val = uint64(readU16(rd))
+ case 252:
+ val = uint64(readU16(rd))
- case 253:
- val = uint64(readU24(rd))
+ case 253:
+ val = uint64(readU24(rd))
- case 254:
- val = readU64(rd)
+ case 254:
+ val = readU64(rd)
- default:
- val = uint64(bb)
- }
- return &val
+ default:
+ val = uint64(bb)
+ }
+ return &val
}
func readNotNullU64(rd io.Reader) (val uint64) {
- nu := readNu64(rd)
- if nu == nil {
- panic(UNEXP_NULL_LCB_ERROR)
- }
- return *nu
+ nu := readNu64(rd)
+ if nu == nil {
+ panic(UNEXP_NULL_LCB_ERROR)
+ }
+ return *nu
}
func writeLCB(wr io.Writer, val uint64) {
- switch {
- case val <= 250:
- writeByte(wr, byte(val))
+ switch {
+ case val <= 250:
+ writeByte(wr, byte(val))
- case val <= 0xffff:
- writeByte(wr, 252)
- writeU16(wr, uint16(val))
+ case val <= 0xffff:
+ writeByte(wr, 252)
+ writeU16(wr, uint16(val))
- case val <= 0xffffff:
- writeByte(wr, 253)
- writeU24(wr, uint32(val))
+ case val <= 0xffffff:
+ writeByte(wr, 253)
+ writeU24(wr, uint32(val))
- default:
- writeByte(wr, 254)
- writeU64(wr, val)
- }
+ default:
+ writeByte(wr, 254)
+ writeU64(wr, val)
+ }
}
func lenLCB(val uint64) int {
- switch {
- case val <= 250:
- return 1
+ switch {
+ case val <= 250:
+ return 1
- case val <= 0xffff:
- return 3
+ case val <= 0xffff:
+ return 3
- case val <= 0xffffff:
- return 4
- }
- return 9
+ case val <= 0xffffff:
+ return 4
+ }
+ return 9
}
func writeNu64(wr io.Writer, nu *uint64) {
- if nu == nil {
- writeByte(wr, 251)
- } else {
- writeLCB(wr, *nu)
- }
+ if nu == nil {
+ writeByte(wr, 251)
+ } else {
+ writeLCB(wr, *nu)
+ }
}
func lenNu64(nu *uint64) int {
- if nu == nil {
- return 1
- }
- return lenLCB(*nu)
+ if nu == nil {
+ return 1
+ }
+ return lenLCB(*nu)
}
func readNbin(rd io.Reader) *[]byte {
- if nu := readNu64(rd); nu != nil {
- buf := make([]byte, *nu)
- readFull(rd, buf)
- return &buf
- }
- return nil
+ if nu := readNu64(rd); nu != nil {
+ buf := make([]byte, *nu)
+ readFull(rd, buf)
+ return &buf
+ }
+ return nil
}
func readNotNullBin(rd io.Reader) []byte {
- nbuf := readNbin(rd)
- if nbuf == nil {
- panic(UNEXP_NULL_LCS_ERROR)
- }
- return *nbuf
+ nbuf := readNbin(rd)
+ if nbuf == nil {
+ panic(UNEXP_NULL_LCS_ERROR)
+ }
+ return *nbuf
}
func writeNbin(wr io.Writer, nbuf *[]byte) {
- if nbuf == nil {
- writeByte(wr, 251)
- return
- }
- writeLCB(wr, uint64(len(*nbuf)))
- write(wr, *nbuf)
+ if nbuf == nil {
+ writeByte(wr, 251)
+ return
+ }
+ writeLCB(wr, uint64(len(*nbuf)))
+ write(wr, *nbuf)
}
func lenNbin(nbuf *[]byte) int {
- if nbuf == nil {
- return 1
- }
- return lenLCB(uint64(len(*nbuf))) + len(*nbuf)
+ if nbuf == nil {
+ return 1
+ }
+ return lenLCB(uint64(len(*nbuf))) + len(*nbuf)
}
func readNstr(rd io.Reader) (nstr *string) {
- if nbuf := readNbin(rd); nbuf != nil {
- str := string(*nbuf)
- nstr = &str
- }
- return
+ if nbuf := readNbin(rd); nbuf != nil {
+ str := string(*nbuf)
+ nstr = &str
+ }
+ return
}
func readNotNullStr(rd io.Reader) (str string) {
- buf := readNotNullBin(rd)
- str = string(buf)
- return
+ buf := readNotNullBin(rd)
+ str = string(buf)
+ return
}
func writeNstr(wr io.Writer, nstr *string) {
- if nstr == nil {
- writeByte(wr, 251)
- return
- }
- writeLCB(wr, uint64(len(*nstr)))
- writeString(wr, *nstr)
+ if nstr == nil {
+ writeByte(wr, 251)
+ return
+ }
+ writeLCB(wr, uint64(len(*nstr)))
+ writeString(wr, *nstr)
}
func lenNstr(nstr *string) int {
- if nstr == nil {
- return 1
- }
- return lenLCB(uint64(len(*nstr))) + len(*nstr)
+ if nstr == nil {
+ return 1
+ }
+ return lenLCB(uint64(len(*nstr))) + len(*nstr)
}
func writeLC(wr io.Writer, v interface{}) {
- switch val := v.(type) {
- case []byte: writeNbin(wr, &val)
- case string: writeNstr(wr, &val)
- case *[]byte: writeNbin(wr, val)
- case *string: writeNstr(wr, val)
- default: panic("Unknown data type for write as lenght coded string")
- }
+ switch val := v.(type) {
+ case []byte:
+ writeNbin(wr, &val)
+ case string:
+ writeNstr(wr, &val)
+ case *[]byte:
+ writeNbin(wr, val)
+ case *string:
+ writeNstr(wr, val)
+ default:
+ panic("Unknown data type for write as lenght coded string")
+ }
}
func lenLC(v interface{}) int {
- switch val := v.(type) {
- case []byte: return lenNbin(&val)
- case string: return lenNstr(&val)
- case *[]byte: return lenNbin(val)
- case *string: return lenNstr(val)
- }
- panic("Unknown data type for write as lenght coded string")
+ switch val := v.(type) {
+ case []byte:
+ return lenNbin(&val)
+ case string:
+ return lenNstr(&val)
+ case *[]byte:
+ return lenNbin(val)
+ case *string:
+ return lenNstr(val)
+ }
+ panic("Unknown data type for write as lenght coded string")
}
func readNTB(rd io.Reader) (buf []byte) {
- bb := new(bytes.Buffer)
- for {
- ch := readByte(rd)
- if ch == 0 {
- return bb.Bytes()
- }
- bb.WriteByte(ch)
- }
- return
+ bb := new(bytes.Buffer)
+ for {
+ ch := readByte(rd)
+ if ch == 0 {
+ return bb.Bytes()
+ }
+ bb.WriteByte(ch)
+ }
+ return
}
func writeNTB(wr io.Writer, buf []byte) {
- write(wr, buf)
- writeByte(wr, 0)
+ write(wr, buf)
+ writeByte(wr, 0)
}
func readNTS(rd io.Reader) (str string) {
- buf := readNTB(rd)
- str = string(buf)
- return
+ buf := readNTB(rd)
+ str = string(buf)
+ return
}
func writeNTS(wr io.Writer, str string) {
- writeNTB(wr, []byte(str))
+ writeNTB(wr, []byte(str))
}
func writeNT(wr io.Writer, v interface{}) {
- switch val := v.(type) {
- case []byte: writeNTB(wr, val)
- case string: writeNTS(wr, val)
- default: panic("Unknown type for write as null terminated data")
- }
+ switch val := v.(type) {
+ case []byte:
+ writeNTB(wr, val)
+ case string:
+ writeNTS(wr, val)
+ default:
+ panic("Unknown type for write as null terminated data")
+ }
}
func readNtime(rd io.Reader) *mysql.Time {
- dlen := readByte(rd)
- switch dlen {
- case 251:
- // Null
- return nil
- case 0:
- // 00:00:00
- return new(mysql.Time)
- case 5, 8, 12:
- // Properly time length
- default:
- panic(WRONG_DATE_LEN_ERROR)
- }
- buf := make([]byte, dlen)
- readFull(rd, buf)
- tt := int64(0)
- switch dlen {
- case 12:
- // Nanosecond part
- tt += int64(DecodeU32(buf[8:]))
- fallthrough
- case 8:
- // HH:MM:SS part
- tt += int64(int(buf[5]) * 3600 + int(buf[6]) * 60 + int(buf[7])) * 1e9
- fallthrough
- case 5:
- // Day part
- tt += int64(DecodeU32(buf[1:5])) * (24 * 3600 * 1e9)
- fallthrough
- }
- if buf[0] != 0 {
- tt = -tt
- }
- return (*mysql.Time)(&tt)
+ dlen := readByte(rd)
+ switch dlen {
+ case 251:
+ // Null
+ return nil
+ case 0:
+ // 00:00:00
+ return new(mysql.Time)
+ case 5, 8, 12:
+ // Properly time length
+ default:
+ panic(WRONG_DATE_LEN_ERROR)
+ }
+ buf := make([]byte, dlen)
+ readFull(rd, buf)
+ tt := int64(0)
+ switch dlen {
+ case 12:
+ // Nanosecond part
+ tt += int64(DecodeU32(buf[8:]))
+ fallthrough
+ case 8:
+ // HH:MM:SS part
+ tt += int64(int(buf[5])*3600+int(buf[6])*60+int(buf[7])) * 1e9
+ fallthrough
+ case 5:
+ // Day part
+ tt += int64(DecodeU32(buf[1:5])) * (24 * 3600 * 1e9)
+ fallthrough
+ }
+ if buf[0] != 0 {
+ tt = -tt
+ }
+ return (*mysql.Time)(&tt)
}
func readNotNullTime(rd io.Reader) mysql.Time {
- tt := readNtime(rd)
- if tt == nil {
- panic(UNEXP_NULL_DATE_ERROR)
- }
- return *tt
+ tt := readNtime(rd)
+ if tt == nil {
+ panic(UNEXP_NULL_DATE_ERROR)
+ }
+ return *tt
}
func EncodeTime(tt *mysql.Time) []byte {
- if tt == nil {
- return []byte{251}
- }
- buf := make([]byte, 13)
- ti := int64(*tt)
- if ti < 0 {
- buf[1] = 1
- ti = -ti
- }
- if ns := uint32(ti % 1e9); ns != 0 {
- copy(buf[9:13], EncodeU32(ns)) // nanosecond
- buf[0] += 4
- }
- ti /= 1e9
- if hms := int(ti % (24 * 3600)); buf[0] != 0 || hms != 0 {
- buf[8] = byte(hms % 60) // second
- hms /= 60
- buf[7] = byte(hms % 60) // minute
- buf[6] = byte(hms / 60) // hour
- buf[0] += 3
- }
- if day := uint32(ti / (24 * 3600)); buf[0] != 0 || day != 0 {
- copy(buf[2:6], EncodeU32(day)) // day
- buf[0] += 4
- }
- buf[0]++ // For sign byte
- buf = buf[0 : buf[0]+1]
- return buf
+ if tt == nil {
+ return []byte{251}
+ }
+ buf := make([]byte, 13)
+ ti := int64(*tt)
+ if ti < 0 {
+ buf[1] = 1
+ ti = -ti
+ }
+ if ns := uint32(ti % 1e9); ns != 0 {
+ copy(buf[9:13], EncodeU32(ns)) // nanosecond
+ buf[0] += 4
+ }
+ ti /= 1e9
+ if hms := int(ti % (24 * 3600)); buf[0] != 0 || hms != 0 {
+ buf[8] = byte(hms % 60) // second
+ hms /= 60
+ buf[7] = byte(hms % 60) // minute
+ buf[6] = byte(hms / 60) // hour
+ buf[0] += 3
+ }
+ if day := uint32(ti / (24 * 3600)); buf[0] != 0 || day != 0 {
+ copy(buf[2:6], EncodeU32(day)) // day
+ buf[0] += 4
+ }
+ buf[0]++ // For sign byte
+ buf = buf[0 : buf[0]+1]
+ return buf
}
func writeNtime(wr io.Writer, tt *mysql.Time) {
- write(wr, EncodeTime(tt))
+ write(wr, EncodeTime(tt))
}
func lenNtime(tt *mysql.Time) int {
- if tt == nil || *tt == 0 {
- return 1
- }
- ti := int64(*tt)
- if ti % 1e9 != 0 {
- return 13
- }
- ti /= 1e9
- if ti % (24 * 3600) != 0 {
- return 9
- }
- return 6
+ if tt == nil || *tt == 0 {
+ return 1
+ }
+ ti := int64(*tt)
+ if ti%1e9 != 0 {
+ return 13
+ }
+ ti /= 1e9
+ if ti%(24*3600) != 0 {
+ return 9
+ }
+ return 6
}
func readNdatetime(rd io.Reader) *mysql.Datetime {
- dlen := readByte(rd)
- switch dlen {
- case 251:
- // Null
- return nil
- case 0:
- // 0000-00-00
- return new(mysql.Datetime)
- case 4, 7, 11:
- // Properly datetime length
- default:
- panic(WRONG_DATE_LEN_ERROR)
- }
-
- buf := make([]byte, dlen)
- readFull(rd, buf)
- var dt mysql.Datetime
- switch dlen {
- case 11:
- // 2006-01-02 15:04:05.001004005
- dt.Nanosec = DecodeU32(buf[7:])
- fallthrough
- case 7:
- // 2006-01-02 15:04:05
- dt.Hour = buf[4]
- dt.Minute = buf[5]
- dt.Second = buf[6]
- fallthrough
- case 4:
- // 2006-01-02
- dt.Year = int16(DecodeU16(buf[0:2]))
- dt.Month = buf[2]
- dt.Day = buf[3]
- }
- return &dt
+ dlen := readByte(rd)
+ switch dlen {
+ case 251:
+ // Null
+ return nil
+ case 0:
+ // 0000-00-00
+ return new(mysql.Datetime)
+ case 4, 7, 11:
+ // Properly datetime length
+ default:
+ panic(WRONG_DATE_LEN_ERROR)
+ }
+
+ buf := make([]byte, dlen)
+ readFull(rd, buf)
+ var dt mysql.Datetime
+ switch dlen {
+ case 11:
+ // 2006-01-02 15:04:05.001004005
+ dt.Nanosec = DecodeU32(buf[7:])
+ fallthrough
+ case 7:
+ // 2006-01-02 15:04:05
+ dt.Hour = buf[4]
+ dt.Minute = buf[5]
+ dt.Second = buf[6]
+ fallthrough
+ case 4:
+ // 2006-01-02
+ dt.Year = int16(DecodeU16(buf[0:2]))
+ dt.Month = buf[2]
+ dt.Day = buf[3]
+ }
+ return &dt
}
func readNotNullDatetime(rd io.Reader) (dt *mysql.Datetime) {
- dt = readNdatetime(rd)
- if dt == nil {
- panic(UNEXP_NULL_DATE_ERROR)
- }
- return
+ dt = readNdatetime(rd)
+ if dt == nil {
+ panic(UNEXP_NULL_DATE_ERROR)
+ }
+ return
}
func EncodeDatetime(dt *mysql.Datetime) []byte {
- if dt == nil {
- return []byte{251}
- }
- buf := make([]byte, 12)
- switch {
- case dt.Nanosec != 0:
- copy(buf[7:12], EncodeU32(dt.Nanosec))
- buf[0] += 4
- fallthrough
-
- case dt.Second != 0 || dt.Minute != 0 || dt.Hour != 0:
- buf[7] = dt.Second
- buf[6] = dt.Minute
- buf[5] = dt.Hour
- buf[0] += 3
- fallthrough
-
- case dt.Day != 0 || dt.Month != 0 || dt.Year != 0:
- buf[4] = dt.Day
- buf[3] = dt.Month
- copy(buf[1:3], EncodeU16(uint16(dt.Year)))
- buf[0] += 4
- }
- buf = buf[0 : buf[0]+1]
- return buf
+ if dt == nil {
+ return []byte{251}
+ }
+ buf := make([]byte, 12)
+ switch {
+ case dt.Nanosec != 0:
+ copy(buf[7:12], EncodeU32(dt.Nanosec))
+ buf[0] += 4
+ fallthrough
+
+ case dt.Second != 0 || dt.Minute != 0 || dt.Hour != 0:
+ buf[7] = dt.Second
+ buf[6] = dt.Minute
+ buf[5] = dt.Hour
+ buf[0] += 3
+ fallthrough
+
+ case dt.Day != 0 || dt.Month != 0 || dt.Year != 0:
+ buf[4] = dt.Day
+ buf[3] = dt.Month
+ copy(buf[1:3], EncodeU16(uint16(dt.Year)))
+ buf[0] += 4
+ }
+ buf = buf[0 : buf[0]+1]
+ return buf
}
func writeNdatetime(wr io.Writer, dt *mysql.Datetime) {
- write(wr, EncodeDatetime(dt))
+ write(wr, EncodeDatetime(dt))
}
func lenNdatetime(dt *mysql.Datetime) int {
- switch {
- case dt == nil:
- return 1
- case dt.Nanosec != 0:
- return 12
- case dt.Second != 0 || dt.Minute != 0 || dt.Hour != 0:
- return 8
- case dt.Day != 0 || dt.Month != 0 || dt.Year != 0:
- return 5
- }
- return 1
+ switch {
+ case dt == nil:
+ return 1
+ case dt.Nanosec != 0:
+ return 12
+ case dt.Second != 0 || dt.Minute != 0 || dt.Hour != 0:
+ return 8
+ case dt.Day != 0 || dt.Month != 0 || dt.Year != 0:
+ return 5
+ }
+ return 1
}
func readNdate(rd io.Reader) *mysql.Date {
- dt := readNdatetime(rd)
- if dt == nil {
- return nil
- }
- return &mysql.Date{Year: dt.Year, Month: dt.Month, Day: dt.Day}
+ dt := readNdatetime(rd)
+ if dt == nil {
+ return nil
+ }
+ return &mysql.Date{Year: dt.Year, Month: dt.Month, Day: dt.Day}
}
func readNotNullDate(rd io.Reader) (dt *mysql.Date) {
- dt = readNdate(rd)
- if dt == nil {
- panic(UNEXP_NULL_DATE_ERROR)
- }
- return
+ dt = readNdate(rd)
+ if dt == nil {
+ panic(UNEXP_NULL_DATE_ERROR)
+ }
+ return
}
func EncodeDate(dd *mysql.Date) []byte {
- return EncodeDatetime(mysql.DateToDatetime(dd))
+ return EncodeDatetime(mysql.DateToDatetime(dd))
}
func writeNdate(wr io.Writer, dd *mysql.Date) {
- write(wr, EncodeDate(dd))
+ write(wr, EncodeDate(dd))
}
func lenNdate(dd *mysql.Date) int {
- return lenNdatetime(mysql.DateToDatetime(dd))
+ return lenNdatetime(mysql.DateToDatetime(dd))
}
// Borrowed from GoMySQL
// SHA1(SHA1(SHA1(password)), scramble) XOR SHA1(password)
func (my *Conn) encryptedPasswd() (out []byte) {
- // Convert password to byte array
- passbytes := []byte(my.passwd)
- // stage1_hash = SHA1(password)
- // SHA1 encode
- crypt := sha1.New()
- crypt.Write(passbytes)
- stg1Hash := crypt.Sum(nil)
- // token = SHA1(SHA1(stage1_hash), scramble) XOR stage1_hash
- // SHA1 encode again
- crypt.Reset()
- crypt.Write(stg1Hash)
- stg2Hash := crypt.Sum(nil)
- // SHA1 2nd hash and scramble
- crypt.Reset()
- crypt.Write(my.info.scramble)
- crypt.Write(stg2Hash)
- stg3Hash := crypt.Sum(nil)
- // XOR with first hash
- out = make([]byte, len(my.info.scramble))
- for ii := range my.info.scramble {
- out[ii] = stg3Hash[ii] ^ stg1Hash[ii]
- }
- return
+ // Convert password to byte array
+ passbytes := []byte(my.passwd)
+ // stage1_hash = SHA1(password)
+ // SHA1 encode
+ crypt := sha1.New()
+ crypt.Write(passbytes)
+ stg1Hash := crypt.Sum(nil)
+ // token = SHA1(SHA1(stage1_hash), scramble) XOR stage1_hash
+ // SHA1 encode again
+ crypt.Reset()
+ crypt.Write(stg1Hash)
+ stg2Hash := crypt.Sum(nil)
+ // SHA1 2nd hash and scramble
+ crypt.Reset()
+ crypt.Write(my.info.scramble)
+ crypt.Write(stg2Hash)
+ stg3Hash := crypt.Sum(nil)
+ // XOR with first hash
+ out = make([]byte, len(my.info.scramble))
+ for ii := range my.info.scramble {
+ out[ii] = stg3Hash[ii] ^ stg1Hash[ii]
+ }
+ return
}
func escapeString(txt string) string {
- var (
- esc string
- buf bytes.Buffer
- )
- last := 0
- for ii, bb := range txt {
- switch bb {
- case 0:
- esc = `\0`
- case '\n':
- esc = `\n`
- case '\r':
- esc = `\r`
- case '\\':
- esc = `\\`
- case '\'':
- esc = `\'`
- case '"':
- esc = `\"`
- case '\032':
- esc = `\Z`
- default:
- continue
- }
- io.WriteString(&buf, txt[last:ii])
- io.WriteString(&buf, esc)
- last = ii + 1
- }
- io.WriteString(&buf, txt[last:])
- return buf.String()
+ var (
+ esc string
+ buf bytes.Buffer
+ )
+ last := 0
+ for ii, bb := range txt {
+ switch bb {
+ case 0:
+ esc = `\0`
+ case '\n':
+ esc = `\n`
+ case '\r':
+ esc = `\r`
+ case '\\':
+ esc = `\\`
+ case '\'':
+ esc = `\'`
+ case '"':
+ esc = `\"`
+ case '\032':
+ esc = `\Z`
+ default:
+ continue
+ }
+ io.WriteString(&buf, txt[last:ii])
+ io.WriteString(&buf, esc)
+ last = ii + 1
+ }
+ io.WriteString(&buf, txt[last:])
+ return buf.String()
}
func escapeQuotes(txt string) string {
- var buf bytes.Buffer
- last := 0
- for ii, bb := range txt {
- if bb == '\'' {
- io.WriteString(&buf, txt[last:ii])
- io.WriteString(&buf, `''`)
- last = ii + 1
- }
- }
- io.WriteString(&buf, txt[last:])
- return buf.String()
+ var buf bytes.Buffer
+ last := 0
+ for ii, bb := range txt {
+ if bb == '\'' {
+ io.WriteString(&buf, txt[last:ii])
+ io.WriteString(&buf, `''`)
+ last = ii + 1
+ }
+ }
+ io.WriteString(&buf, txt[last:])
+ return buf.String()
}
View
190 native/command.go
@@ -3,99 +3,99 @@ package native
import "log"
func (my *Conn) sendCmd(cmd byte, argv ...interface{}) {
- // Reset sequence number
- my.seq = 0
- // Write command
- switch cmd {
- case _COM_QUERY, _COM_INIT_DB, _COM_CREATE_DB, _COM_DROP_DB,
- _COM_STMT_PREPARE:
- pw := my.newPktWriter(1 + lenBS(argv[0]))
- writeByte(pw, cmd)
- writeBS(pw, argv[0])
-
- case _COM_STMT_SEND_LONG_DATA:
- pw := my.newPktWriter(1 + 4 + 2 + lenBS(argv[2]))
- writeByte(pw, cmd)
- writeU32(pw, argv[0].(uint32)) // Statement ID
- writeU16(pw, argv[1].(uint16)) // Parameter number
- writeBS(pw, argv[2]) // payload
-
- case _COM_QUIT, _COM_STATISTICS, _COM_PROCESS_INFO, _COM_DEBUG, _COM_PING:
- pw := my.newPktWriter(1)
- writeByte(pw, cmd)
-
- case _COM_FIELD_LIST:
- pay_len := 1 + lenBS(argv[0])+1
- if len(argv) > 1 {
- pay_len += lenBS(argv[1])
- }
-
- pw := my.newPktWriter(pay_len)
- writeByte(pw, cmd)
- writeNT(pw, argv[0])
- if len(argv) > 1 {
- writeBS(pw, argv[1])
- }
-
- case _COM_TABLE_DUMP:
- pw := my.newPktWriter(1 + lenLC(argv[0]) + lenLC(argv[1]))
- writeByte(pw, cmd)
- writeLC(pw, argv[0])
- writeLC(pw, argv[1])
-
- case _COM_REFRESH, _COM_SHUTDOWN :
- pw := my.newPktWriter(1 + 1)
- writeByte(pw, cmd)
- writeByte(pw, argv[0].(byte))
-
- case _COM_STMT_FETCH:
- pw := my.newPktWriter(1 + 4 + 4)
- writeByte(pw, cmd)
- writeU32(pw, argv[0].(uint32))
- writeU32(pw, argv[1].(uint32))
-
- case _COM_PROCESS_KILL, _COM_STMT_CLOSE, _COM_STMT_RESET:
- pw := my.newPktWriter(1 + 4)
- writeByte(pw, cmd)
- writeU32(pw, argv[0].(uint32))
-
- case _COM_SET_OPTION:
- pw := my.newPktWriter(1 + 2)
- writeByte(pw, cmd)
- writeU16(pw, argv[0].(uint16))
-
- case _COM_CHANGE_USER:
- pw := my.newPktWriter(
- 1 + lenBS(argv[0])+1 + lenLC(argv[1]) + lenBS(argv[2])+1,
- )
- writeByte(pw, cmd)
- writeNT(pw, argv[0]) // User name
- writeLC(pw, argv[1]) // Scrambled password
- writeNT(pw, argv[2]) // Database name
- //writeU16(pw, argv[3]) // Character set number (since 5.1.23?)
-
- case _COM_BINLOG_DUMP:
- pay_len := 1 + 4 + 2 + 4
- if len(argv) > 3 {
- pay_len += lenBS(argv[3])
- }
-
- pw := my.newPktWriter(pay_len)
- writeByte(pw, cmd)
- writeU32(pw, argv[0].(uint32)) // Start position
- writeU16(pw, argv[1].(uint16)) // Flags
- writeU32(pw, argv[2].(uint32)) // Slave server id
- if len(argv) > 3 {
- writeBS(pw, argv[3])
- }
-
- // TODO: case COM_REGISTER_SLAVE:
-
- default:
- panic("Unknown code for MySQL command")
- }
-
- if my.Debug {
- log.Printf("[%2d <-] Command packet: Cmd=0x%x", my.seq - 1, cmd)
- }
+ // Reset sequence number
+ my.seq = 0
+ // Write command
+ switch cmd {
+ case _COM_QUERY, _COM_INIT_DB, _COM_CREATE_DB, _COM_DROP_DB,
+ _COM_STMT_PREPARE:
+ pw := my.newPktWriter(1 + lenBS(argv[0]))
+ writeByte(pw, cmd)
+ writeBS(pw, argv[0])
+
+ case _COM_STMT_SEND_LONG_DATA:
+ pw := my.newPktWriter(1 + 4 + 2 + lenBS(argv[2]))
+ writeByte(pw, cmd)
+ writeU32(pw, argv[0].(uint32)) // Statement ID
+ writeU16(pw, argv[1].(uint16)) // Parameter number
+ writeBS(pw, argv[2]) // payload
+
+ case _COM_QUIT, _COM_STATISTICS, _COM_PROCESS_INFO, _COM_DEBUG, _COM_PING:
+ pw := my.newPktWriter(1)
+ writeByte(pw, cmd)
+
+ case _COM_FIELD_LIST:
+ pay_len := 1 + lenBS(argv[0]) + 1
+ if len(argv) > 1 {
+ pay_len += lenBS(argv[1])
+ }
+
+ pw := my.newPktWriter(pay_len)
+ writeByte(pw, cmd)
+ writeNT(pw, argv[0])
+ if len(argv) > 1 {
+ writeBS(pw, argv[1])
+ }
+
+ case _COM_TABLE_DUMP:
+ pw := my.newPktWriter(1 + lenLC(argv[0]) + lenLC(argv[1]))
+ writeByte(pw, cmd)
+ writeLC(pw, argv[0])
+ writeLC(pw, argv[1])
+
+ case _COM_REFRESH, _COM_SHUTDOWN:
+ pw := my.newPktWriter(1 + 1)
+ writeByte(pw, cmd)
+ writeByte(pw, argv[0].(byte))
+
+ case _COM_STMT_FETCH:
+ pw := my.newPktWriter(1 + 4 + 4)
+ writeByte(pw, cmd)
+ writeU32(pw, argv[0].(uint32))
+ writeU32(pw, argv[1].(uint32))
+
+ case _COM_PROCESS_KILL, _COM_STMT_CLOSE, _COM_STMT_RESET:
+ pw := my.newPktWriter(1 + 4)
+ writeByte(pw, cmd)
+ writeU32(pw, argv[0].(uint32))
+
+ case _COM_SET_OPTION:
+ pw := my.newPktWriter(1 + 2)
+ writeByte(pw, cmd)
+ writeU16(pw, argv[0].(uint16))
+
+ case _COM_CHANGE_USER:
+ pw := my.newPktWriter(
+ 1 + lenBS(argv[0]) + 1 + lenLC(argv[1]) + lenBS(argv[2]) + 1,
+ )
+ writeByte(pw, cmd)
+ writeNT(pw, argv[0]) // User name
+ writeLC(pw, argv[1]) // Scrambled password
+ writeNT(pw, argv[2]) // Database name
+ //writeU16(pw, argv[3]) // Character set number (since 5.1.23?)
+
+ case _COM_BINLOG_DUMP:
+ pay_len := 1 + 4 + 2 + 4
+ if len(argv) > 3 {
+ pay_len += lenBS(argv[3])
+ }
+
+ pw := my.newPktWriter(pay_len)
+ writeByte(pw, cmd)
+ writeU32(pw, argv[0].(uint32)) // Start position
+ writeU16(pw, argv[1].(uint16)) // Flags
+ writeU32(pw, argv[2].(uint32)) // Slave server id
+ if len(argv) > 3 {
+ writeBS(pw, argv[3])
+ }
+
+ // TODO: case COM_REGISTER_SLAVE:
+
+ default:
+ panic("Unknown code for MySQL command")
+ }
+
+ if my.Debug {
+ log.Printf("[%2d <-] Command packet: Cmd=0x%x", my.seq-1, cmd)
+ }
}
View
290 native/consts.go
@@ -4,108 +4,108 @@ import "strconv"
// Client caps - borrowed from GoMySQL
const (
- _CLIENT_LONG_PASSWORD = 1 << iota
- _CLIENT_FOUND_ROWS
- _CLIENT_LONG_FLAG
- _CLIENT_CONNECT_WITH_DB
- _CLIENT_NO_SCHEMA
- _CLIENT_COMPRESS
- _CLIENT_ODBC
- _CLIENT_LOCAL_FILES
- _CLIENT_IGNORE_SPACE
- _CLIENT_PROTOCOL_41
- _CLIENT_INTERACTIVE
- _CLIENT_SSL
- _CLIENT_IGNORE_SIGPIPE
- _CLIENT_TRANSACTIONS
- _CLIENT_RESERVED
- _CLIENT_SECURE_CONN
- _CLIENT_MULTI_STATEMENTS
- _CLIENT_MULTI_RESULTS
+ _CLIENT_LONG_PASSWORD = 1 << iota
+ _CLIENT_FOUND_ROWS
+ _CLIENT_LONG_FLAG
+ _CLIENT_CONNECT_WITH_DB
+ _CLIENT_NO_SCHEMA
+ _CLIENT_COMPRESS
+ _CLIENT_ODBC
+ _CLIENT_LOCAL_FILES
+ _CLIENT_IGNORE_SPACE
+ _CLIENT_PROTOCOL_41
+ _CLIENT_INTERACTIVE
+ _CLIENT_SSL
+ _CLIENT_IGNORE_SIGPIPE
+ _CLIENT_TRANSACTIONS
+ _CLIENT_RESERVED
+ _CLIENT_SECURE_CONN
+ _CLIENT_MULTI_STATEMENTS
+ _CLIENT_MULTI_RESULTS
)
// Commands - borrowed from GoMySQL
const (
- _COM_QUIT = 0x01
- _COM_INIT_DB = 0x02
- _COM_QUERY = 0x03
- _COM_FIELD_LIST = 0x04
- _COM_CREATE_DB = 0x05
- _COM_DROP_DB = 0x06
- _COM_REFRESH = 0x07
- _COM_SHUTDOWN = 0x08
- _COM_STATISTICS = 0x09
- _COM_PROCESS_INFO = 0x0a
- _COM_CONNECT = 0x0b
- _COM_PROCESS_KILL = 0x0c
- _COM_DEBUG = 0x0d
- _COM_PING = 0x0e
- _COM_TIME = 0x0f
- _COM_DELAYED_INSERT = 0x10
- _COM_CHANGE_USER = 0x11
- _COM_BINLOG_DUMP = 0x12
- _COM_TABLE_DUMP = 0x13
- _COM_CONNECT_OUT = 0x14
- _COM_REGISTER_SLAVE = 0x15
- _COM_STMT_PREPARE = 0x16
- _COM_STMT_EXECUTE = 0x17
- _COM_STMT_SEND_LONG_DATA = 0x18
- _COM_STMT_CLOSE = 0x19
- _COM_STMT_RESET = 0x1a
- _COM_SET_OPTION = 0x1b
- _COM_STMT_FETCH = 0x1c
+ _COM_QUIT = 0x01
+ _COM_INIT_DB = 0x02
+ _COM_QUERY = 0x03
+ _COM_FIELD_LIST = 0x04
+ _COM_CREATE_DB = 0x05
+ _COM_DROP_DB = 0x06
+ _COM_REFRESH = 0x07
+ _COM_SHUTDOWN = 0x08
+ _COM_STATISTICS = 0x09
+ _COM_PROCESS_INFO = 0x0a
+ _COM_CONNECT = 0x0b
+ _COM_PROCESS_KILL = 0x0c
+ _COM_DEBUG = 0x0d
+ _COM_PING = 0x0e
+ _COM_TIME = 0x0f
+ _COM_DELAYED_INSERT = 0x10
+ _COM_CHANGE_USER = 0x11
+ _COM_BINLOG_DUMP = 0x12
+ _COM_TABLE_DUMP = 0x13
+ _COM_CONNECT_OUT = 0x14
+ _COM_REGISTER_SLAVE = 0x15
+ _COM_STMT_PREPARE = 0x16
+ _COM_STMT_EXECUTE = 0x17
+ _COM_STMT_SEND_LONG_DATA = 0x18
+ _COM_STMT_CLOSE = 0x19
+ _COM_STMT_RESET = 0x1a
+ _COM_SET_OPTION = 0x1b
+ _COM_STMT_FETCH = 0x1c
)
// Server status
const (
- _SERVER_STATUS_IN_TRANS = 0x01 // Transaction has started
- _SERVER_STATUS_AUTOCOMMIT = 0x02 // Server in auto_commit mode
- _SERVER_STATUS_MORE_RESULTS = 0x04
- _SERVER_MORE_RESULTS_EXISTS = 0x08 // Multi query - next query exists
- _SERVER_QUERY_NO_GOOD_INDEX_USED = 0x10
- _SERVER_QUERY_NO_INDEX_USED = 0x20
- // Server opened a read-only non-scrollable cursor for a query
- _SERVER_STATUS_CURSOR_EXISTS = 0x40
- _SERVER_STATUS_LAST_ROW_SENT = 0x80
+ _SERVER_STATUS_IN_TRANS = 0x01 // Transaction has started
+ _SERVER_STATUS_AUTOCOMMIT = 0x02 // Server in auto_commit mode
+ _SERVER_STATUS_MORE_RESULTS = 0x04
+ _SERVER_MORE_RESULTS_EXISTS = 0x08 // Multi query - next query exists
+ _SERVER_QUERY_NO_GOOD_INDEX_USED = 0x10
+ _SERVER_QUERY_NO_INDEX_USED = 0x20
+ // Server opened a read-only non-scrollable cursor for a query
+ _SERVER_STATUS_CURSOR_EXISTS = 0x40
+ _SERVER_STATUS_LAST_ROW_SENT = 0x80
- _SERVER_STATUS_DB_DROPPED = 0x100
- _SERVER_STATUS_NO_BACKSLASH_ESCAPES = 0x200
+ _SERVER_STATUS_DB_DROPPED = 0x100
+ _SERVER_STATUS_NO_BACKSLASH_ESCAPES = 0x200
)
// MySQL protocol types.
//
// mymysql uses only some of them for send data to the MySQL server. Used
// MySQL types are marked with a comment contains mymysql type that uses it.
const (
- MYSQL_TYPE_DECIMAL = 0x00
- MYSQL_TYPE_TINY = 0x01 // int8, uint8
- MYSQL_TYPE_SHORT = 0x02 // int16, uint16
- MYSQL_TYPE_LONG = 0x03 // int32, uint32
- MYSQL_TYPE_FLOAT = 0x04 // float32
- MYSQL_TYPE_DOUBLE = 0x05 // float64
- MYSQL_TYPE_NULL = 0x06 // nil
- MYSQL_TYPE_TIMESTAMP = 0x07 // *Timestamp
- MYSQL_TYPE_LONGLONG = 0x08 // int64, uint64
- MYSQL_TYPE_INT24 = 0x09
- MYSQL_TYPE_DATE = 0x0a // *Date
- MYSQL_TYPE_TIME = 0x0b // Time
- MYSQL_TYPE_DATETIME = 0x0c // *Datetime
- MYSQL_TYPE_YEAR = 0x0d
- MYSQL_TYPE_NEWDATE = 0x0e
- MYSQL_TYPE_VARCHAR = 0x0f
- MYSQL_TYPE_BIT = 0x10
- MYSQL_TYPE_NEWDECIMAL = 0xf6
- MYSQL_TYPE_ENUM = 0xf7
- MYSQL_TYPE_SET = 0xf8
- MYSQL_TYPE_TINY_BLOB = 0xf9
- MYSQL_TYPE_MEDIUM_BLOB = 0xfa
- MYSQL_TYPE_LONG_BLOB = 0xfb
- MYSQL_TYPE_BLOB = 0xfc // Blob
- MYSQL_TYPE_VAR_STRING = 0xfd // []byte
- MYSQL_TYPE_STRING = 0xfe // string
- MYSQL_TYPE_GEOMETRY = 0xff
+ MYSQL_TYPE_DECIMAL = 0x00
+ MYSQL_TYPE_TINY = 0x01 // int8, uint8
+ MYSQL_TYPE_SHORT = 0x02 // int16, uint16
+ MYSQL_TYPE_LONG = 0x03 // int32, uint32
+ MYSQL_TYPE_FLOAT = 0x04 // float32
+ MYSQL_TYPE_DOUBLE = 0x05 // float64
+ MYSQL_TYPE_NULL = 0x06 // nil
+ MYSQL_TYPE_TIMESTAMP = 0x07 // *Timestamp
+ MYSQL_TYPE_LONGLONG = 0x08 // int64, uint64
+ MYSQL_TYPE_INT24 = 0x09
+ MYSQL_TYPE_DATE = 0x0a // *Date
+ MYSQL_TYPE_TIME = 0x0b // Time
+ MYSQL_TYPE_DATETIME = 0x0c // *Datetime
+ MYSQL_TYPE_YEAR = 0x0d
+ MYSQL_TYPE_NEWDATE = 0x0e
+ MYSQL_TYPE_VARCHAR = 0x0f
+ MYSQL_TYPE_BIT = 0x10
+ MYSQL_TYPE_NEWDECIMAL = 0xf6
+ MYSQL_TYPE_ENUM = 0xf7
+ MYSQL_TYPE_SET = 0xf8
+ MYSQL_TYPE_TINY_BLOB = 0xf9
+ MYSQL_TYPE_MEDIUM_BLOB = 0xfa
+ MYSQL_TYPE_LONG_BLOB = 0xfb
+ MYSQL_TYPE_BLOB = 0xfc // Blob
+ MYSQL_TYPE_VAR_STRING = 0xfd // []byte
+ MYSQL_TYPE_STRING = 0xfe // string
+ MYSQL_TYPE_GEOMETRY = 0xff
- MYSQL_UNSIGNED_MASK = uint16(1 << 15)
+ MYSQL_UNSIGNED_MASK = uint16(1 << 15)
)
// Mapping of MySQL types to (prefered) protocol types. Use it if you create
@@ -116,77 +116,77 @@ const (