diff options
* Renamed a bunch of Data baseclasses for clarity and readability
(Slowly getting there)
Diffstat (limited to 'ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs')
-rw-r--r-- | ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs | 159 |
1 files changed, 0 insertions, 159 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs deleted file mode 100644 index f2ee4f8..0000000 --- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs +++ /dev/null | |||
@@ -1,159 +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.Data; | ||
29 | using System.Data.Common; | ||
30 | |||
31 | namespace TribalMedia.Framework.Data | ||
32 | { | ||
33 | public abstract class ObjectTableMapper<TRowMapper, TPrimaryKey> : TableMapper | ||
34 | { | ||
35 | public ObjectTableMapper(DatabaseMapper database, string tableName) | ||
36 | : base(database, tableName) | ||
37 | { | ||
38 | } | ||
39 | |||
40 | public bool TryGetValue(TPrimaryKey primaryKey, out TRowMapper value) | ||
41 | { | ||
42 | TRowMapper result = default(TRowMapper); | ||
43 | bool success = false; | ||
44 | |||
45 | WithConnection(delegate(DbConnection connection) | ||
46 | { | ||
47 | using ( | ||
48 | DbCommand command = | ||
49 | CreateSelectCommand(connection, KeyFieldMapper.FieldName, primaryKey)) | ||
50 | { | ||
51 | using (IDataReader reader = command.ExecuteReader()) | ||
52 | { | ||
53 | if (reader.Read()) | ||
54 | { | ||
55 | result = FromReader( CreateReader(reader)); | ||
56 | success = true; | ||
57 | } | ||
58 | else | ||
59 | { | ||
60 | success = false; | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | }); | ||
65 | |||
66 | value = result; | ||
67 | |||
68 | return success; | ||
69 | } | ||
70 | |||
71 | public virtual bool Remove(TPrimaryKey id) | ||
72 | { | ||
73 | int deleted = 0; | ||
74 | |||
75 | WithConnection(delegate(DbConnection connection) | ||
76 | { | ||
77 | using ( | ||
78 | DbCommand command = | ||
79 | CreateDeleteCommand(connection, KeyFieldMapper.FieldName, id)) | ||
80 | { | ||
81 | deleted = command.ExecuteNonQuery(); | ||
82 | } | ||
83 | }); | ||
84 | |||
85 | if (deleted == 1) | ||
86 | { | ||
87 | return true; | ||
88 | } | ||
89 | else | ||
90 | { | ||
91 | return false; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | |||
96 | public DbCommand CreateDeleteCommand(DbConnection connection, string fieldName, TPrimaryKey primaryKey) | ||
97 | { | ||
98 | string table = TableName; | ||
99 | |||
100 | DbCommand command = connection.CreateCommand(); | ||
101 | |||
102 | string conditionString = CreateCondition(command, fieldName, primaryKey); | ||
103 | |||
104 | string query = | ||
105 | String.Format("delete from {0} where {1}", table, conditionString); | ||
106 | |||
107 | command.CommandText = query; | ||
108 | command.CommandType = CommandType.Text; | ||
109 | |||
110 | return command; | ||
111 | } | ||
112 | |||
113 | public virtual bool Update(TPrimaryKey primaryKey, TRowMapper value) | ||
114 | { | ||
115 | int updated = 0; | ||
116 | |||
117 | WithConnection(delegate(DbConnection connection) | ||
118 | { | ||
119 | using (DbCommand command = CreateUpdateCommand(connection, value, primaryKey)) | ||
120 | { | ||
121 | updated = command.ExecuteNonQuery(); | ||
122 | } | ||
123 | }); | ||
124 | |||
125 | if (updated == 1) | ||
126 | { | ||
127 | return true; | ||
128 | } | ||
129 | else | ||
130 | { | ||
131 | return false; | ||
132 | } | ||
133 | } | ||
134 | |||
135 | public virtual bool Add(TRowMapper value) | ||
136 | { | ||
137 | int added = 0; | ||
138 | |||
139 | WithConnection(delegate(DbConnection connection) | ||
140 | { | ||
141 | using (DbCommand command = CreateInsertCommand(connection, value)) | ||
142 | { | ||
143 | added = command.ExecuteNonQuery(); | ||
144 | } | ||
145 | }); | ||
146 | |||
147 | if (added == 1) | ||
148 | { | ||
149 | return true; | ||
150 | } | ||
151 | else | ||
152 | { | ||
153 | return false; | ||
154 | } | ||
155 | } | ||
156 | |||
157 | public abstract TRowMapper FromReader(DataReader reader); | ||
158 | } | ||
159 | } \ No newline at end of file | ||