forked from ret-Phoenix/oscript-sql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQueryResult.cs
More file actions
146 lines (128 loc) · 5.89 KB
/
QueryResult.cs
File metadata and controls
146 lines (128 loc) · 5.89 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
using ScriptEngine.Machine.Contexts;
using ScriptEngine.HostedScript.Library.ValueTable;
using ScriptEngine.Machine;
using System;
using System.Data.Common;
using ScriptEngine.HostedScript.Library.Binary;
namespace OScriptSql
{
/// <summary>
/// Содержит результат выполнения запроса. Предназначен для хранения и обработки полученных данных.
/// </summary>
[ContextClass("РезультатЗапроса", "QueryResult")]
public class QueryResult : AutoContext<QueryResult>
{
private DbDataReader _reader;
/// <summary>
/// Создает новый экземпляр класса РезультатЗапроса.
/// </summary>
public QueryResult()
{
}
/// <summary>
/// Создает новый экземпляр класса РезультатЗапроса.
/// </summary>
/// <param name="reader">Чтение</param>
public QueryResult(DbDataReader reader)
{
_reader = reader;
}
/// <summary>
/// Определяет, есть ли в результате записи
/// </summary>
/// <returns>Булево. Истина - нет ни одной записи; Ложь - если есть записи.</returns>
[ContextMethod("Пустой", "IsEmpty")]
public bool IsEmpty()
{
return !_reader.HasRows;
}
/// <summary>
/// Создает таблицу значений и копирует в нее все записи набора.
/// </summary>
/// <returns>ТаблицаЗначений</returns>
[ContextMethod("Выгрузить", "Unload")]
public ValueTable Unload()
{
ValueTable resultTable = new ValueTable();
for (int ColIdx = 0; ColIdx < _reader.FieldCount; ColIdx++)
{
resultTable.Columns.Add(_reader.GetName(ColIdx));
}
foreach (DbDataRecord record in _reader)
{
ValueTableRow row = resultTable.Add();
for (int ColIdx = 0; ColIdx < _reader.FieldCount; ColIdx++)
{
if (record.IsDBNull(ColIdx))
{
row.Set(ColIdx, ValueFactory.Create());
continue;
}
//Console.WriteLine("queryresult-col-type:" + _reader.GetName(ColIdx) + " = " + record.GetFieldType(ColIdx).ToString() + "::" + record.GetDataTypeName(ColIdx));
if (record.GetFieldType(ColIdx) == typeof(SByte))
{
row.Set(ColIdx, ValueFactory.Create((int)record.GetValue(ColIdx)));
}
else if (record.GetFieldType(ColIdx) == typeof(Int32))
{
row.Set(ColIdx, ValueFactory.Create((int)record.GetValue(ColIdx)));
}
else if (record.GetFieldType(ColIdx) == typeof(Int64))
{
row.Set(ColIdx, ValueFactory.Create(record.GetInt64(ColIdx)));
}
else if (record.GetFieldType(ColIdx) == typeof(Boolean))
{
row.Set(ColIdx, ValueFactory.Create(record.GetBoolean(ColIdx)));
}
else if (record.GetFieldType(ColIdx) == typeof(UInt64))
{
row.Set(ColIdx, ValueFactory.Create(record.GetValue(ColIdx).ToString()));
}
else if (record.GetFieldType(ColIdx).ToString() == "System.Double")
{
double val = record.GetDouble(ColIdx);
row.Set(ColIdx, ValueFactory.Create(val.ToString()));
}
else if (record.GetFieldType(ColIdx) == typeof(Single))
{
float val = record.GetFloat(ColIdx);
row.Set(ColIdx, ValueFactory.Create(val.ToString()));
}
else if (record.GetFieldType(ColIdx) == typeof(Decimal))
{
row.Set(ColIdx, ValueFactory.Create(record.GetDecimal(ColIdx)));
}
else if (record.GetFieldType(ColIdx).ToString() == "System.String")
{
row.Set(ColIdx, ValueFactory.Create(record.GetString(ColIdx)));
}
else if (record.GetFieldType(ColIdx).ToString() == "System.DateTime")
{
row.Set(ColIdx, ValueFactory.Create(record.GetDateTime(ColIdx)));
}
else if (record.GetFieldType(ColIdx).ToString() == "System.Byte[]")
{
var data = (byte[])record[ColIdx];
var newData = new BinaryDataContext(data);
row.Set(ColIdx, ValueFactory.Create(newData));
}
else if (record.GetDataTypeName(ColIdx) == "uniqueidentifier")
{
row.Set(ColIdx, ValueFactory.Create(record.GetValue(ColIdx).ToString()));
}
else if (record.GetDataTypeName(ColIdx) == "tinyint")
{
row.Set(ColIdx, ValueFactory.Create((int)record.GetByte(ColIdx)));
}
else
{
row.Set(ColIdx, ValueFactory.Create(record.GetString(ColIdx)));
}
}
}
_reader.Close();
return resultTable;
}
}
}