@@ -5,77 +5,152 @@ const connection = common.createConnection();
5
5
const assert = require ( 'assert' ) ;
6
6
7
7
connection . query ( 'CREATE TEMPORARY TABLE json_test (json_test JSON)' ) ;
8
- connection . query ( 'INSERT INTO json_test VALUES (?)' , JSON . stringify ( { test : 42 } ) ) ;
8
+ connection . query (
9
+ 'INSERT INTO json_test VALUES (?)' ,
10
+ JSON . stringify ( { test : 42 } ) ,
11
+ ) ;
12
+
13
+ connection . query (
14
+ 'CREATE TEMPORARY TABLE geom_test (p POINT, g GEOMETRY NOT NULL)' ,
15
+ ) ;
16
+ connection . query (
17
+ 'INSERT INTO geom_test VALUES (ST_GeomFromText("POINT(1 1)"), ' +
18
+ 'ST_GeomFromText("LINESTRING(-71.160281 42.258729,-71.160837 42.259113,-71.161144 42.25932)"))' ,
19
+ ) ;
9
20
10
21
connection . query (
11
22
{
12
23
sql : 'select "foo uppercase" as foo' ,
13
- typeCast : function ( field , next ) {
14
- assert . equal ( " number" , typeof field . length ) ;
24
+ typeCast : function ( field , next ) {
25
+ assert . equal ( ' number' , typeof field . length ) ;
15
26
if ( field . type === 'VAR_STRING' ) {
16
-
17
27
return field . string ( ) . toUpperCase ( ) ;
18
28
}
19
29
return next ( ) ;
20
- }
30
+ } ,
21
31
} ,
22
32
( err , res ) => {
23
33
assert . ifError ( err ) ;
24
34
assert . equal ( res [ 0 ] . foo , 'FOO UPPERCASE' ) ;
25
- }
35
+ } ,
26
36
) ;
27
37
28
38
connection . query (
29
39
{
30
40
sql : 'select "foobar" as foo' ,
31
- typeCast : false
41
+ typeCast : false ,
32
42
} ,
33
43
( err , res ) => {
34
44
assert . ifError ( err ) ;
35
45
assert ( Buffer . isBuffer ( res [ 0 ] . foo ) ) ;
36
46
assert . equal ( res [ 0 ] . foo . toString ( 'utf8' ) , 'foobar' ) ;
37
- }
47
+ } ,
38
48
) ;
39
49
40
50
connection . query (
41
51
{
42
52
sql : 'SELECT NULL as test, 6 as value;' ,
43
- typeCast : function ( field , next ) {
53
+ typeCast : function ( field , next ) {
44
54
return next ( ) ;
45
- }
55
+ } ,
46
56
} ,
47
57
( err , _rows ) => {
48
58
assert . ifError ( err ) ;
49
59
assert . equal ( _rows [ 0 ] . test , null ) ;
50
60
assert . equal ( _rows [ 0 ] . value , 6 ) ;
51
- }
61
+ } ,
52
62
) ;
53
63
54
-
55
64
connection . query (
56
65
{
57
66
sql : 'SELECT * from json_test' ,
58
- typeCast : function ( _field , next ) {
67
+ typeCast : function ( _field , next ) {
59
68
return next ( ) ;
60
- }
69
+ } ,
61
70
} ,
62
71
( err , _rows ) => {
63
72
assert . ifError ( err ) ;
64
73
assert . equal ( _rows [ 0 ] . json_test . test , 42 ) ;
65
- }
74
+ } ,
66
75
) ;
67
76
68
77
connection . execute (
69
78
{
70
79
sql : 'SELECT * from json_test' ,
71
- typeCast : function ( _field , next ) {
80
+ typeCast : function ( _field , next ) {
72
81
return next ( ) ;
73
- }
82
+ } ,
74
83
} ,
75
84
( err , _rows ) => {
76
85
assert . ifError ( err ) ;
77
86
assert . equal ( _rows [ 0 ] . json_test . test , 42 ) ;
78
- }
87
+ } ,
88
+ ) ;
89
+
90
+ // read geo fields
91
+ connection . query (
92
+ {
93
+ sql : 'select * from geom_test' ,
94
+ } ,
95
+ ( err , res ) => {
96
+ assert . ifError ( err ) ;
97
+ assert . deepEqual ( { x : 1 , y : 1 } , res [ 0 ] . p ) ;
98
+ assert . deepEqual (
99
+ [
100
+ { x : - 71.160281 , y : 42.258729 } ,
101
+ { x : - 71.160837 , y : 42.259113 } ,
102
+ { x : - 71.161144 , y : 42.25932 } ,
103
+ ] ,
104
+ res [ 0 ] . g ,
105
+ ) ;
106
+ } ,
107
+ ) ;
108
+
109
+ connection . query (
110
+ {
111
+ sql : 'select * from geom_test' ,
112
+ typeCast : function ( field , next ) {
113
+ assert . equal ( 'geom_test' , field . table ) ;
114
+
115
+ if ( field . name === 'p' && field . type === 'GEOMETRY' ) {
116
+ assert . deepEqual ( { x : 1 , y : 1 } , field . geometry ( ) ) ;
117
+ return { x : 2 , y : 2 } ;
118
+ }
119
+
120
+ if ( field . name === 'g' && field . type === 'GEOMETRY' ) {
121
+ assert . deepEqual (
122
+ [
123
+ { x : - 71.160281 , y : 42.258729 } ,
124
+ { x : - 71.160837 , y : 42.259113 } ,
125
+ { x : - 71.161144 , y : 42.25932 } ,
126
+ ] ,
127
+ field . geometry ( ) ,
128
+ ) ;
129
+
130
+ return [
131
+ { x : - 70 , y : 40 } ,
132
+ { x : - 60 , y : 50 } ,
133
+ { x : - 50 , y : 60 } ,
134
+ ] ;
135
+ }
136
+
137
+ assert . fail ( 'should not reach here' ) ;
138
+
139
+ return next ( ) ;
140
+ } ,
141
+ } ,
142
+ ( err , res ) => {
143
+ assert . ifError ( err ) ;
144
+ assert . deepEqual ( { x : 2 , y : 2 } , res [ 0 ] . p ) ;
145
+ assert . deepEqual (
146
+ [
147
+ { x : - 70 , y : 40 } ,
148
+ { x : - 60 , y : 50 } ,
149
+ { x : - 50 , y : 60 } ,
150
+ ] ,
151
+ res [ 0 ] . g ,
152
+ ) ;
153
+ } ,
79
154
) ;
80
155
81
156
connection . end ( ) ;
0 commit comments