20
20
#include "jsquery.h"
21
21
22
22
static int32
23
- copyJsQuery (StringInfo buf ,char * jqBase , int32 jqPos )
23
+ copyJsQuery (StringInfo buf ,JsQueryItemR * jsq )
24
24
{
25
- int32 resPos = buf -> len - VARHDRSZ ; /* position from begining of jsquery data */
26
- JsQueryItemType type ;
27
- int32 nextPos , chld , next ;
25
+ JsQueryItemR elem ;
26
+ int32 next , chld ;
27
+ int32 resPos = buf -> len - VARHDRSZ ; /* position from begining of jsquery data */
28
28
29
29
check_stack_depth ();
30
30
31
- jqPos = readJsQueryHeader (jqBase ,jqPos ,& type ,& nextPos );
32
-
33
- appendStringInfoChar (buf , (char )type );
31
+ appendStringInfoChar (buf , (char )jsq -> type );
34
32
alignStringInfoInt (buf );
35
33
36
- next = (nextPos > 0 ) ?buf -> len :0 ; ;
34
+ next = (jsqGetNext ( jsq , NULL )) ?buf -> len :0 ;
37
35
appendBinaryStringInfo (buf , (char * )& next /* fake value */ ,sizeof (next ));
38
36
39
- switch (type )
37
+ switch (jsq -> type )
40
38
{
41
39
case jqiKey :
42
40
case jqiString :
43
41
{
44
- int32 len ;
42
+ int32 len ;
43
+ char * s ;
45
44
46
- read_int32 ( len , jqBase , jqPos );
45
+ s = jsqGetString ( jsq , & len );
47
46
appendBinaryStringInfo (buf , (char * )& len ,sizeof (len ));
48
- appendBinaryStringInfo (buf ,jqBase + jqPos ,len + 1 /* \0 */ );
47
+ appendBinaryStringInfo (buf ,s ,len + 1 /* \0 */ );
49
48
}
50
49
break ;
51
50
case jqiNumeric :
52
- appendBinaryStringInfo (buf ,jqBase + jqPos ,VARSIZE (jqBase + jqPos ));
51
+ {
52
+ Numeric n = jsqGetNumeric (jsq );
53
+
54
+ appendBinaryStringInfo (buf , (char * )n ,VARSIZE_ANY (n ));
55
+ }
53
56
break ;
54
57
case jqiBool :
55
- appendBinaryStringInfo (buf ,jqBase + jqPos ,1 );
58
+ {
59
+ bool v = jsqGetBool (jsq );
60
+
61
+ appendBinaryStringInfo (buf , (char * )& v ,1 );
62
+ }
56
63
break ;
57
64
case jqiArray :
58
65
{
59
- int32 i ,nelems , arrayStart , * arrayPosIn ;
66
+ int32 i ,arrayStart ;
60
67
61
- read_int32 ( nelems , jqBase , jqPos );
62
- appendBinaryStringInfo ( buf , ( char * ) & nelems /* fake value */ , sizeof (nelems ));
68
+ appendBinaryStringInfo ( buf , ( char * ) & jsq -> array . nelems ,
69
+ sizeof (jsq -> array . nelems ));
63
70
64
71
arrayStart = buf -> len ;
65
- arrayPosIn = (int32 * )(jqBase + jqPos );
66
72
67
73
/* reserve place for "pointers" to array's elements */
68
- for (i = 0 ;i < nelems ;i ++ )
74
+ for (i = 0 ;i < jsq -> array . nelems ;i ++ )
69
75
appendBinaryStringInfo (buf , (char * )& i /* fake value */ ,sizeof (i ));
70
76
71
- for ( i = 0 ; i < nelems ; i ++ )
77
+ while ( jsqIterateArray ( jsq , & elem ) )
72
78
{
73
- chld = copyJsQuery (buf ,jqBase , arrayPosIn [ i ] );
79
+ chld = copyJsQuery (buf ,& elem );
74
80
* (int32 * )(buf -> data + arrayStart + i * sizeof (i ))= chld ;
81
+ i ++ ;
75
82
}
76
83
}
77
84
break ;
78
85
case jqiAnd :
79
86
case jqiOr :
80
87
{
81
- int32 leftIn , rightIn , leftOut ,rightOut ;
88
+ int32 leftOut ,rightOut ;
82
89
83
90
leftOut = buf -> len ;
84
91
appendBinaryStringInfo (buf , (char * )& leftOut /* fake value */ ,sizeof (leftOut ));
85
92
rightOut = buf -> len ;
86
93
appendBinaryStringInfo (buf , (char * )& rightOut /* fake value */ ,sizeof (rightOut ));
87
94
88
- read_int32 ( leftIn , jqBase , jqPos );
89
- chld = copyJsQuery (buf ,jqBase , leftIn );
95
+ jsqGetLeftArg ( jsq , & elem );
96
+ chld = copyJsQuery (buf ,& elem );
90
97
* (int32 * )(buf -> data + leftOut )= chld ;
91
98
92
- read_int32 ( rightIn , jqBase , jqPos );
93
- chld = copyJsQuery (buf ,jqBase , rightIn );
99
+ jsqGetRightArg ( jsq , & elem );
100
+ chld = copyJsQuery (buf ,& elem );
94
101
* (int32 * )(buf -> data + rightOut )= chld ;
95
102
}
96
103
break ;
@@ -105,13 +112,12 @@ copyJsQuery(StringInfo buf, char *jqBase, int32 jqPos)
105
112
case jqiOverlap :
106
113
case jqiNot :
107
114
{
108
- int32 argIn , argOut ;
115
+ int32 argOut = buf -> len ;
109
116
110
- argOut = buf -> len ;
111
117
appendBinaryStringInfo (buf , (char * )& argOut /* fake value */ ,sizeof (argOut ));
112
118
113
- read_int32 ( argIn , jqBase , jqPos );
114
- chld = copyJsQuery (buf ,jqBase , argIn );
119
+ jsqGetArg ( jsq , & elem );
120
+ chld = copyJsQuery (buf ,& elem );
115
121
* (int32 * )(buf -> data + argOut )= chld ;
116
122
}
117
123
break ;
@@ -122,11 +128,11 @@ copyJsQuery(StringInfo buf, char *jqBase, int32 jqPos)
122
128
case jqiAnyKey :
123
129
break ;
124
130
default :
125
- elog (ERROR ,"Unknown JsQueryItem type: %d" ,type );
131
+ elog (ERROR ,"Unknown JsQueryItem type: %d" ,jsq -> type );
126
132
}
127
133
128
- if (nextPos )
129
- * (int32 * )(buf -> data + next )= copyJsQuery (buf ,jqBase , nextPos );
134
+ if (jsqGetNext ( jsq , & elem ) )
135
+ * (int32 * )(buf -> data + next )= copyJsQuery (buf ,& elem );
130
136
131
137
return resPos ;
132
138
}
@@ -137,27 +143,32 @@ joinJsQuery(JsQueryItemType type, JsQuery *jq1, JsQuery *jq2)
137
143
JsQuery * out ;
138
144
StringInfoData buf ;
139
145
int32 left ,right ,chld ;
146
+ JsQueryItemR v ;
140
147
141
148
initStringInfo (& buf );
142
149
enlargeStringInfo (& buf ,VARSIZE_ANY (jq1 )+ VARSIZE_ANY (jq2 )+ 4 * sizeof (int32 )+ VARHDRSZ );
143
150
144
151
appendStringInfoSpaces (& buf ,VARHDRSZ );
145
152
153
+ /* form jqiAnd/jqiOr header */
146
154
appendStringInfoChar (& buf , (char )type );
147
155
alignStringInfoInt (& buf );
148
156
149
- /*next */
150
- chld = 0 ;
157
+ /*nextPos field of header */
158
+ chld = 0 ;/* actual value, not a fake */
151
159
appendBinaryStringInfo (& buf , (char * )& chld ,sizeof (chld ));
152
160
153
161
left = buf .len ;
154
162
appendBinaryStringInfo (& buf , (char * )& left /* fake value */ ,sizeof (left ));
155
163
right = buf .len ;
156
164
appendBinaryStringInfo (& buf , (char * )& right /* fake value */ ,sizeof (right ));
157
165
158
- chld = copyJsQuery (& buf ,VARDATA (jq1 ),0 );
166
+ /* dump left and right subtree */
167
+ jsqInit (& v ,jq1 );
168
+ chld = copyJsQuery (& buf ,& v );
159
169
* (int32 * )(buf .data + left )= chld ;
160
- chld = copyJsQuery (& buf ,VARDATA (jq2 ),0 );
170
+ jsqInit (& v ,jq2 );
171
+ chld = copyJsQuery (& buf ,& v );
161
172
* (int32 * )(buf .data + right )= chld ;
162
173
163
174
out = (JsQuery * )buf .data ;
@@ -206,23 +217,26 @@ jsquery_not(PG_FUNCTION_ARGS)
206
217
JsQuery * out ;
207
218
StringInfoData buf ;
208
219
int32 arg ,chld ;
220
+ JsQueryItemR v ;
209
221
210
222
initStringInfo (& buf );
211
223
enlargeStringInfo (& buf ,VARSIZE_ANY (jq )+ 4 * sizeof (int32 )+ VARHDRSZ );
212
224
213
225
appendStringInfoSpaces (& buf ,VARHDRSZ );
214
226
227
+ /* form jsquery header */
215
228
appendStringInfoChar (& buf , (char )jqiNot );
216
229
alignStringInfoInt (& buf );
217
230
218
- /*next */
219
- chld = 0 ;
231
+ /*nextPos field of header */
232
+ chld = 0 ;/* actual value, not a fake */
220
233
appendBinaryStringInfo (& buf , (char * )& chld ,sizeof (chld ));
221
234
222
235
arg = buf .len ;
223
236
appendBinaryStringInfo (& buf , (char * )& arg /* fake value */ ,sizeof (arg ));
224
237
225
- chld = copyJsQuery (& buf ,VARDATA (jq ),0 );
238
+ jsqInit (& v ,jq );
239
+ chld = copyJsQuery (& buf ,& v );
226
240
* (int32 * )(buf .data + arg )= chld ;
227
241
228
242
out = (JsQuery * )buf .data ;