diff options
Diffstat (limited to 'ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs')
-rw-r--r-- | ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs | 268 |
1 files changed, 134 insertions, 134 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs index fe31177..0b9d16d 100644 --- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs | |||
@@ -1,135 +1,135 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (c) Tribal Media AB, http://tribalmedia.se/ | 2 | * Copyright (c) Tribal Media AB, http://tribalmedia.se/ |
3 | * | 3 | * |
4 | * Redistribution and use in source and binary forms, with or without | 4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are met: | 5 | * modification, are permitted provided that the following conditions are met: |
6 | * * Redistributions of source code must retain the above copyright | 6 | * * Redistributions of source code must retain the above copyright |
7 | * notice, this list of conditions and the following disclaimer. | 7 | * notice, this list of conditions and the following disclaimer. |
8 | * * Redistributions in binary form must reproduce the above copyright | 8 | * * Redistributions in binary form must reproduce the above copyright |
9 | * notice, this list of conditions and the following disclaimer in the | 9 | * notice, this list of conditions and the following disclaimer in the |
10 | * documentation and/or other materials provided with the distribution. | 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 | 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. | 12 | * derived from this software without specific prior written permission. |
13 | * | 13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | 14 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
17 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | 17 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY |
18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | * | 24 | * |
25 | */ | 25 | */ |
26 | 26 | ||
27 | using System; | 27 | using System; |
28 | using System.Collections.Generic; | 28 | using System.Collections.Generic; |
29 | using System.Data; | 29 | using System.Data; |
30 | using System.Data.Common; | 30 | using System.Data.Common; |
31 | 31 | ||
32 | namespace TribalMedia.Framework.Data | 32 | namespace TribalMedia.Framework.Data |
33 | { | 33 | { |
34 | public abstract class DatabaseMapper | 34 | public abstract class DatabaseMapper |
35 | { | 35 | { |
36 | protected string m_connectionString; | 36 | protected string m_connectionString; |
37 | 37 | ||
38 | public DatabaseMapper(string connectionString) | 38 | public DatabaseMapper(string connectionString) |
39 | { | 39 | { |
40 | m_connectionString = connectionString; | 40 | m_connectionString = connectionString; |
41 | } | 41 | } |
42 | 42 | ||
43 | public abstract DbConnection GetNewConnection(); | 43 | public abstract DbConnection GetNewConnection(); |
44 | 44 | ||
45 | public abstract string CreateParamName(string fieldName); | 45 | public abstract string CreateParamName(string fieldName); |
46 | 46 | ||
47 | public DbCommand CreateSelectCommand(TableMapper mapper, DbConnection connection, string fieldName, object key) | 47 | public DbCommand CreateSelectCommand(TableMapper mapper, DbConnection connection, string fieldName, object key) |
48 | { | 48 | { |
49 | string table = mapper.TableName; | 49 | string table = mapper.TableName; |
50 | 50 | ||
51 | DbCommand command = connection.CreateCommand(); | 51 | DbCommand command = connection.CreateCommand(); |
52 | 52 | ||
53 | string conditionString = CreateCondition(mapper, command, fieldName, key); | 53 | string conditionString = CreateCondition(mapper, command, fieldName, key); |
54 | 54 | ||
55 | string query = | 55 | string query = |
56 | String.Format("select * from {0} where {1}", table, conditionString); | 56 | String.Format("select * from {0} where {1}", table, conditionString); |
57 | 57 | ||
58 | command.CommandText = query; | 58 | command.CommandText = query; |
59 | command.CommandType = CommandType.Text; | 59 | command.CommandType = CommandType.Text; |
60 | 60 | ||
61 | return command; | 61 | return command; |
62 | } | 62 | } |
63 | 63 | ||
64 | public string CreateCondition(TableMapper mapper, DbCommand command, string fieldName, object key) | 64 | public string CreateCondition(TableMapper mapper, DbCommand command, string fieldName, object key) |
65 | { | 65 | { |
66 | string keyFieldParamName = mapper.CreateParamName(fieldName); | 66 | string keyFieldParamName = mapper.CreateParamName(fieldName); |
67 | 67 | ||
68 | DbParameter param = command.CreateParameter(); | 68 | DbParameter param = command.CreateParameter(); |
69 | param.ParameterName = keyFieldParamName; | 69 | param.ParameterName = keyFieldParamName; |
70 | param.Value = FieldMapper.ConvertToDbType(key); | 70 | param.Value = FieldMapper.ConvertToDbType(key); |
71 | command.Parameters.Add(param); | 71 | command.Parameters.Add(param); |
72 | 72 | ||
73 | return String.Format("{0}={1}", fieldName, keyFieldParamName); | 73 | return String.Format("{0}={1}", fieldName, keyFieldParamName); |
74 | } | 74 | } |
75 | 75 | ||
76 | public DbCommand CreateUpdateCommand(TableMapper mapper, DbConnection connection, object rowMapper, object primaryKey) | 76 | public DbCommand CreateUpdateCommand(TableMapper mapper, DbConnection connection, object rowMapper, object primaryKey) |
77 | { | 77 | { |
78 | string table = mapper.TableName; | 78 | string table = mapper.TableName; |
79 | 79 | ||
80 | List<string> fieldNames = new List<string>(); | 80 | List<string> fieldNames = new List<string>(); |
81 | 81 | ||
82 | DbCommand command = connection.CreateCommand(); | 82 | DbCommand command = connection.CreateCommand(); |
83 | 83 | ||
84 | foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values) | 84 | foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values) |
85 | { | 85 | { |
86 | if (fieldMapper != mapper.KeyFieldMapper) | 86 | if (fieldMapper != mapper.KeyFieldMapper) |
87 | { | 87 | { |
88 | fieldMapper.ExpandField(rowMapper, command, fieldNames); | 88 | fieldMapper.ExpandField(rowMapper, command, fieldNames); |
89 | } | 89 | } |
90 | } | 90 | } |
91 | 91 | ||
92 | List<string> assignments = new List<string>(); | 92 | List<string> assignments = new List<string>(); |
93 | 93 | ||
94 | foreach (string field in fieldNames) | 94 | foreach (string field in fieldNames) |
95 | { | 95 | { |
96 | assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field))); | 96 | assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field))); |
97 | } | 97 | } |
98 | 98 | ||
99 | string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey); | 99 | string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey); |
100 | 100 | ||
101 | command.CommandText = | 101 | command.CommandText = |
102 | String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()), | 102 | String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()), |
103 | conditionString); | 103 | conditionString); |
104 | 104 | ||
105 | return command; | 105 | return command; |
106 | } | 106 | } |
107 | 107 | ||
108 | public DbCommand CreateInsertCommand(TableMapper mapper, DbConnection connection, object obj) | 108 | public DbCommand CreateInsertCommand(TableMapper mapper, DbConnection connection, object obj) |
109 | { | 109 | { |
110 | string table = mapper.TableName; | 110 | string table = mapper.TableName; |
111 | 111 | ||
112 | List<string> fieldNames = new List<string>(); | 112 | List<string> fieldNames = new List<string>(); |
113 | 113 | ||
114 | DbCommand command = connection.CreateCommand(); | 114 | DbCommand command = connection.CreateCommand(); |
115 | 115 | ||
116 | foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values) | 116 | foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values) |
117 | { | 117 | { |
118 | fieldMapper.ExpandField(obj, command, fieldNames); | 118 | fieldMapper.ExpandField(obj, command, fieldNames); |
119 | } | 119 | } |
120 | 120 | ||
121 | List<string> paramNames = new List<string>(); | 121 | List<string> paramNames = new List<string>(); |
122 | 122 | ||
123 | foreach (string field in fieldNames) | 123 | foreach (string field in fieldNames) |
124 | { | 124 | { |
125 | paramNames.Add(mapper.CreateParamName(field)); | 125 | paramNames.Add(mapper.CreateParamName(field)); |
126 | } | 126 | } |
127 | 127 | ||
128 | command.CommandText = | 128 | command.CommandText = |
129 | String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()), | 129 | String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()), |
130 | String.Join(", ", paramNames.ToArray())); | 130 | String.Join(", ", paramNames.ToArray())); |
131 | 131 | ||
132 | return command; | 132 | return command; |
133 | } | 133 | } |
134 | } | 134 | } |
135 | } \ No newline at end of file | 135 | } \ No newline at end of file |