1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using System.Data;
|
21
|
|
using Seasar.Extension.ADO;
|
22
|
|
using Seasar.Extension.ADO.Impl;
|
23
|
|
using Seasar.Extension.DataSets.Types;
|
24
|
|
using Seasar.Framework.Util;
|
25
|
|
|
26
|
|
namespace Seasar.Extension.DataSets.Impl
|
27
|
|
{
|
28
|
|
public class DataTableDataReaderHandler : IDataReaderHandler
|
29
|
|
{
|
30
|
|
private string tableName_;
|
31
|
|
|
32
|
10
|
public DataTableDataReaderHandler(string tableName)
|
33
|
|
{
|
34
|
10
|
tableName_ = tableName;
|
35
|
|
}
|
36
|
|
|
37
|
|
#region IDataReaderHandler メンバ
|
38
|
|
|
39
|
10
|
public object Handle(System.Data.IDataReader reader)
|
40
|
|
{
|
41
|
10
|
IPropertyType[] propertyTypes = PropertyTypeUtil.CreatePropertyTypes(reader.GetSchemaTable());
|
42
|
10
|
DataTable table = new DataTable(tableName_);
|
43
|
88
|
for (int i = 0; i < propertyTypes.Length; ++i)
|
44
|
|
{
|
45
|
78
|
String propertyName = propertyTypes[i].PropertyName;
|
46
|
78
|
Type type = ColumnTypes.GetColumnType(propertyTypes[i].PropertyType).GetColumnType();
|
47
|
78
|
table.Columns.Add(propertyName, type);
|
48
|
|
}
|
49
|
44
|
while (reader.Read())
|
50
|
|
{
|
51
|
34
|
AddRow(reader, propertyTypes, table);
|
52
|
|
}
|
53
|
10
|
return table;
|
54
|
|
}
|
55
|
|
|
56
|
|
#endregion
|
57
|
|
|
58
|
34
|
private void AddRow(System.Data.IDataReader reader, IPropertyType[] propertyTypes, DataTable table)
|
59
|
|
{
|
60
|
34
|
DataRow row = table.NewRow();
|
61
|
328
|
for (int i = 0; i < table.Columns.Count; ++i)
|
62
|
|
{
|
63
|
294
|
row[i] = GetValue(reader, i, propertyTypes);
|
64
|
|
}
|
65
|
34
|
table.Rows.Add(row);
|
66
|
|
}
|
67
|
|
|
68
|
294
|
private object GetValue(System.Data.IDataReader reader, int index, IPropertyType[] propertyTypes)
|
69
|
|
{
|
70
|
294
|
Type type = propertyTypes[index].PropertyType;
|
71
|
294
|
object value = propertyTypes[index].ValueType.GetValue(reader, index);
|
72
|
294
|
if (value == null)
|
73
|
|
{
|
74
|
32
|
return DBNull.Value;
|
75
|
|
}
|
76
|
262
|
object ret = ColumnTypes.GetColumnType(type).Convert(value, null);
|
77
|
262
|
if (ret is string)
|
78
|
|
{
|
79
|
67
|
string s = ret as string;
|
80
|
67
|
if (s != null)
|
81
|
|
{
|
82
|
67
|
s = s.TrimEnd(null);
|
83
|
|
}
|
84
|
67
|
if (IsCellBase64Formatted(s))
|
85
|
|
{
|
86
|
0
|
return Convert.FromBase64String(s);
|
87
|
|
}
|
88
|
67
|
return s;
|
89
|
|
}
|
90
|
195
|
return ret;
|
91
|
|
}
|
92
|
|
|
93
|
67
|
private bool IsCellBase64Formatted(string s)
|
94
|
|
{
|
95
|
67
|
return DataSetConstants.BASE64_FORMAT.StartsWith(s);
|
96
|
|
}
|
97
|
|
}
|
98
|
|
}
|
99
|
|
|