@@ -11,7 +11,7 @@ char *Execute(char *query, char *format) {
1111 char *localQuery;
1212 // Total 4 = 3 arguments + 1 programm name
1313 int argc = 4 ;
14- struct local_result *result;
14+ struct local_result *result;
1515
1616 // Format
1717 snprintf (dataFormat, sizeof (dataFormat), " --format=%s" , format);
@@ -39,6 +39,46 @@ char *Execute(char *query, char *format) {
3939 return result->buf ;
4040}
4141
42+ char *ExecuteSession (char *query, char *format, char *path) {
43+
44+ char * argv[] = {(char *)" clickhouse" , (char *)" --multiquery" , (char *)" --output-format=CSV" , (char *)" --query=" , (char *)" --path=" };
45+ char dataFormat[100 ];
46+ char dataPath[100 ];
47+ char *localQuery;
48+ // Total 4 = 3 arguments + 1 programm name + 1 path for session
49+ int argc = 5 ;
50+ struct local_result *result;
51+
52+ // Format
53+ snprintf (dataFormat, sizeof (dataFormat), " --format=%s" , format);
54+ argv[2 ]=strdup (dataFormat);
55+
56+ // Query - 10 characters + length of query
57+ localQuery = (char *) malloc (strlen (query)+10 );
58+ if (localQuery == NULL ) {
59+
60+ printf (" Out of memmory\n " );
61+ return NULL ;
62+ }
63+
64+ sprintf (localQuery, " --query=%s" , query);
65+ argv[3 ]=strdup (localQuery);
66+ free (localQuery);
67+
68+ // Path
69+ snprintf (dataPath, sizeof (dataPath), " --path=%s" , path);
70+ argv[4 ]=strdup (dataFormat);
71+
72+ // Main query and result
73+ result = query_stable (argc, argv);
74+
75+ // Free it
76+ free (argv[2 ]);
77+ free (argv[3 ]);
78+
79+ return result->buf ;
80+ }
81+
4282Napi::Value ExecuteWrapped (const Napi::CallbackInfo& info) {
4383 Napi::Env env = info.Env ();
4484
@@ -61,9 +101,34 @@ Napi::Value ExecuteWrapped(const Napi::CallbackInfo& info) {
61101 return returnValue;
62102}
63103
104+ Napi::Value SessionWrapped (const Napi::CallbackInfo& info) {
105+ Napi::Env env = info.Env ();
106+
107+ if (info.Length () < 2 || !info[0 ].IsString () || !info[1 ].IsString ()) {
108+ Napi::TypeError::New (env, " String expected" ).ThrowAsJavaScriptException ();
109+ return env.Null ();
110+ }
111+
112+ std::string query = info[0 ].As <Napi::String>();
113+ std::string format = info[1 ].As <Napi::String>();
114+ std::string path = info[2 ].As <Napi::String>();
115+
116+ char *result = ExecuteSession ((char *)query.c_str (), (char *)format.c_str (), (char *)path.c_str ());
117+ if (result == NULL ) {
118+ Napi::TypeError::New (env, " Out of memory" ).ThrowAsJavaScriptException ();
119+ return env.Null ();
120+ }
121+
122+ Napi::String returnValue = Napi::String::New (env, result);
123+ free (result);
124+ return returnValue;
125+ }
126+
64127Napi::Object Init (Napi::Env env, Napi::Object exports) {
65128 exports.Set (Napi::String::New (env, " Execute" ),
66129 Napi::Function::New (env, ExecuteWrapped));
130+ exports.Set (Napi::String::New (env, " Session" ),
131+ Napi::Function::New (env, SessionWrapped));
67132 return exports;
68133}
69134
0 commit comments