@@ -11,11 +11,11 @@ package chdb
11
11
char *Execute(char *query, char *format) {
12
12
13
13
char * argv[] = {(char *)"clickhouse", (char *)"--multiquery", (char *)"--output-format=CSV", (char *)"--query="};
14
- char dataFormat[100];
14
+ char dataFormat[100];
15
15
char *localQuery;
16
16
// Total 4 = 3 arguments + 1 programm name
17
17
int argc = 4;
18
- struct local_result *result;
18
+ struct local_result *result;
19
19
20
20
// Format
21
21
snprintf(dataFormat, sizeof(dataFormat), "--format=%s", format);
@@ -24,33 +24,94 @@ char *Execute(char *query, char *format) {
24
24
// Query - 10 characters + length of query
25
25
localQuery = (char *) malloc(strlen(query)+10);
26
26
if(localQuery == NULL) {
27
-
27
+
28
+ printf("Out of memmory\n");
29
+ return NULL;
30
+ }
31
+
32
+ sprintf(localQuery, "--query=%s", query);
33
+ argv[3]=strdup(localQuery);
34
+ free(localQuery);
35
+
36
+ // Main query and result
37
+ result = query_stable(argc, argv);
38
+
39
+ //Free it
40
+ free(argv[2]);
41
+ free(argv[3]);
42
+
43
+ return result->buf;
44
+ }
45
+
46
+ char *Session(char *query, char *format, char* path) {
47
+
48
+ char * argv[] = {(char *)"clickhouse", (char *)"--multiquery", (char *)"--output-format=CSV", (char *)"--query=", (char *)"--path=/tmp/"};
49
+ char dataFormat[100];
50
+ char dataPath[100];
51
+ char *localQuery;
52
+ // Total 4 = 4 arguments + 1 programm name
53
+ int argc = 5;
54
+ struct local_result *result;
55
+
56
+ // Format
57
+ snprintf(dataFormat, sizeof(dataFormat), "--format=%s", format);
58
+ argv[2]=strdup(dataFormat);
59
+
60
+ // Query - 10 characters + length of query
61
+ localQuery = (char *) malloc(strlen(query)+10);
62
+ if(localQuery == NULL) {
63
+
28
64
printf("Out of memmory\n");
29
65
return NULL;
30
66
}
31
-
67
+
32
68
sprintf(localQuery, "--query=%s", query);
33
69
argv[3]=strdup(localQuery);
34
70
free(localQuery);
35
71
72
+ // Path
73
+ snprintf(dataPath, sizeof(dataPath), "--path=%s", path);
74
+ argv[4]=strdup(dataPath);
75
+
36
76
// Main query and result
37
77
result = query_stable(argc, argv);
38
78
39
79
//Free it
40
80
free(argv[2]);
41
81
free(argv[3]);
82
+ free(argv[4]);
42
83
43
84
return result->buf;
44
85
}
45
86
*/
46
87
import "C"
47
88
import "unsafe"
48
89
49
- func Query (str1 , str2 string ) string {
50
- query := C .CString (str1 )
51
- defer C .free (unsafe .Pointer (query ))
52
- format := C .CString (str2 )
53
- defer C .free (unsafe .Pointer (format ))
54
- resultData := C .Execute (query , format )
55
- return C .GoString (resultData )
90
+ func Query (str1 string , str2 string ) string {
91
+ if str2 == "" {
92
+ str2 = "CSV"
93
+ }
94
+ query := C .CString (str1 )
95
+ defer C .free (unsafe .Pointer (query ))
96
+ format := C .CString (str2 )
97
+ defer C .free (unsafe .Pointer (format ))
98
+ resultData := C .Execute (query , format )
99
+ return C .GoString (resultData )
100
+ }
101
+
102
+ func Session (str1 string , str2 string , str3 string ) string {
103
+ if str3 == "" {
104
+ str3 = "/tmp/"
105
+ }
106
+ if str2 == "" {
107
+ str2 = "CSV"
108
+ }
109
+ query := C .CString (str1 )
110
+ defer C .free (unsafe .Pointer (query ))
111
+ format := C .CString (str2 )
112
+ defer C .free (unsafe .Pointer (format ))
113
+ path := C .CString (str3 )
114
+ defer C .free (unsafe .Pointer (path ))
115
+ resultData := C .Session (query , format , path )
116
+ return C .GoString (resultData )
56
117
}
0 commit comments