-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathWSRows.cs
More file actions
194 lines (169 loc) · 6.97 KB
/
WSRows.cs
File metadata and controls
194 lines (169 loc) · 6.97 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
using System;
using System.Collections.Generic;
using System.Text;
using TDengine.Driver.Impl.WebSocketMethods;
using TDengine.Driver.Impl.WebSocketMethods.Protocol;
namespace TDengine.Driver.Client.Websocket
{
public class WSRows : IRows
{
private readonly Connection _connection;
private readonly ulong _resultId;
private bool _freed;
private int _currentRow;
private readonly bool _isUpdate;
private readonly List<TDengineMeta> _metas;
private readonly Encoding _encoding;
private int _blockSize;
private byte[] _block;
private bool _completed;
private readonly BlockReader _blockReader;
public WSRows(int affectedRows)
{
_isUpdate = true;
AffectRows = affectedRows;
}
public WSRows(WSQueryResp result, Connection connection, TimeZoneInfo tz)
{
_connection = connection;
_resultId = result.ResultId;
_isUpdate = result.IsUpdate;
if (_isUpdate)
{
AffectRows = result.AffectedRows;
return;
}
AffectRows = -1;
FieldCount = result.FieldsCount;
_metas = ParseMetas(result);
_encoding = Encoding.UTF8;
_blockReader = new BlockReader(55, FieldCount, result.Precision, result.FieldsTypes, tz);
}
public WSRows(WSStmtUseResultResp result, Connection connection, TimeZoneInfo tz)
{
_connection = connection;
_resultId = result.ResultId;
_isUpdate = false;
AffectRows = -1;
FieldCount = result.FieldsCount;
_metas = ParseMetas(result);
_encoding = Encoding.UTF8;
_blockReader = new BlockReader(55, FieldCount, result.Precision, result.FieldsTypes, tz);
}
private List<TDengineMeta> ParseMetas(WSQueryResp result)
{
List<TDengineMeta> metaList = new List<TDengineMeta>();
for (int i = 0; i < FieldCount; i++)
{
TDengineMeta meta = new TDengineMeta
{
name = result.FieldsNames[i],
type = result.FieldsTypes[i],
size = (int)result.FieldsLengths[i]
};
metaList.Add(meta);
}
return metaList;
}
private List<TDengineMeta> ParseMetas(WSStmtUseResultResp result)
{
List<TDengineMeta> metaList = new List<TDengineMeta>();
for (int i = 0; i < FieldCount; i++)
{
TDengineMeta meta = new TDengineMeta
{
name = result.FieldsNames[i],
type = result.FieldsTypes[i],
size = (int)result.FieldsLengths[i]
};
metaList.Add(meta);
}
return metaList;
}
public bool HasRows => _isUpdate == false;
public int AffectRows { get; }
public int FieldCount { get; }
public void Dispose()
{
if (_freed) return;
_freed = true;
if (_connection == null || !_connection.IsAvailable()) return;
try
{
_connection.FreeResult(_resultId);
}
catch (Exception)
{
// ignored
}
}
public long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length)
{
return _blockReader.GetBytes(_currentRow, ordinal, dataOffset, buffer, bufferOffset, length);
}
public char GetChar(int ordinal)
{
return _blockReader.GetChar(_currentRow, ordinal);
}
public long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length)
{
return _blockReader.GetChars(_currentRow, ordinal, dataOffset, buffer, bufferOffset, length);
}
public string GetDataTypeName(int ordinal) => _metas[ordinal].TypeName();
public object GetValue(int ordinal)
{
return _blockReader.Read(_currentRow, ordinal);
}
public Type GetFieldType(int ordinal) => _metas[ordinal].ScanType();
public int GetFieldSize(int ordinal) => _metas[ordinal].size;
public string GetName(int ordinal) => _metas[ordinal].name;
public int GetOrdinal(string name) => _metas.FindIndex(m => m.name == name);
public bool Read()
{
if (_completed) return false;
if (_block == null)
{
FetchBlock();
return !_completed;
}
_currentRow += 1;
if (_currentRow != _blockSize) return true;
FetchBlock();
return !_completed;
}
private void FetchBlock()
{
var fetchRawBlockResult = _connection.FetchRawBlockBinary(_resultId);
//Flag uint64 //8 0
//Action uint64 //8 8
//Version uint16 //2 16
//Time uint64 //8 18
//ReqID uint64 //8 26
//Code uint32 //4 34
//MessageLen uint32 //4 38
//Message string //MessageLen 42
//ResultID uint64 //8 42 + MessageLen
//Finished bool //1 50 + MessageLen
//RawBlockLength uint32 //4 51 + MessageLen
//RawBlock []byte //RawBlockLength 55 + MessageLen + RawBlockLength
var version = BitConverter.ToUInt16(fetchRawBlockResult, 16);
if (version != 1)
throw new Exception("Unsupported fetch raw block version " + version);
var code = BitConverter.ToUInt32(fetchRawBlockResult, 34);
var messageLen = BitConverter.ToUInt32(fetchRawBlockResult, 38);
var message = _encoding.GetString(fetchRawBlockResult, 42, (int)messageLen);
if (code != 0)
throw new TDengineError((int)code, message);
_completed = BitConverter.ToBoolean(fetchRawBlockResult, 50 + (int)messageLen);
if (_completed)
return;
var rawBlockLength = BitConverter.ToUInt32(fetchRawBlockResult, 51 + (int)messageLen);
if (fetchRawBlockResult.Length != 55 + (int)messageLen + rawBlockLength)
throw new Exception("Invalid fetch raw block result length");
_block = fetchRawBlockResult;
_blockReader.SetBlock(_block);
_blockSize = _blockReader.GetRows();
_currentRow = 0;
}
}
}