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.Text;
|
23
|
|
|
24
|
|
namespace Seasar.Extension.DataSets.States
|
25
|
|
{
|
26
|
|
public class CreatedState : AbstractRowState
|
27
|
|
{
|
28
|
|
private static Hashtable sqlCache_ = new Hashtable();
|
29
|
|
|
30
|
|
private static Hashtable argNamesCache_ = new Hashtable();
|
31
|
|
|
32
|
0
|
public override string ToString()
|
33
|
|
{
|
34
|
|
return DataRowState.Added.ToString();
|
35
|
|
}
|
36
|
|
|
37
|
2
|
protected override string GetSql(DataTable table)
|
38
|
|
{
|
39
|
2
|
string sql = null;
|
40
|
2
|
WeakReference reference = (WeakReference) sqlCache_[table];
|
41
|
2
|
if (reference == null || !reference.IsAlive)
|
42
|
|
{
|
43
|
2
|
sql = CreateSql(table);
|
44
|
2
|
sqlCache_.Add(table, new WeakReference(sql));
|
45
|
|
}
|
46
|
|
else
|
47
|
|
{
|
48
|
0
|
sql = (string) reference.Target;
|
49
|
|
}
|
50
|
2
|
return sql;
|
51
|
|
}
|
52
|
|
|
53
|
2
|
private static string CreateSql(DataTable table)
|
54
|
|
{
|
55
|
2
|
StringBuilder buf = new StringBuilder(100);
|
56
|
2
|
StringBuilder paramBuf = new StringBuilder(100);
|
57
|
2
|
buf.Append("INSERT INTO ");
|
58
|
2
|
buf.Append(table.TableName);
|
59
|
2
|
buf.Append(" (");
|
60
|
2
|
int writableColumnSize = 0;
|
61
|
2
|
foreach (DataColumn column in table.Columns)
|
62
|
|
{
|
63
|
5
|
if (!column.ReadOnly)
|
64
|
|
{
|
65
|
4
|
++writableColumnSize;
|
66
|
4
|
buf.Append(column.ColumnName);
|
67
|
4
|
buf.Append(", ");
|
68
|
|
|
69
|
4
|
paramBuf.Append("@");
|
70
|
4
|
paramBuf.Append(column.ColumnName);
|
71
|
4
|
paramBuf.Append(", ");
|
72
|
|
}
|
73
|
|
}
|
74
|
2
|
buf.Length -= 2;
|
75
|
2
|
buf.Append(") VALUES (");
|
76
|
|
|
77
|
2
|
paramBuf.Length -= 2;
|
78
|
2
|
paramBuf.Append(")");
|
79
|
|
|
80
|
2
|
buf.Append(paramBuf);
|
81
|
|
|
82
|
2
|
return buf.ToString();
|
83
|
|
}
|
84
|
|
|
85
|
2
|
protected override string[] GetArgNames(DataTable table)
|
86
|
|
{
|
87
|
2
|
string[] argNames = null;
|
88
|
2
|
WeakReference reference = (WeakReference) argNamesCache_[table];
|
89
|
2
|
if (reference == null || !reference.IsAlive)
|
90
|
|
{
|
91
|
2
|
argNames = CreateArgNames(table);
|
92
|
2
|
argNamesCache_.Add(table, new WeakReference(argNames));
|
93
|
|
}
|
94
|
|
else
|
95
|
|
{
|
96
|
0
|
argNames = (string[]) reference.Target;
|
97
|
|
}
|
98
|
2
|
return argNames;
|
99
|
|
}
|
100
|
|
|
101
|
2
|
private static string[] CreateArgNames(DataTable table)
|
102
|
|
{
|
103
|
2
|
ArrayList argNames = new ArrayList();
|
104
|
7
|
for (int i = 0; i < table.Columns.Count; ++i)
|
105
|
|
{
|
106
|
5
|
DataColumn column = table.Columns[i];
|
107
|
5
|
if (!column.ReadOnly)
|
108
|
|
{
|
109
|
4
|
argNames.Add(column.ColumnName);
|
110
|
|
}
|
111
|
|
}
|
112
|
2
|
return (string[]) argNames.ToArray(typeof(string));
|
113
|
|
}
|
114
|
|
|
115
|
2
|
protected override object[] GetArgs(DataRow row)
|
116
|
|
{
|
117
|
2
|
DataTable table = row.Table;
|
118
|
2
|
ArrayList bindVariables = new ArrayList();
|
119
|
7
|
for (int i = 0; i < table.Columns.Count; ++i)
|
120
|
|
{
|
121
|
5
|
DataColumn column = table.Columns[i];
|
122
|
5
|
if (!column.ReadOnly)
|
123
|
|
{
|
124
|
4
|
bindVariables.Add(row[i]);
|
125
|
|
}
|
126
|
|
}
|
127
|
2
|
return bindVariables.ToArray();
|
128
|
|
}
|
129
|
|
|
130
|
|
}
|
131
|
|
}
|
132
|
|
|