diff options
Diffstat (limited to 'OpenSim/Data/Base/BaseDatabaseConnector.cs')
-rw-r--r-- | OpenSim/Data/Base/BaseDatabaseConnector.cs | 185 |
1 files changed, 0 insertions, 185 deletions
diff --git a/OpenSim/Data/Base/BaseDatabaseConnector.cs b/OpenSim/Data/Base/BaseDatabaseConnector.cs deleted file mode 100644 index 542004c..0000000 --- a/OpenSim/Data/Base/BaseDatabaseConnector.cs +++ /dev/null | |||
@@ -1,185 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Data; | ||
31 | using System.Data.Common; | ||
32 | |||
33 | namespace OpenSim.Data.Base | ||
34 | { | ||
35 | /// <summary> | ||
36 | /// | ||
37 | /// </summary> | ||
38 | public abstract class BaseDatabaseConnector | ||
39 | { | ||
40 | protected string m_connectionString; | ||
41 | |||
42 | /// <summary> | ||
43 | /// | ||
44 | /// </summary> | ||
45 | /// <param name="connectionString"></param> | ||
46 | public BaseDatabaseConnector(string connectionString) | ||
47 | { | ||
48 | m_connectionString = connectionString; | ||
49 | } | ||
50 | |||
51 | public abstract DbConnection GetNewConnection(); | ||
52 | public abstract string CreateParamName(string fieldName); | ||
53 | |||
54 | /// <summary> | ||
55 | /// | ||
56 | /// </summary> | ||
57 | /// <param name="mapper"></param> | ||
58 | /// <param name="connection"></param> | ||
59 | /// <param name="fieldName"></param> | ||
60 | /// <param name="key"></param> | ||
61 | /// <returns></returns> | ||
62 | public DbCommand CreateSelectCommand(BaseTableMapper mapper, DbConnection connection, string fieldName, object key) | ||
63 | { | ||
64 | string table = mapper.TableName; | ||
65 | |||
66 | DbCommand command = connection.CreateCommand(); | ||
67 | |||
68 | string conditionString = CreateCondition(mapper, command, fieldName, key); | ||
69 | |||
70 | string query = | ||
71 | String.Format("select * from {0} where {1}", table, conditionString); | ||
72 | |||
73 | command.CommandText = query; | ||
74 | command.CommandType = CommandType.Text; | ||
75 | |||
76 | return command; | ||
77 | } | ||
78 | |||
79 | /// <summary> | ||
80 | /// | ||
81 | /// </summary> | ||
82 | /// <param name="mapper"></param> | ||
83 | /// <param name="command"></param> | ||
84 | /// <param name="fieldName"></param> | ||
85 | /// <param name="key"></param> | ||
86 | /// <returns></returns> | ||
87 | public string CreateCondition(BaseTableMapper mapper, DbCommand command, string fieldName, object key) | ||
88 | { | ||
89 | string keyFieldParamName = mapper.CreateParamName(fieldName); | ||
90 | |||
91 | DbParameter param = command.CreateParameter(); | ||
92 | param.ParameterName = keyFieldParamName; | ||
93 | param.Value = ConvertToDbType(key); | ||
94 | command.Parameters.Add(param); | ||
95 | |||
96 | return String.Format("{0}={1}", fieldName, keyFieldParamName); | ||
97 | } | ||
98 | |||
99 | /// <summary> | ||
100 | /// | ||
101 | /// </summary> | ||
102 | /// <param name="mapper"></param> | ||
103 | /// <param name="connection"></param> | ||
104 | /// <param name="rowMapper"></param> | ||
105 | /// <param name="primaryKey"></param> | ||
106 | /// <returns></returns> | ||
107 | public DbCommand CreateUpdateCommand(BaseTableMapper mapper, DbConnection connection, object rowMapper, object primaryKey) | ||
108 | { | ||
109 | string table = mapper.TableName; | ||
110 | |||
111 | List<string> fieldNames = new List<string>(); | ||
112 | |||
113 | DbCommand command = connection.CreateCommand(); | ||
114 | |||
115 | foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values) | ||
116 | { | ||
117 | if (fieldMapper != mapper.KeyFieldMapper) | ||
118 | { | ||
119 | fieldMapper.ExpandField(rowMapper, command, fieldNames); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | List<string> assignments = new List<string>(); | ||
124 | |||
125 | foreach (string field in fieldNames) | ||
126 | { | ||
127 | assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field))); | ||
128 | } | ||
129 | |||
130 | string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey); | ||
131 | |||
132 | command.CommandText = | ||
133 | String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()), | ||
134 | conditionString); | ||
135 | |||
136 | return command; | ||
137 | } | ||
138 | |||
139 | /// <summary> | ||
140 | /// | ||
141 | /// </summary> | ||
142 | /// <param name="mapper"></param> | ||
143 | /// <param name="connection"></param> | ||
144 | /// <param name="obj"></param> | ||
145 | /// <returns></returns> | ||
146 | public DbCommand CreateInsertCommand(BaseTableMapper mapper, DbConnection connection, object obj) | ||
147 | { | ||
148 | string table = mapper.TableName; | ||
149 | |||
150 | List<string> fieldNames = new List<string>(); | ||
151 | |||
152 | DbCommand command = connection.CreateCommand(); | ||
153 | |||
154 | foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values) | ||
155 | { | ||
156 | fieldMapper.ExpandField(obj, command, fieldNames); | ||
157 | } | ||
158 | |||
159 | List<string> paramNames = new List<string>(); | ||
160 | |||
161 | foreach (string field in fieldNames) | ||
162 | { | ||
163 | paramNames.Add(mapper.CreateParamName(field)); | ||
164 | } | ||
165 | |||
166 | command.CommandText = | ||
167 | String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()), | ||
168 | String.Join(", ", paramNames.ToArray())); | ||
169 | |||
170 | return command; | ||
171 | } | ||
172 | |||
173 | /// <summary> | ||
174 | /// | ||
175 | /// </summary> | ||
176 | /// <param name="value"></param> | ||
177 | /// <returns></returns> | ||
178 | public virtual object ConvertToDbType(object value) | ||
179 | { | ||
180 | return value; | ||
181 | } | ||
182 | |||
183 | public abstract BaseDataReader CreateReader(IDataReader reader); | ||
184 | } | ||
185 | } | ||