@@ -85,7 +85,7 @@ func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
85
85
}
86
86
87
87
var server = "127.0.0.1:3013"
88
- var spaceNo = uint32 (512 )
88
+ var spaceNo = uint32 (515 )
89
89
var spaceName = "test"
90
90
var indexNo = uint32 (0 )
91
91
var indexName = "primary"
@@ -693,6 +693,324 @@ func TestClient(t *testing.T) {
693
693
}
694
694
}
695
695
696
+ const (
697
+ createTableQuery = "CREATE TABLE SQL_TEST (id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING COLLATE \" unicode\" DEFAULT NULL);"
698
+ insertQuery = "INSERT INTO SQL_TEST VALUES (?, ?);"
699
+ selectNamedQuery = "SELECT id, name FROM SQL_TEST WHERE id=:id AND name=:name;"
700
+ selectPosQuery = "SELECT id, name FROM SQL_TEST WHERE id=? AND name=?;"
701
+ updateQuery = "UPDATE SQL_TEST SET name=? WHERE id=?;"
702
+ enableFullMetaData = "SET SESSION \" sql_full_metadata\" = true;"
703
+ selectSpanDifQuery = "SELECT id*2, name, id FROM SQL_TEST WHERE name=?;"
704
+ alterQueryIncrement = "ALTER TABLE SQL_TEST RENAME TO SQL_TEST2;"
705
+ insertIncrQuery = "INSERT INTO SQL_TEST2 VALUES (?, ?);"
706
+ deleteQuery = "DELETE FROM SQL_TEST2 WHERE name=?;"
707
+ dropQuery = "DROP TABLE SQL_TEST2;"
708
+ disableFullMetaData = "SET SESSION \" sql_full_metadata\" = false;"
709
+ )
710
+
711
+ func TestSQL (t * testing.T ) {
712
+ // Data for test table
713
+ testData := map [int ]string {
714
+ 1 : "test" ,
715
+ 2 : "test" ,
716
+ 3 : "test" ,
717
+ 4 : "test" ,
718
+ 5 : "test" ,
719
+ }
720
+
721
+ // Check for skip SQL tests if tarantool version < 2.0.0
722
+ isLess , err := test_helpers .IsTarantoolVersionLess (2 , 0 , 0 )
723
+ if err != nil {
724
+ t .Errorf ("Could not check the Tarantool version" )
725
+ return
726
+ }
727
+ if isLess {
728
+ return
729
+ }
730
+
731
+ var resp * Response
732
+ var conn * Connection
733
+
734
+ conn , err = Connect (server , opts )
735
+ if err != nil {
736
+ t .Errorf ("Failed to connect: %s" , err .Error ())
737
+ return
738
+ }
739
+ if conn == nil {
740
+ t .Errorf ("conn is nil after Connect" )
741
+ return
742
+ }
743
+ defer conn .Close ()
744
+
745
+ resp , err = conn .Execute (createTableQuery , []interface {}{})
746
+ if err != nil {
747
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
748
+ }
749
+ if resp == nil {
750
+ t .Errorf ("Response is nil after Execute" )
751
+ }
752
+ if resp .Code != 0 {
753
+ t .Errorf ("Failed to Execute: %d" , resp .Code )
754
+ }
755
+ if resp .SQLInfo .AffectedCount != 1 {
756
+ t .Errorf ("Incorrect count of created spaces: %d" , resp .SQLInfo .AffectedCount )
757
+ }
758
+
759
+ // Create a table with the same name
760
+ resp , err = conn .Execute (createTableQuery , []interface {}{})
761
+ if err == nil {
762
+ t .Errorf ("Must be a failure for creating space with the same name" )
763
+ }
764
+ if resp .Code != ErSpaceExistsCode {
765
+ t .Errorf ("Wrong response code: %d" , resp .Code )
766
+ }
767
+
768
+ // Execute with nil sql bind
769
+ resp , err = conn .Execute (createTableQuery , nil )
770
+ if err == nil {
771
+ t .Errorf ("Must be a failure for creating space with the same name" )
772
+ }
773
+ if resp .Code != IteratorCode {
774
+ t .Errorf ("Wrong response code: %d" , resp .Code )
775
+ }
776
+
777
+ // Execute with empty query
778
+ resp , err = conn .Execute ("" , nil )
779
+ if err == nil {
780
+ t .Errorf ("Must be a failure for creating space with the same name" )
781
+ }
782
+ if resp .Code != IteratorCode {
783
+ t .Errorf ("Wrong response code: %d" , resp .Code )
784
+ }
785
+
786
+ for i := 1 ; i < 5 ; i ++ {
787
+ resp , err = conn .Execute (insertQuery , []interface {}{i , testData [i ]})
788
+ if err != nil {
789
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
790
+ }
791
+ if resp == nil {
792
+ t .Errorf ("Response is nil after Execute" )
793
+ }
794
+ if resp .Code != 0 {
795
+ t .Errorf ("Failed to Execute: %d" , resp .Code )
796
+ }
797
+ if resp .SQLInfo .AffectedCount != 1 {
798
+ t .Errorf ("Incorrect count of affected rows: %d" , resp .SQLInfo .AffectedCount )
799
+ }
800
+ }
801
+
802
+ // Test insert with positioned arguments
803
+ resp , err = conn .Execute (insertQuery , 5 , testData [5 ])
804
+ if err != nil {
805
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
806
+ }
807
+ if resp == nil {
808
+ t .Errorf ("Response is nil after Execute" )
809
+ }
810
+ if resp .Code != 0 {
811
+ t .Errorf ("Failed to Execute: %d" , resp .Code )
812
+ }
813
+ if resp .SQLInfo .AffectedCount != 1 {
814
+ t .Errorf ("Incorrect count of affected rows: %d" , resp .SQLInfo .AffectedCount )
815
+ }
816
+
817
+ // test all types of supported bindings
818
+ // prepare named sql bind
819
+ sqlBind := map [string ]interface {}{
820
+ "id" : 1 ,
821
+ "name" : "test" ,
822
+ }
823
+ sqlBind2 := struct {
824
+ Id int
825
+ Name string
826
+ }{2 , "test" }
827
+
828
+ type kv struct {
829
+ Key string
830
+ Value interface {}
831
+ }
832
+ sqlBind3 := []struct {
833
+ Key string
834
+ Value interface {}
835
+ }{
836
+ kv {"id" , 3 },
837
+ kv {"name" , "test" },
838
+ }
839
+
840
+ // positioned sql bind
841
+ sqlBind4 := []interface {}{
842
+ 4 , "test" ,
843
+ }
844
+
845
+ namedSQLBinds := []interface {}{
846
+ sqlBind ,
847
+ sqlBind2 ,
848
+ sqlBind3 ,
849
+ }
850
+
851
+ for i , bind := range namedSQLBinds {
852
+ resp , err = conn .Execute (selectNamedQuery , bind )
853
+ if err != nil {
854
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
855
+ }
856
+ if resp == nil {
857
+ t .Errorf ("Response is nil after Execute" )
858
+ }
859
+ if resp .Data [0 ] == i && resp .Data [1 ] == testData [i ] {
860
+ t .Errorf ("Select with named arguments failed" )
861
+ }
862
+ if resp .MetaData .ColumnsInfo [0 ].FieldType != "integer" ||
863
+ resp .MetaData .ColumnsInfo [0 ].FieldName != "ID" ||
864
+ resp .MetaData .ColumnsInfo [1 ].FieldType != "string" ||
865
+ resp .MetaData .ColumnsInfo [1 ].FieldName != "NAME" {
866
+ t .Errorf ("Wrong metadata" )
867
+ }
868
+ }
869
+
870
+ resp , err = conn .Execute (selectPosQuery , sqlBind4 )
871
+ if err != nil {
872
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
873
+ }
874
+ if resp == nil {
875
+ t .Errorf ("Response is nil after Execute" )
876
+ }
877
+ if resp .Data [0 ] == 4 && resp .Data [1 ] == testData [4 ] {
878
+ t .Errorf ("Select with positioned arguments failed" )
879
+ }
880
+ if resp .MetaData .ColumnsInfo [0 ].FieldType != "integer" ||
881
+ resp .MetaData .ColumnsInfo [0 ].FieldName != "ID" ||
882
+ resp .MetaData .ColumnsInfo [1 ].FieldType != "string" ||
883
+ resp .MetaData .ColumnsInfo [1 ].FieldName != "NAME" {
884
+ t .Errorf ("Wrong metadata" )
885
+ }
886
+
887
+ resp , err = conn .Execute (selectPosQuery , 5 , testData [5 ])
888
+ if err != nil {
889
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
890
+ }
891
+ if resp == nil {
892
+ t .Errorf ("Response is nil after Execute" )
893
+ }
894
+ if resp .Code != 0 {
895
+ t .Errorf ("Failed to Execute: %d" , resp .Code )
896
+ }
897
+ if resp .Data [0 ] == 5 && resp .Data [1 ] == testData [5 ] {
898
+ t .Errorf ("Select with positioned arguments failed: wrong values received %v" , resp .Data )
899
+ }
900
+ if resp .MetaData .ColumnsInfo [0 ].FieldType != "integer" ||
901
+ resp .MetaData .ColumnsInfo [0 ].FieldName != "ID" ||
902
+ resp .MetaData .ColumnsInfo [1 ].FieldType != "string" ||
903
+ resp .MetaData .ColumnsInfo [1 ].FieldName != "NAME" {
904
+ t .Errorf ("Wrong metadata" )
905
+ }
906
+
907
+ sqlUpdateBind := []interface {}{"test2" , 2 }
908
+ resp , err = conn .Execute (updateQuery , sqlUpdateBind )
909
+ if err != nil {
910
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
911
+ }
912
+ if resp == nil {
913
+ t .Errorf ("Response is nil after Execute" )
914
+ }
915
+ if resp .SQLInfo .AffectedCount != 1 {
916
+ t .Errorf ("Incorrect count of affected rows: %d" , resp .SQLInfo .AffectedCount )
917
+ }
918
+
919
+ // Enable full metadata
920
+ resp , err = conn .Execute (enableFullMetaData , []interface {}{})
921
+ if err != nil {
922
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
923
+ }
924
+ if resp == nil {
925
+ t .Errorf ("Response is nil after Execute" )
926
+ }
927
+ if resp .Code != 0 {
928
+ t .Errorf ("Failed to Execute: %d" , resp .Code )
929
+ }
930
+
931
+ // Check all extended fields coming with metadata
932
+ resp , err = conn .Execute (selectSpanDifQuery , []interface {}{"test2" })
933
+ if err != nil {
934
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
935
+ }
936
+ if resp == nil {
937
+ t .Errorf ("Response is nil after Execute" )
938
+ }
939
+ if resp .Data [0 ] == 4 && resp .Data [1 ] == "test2" {
940
+ t .Errorf ("Select failed" )
941
+ }
942
+ if resp .MetaData .ColumnsInfo [0 ].FieldSpan != "id*2" ||
943
+ resp .MetaData .ColumnsInfo [1 ].FieldSpan != "name" ||
944
+ resp .MetaData .ColumnsInfo [1 ].FieldIsNullable != true ||
945
+ resp .MetaData .ColumnsInfo [1 ].FieldCollation != "unicode" ||
946
+ resp .MetaData .ColumnsInfo [2 ].FieldIsAutoincrement != true {
947
+ t .Errorf ("Wrong metadata: %v" , resp .MetaData )
948
+ }
949
+
950
+ resp , err = conn .Execute (alterQueryIncrement , []interface {}{})
951
+ if err != nil {
952
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
953
+ }
954
+ if resp == nil {
955
+ t .Errorf ("Response is nil after Execute" )
956
+ }
957
+
958
+ sqlBind5 := []interface {}{nil , "test" }
959
+ resp , err = conn .Execute (insertIncrQuery , sqlBind5 )
960
+ if err != nil {
961
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
962
+ }
963
+ if resp == nil {
964
+ t .Errorf ("Response is nil after Execute" )
965
+ }
966
+ if resp .SQLInfo .AffectedCount != 1 {
967
+ t .Errorf ("Incorrect count of affected rows: %d" , resp .SQLInfo .AffectedCount )
968
+ }
969
+ if resp .SQLInfo .InfoAutoincrementIds [0 ] != 6 {
970
+ t .Errorf ("Incorrect autoincrement ids: %v" , resp .SQLInfo .InfoAutoincrementIds )
971
+ }
972
+
973
+ resp , err = conn .Execute (deleteQuery , []interface {}{"test" })
974
+ if err != nil {
975
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
976
+ }
977
+ if resp == nil {
978
+ t .Errorf ("Response is nil after Execute" )
979
+ }
980
+ if resp .Code != 0 {
981
+ t .Errorf ("Failed to Execute: %d" , resp .Code )
982
+ }
983
+ if resp .SQLInfo .AffectedCount != 5 {
984
+ t .Errorf ("Incorrect count of affected rows: %d" , resp .SQLInfo .AffectedCount )
985
+ }
986
+
987
+ resp , err = conn .Execute (dropQuery , []interface {}{})
988
+ if err != nil {
989
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
990
+ }
991
+ if resp == nil {
992
+ t .Errorf ("Response is nil after Execute" )
993
+ }
994
+ if resp .Code != 0 {
995
+ t .Errorf ("Failed to Execute: %d" , resp .Code )
996
+ }
997
+ if resp .SQLInfo .AffectedCount != 1 {
998
+ t .Errorf ("Incorrect count of dropped spaces: %d" , resp .SQLInfo .AffectedCount )
999
+ }
1000
+
1001
+ // Disable full metadata
1002
+ resp , err = conn .Execute (disableFullMetaData , []interface {}{})
1003
+ if err != nil {
1004
+ t .Errorf ("Failed to Execute: %s" , err .Error ())
1005
+ }
1006
+ if resp == nil {
1007
+ t .Errorf ("Response is nil after Execute" )
1008
+ }
1009
+ if resp .Code != 0 {
1010
+ t .Errorf ("Failed to Execute: %d" , resp .Code )
1011
+ }
1012
+ }
1013
+
696
1014
func TestSchema (t * testing.T ) {
697
1015
var err error
698
1016
var conn * Connection
0 commit comments