diff options
Diffstat (limited to '')
-rw-r--r-- | ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs new file mode 100644 index 0000000..f041e79 --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs | |||
@@ -0,0 +1,108 @@ | |||
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 | using TribalMedia.Framework.Data; | ||
31 | |||
32 | namespace TribalMedia.Framework.Data | ||
33 | { | ||
34 | public abstract class TableMapper | ||
35 | { | ||
36 | private readonly DatabaseMapper m_connectionPool; | ||
37 | private readonly object m_syncRoot = new object(); | ||
38 | |||
39 | protected void WithConnection(Action<DbConnection> action) | ||
40 | { | ||
41 | lock (m_syncRoot) | ||
42 | { | ||
43 | DbConnection m_connection = m_connectionPool.GetNewConnection(); | ||
44 | |||
45 | if (m_connection.State != ConnectionState.Open) | ||
46 | { | ||
47 | m_connection.Open(); | ||
48 | } | ||
49 | |||
50 | action(m_connection); | ||
51 | |||
52 | if (m_connection.State == ConnectionState.Open) | ||
53 | { | ||
54 | m_connection.Close(); | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | |||
59 | private readonly string m_tableName; | ||
60 | public string TableName | ||
61 | { | ||
62 | get { return m_tableName; } | ||
63 | } | ||
64 | |||
65 | private Schema m_schema; | ||
66 | public Schema Schema | ||
67 | { | ||
68 | get { return m_schema; } | ||
69 | } | ||
70 | |||
71 | private FieldMapper m_keyFieldMapper; | ||
72 | public FieldMapper KeyFieldMapper | ||
73 | { | ||
74 | get { return m_keyFieldMapper; } | ||
75 | } | ||
76 | |||
77 | public TableMapper(DatabaseMapper connectionPool, string tableName) | ||
78 | { | ||
79 | m_connectionPool = connectionPool; | ||
80 | m_tableName = tableName.ToLower(); // Stupid MySQL hack. | ||
81 | } | ||
82 | |||
83 | public string CreateParamName(string fieldName) | ||
84 | { | ||
85 | return m_connectionPool.CreateParamName(fieldName); | ||
86 | } | ||
87 | |||
88 | protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey) | ||
89 | { | ||
90 | return m_connectionPool.CreateSelectCommand(this, connection, fieldName, primaryKey); | ||
91 | } | ||
92 | |||
93 | public string CreateCondition(DbCommand command, string fieldName, object key) | ||
94 | { | ||
95 | return m_connectionPool.CreateCondition(this, command, fieldName, key); | ||
96 | } | ||
97 | |||
98 | public DbCommand CreateInsertCommand(DbConnection connection, object obj) | ||
99 | { | ||
100 | return m_connectionPool.CreateInsertCommand(this, connection, obj); | ||
101 | } | ||
102 | |||
103 | public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey) | ||
104 | { | ||
105 | return m_connectionPool.CreateUpdateCommand(this, connection, rowMapper, primaryKey); | ||
106 | } | ||
107 | } | ||
108 | } \ No newline at end of file | ||