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.Collections;
|
21
|
|
using System.Data;
|
22
|
|
using System.Data.Common;
|
23
|
|
using Seasar.Extension.ADO;
|
24
|
|
using Seasar.Framework.Util;
|
25
|
|
using Seasar.Framework.Exceptions;
|
26
|
|
|
27
|
|
namespace Seasar.Extension.ADO.Impl
|
28
|
|
{
|
29
|
|
public class DatabaseMetaDataImpl : IDatabaseMetaData
|
30
|
|
{
|
31
|
|
private IDictionary primaryKeys = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
|
32
|
|
|
33
|
|
private IDictionary columns = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
|
34
|
|
|
35
|
|
private IDataSource dataSource;
|
36
|
|
|
37
|
9
|
public DatabaseMetaDataImpl(IDataSource dataSource)
|
38
|
|
{
|
39
|
9
|
this.dataSource = dataSource;
|
40
|
|
}
|
41
|
|
|
42
|
|
#region IDatabaseMetaData メンバ
|
43
|
|
|
44
|
8
|
public System.Collections.IList GetPrimaryKeySet(string tableName)
|
45
|
|
{
|
46
|
8
|
if(!this.primaryKeys.Contains(tableName)) CreateTableMetaData(tableName);
|
47
|
8
|
return (IList) primaryKeys[tableName];
|
48
|
|
}
|
49
|
|
|
50
|
8
|
public IList GetColumnSet(string tableName)
|
51
|
|
{
|
52
|
8
|
if(!this.columns.Contains(tableName)) CreateTableMetaData(tableName);
|
53
|
8
|
return (IList) columns[tableName];
|
54
|
|
}
|
55
|
|
|
56
|
|
#endregion
|
57
|
|
|
58
|
|
|
59
|
|
|
60
|
|
|
61
|
|
|
62
|
9
|
private void CreateTableMetaData(string tableName)
|
63
|
|
{
|
64
|
9
|
lock(this)
|
65
|
|
{
|
66
|
|
|
67
|
9
|
IDbConnection cn = DataSourceUtil.GetConnection(dataSource);
|
68
|
9
|
try
|
69
|
|
{
|
70
|
|
|
71
|
9
|
string sql = "SELECT * FROM " + tableName;
|
72
|
|
|
73
|
|
|
74
|
9
|
IDbCommand cmd = dataSource.GetCommand(sql, cn);
|
75
|
|
|
76
|
|
|
77
|
9
|
DataSourceUtil.SetTransaction(dataSource, cmd);
|
78
|
|
|
79
|
|
|
80
|
9
|
IDataAdapter adapter = dataSource.GetDataAdapter(cmd);
|
81
|
|
|
82
|
|
|
83
|
9
|
DataTable[] metaDataTables = adapter.FillSchema(new DataSet(), SchemaType.Mapped);
|
84
|
|
|
85
|
9
|
if (metaDataTables.Length == 0)
|
86
|
|
{
|
87
|
|
|
88
|
0
|
throw new SRuntimeException("ESSR0067", new object[] { tableName });
|
89
|
|
}
|
90
|
|
|
91
|
|
|
92
|
9
|
primaryKeys[tableName] = GetPrimaryKeySet(metaDataTables[0].PrimaryKey);
|
93
|
|
|
94
|
|
|
95
|
9
|
columns[tableName] = GetColumnSet(metaDataTables[0].Columns);
|
96
|
|
}
|
97
|
|
finally
|
98
|
|
{
|
99
|
|
|
100
|
9
|
DataSourceUtil.CloseConnection(dataSource, cn);
|
101
|
|
}
|
102
|
|
}
|
103
|
|
}
|
104
|
|
|
105
|
9
|
private IList GetPrimaryKeySet(DataColumn[] primarykeys)
|
106
|
|
{
|
107
|
9
|
IList list = new CaseInsentiveSet();
|
108
|
9
|
foreach (DataColumn pkey in primarykeys)
|
109
|
|
{
|
110
|
9
|
list.Add(pkey.ColumnName);
|
111
|
|
}
|
112
|
9
|
return list;
|
113
|
|
}
|
114
|
|
|
115
|
9
|
private IList GetColumnSet(DataColumnCollection columns)
|
116
|
|
{
|
117
|
9
|
IList list = new CaseInsentiveSet();
|
118
|
9
|
foreach (DataColumn column in columns)
|
119
|
|
{
|
120
|
81
|
list.Add(column.ColumnName);
|
121
|
|
}
|
122
|
9
|
return list;
|
123
|
|
}
|
124
|
|
|
125
|
|
}
|
126
|
|
}
|
127
|
|
|