@@ -17,18 +17,32 @@ void construct_arg(char *dest, const char *prefix, const char *value,
17
17
}
18
18
19
19
// Generalized query function
20
- char *general_query (int argc, char *args[]) {
21
- struct local_result *result = query_stable (argc, args);
20
+ char *general_query (int argc, char *args[], char **error_message ) {
21
+ struct local_result_v2 *result = query_stable_v2 (argc, args);
22
22
23
23
if (result == NULL ) {
24
24
return NULL ;
25
+ }
26
+
27
+ if (result->error_message != NULL ) {
28
+ if (error_message != NULL ) {
29
+ *error_message = strdup (result->error_message );
30
+ }
31
+ free_result_v2 (result);
32
+ return NULL ;
25
33
} else {
26
- return result->buf ;
34
+ if (result->buf == NULL ) {
35
+ free_result_v2 (result);
36
+ return NULL ;
37
+ }
38
+ char *output = strdup (result->buf ); // copy the result buffer
39
+ free_result_v2 (result);
40
+ return output;
27
41
}
28
42
}
29
43
30
44
// Query function without session
31
- char *Query (const char *query, const char *format) {
45
+ char *Query (const char *query, const char *format, char **error_message ) {
32
46
char dataFormat[MAX_FORMAT_LENGTH];
33
47
char *dataQuery;
34
48
char *args[MAX_ARG_COUNT] = {" clickhouse" , " --multiquery" , NULL , NULL };
@@ -45,14 +59,14 @@ char *Query(const char *query, const char *format) {
45
59
strlen (query) + strlen (" --query=" ) + 1 );
46
60
args[3 ] = dataQuery;
47
61
48
- char *result = general_query (argc, args);
62
+ char *result = general_query (argc, args, error_message );
49
63
free (dataQuery);
50
64
return result;
51
65
}
52
66
53
67
// QuerySession function will save the session to the path
54
- // queries with same path will use the same session
55
- char * QuerySession ( const char *query, const char *format, const char *path ) {
68
+ char * QuerySession ( const char *query, const char *format, const char *path,
69
+ char **error_message ) {
56
70
char dataFormat[MAX_FORMAT_LENGTH];
57
71
char dataPath[MAX_PATH_LENGTH];
58
72
char *dataQuery;
@@ -73,7 +87,7 @@ char *QuerySession(const char *query, const char *format, const char *path) {
73
87
construct_arg (dataPath, " --path=" , path, MAX_PATH_LENGTH);
74
88
args[4 ] = dataPath;
75
89
76
- char *result = general_query (argc, args);
90
+ char *result = general_query (argc, args, error_message );
77
91
free (dataQuery);
78
92
return result;
79
93
}
@@ -91,8 +105,17 @@ Napi::String QueryWrapper(const Napi::CallbackInfo &info) {
91
105
std::string query = info[0 ].As <Napi::String>().Utf8Value ();
92
106
std::string format = info[1 ].As <Napi::String>().Utf8Value ();
93
107
108
+ char *error_message = nullptr ;
94
109
// Call the native function
95
- char *result = Query (query.c_str (), format.c_str ());
110
+ char *result = Query (query.c_str (), format.c_str (), &error_message);
111
+
112
+ if (result == NULL ) {
113
+ if (error_message != NULL ) {
114
+ Napi::Error::New (env, error_message).ThrowAsJavaScriptException ();
115
+ free (error_message);
116
+ }
117
+ return Napi::String::New (env, " " );
118
+ }
96
119
97
120
// Return the result
98
121
return Napi::String::New (env, result);
@@ -113,13 +136,16 @@ Napi::String QuerySessionWrapper(const Napi::CallbackInfo &info) {
113
136
std::string format = info[1 ].As <Napi::String>().Utf8Value ();
114
137
std::string path = info[2 ].As <Napi::String>().Utf8Value ();
115
138
116
- // std::cerr << query << std::endl;
117
- // std::cerr << format << std::endl;
118
- // std::cerr << path << std::endl;
139
+ char *error_message = nullptr ;
119
140
// Call the native function
120
- char *result = QuerySession (query.c_str (), format.c_str (), path.c_str ());
141
+ char *result =
142
+ QuerySession (query.c_str (), format.c_str (), path.c_str (), &error_message);
143
+
121
144
if (result == NULL ) {
122
- // std::cerr << "result is null" << std::endl;
145
+ if (error_message != NULL ) {
146
+ Napi::Error::New (env, error_message).ThrowAsJavaScriptException ();
147
+ free (error_message);
148
+ }
123
149
return Napi::String::New (env, " " );
124
150
}
125
151
@@ -134,4 +160,4 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
134
160
return exports;
135
161
}
136
162
137
- NODE_API_MODULE (NODE_GYP_MODULE_NAME, Init)
163
+ NODE_API_MODULE (NODE_GYP_MODULE_NAME, Init)
0 commit comments