@@ -17,6 +17,9 @@ import (
17
17
"time"
18
18
)
19
19
20
+ // Packets documentation:
21
+ // http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol
22
+
20
23
// Read packet to buffer 'data'
21
24
func (mc * mysqlConn ) readPacket () (data []byte , e error ) {
22
25
// Packet Length
@@ -366,7 +369,7 @@ n (until end of packet) message
366
369
*/
367
370
func (mc * mysqlConn ) handleOkPacket (data []byte ) (e error ) {
368
371
if data [0 ] != 0 {
369
- e = errors .New ("Wrong Packet-Type: Not a OK-Packet" )
372
+ e = errors .New ("Wrong Packet-Type: Not an OK-Packet" )
370
373
return
371
374
}
372
375
@@ -451,32 +454,37 @@ func (mc *mysqlConn) readColumns(n int) (columns []*mysqlField, e error) {
451
454
}
452
455
453
456
var pos , n int
454
- var catalog , database , table , orgTable , name , orgName []byte
455
- var defaultVal uint64
457
+ var name []byte
458
+ //var catalog, database, table, orgTable, name, orgName []byte
459
+ //var defaultVal uint64
456
460
457
461
// Catalog
458
- catalog , n , _ , e = readLengthCodedBinary (data )
462
+ //catalog, n, _, e = readLengthCodedBinary(data)
463
+ n , e = readAndDropLengthCodedBinary (data )
459
464
if e != nil {
460
465
return
461
466
}
462
467
pos += n
463
468
464
469
// Database [len coded string]
465
- database , n , _ , e = readLengthCodedBinary (data [pos :])
470
+ //database, n, _, e = readLengthCodedBinary(data[pos:])
471
+ n , e = readAndDropLengthCodedBinary (data [pos :])
466
472
if e != nil {
467
473
return
468
474
}
469
475
pos += n
470
476
471
477
// Table [len coded string]
472
- table , n , _ , e = readLengthCodedBinary (data [pos :])
478
+ //table, n, _, e = readLengthCodedBinary(data[pos:])
479
+ n , e = readAndDropLengthCodedBinary (data [pos :])
473
480
if e != nil {
474
481
return
475
482
}
476
483
pos += n
477
484
478
485
// Original table [len coded string]
479
- orgTable , n , _ , e = readLengthCodedBinary (data [pos :])
486
+ //orgTable, n, _, e = readLengthCodedBinary(data[pos:])
487
+ n , e = readAndDropLengthCodedBinary (data [pos :])
480
488
if e != nil {
481
489
return
482
490
}
@@ -490,7 +498,8 @@ func (mc *mysqlConn) readColumns(n int) (columns []*mysqlField, e error) {
490
498
pos += n
491
499
492
500
// Original name [len coded string]
493
- orgName , n , _ , e = readLengthCodedBinary (data [pos :])
501
+ //orgName, n, _, e = readLengthCodedBinary(data[pos:])
502
+ n , e = readAndDropLengthCodedBinary (data [pos :])
494
503
if e != nil {
495
504
return
496
505
}
@@ -500,11 +509,11 @@ func (mc *mysqlConn) readColumns(n int) (columns []*mysqlField, e error) {
500
509
pos ++
501
510
502
511
// Charset [16 bit uint]
503
- charsetNumber := bytesToUint16 (data [pos : pos + 2 ])
512
+ // charsetNumber := bytesToUint16(data[pos : pos+2])
504
513
pos += 2
505
514
506
515
// Length [32 bit uint]
507
- length := bytesToUint32 (data [pos : pos + 4 ])
516
+ // length := bytesToUint32(data[pos : pos+4])
508
517
pos += 4
509
518
510
519
// Field type [byte]
@@ -513,18 +522,16 @@ func (mc *mysqlConn) readColumns(n int) (columns []*mysqlField, e error) {
513
522
514
523
// Flags [16 bit uint]
515
524
flags := FieldFlag (bytesToUint16 (data [pos : pos + 2 ]))
516
- pos += 2
525
+ // pos += 2
517
526
518
527
// Decimals [8 bit uint]
519
- decimals := data [pos ]
520
- pos ++
528
+ // decimals := data[pos]
529
+ // pos++
521
530
522
531
// Default value [len coded binary]
523
- if pos < len (data ) {
524
- defaultVal , _ , e = bytesToLengthCodedBinary (data [pos :])
525
- }
526
-
527
- fmt .Printf ("catalog=%s database=%s table=%s orgTable=%s name=%s orgName=%s charsetNumber=%d length=%d fieldType=%d flags=%d decimals=%d defaultVal=%d \n " , catalog , database , table , orgTable , name , orgName , charsetNumber , length , fieldType , flags , decimals , defaultVal )
532
+ //if pos < len(data) {
533
+ // defaultVal, _, e = bytesToLengthCodedBinary(data[pos:])
534
+ //}
528
535
529
536
columns = append (columns , & mysqlField {name : string (name ), fieldType : fieldType , flags : flags })
530
537
}
@@ -628,8 +635,8 @@ Prepare OK Packet
628
635
(EOF packet)
629
636
630
637
*/
631
- func (mc * mysqlConn ) readPrepareResultPacket () (stmtID uint32 , columnCount uint16 , paramCount uint16 , e error ) {
632
- data , e := mc .readPacket ()
638
+ func (stmt mysqlStmt ) readPrepareResultPacket () (columnCount uint16 , e error ) {
639
+ data , e := stmt . mc .readPacket ()
633
640
if e != nil {
634
641
return
635
642
}
@@ -638,20 +645,20 @@ func (mc *mysqlConn) readPrepareResultPacket() (stmtID uint32, columnCount uint1
638
645
pos := 0
639
646
640
647
if data [pos ] != 0 {
641
- e = mc .handleErrorPacket (data )
648
+ e = stmt . mc .handleErrorPacket (data )
642
649
return
643
650
}
644
651
pos ++
645
652
646
- stmtID = bytesToUint32 (data [pos : pos + 4 ])
653
+ stmt . id = bytesToUint32 (data [pos : pos + 4 ])
647
654
pos += 4
648
655
649
656
// Column count [16 bit uint]
650
657
columnCount = bytesToUint16 (data [pos : pos + 2 ])
651
658
pos += 2
652
659
653
660
// Param count [16 bit uint]
654
- paramCount = bytesToUint16 (data [pos : pos + 2 ])
661
+ stmt . paramCount = int ( bytesToUint16 (data [pos : pos + 2 ]) )
655
662
pos += 2
656
663
657
664
// Warning count [16 bit uint]
@@ -751,10 +758,20 @@ func (stmt mysqlStmt) buildExecutePacket(args *[]driver.Value) (e error) {
751
758
byte (FIELD_TYPE_NULL ),
752
759
0x0 }... )
753
760
continue
761
+
754
762
case []byte :
755
- fmt .Println ("[]byte" , (* args )[i ])
763
+ data = append (data , []byte {
764
+ byte (FIELD_TYPE_STRING ),
765
+ 0x0 }... )
766
+ val := (* args )[i ].([]byte )
767
+ paramValues = append (paramValues , lengthCodedBinaryToBytes (uint64 (len (val )))... )
768
+ paramValues = append (paramValues , val ... )
769
+ continue
770
+
756
771
case time.Time :
757
- fmt .Println ("time.Time" , (* args )[i ])
772
+ // Format to string for time+date Fields
773
+ // Data is packed in case reflect.String below
774
+ (* args )[i ] = (* args )[i ].(time.Time ).Format (TIME_FORMAT )
758
775
}
759
776
760
777
pv = reflect .ValueOf ((* args )[i ])
@@ -764,10 +781,14 @@ func (stmt mysqlStmt) buildExecutePacket(args *[]driver.Value) (e error) {
764
781
byte (FIELD_TYPE_LONGLONG ),
765
782
0x0 }... )
766
783
paramValues = append (paramValues , int64ToBytes (pv .Int ())... )
767
- fmt . Println ( "int64" , ( * args )[ i ])
784
+ continue
768
785
769
786
case reflect .Float64 :
770
- fmt .Println ("float64" , (* args )[i ])
787
+ data = append (data , []byte {
788
+ byte (FIELD_TYPE_DOUBLE ),
789
+ 0x0 }... )
790
+ paramValues = append (paramValues , float64ToBytes (pv .Float ())... )
791
+ continue
771
792
772
793
case reflect .Bool :
773
794
data = append (data , []byte {
@@ -779,7 +800,7 @@ func (stmt mysqlStmt) buildExecutePacket(args *[]driver.Value) (e error) {
779
800
} else {
780
801
paramValues = append (paramValues , byte (0 ))
781
802
}
782
- fmt . Println ( "bool" , ( * args )[ i ])
803
+ continue
783
804
784
805
case reflect .String :
785
806
data = append (data , []byte {
@@ -788,7 +809,7 @@ func (stmt mysqlStmt) buildExecutePacket(args *[]driver.Value) (e error) {
788
809
val := pv .String ()
789
810
paramValues = append (paramValues , lengthCodedBinaryToBytes (uint64 (len (val )))... )
790
811
paramValues = append (paramValues , []byte (val )... )
791
- fmt . Println ( "string" , string ([] byte ( val )))
812
+ continue
792
813
793
814
default :
794
815
return fmt .Errorf ("Invalid Value: %s" , pv .Kind ().String ())
@@ -797,7 +818,6 @@ func (stmt mysqlStmt) buildExecutePacket(args *[]driver.Value) (e error) {
797
818
798
819
// append cached values
799
820
data = append (data , paramValues ... )
800
- fmt .Println ("data" , string (data ))
801
821
}
802
822
803
823
// Save args
@@ -855,7 +875,6 @@ func (mc *mysqlConn) readBinaryRows(rc *rowsContent) (e error) {
855
875
row [i ] = intToByteStr (int64 (int8 (byteToUint8 (data [pos ]))))
856
876
}
857
877
pos ++
858
- fmt .Println ("TINY" , string (* row [i ]))
859
878
860
879
case FIELD_TYPE_SHORT , FIELD_TYPE_YEAR :
861
880
if unsigned {
@@ -864,7 +883,6 @@ func (mc *mysqlConn) readBinaryRows(rc *rowsContent) (e error) {
864
883
row [i ] = intToByteStr (int64 (int16 (bytesToUint16 (data [pos : pos + 2 ]))))
865
884
}
866
885
pos += 2
867
- fmt .Println ("SHORT" , string (* row [i ]))
868
886
869
887
case FIELD_TYPE_INT24 , FIELD_TYPE_LONG :
870
888
if unsigned {
@@ -873,7 +891,6 @@ func (mc *mysqlConn) readBinaryRows(rc *rowsContent) (e error) {
873
891
row [i ] = intToByteStr (int64 (int32 (bytesToUint32 (data [pos : pos + 4 ]))))
874
892
}
875
893
pos += 4
876
- fmt .Println ("LONG" , string (* row [i ]))
877
894
878
895
case FIELD_TYPE_LONGLONG :
879
896
if unsigned {
@@ -882,17 +899,14 @@ func (mc *mysqlConn) readBinaryRows(rc *rowsContent) (e error) {
882
899
row [i ] = intToByteStr (int64 (bytesToUint64 (data [pos : pos + 8 ])))
883
900
}
884
901
pos += 8
885
- fmt .Println ("LONGLONG" , string (* row [i ]))
886
902
887
903
case FIELD_TYPE_FLOAT :
888
904
row [i ] = float32ToByteStr (bytesToFloat32 (data [pos : pos + 4 ]))
889
905
pos += 4
890
- fmt .Println ("FLOAT" , string (* row [i ]))
891
906
892
907
case FIELD_TYPE_DOUBLE :
893
908
row [i ] = float64ToByteStr (bytesToFloat64 (data [pos : pos + 8 ]))
894
909
pos += 8
895
- fmt .Println ("DOUBLE" , string (* row [i ]))
896
910
897
911
case FIELD_TYPE_DECIMAL , FIELD_TYPE_NEWDECIMAL :
898
912
var tmp []byte
@@ -903,10 +917,8 @@ func (mc *mysqlConn) readBinaryRows(rc *rowsContent) (e error) {
903
917
904
918
if isNull && rc .columns [i ].flags & FLAG_NOT_NULL == 0 {
905
919
row [i ] = nil
906
- fmt .Println ("DECIMAL" , nil )
907
920
} else {
908
921
row [i ] = & tmp
909
- fmt .Println ("DECIMAL" , string (tmp ))
910
922
}
911
923
pos += n
912
924
@@ -923,10 +935,8 @@ func (mc *mysqlConn) readBinaryRows(rc *rowsContent) (e error) {
923
935
924
936
if isNull && rc .columns [i ].flags & FLAG_NOT_NULL == 0 {
925
937
row [i ] = nil
926
- fmt .Println ("STRING" , nil )
927
938
} else {
928
939
row [i ] = & tmp
929
- fmt .Println ("STRING" , string (tmp ))
930
940
}
931
941
pos += n
932
942
@@ -950,7 +960,6 @@ func (mc *mysqlConn) readBinaryRows(rc *rowsContent) (e error) {
950
960
}
951
961
row [i ] = & tmp
952
962
pos += int (num )
953
- fmt .Println ("DATE" , string (* row [i ]))
954
963
955
964
// Time HH:MM:SS
956
965
case FIELD_TYPE_TIME :
@@ -971,7 +980,6 @@ func (mc *mysqlConn) readBinaryRows(rc *rowsContent) (e error) {
971
980
}
972
981
row [i ] = & tmp
973
982
pos += n + int (num )
974
- fmt .Println ("TIME" , string (* row [i ]))
975
983
976
984
// Timestamp YYYY-MM-DD HH:MM:SS
977
985
case FIELD_TYPE_TIMESTAMP , FIELD_TYPE_DATETIME :
@@ -996,7 +1004,6 @@ func (mc *mysqlConn) readBinaryRows(rc *rowsContent) (e error) {
996
1004
}
997
1005
row [i ] = & tmp
998
1006
pos += int (num )
999
- fmt .Println ("DATE" , string (* row [i ]))
1000
1007
1001
1008
// Please report if this happens!
1002
1009
default :
0 commit comments