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