aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs
diff options
context:
space:
mode:
authorlbsa712008-01-13 19:31:56 +0000
committerlbsa712008-01-13 19:31:56 +0000
commit6946050ada5fd1fb1daff58233ad4dad7bec875a (patch)
tree54b399dec776643e6303194d4c2a3aecedc69914 /ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs
parentAnti-crash fix: Allow int (numbers without dot) when changing Estate terrain ... (diff)
downloadopensim-SC_OLD-6946050ada5fd1fb1daff58233ad4dad7bec875a.zip
opensim-SC_OLD-6946050ada5fd1fb1daff58233ad4dad7bec875a.tar.gz
opensim-SC_OLD-6946050ada5fd1fb1daff58233ad4dad7bec875a.tar.bz2
opensim-SC_OLD-6946050ada5fd1fb1daff58233ad4dad7bec875a.tar.xz
* Added ThirdParty node for external library sources that are shipped with the solution.
* Added conceptual TribalMedia.Framework.Data library; this is meant as a generic database layer abstraction library, that should be specialized into OpenSim.Framework.Data * OpenSim.Framework.Data should subclass FieldMappers to extend LLVector3 and LLQuaternions
Diffstat (limited to 'ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs')
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs135
1 files changed, 135 insertions, 0 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs
new file mode 100644
index 0000000..fe31177
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs
@@ -0,0 +1,135 @@
1/*
2* Copyright (c) Tribal Media AB, http://tribalmedia.se/
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are met:
6* * Redistributions of source code must retain the above copyright
7* notice, this list of conditions and the following disclaimer.
8* * Redistributions in binary form must reproduce the above copyright
9* notice, this list of conditions and the following disclaimer in the
10* documentation and/or other materials provided with the distribution.
11* * The name of Tribal Media AB may not be used to endorse or promote products
12* derived from this software without specific prior written permission.
13*
14* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
15* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
18* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*
25*/
26
27using System;
28using System.Collections.Generic;
29using System.Data;
30using System.Data.Common;
31
32namespace TribalMedia.Framework.Data
33{
34 public abstract class DatabaseMapper
35 {
36 protected string m_connectionString;
37
38 public DatabaseMapper(string connectionString)
39 {
40 m_connectionString = connectionString;
41 }
42
43 public abstract DbConnection GetNewConnection();
44
45 public abstract string CreateParamName(string fieldName);
46
47 public DbCommand CreateSelectCommand(TableMapper 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(TableMapper 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 = FieldMapper.ConvertToDbType(key);
71 command.Parameters.Add(param);
72
73 return String.Format("{0}={1}", fieldName, keyFieldParamName);
74 }
75
76 public DbCommand CreateUpdateCommand(TableMapper 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 (FieldMapper 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(TableMapper 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 (FieldMapper 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} \ No newline at end of file