forked from questdb/c-questdb-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_sender_c_example.c
135 lines (107 loc) · 3.61 KB
/
line_sender_c_example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <questdb/ilp/line_sender.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
static bool example(const char* host, const char* port)
{
line_sender_error* err = NULL;
line_sender* sender = NULL;
sender = line_sender_connect("0.0.0.0", host, port, &err);
if (!sender)
goto on_error;
// We prepare all our table names and colum names in advance.
// If we're inserting multiple rows, this allows us to avoid
// re-validating the same strings over and over again.
line_sender_name table_name;
if (!line_sender_name_init(&table_name, 6, "c_cars", &err))
goto on_error;
line_sender_name id_name;
if (!line_sender_name_init(&id_name, 2, "id", &err))
goto on_error;
line_sender_name x_name;
if (!line_sender_name_init(&x_name, 1, "x", &err))
goto on_error;
line_sender_name y_name;
if (!line_sender_name_init(&y_name, 1, "y", &err))
goto on_error;
line_sender_name booked_name;
if (!line_sender_name_init(&booked_name, 6, "booked", &err))
goto on_error;
line_sender_name passengers_name;
if (!line_sender_name_init(&passengers_name, 10, "passengers", &err))
goto on_error;
line_sender_name driver_name;
if (!line_sender_name_init(&driver_name, 6, "driver", &err))
goto on_error;
if (!line_sender_table(sender, table_name, &err))
goto on_error;
line_sender_utf8 id_value;
if (!line_sender_utf8_init(
&id_value,
36,
"d6e5fe92-d19f-482a-a97a-c105f547f721",
&err))
goto on_error;
if (!line_sender_symbol(sender, id_name, id_value, &err))
goto on_error;
if (!line_sender_column_f64(sender, x_name, 30.5, &err))
goto on_error;
if (!line_sender_column_f64(sender, y_name, -150.25, &err))
goto on_error;
if (!line_sender_column_bool(sender, booked_name, true, &err))
goto on_error;
if (!line_sender_column_i64(sender, passengers_name, 3, &err))
goto on_error;
line_sender_utf8 driver_value;
if (!line_sender_utf8_init(
&driver_value,
12,
"Marco Bonino",
&err))
goto on_error;
if (!line_sender_column_str(sender, driver_name, driver_value, &err))
goto on_error;
if (!line_sender_at_now(sender, &err))
goto on_error;
// To insert more records, call `line_sender_table(..)...` again.
if (!line_sender_flush(sender, &err))
goto on_error;
line_sender_close(sender);
return true;
on_error: ;
size_t err_len = 0;
const char* err_msg = line_sender_error_msg(err, &err_len);
fprintf(stderr, "Error running example: %.*s\n", (int)err_len, err_msg);
line_sender_error_free(err);
if (sender)
line_sender_close(sender);
return false;
}
static bool displayed_help(int argc, const char* argv[])
{
for (int index = 1; index < argc; ++index)
{
const char* arg = argv[index];
if ((strncmp(arg, "-h", 2) == 0) || (strncmp(arg, "--help", 6) == 0))
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, "line_sender_c_example: [HOST [PORT]]\n");
fprintf(stderr," HOST: ILP host (defaults to \"localhost\".\n");
fprintf(stderr," PORT: ILP port (defaults to \"9009\".\n");
return true;
}
}
return false;
}
int main(int argc, const char* argv[])
{
if (displayed_help(argc, argv))
return 0;
const char* host = "localhost";
if (argc >= 2)
host = argv[1];
const char* port = "9009";
if (argc >= 3)
port = argv[2];
return !example(host, port);
}