diff options
* Renamed a bunch of Data baseclasses for clarity and readability
(Slowly getting there)
Diffstat (limited to 'ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs')
-rw-r--r-- | ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs | 139 |
1 files changed, 0 insertions, 139 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs deleted file mode 100644 index 63e59aa..0000000 --- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs +++ /dev/null | |||
@@ -1,139 +0,0 @@ | |||
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 | |||
27 | using System; | ||
28 | using System.Collections.Generic; | ||
29 | using System.Data; | ||
30 | using System.Data.Common; | ||
31 | |||
32 | namespace 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 | public abstract string CreateParamName(string fieldName); | ||
45 | |||
46 | public DbCommand CreateSelectCommand(TableMapper mapper, DbConnection connection, string fieldName, object key) | ||
47 | { | ||
48 | string table = mapper.TableName; | ||
49 | |||
50 | DbCommand command = connection.CreateCommand(); | ||
51 | |||
52 | string conditionString = CreateCondition(mapper, command, fieldName, key); | ||
53 | |||
54 | string query = | ||
55 | String.Format("select * from {0} where {1}", table, conditionString); | ||
56 | |||
57 | command.CommandText = query; | ||
58 | command.CommandType = CommandType.Text; | ||
59 | |||
60 | return command; | ||
61 | } | ||
62 | |||
63 | public string CreateCondition(TableMapper mapper, DbCommand command, string fieldName, object key) | ||
64 | { | ||
65 | string keyFieldParamName = mapper.CreateParamName(fieldName); | ||
66 | |||
67 | DbParameter param = command.CreateParameter(); | ||
68 | param.ParameterName = keyFieldParamName; | ||
69 | param.Value = ConvertToDbType(key); | ||
70 | command.Parameters.Add(param); | ||
71 | |||
72 | return String.Format("{0}={1}", fieldName, keyFieldParamName); | ||
73 | } | ||
74 | |||
75 | public DbCommand CreateUpdateCommand(TableMapper mapper, DbConnection connection, object rowMapper, object primaryKey) | ||
76 | { | ||
77 | string table = mapper.TableName; | ||
78 | |||
79 | List<string> fieldNames = new List<string>(); | ||
80 | |||
81 | DbCommand command = connection.CreateCommand(); | ||
82 | |||
83 | foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values) | ||
84 | { | ||
85 | if (fieldMapper != mapper.KeyFieldMapper) | ||
86 | { | ||
87 | fieldMapper.ExpandField(rowMapper, command, fieldNames); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | List<string> assignments = new List<string>(); | ||
92 | |||
93 | foreach (string field in fieldNames) | ||
94 | { | ||
95 | assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field))); | ||
96 | } | ||
97 | |||
98 | string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey); | ||
99 | |||
100 | command.CommandText = | ||
101 | String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()), | ||
102 | conditionString); | ||
103 | |||
104 | return command; | ||
105 | } | ||
106 | |||
107 | public DbCommand CreateInsertCommand(TableMapper mapper, DbConnection connection, object obj) | ||
108 | { | ||
109 | string table = mapper.TableName; | ||
110 | |||
111 | List<string> fieldNames = new List<string>(); | ||
112 | |||
113 | DbCommand command = connection.CreateCommand(); | ||
114 | |||
115 | foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values) | ||
116 | { | ||
117 | fieldMapper.ExpandField(obj, command, fieldNames); | ||
118 | } | ||
119 | |||
120 | List<string> paramNames = new List<string>(); | ||
121 | |||
122 | foreach (string field in fieldNames) | ||
123 | { | ||
124 | paramNames.Add(mapper.CreateParamName(field)); | ||
125 | } | ||
126 | |||
127 | command.CommandText = | ||
128 | String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()), | ||
129 | String.Join(", ", paramNames.ToArray())); | ||
130 | |||
131 | return command; | ||
132 | } | ||
133 | |||
134 | public virtual object ConvertToDbType(object value) | ||
135 | { | ||
136 | return value; | ||
137 | } | ||
138 | } | ||
139 | } \ No newline at end of file | ||