-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathWSStmt.cs
More file actions
246 lines (224 loc) · 8.06 KB
/
WSStmt.cs
File metadata and controls
246 lines (224 loc) · 8.06 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using TDengine.Driver.Impl.WebSocketMethods;
namespace TDengine.Driver.Client.Websocket
{
public class WSStmt : IStmt
{
private readonly ulong _stmt;
private readonly TimeZoneInfo _tz;
private readonly Connection _connection;
private bool _closed;
private long _lastAffected;
private bool _isInsert;
public WSStmt(ulong stmt, TimeZoneInfo tz, Connection connection)
{
_stmt = stmt;
_tz = tz;
_connection = connection;
}
public void Dispose()
{
if (_closed) return;
_closed = true;
if (_connection == null || !_connection.IsAvailable()) return;
try
{
_connection.StmtClose(_stmt);
}
catch (Exception)
{
// ignored
}
}
public void Prepare(string query)
{
var resp = _connection.StmtPrepare(_stmt, query);
_isInsert = resp.IsInsert;
}
public bool IsInsert()
{
return _isInsert;
}
public void SetTableName(string tableName)
{
_connection.StmtSetTableName(_stmt, tableName);
}
public void SetTags(object[] tags)
{
var fields = GetTagFields();
_connection.StmtSetTags(_stmt, fields, tags);
}
public TaosFieldE[] GetTagFields()
{
var resp = _connection.StmtGetTagFields(_stmt);
TaosFieldE[] fields = new TaosFieldE[resp.Fields.Count];
for (int i = 0; i < resp.Fields.Count; i++)
{
fields[i] = new TaosFieldE
{
name = resp.Fields[i].Name,
type = resp.Fields[i].FieldType,
precision = resp.Fields[i].Precision,
scale = resp.Fields[i].Scale,
bytes = resp.Fields[i].Bytes
};
}
return fields;
}
public TaosFieldE[] GetColFields()
{
var resp = _connection.StmtGetColFields(_stmt);
TaosFieldE[] fields = new TaosFieldE[resp.Fields.Count];
for (int i = 0; i < resp.Fields.Count; i++)
{
fields[i] = new TaosFieldE
{
name = resp.Fields[i].Name,
type = resp.Fields[i].FieldType,
precision = resp.Fields[i].Precision,
scale = resp.Fields[i].Scale,
bytes = resp.Fields[i].Bytes
};
}
return fields;
}
public void BindRow(object[] row)
{
if (IsInsert())
{
_connection.StmtBind(_stmt, GetColFields(), row);
}
else
{
var tmpRow = new object[row.Length];
Array.Copy(row, tmpRow, row.Length);
_connection.StmtBind(_stmt, GenerateStmtQueryColFields(tmpRow), tmpRow);
}
}
private TaosFieldE[] GenerateStmtQueryColFields(object[] row)
{
var result = new TaosFieldE[row.Length];
for (int i = 0; i < row.Length; i++)
{
switch (row[i])
{
case bool _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_BOOL
};
break;
case sbyte _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_TINYINT
};
break;
case short _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_SMALLINT
};
break;
case int _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_INT
};
break;
case long _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_BIGINT
};
break;
case byte _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_UTINYINT
};
break;
case ushort _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_USMALLINT
};
break;
case uint _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_UINT
};
break;
case ulong _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_UBIGINT
};
break;
case float _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_FLOAT
};
break;
case double _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_DOUBLE
};
break;
case byte[] _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_BINARY
};
break;
case DateTime val:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_BINARY
};
var time = val.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK");
row[i] = time;
break;
case string _:
result[i] = new TaosFieldE
{
type = (sbyte)TDengineDataType.TSDB_DATA_TYPE_BINARY
};
break;
default:
throw new ArgumentException("Unsupported type, only support basic types and DateTime");
}
}
return result;
}
public void BindColumn(TaosFieldE[] field, params Array[] arrays)
{
_connection.StmtBind(_stmt, field, arrays);
}
public void AddBatch()
{
_connection.StmtAddBatch(_stmt);
}
public void Exec()
{
var resp = _connection.StmtExec(_stmt);
_lastAffected = resp.Affected;
}
public long Affected()
{
return _lastAffected;
}
public IRows Result()
{
if (IsInsert())
{
return new WSRows((int)Affected());
}
var resp = _connection.StmtUseResult(_stmt);
return new WSRows(resp, _connection, _tz);
}
}
}