-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (108 loc) · 3.8 KB
/
Copy pathProgram.cs
File metadata and controls
115 lines (108 loc) · 3.8 KB
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
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2026 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
using QuestDB;
using QuestDB.Qwp.Query;
using System;
using System.Threading.Tasks;
var example = args.Length > 0 ? args[0].ToLowerInvariant() : "basic";
var connStr = Environment.GetEnvironmentVariable("QDB_QUERY")
?? "ws::addr=localhost:9000;target=any;";
switch (example)
{
case "basic":
await RunBasic(connStr);
break;
case "binds":
await RunWithBinds(connStr);
break;
case "errors":
await RunErrorHandling(connStr);
break;
default:
Console.Error.WriteLine($"unknown example: {example}");
Console.Error.WriteLine("usage: example-qwp-query [basic|binds|errors]");
Environment.Exit(2);
return;
}
static async Task RunBasic(string connStr)
{
await using var client = await QueryClient.NewAsync(connStr);
await using var reader = await client.ExecuteReaderAsync("SELECT 1 AS one, 'hello' AS greeting");
while (await reader.ReadBatchAsync())
{
PrintBatch(reader.Current);
}
Console.WriteLine($"-- end (totalRows={reader.TotalRows}) --");
}
static async Task RunWithBinds(string connStr)
{
await using var client = await QueryClient.NewAsync(connStr);
QwpBindSetter binds = b =>
{
b.SetLong(0, 42L);
b.SetVarchar(1, "hello");
};
await using var reader = await client.ExecuteReaderAsync("SELECT $1 AS num, $2 AS s", binds);
while (await reader.ReadBatchAsync())
{
PrintBatch(reader.Current);
}
Console.WriteLine($"-- end (totalRows={reader.TotalRows}) --");
}
static async Task RunErrorHandling(string connStr)
{
await using var client = await QueryClient.NewAsync(connStr);
try
{
await using var reader = await client.ExecuteReaderAsync("SELECT * FROM no_such_table_does_it");
while (await reader.ReadBatchAsync())
{
PrintBatch(reader.Current);
}
}
catch (QwpQueryException ex)
{
// A query-level error ends at a clean frame boundary; the client stays usable.
Console.Error.WriteLine($"-- error {ex.Status} (0x{(byte)ex.Status:X2}): {ex.ServerMessage} --");
}
}
static void PrintBatch(QwpColumnBatch batch)
{
Console.WriteLine($"-- batch_seq={batch.BatchSeq} rows={batch.RowCount} cols={batch.ColumnCount} --");
for (var c = 0; c < batch.ColumnCount; c++)
{
Console.Write($"{batch.GetColumnName(c)}({batch.GetColumnWireType(c)})\t");
}
Console.WriteLine();
for (var r = 0; r < batch.RowCount; r++)
{
for (var c = 0; c < batch.ColumnCount; c++)
{
// Values (and spans) are only valid until the next ReadBatchAsync; printing copies them.
Console.Write(batch.IsNull(c, r) ? "<null>" : batch.GetString(c, r) ?? "<null>");
Console.Write('\t');
}
Console.WriteLine();
}
}