aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/Base/BaseDatabaseConnector.cs
diff options
context:
space:
mode:
authorSean Dague2008-04-02 15:24:31 +0000
committerSean Dague2008-04-02 15:24:31 +0000
commitc52c68f314c67c76c7181a6d0828f476290fbd66 (patch)
tree66ab347502892902a096fa985f31b25738eb1381 /OpenSim/Data/Base/BaseDatabaseConnector.cs
parentreorganizing namespaces to put all the Data stuff into it's own namespace (diff)
downloadopensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.zip
opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.gz
opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.bz2
opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.xz
whole lot more moving
Diffstat (limited to 'OpenSim/Data/Base/BaseDatabaseConnector.cs')
-rw-r--r--OpenSim/Data/Base/BaseDatabaseConnector.cs142
1 files changed, 142 insertions, 0 deletions
diff --git a/OpenSim/Data/Base/BaseDatabaseConnector.cs b/OpenSim/Data/Base/BaseDatabaseConnector.cs
new file mode 100644
index 0000000..fa3a6c3
--- /dev/null
+++ b/OpenSim/Data/Base/BaseDatabaseConnector.cs
@@ -0,0 +1,142 @@
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
28using System;
29using System.Collections.Generic;
30using System.Data;
31using System.Data.Common;
32
33namespace OpenSim.Framework.Data.Base
34{
35 public abstract class BaseDatabaseConnector
36 {
37 protected string m_connectionString;
38
39 public BaseDatabaseConnector(string connectionString)
40 {
41 m_connectionString = connectionString;
42 }
43
44 public abstract DbConnection GetNewConnection();
45 public abstract string CreateParamName(string fieldName);
46
47 public DbCommand CreateSelectCommand(BaseTableMapper mapper, DbConnection connection, string fieldName, object key)
48 {
49 string table = mapper.TableName;
50
51 DbCommand command = connection.CreateCommand();
52
53 string conditionString = CreateCondition(mapper, command, fieldName, key);
54
55 string query =
56 String.Format("select * from {0} where {1}", table, conditionString);
57
58 command.CommandText = query;
59 command.CommandType = CommandType.Text;
60
61 return command;
62 }
63
64 public string CreateCondition(BaseTableMapper mapper, DbCommand command, string fieldName, object key)
65 {
66 string keyFieldParamName = mapper.CreateParamName(fieldName);
67
68 DbParameter param = command.CreateParameter();
69 param.ParameterName = keyFieldParamName;
70 param.Value = ConvertToDbType(key);
71 command.Parameters.Add(param);
72
73 return String.Format("{0}={1}", fieldName, keyFieldParamName);
74 }
75
76 public DbCommand CreateUpdateCommand(BaseTableMapper mapper, DbConnection connection, object rowMapper, object primaryKey)
77 {
78 string table = mapper.TableName;
79
80 List<string> fieldNames = new List<string>();
81
82 DbCommand command = connection.CreateCommand();
83
84 foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values)
85 {
86 if (fieldMapper != mapper.KeyFieldMapper)
87 {
88 fieldMapper.ExpandField(rowMapper, command, fieldNames);
89 }
90 }
91
92 List<string> assignments = new List<string>();
93
94 foreach (string field in fieldNames)
95 {
96 assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field)));
97 }
98
99 string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey);
100
101 command.CommandText =
102 String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()),
103 conditionString);
104
105 return command;
106 }
107
108 public DbCommand CreateInsertCommand(BaseTableMapper mapper, DbConnection connection, object obj)
109 {
110 string table = mapper.TableName;
111
112 List<string> fieldNames = new List<string>();
113
114 DbCommand command = connection.CreateCommand();
115
116 foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values)
117 {
118 fieldMapper.ExpandField(obj, command, fieldNames);
119 }
120
121 List<string> paramNames = new List<string>();
122
123 foreach (string field in fieldNames)
124 {
125 paramNames.Add(mapper.CreateParamName(field));
126 }
127
128 command.CommandText =
129 String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()),
130 String.Join(", ", paramNames.ToArray()));
131
132 return command;
133 }
134
135 public virtual object ConvertToDbType(object value)
136 {
137 return value;
138 }
139
140 public abstract BaseDataReader CreateReader(IDataReader reader);
141 }
142}