diff options
author | Sean Dague | 2008-04-02 15:24:31 +0000 |
---|---|---|
committer | Sean Dague | 2008-04-02 15:24:31 +0000 |
commit | c52c68f314c67c76c7181a6d0828f476290fbd66 (patch) | |
tree | 66ab347502892902a096fa985f31b25738eb1381 /OpenSim/Data/Base | |
parent | reorganizing namespaces to put all the Data stuff into it's own namespace (diff) | |
download | opensim-SC-c52c68f314c67c76c7181a6d0828f476290fbd66.zip opensim-SC-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.gz opensim-SC-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.bz2 opensim-SC-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.xz |
whole lot more moving
Diffstat (limited to 'OpenSim/Data/Base')
-rw-r--r-- | OpenSim/Data/Base/BaseDataReader.cs | 139 | ||||
-rw-r--r-- | OpenSim/Data/Base/BaseDatabaseConnector.cs | 142 | ||||
-rw-r--r-- | OpenSim/Data/Base/BaseFieldMapper.cs | 168 | ||||
-rw-r--r-- | OpenSim/Data/Base/BaseRowMapper.cs | 61 | ||||
-rw-r--r-- | OpenSim/Data/Base/BaseSchema.cs | 69 | ||||
-rw-r--r-- | OpenSim/Data/Base/BaseTableMapper.cs | 281 | ||||
-rw-r--r-- | OpenSim/Data/Base/OpenSim.Framework.Data.Base.snk | bin | 0 -> 596 bytes | |||
-rw-r--r-- | OpenSim/Data/Base/Properties/AssemblyInfo.cs | 67 |
8 files changed, 927 insertions, 0 deletions
diff --git a/OpenSim/Data/Base/BaseDataReader.cs b/OpenSim/Data/Base/BaseDataReader.cs new file mode 100644 index 0000000..3baefcd --- /dev/null +++ b/OpenSim/Data/Base/BaseDataReader.cs | |||
@@ -0,0 +1,139 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Data; | ||
30 | using System.IO; | ||
31 | |||
32 | namespace OpenSim.Framework.Data.Base | ||
33 | { | ||
34 | public abstract class BaseDataReader | ||
35 | { | ||
36 | private readonly IDataReader m_source; | ||
37 | |||
38 | public BaseDataReader(IDataReader source) | ||
39 | { | ||
40 | m_source = source; | ||
41 | } | ||
42 | |||
43 | public object Get(string name) | ||
44 | { | ||
45 | return m_source[name]; | ||
46 | } | ||
47 | |||
48 | public ushort GetUShort(string name) | ||
49 | { | ||
50 | return (ushort)m_source.GetInt32(m_source.GetOrdinal(name)); | ||
51 | } | ||
52 | |||
53 | public byte GetByte(string name) | ||
54 | { | ||
55 | int ordinal = m_source.GetOrdinal(name); | ||
56 | byte value = (byte)m_source.GetInt16(ordinal); | ||
57 | return value; | ||
58 | } | ||
59 | |||
60 | public sbyte GetSByte(string name) | ||
61 | { | ||
62 | return (sbyte)m_source.GetInt16(m_source.GetOrdinal(name)); | ||
63 | } | ||
64 | |||
65 | public float GetFloat(string name) | ||
66 | { | ||
67 | return m_source.GetFloat(m_source.GetOrdinal(name)); | ||
68 | } | ||
69 | |||
70 | public byte[] GetBytes(string name) | ||
71 | { | ||
72 | int ordinal = m_source.GetOrdinal(name); | ||
73 | |||
74 | if (m_source.GetValue(ordinal) == DBNull.Value) | ||
75 | { | ||
76 | return null; | ||
77 | } | ||
78 | |||
79 | byte[] buffer = new byte[16384]; | ||
80 | |||
81 | MemoryStream memStream = new MemoryStream(); | ||
82 | |||
83 | long totalRead = 0; | ||
84 | |||
85 | int bytesRead; | ||
86 | do | ||
87 | { | ||
88 | bytesRead = (int)m_source.GetBytes(ordinal, totalRead, buffer, 0, buffer.Length); | ||
89 | totalRead += bytesRead; | ||
90 | |||
91 | memStream.Write(buffer, 0, bytesRead); | ||
92 | } while (bytesRead == buffer.Length); | ||
93 | |||
94 | return memStream.ToArray(); | ||
95 | } | ||
96 | |||
97 | public string GetString(string name) | ||
98 | { | ||
99 | int ordinal = m_source.GetOrdinal(name); | ||
100 | object value = m_source.GetValue(ordinal); | ||
101 | |||
102 | if (value is DBNull) | ||
103 | { | ||
104 | return null; | ||
105 | } | ||
106 | |||
107 | return (string)value; | ||
108 | } | ||
109 | |||
110 | public bool Read() | ||
111 | { | ||
112 | return m_source.Read(); | ||
113 | } | ||
114 | |||
115 | public virtual Guid GetGuid(string name) | ||
116 | { | ||
117 | return m_source.GetGuid(m_source.GetOrdinal(name)); | ||
118 | } | ||
119 | |||
120 | public UInt32 GetUInt32(string name ) | ||
121 | { | ||
122 | return (UInt32)GetInt32(name); | ||
123 | } | ||
124 | |||
125 | private Int32 GetInt32(string name) | ||
126 | { | ||
127 | int ordinal = m_source.GetOrdinal(name); | ||
128 | int int32 = m_source.GetInt32(ordinal); | ||
129 | return int32; | ||
130 | } | ||
131 | |||
132 | public Int64 GetInt64(string name) | ||
133 | { | ||
134 | int ordinal = m_source.GetOrdinal( name ); | ||
135 | long int64 = m_source.GetInt64(ordinal); | ||
136 | return int64; | ||
137 | } | ||
138 | } | ||
139 | } | ||
diff --git a/OpenSim/Data/Base/BaseDatabaseConnector.cs b/OpenSim/Data/Base/BaseDatabaseConnector.cs new file mode 100644 index 0000000..fa3a6c3 --- /dev/null +++ b/OpenSim/Data/Base/BaseDatabaseConnector.cs | |||
@@ -0,0 +1,142 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Data; | ||
31 | using System.Data.Common; | ||
32 | |||
33 | namespace OpenSim.Framework.Data.Base | ||
34 | { | ||
35 | public abstract class BaseDatabaseConnector | ||
36 | { | ||
37 | protected string m_connectionString; | ||
38 | |||
39 | public BaseDatabaseConnector(string connectionString) | ||
40 | { | ||
41 | m_connectionString = connectionString; | ||
42 | } | ||
43 | |||
44 | public abstract DbConnection GetNewConnection(); | ||
45 | public abstract string CreateParamName(string fieldName); | ||
46 | |||
47 | public DbCommand CreateSelectCommand(BaseTableMapper mapper, DbConnection connection, string fieldName, object key) | ||
48 | { | ||
49 | string table = mapper.TableName; | ||
50 | |||
51 | DbCommand command = connection.CreateCommand(); | ||
52 | |||
53 | string conditionString = CreateCondition(mapper, command, fieldName, key); | ||
54 | |||
55 | string query = | ||
56 | String.Format("select * from {0} where {1}", table, conditionString); | ||
57 | |||
58 | command.CommandText = query; | ||
59 | command.CommandType = CommandType.Text; | ||
60 | |||
61 | return command; | ||
62 | } | ||
63 | |||
64 | public string CreateCondition(BaseTableMapper mapper, DbCommand command, string fieldName, object key) | ||
65 | { | ||
66 | string keyFieldParamName = mapper.CreateParamName(fieldName); | ||
67 | |||
68 | DbParameter param = command.CreateParameter(); | ||
69 | param.ParameterName = keyFieldParamName; | ||
70 | param.Value = ConvertToDbType(key); | ||
71 | command.Parameters.Add(param); | ||
72 | |||
73 | return String.Format("{0}={1}", fieldName, keyFieldParamName); | ||
74 | } | ||
75 | |||
76 | public DbCommand CreateUpdateCommand(BaseTableMapper mapper, DbConnection connection, object rowMapper, object primaryKey) | ||
77 | { | ||
78 | string table = mapper.TableName; | ||
79 | |||
80 | List<string> fieldNames = new List<string>(); | ||
81 | |||
82 | DbCommand command = connection.CreateCommand(); | ||
83 | |||
84 | foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values) | ||
85 | { | ||
86 | if (fieldMapper != mapper.KeyFieldMapper) | ||
87 | { | ||
88 | fieldMapper.ExpandField(rowMapper, command, fieldNames); | ||
89 | } | ||
90 | } | ||
91 | |||
92 | List<string> assignments = new List<string>(); | ||
93 | |||
94 | foreach (string field in fieldNames) | ||
95 | { | ||
96 | assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field))); | ||
97 | } | ||
98 | |||
99 | string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey); | ||
100 | |||
101 | command.CommandText = | ||
102 | String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()), | ||
103 | conditionString); | ||
104 | |||
105 | return command; | ||
106 | } | ||
107 | |||
108 | public DbCommand CreateInsertCommand(BaseTableMapper mapper, DbConnection connection, object obj) | ||
109 | { | ||
110 | string table = mapper.TableName; | ||
111 | |||
112 | List<string> fieldNames = new List<string>(); | ||
113 | |||
114 | DbCommand command = connection.CreateCommand(); | ||
115 | |||
116 | foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values) | ||
117 | { | ||
118 | fieldMapper.ExpandField(obj, command, fieldNames); | ||
119 | } | ||
120 | |||
121 | List<string> paramNames = new List<string>(); | ||
122 | |||
123 | foreach (string field in fieldNames) | ||
124 | { | ||
125 | paramNames.Add(mapper.CreateParamName(field)); | ||
126 | } | ||
127 | |||
128 | command.CommandText = | ||
129 | String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()), | ||
130 | String.Join(", ", paramNames.ToArray())); | ||
131 | |||
132 | return command; | ||
133 | } | ||
134 | |||
135 | public virtual object ConvertToDbType(object value) | ||
136 | { | ||
137 | return value; | ||
138 | } | ||
139 | |||
140 | public abstract BaseDataReader CreateReader(IDataReader reader); | ||
141 | } | ||
142 | } | ||
diff --git a/OpenSim/Data/Base/BaseFieldMapper.cs b/OpenSim/Data/Base/BaseFieldMapper.cs new file mode 100644 index 0000000..03c7bfb --- /dev/null +++ b/OpenSim/Data/Base/BaseFieldMapper.cs | |||
@@ -0,0 +1,168 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Data.Common; | ||
31 | |||
32 | namespace OpenSim.Framework.Data.Base | ||
33 | { | ||
34 | public delegate TField ObjectGetAccessor<TObj, TField>(TObj obj); | ||
35 | public delegate void ObjectSetAccessor<TObj, TField>(TObj obj, TField value); | ||
36 | |||
37 | public abstract class BaseFieldMapper | ||
38 | { | ||
39 | private readonly BaseTableMapper m_tableMapper; | ||
40 | private readonly string m_fieldName; | ||
41 | |||
42 | public string FieldName | ||
43 | { | ||
44 | get { return m_fieldName; } | ||
45 | } | ||
46 | |||
47 | protected Type m_valueType; | ||
48 | |||
49 | public Type ValueType | ||
50 | { | ||
51 | get { return m_valueType; } | ||
52 | } | ||
53 | |||
54 | public abstract object GetParamValue(object obj); | ||
55 | |||
56 | public BaseFieldMapper(BaseTableMapper tableMapper, string fieldName, Type valueType) | ||
57 | { | ||
58 | m_fieldName = fieldName; | ||
59 | m_valueType = valueType; | ||
60 | m_tableMapper = tableMapper; | ||
61 | } | ||
62 | |||
63 | public abstract void SetPropertyFromReader(object mapper, BaseDataReader reader); | ||
64 | |||
65 | public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value) | ||
66 | { | ||
67 | string paramName = m_tableMapper.CreateParamName(fieldName); | ||
68 | fieldNames.Add(fieldName); | ||
69 | |||
70 | DbParameter param = command.CreateParameter(); | ||
71 | param.ParameterName = paramName; | ||
72 | param.Value = value; | ||
73 | |||
74 | command.Parameters.Add(param); | ||
75 | } | ||
76 | |||
77 | public virtual void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames) | ||
78 | { | ||
79 | string fieldName = FieldName; | ||
80 | object value = GetParamValue(obj); | ||
81 | |||
82 | RawAddParam(command, fieldNames, fieldName, m_tableMapper.ConvertToDbType(value)); | ||
83 | } | ||
84 | |||
85 | protected virtual object GetValue(BaseDataReader reader) | ||
86 | { | ||
87 | object value; | ||
88 | |||
89 | if (ValueType == typeof(Guid)) | ||
90 | { | ||
91 | value = reader.GetGuid(m_fieldName); | ||
92 | } | ||
93 | else if (ValueType == typeof(bool)) | ||
94 | { | ||
95 | uint boolVal = reader.GetUShort(m_fieldName); | ||
96 | value = (boolVal == 1); | ||
97 | } | ||
98 | else | ||
99 | if (ValueType == typeof(byte)) | ||
100 | { | ||
101 | value = reader.GetByte(m_fieldName); | ||
102 | } | ||
103 | else if (ValueType == typeof(sbyte)) | ||
104 | { | ||
105 | value = reader.GetSByte(m_fieldName); | ||
106 | } | ||
107 | else if (ValueType == typeof(ushort)) | ||
108 | { | ||
109 | value = reader.GetUShort(m_fieldName); | ||
110 | } | ||
111 | else if (ValueType == typeof(uint)) | ||
112 | { | ||
113 | value = reader.GetUInt32(m_fieldName); | ||
114 | } | ||
115 | else if (ValueType == typeof(byte[])) | ||
116 | { | ||
117 | value = reader.GetBytes(m_fieldName); | ||
118 | } | ||
119 | else | ||
120 | { | ||
121 | value = reader.Get(m_fieldName); | ||
122 | } | ||
123 | |||
124 | if (value is DBNull) | ||
125 | { | ||
126 | value = default(ValueType); | ||
127 | } | ||
128 | |||
129 | return value; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | public class ObjectField<TObject, TField> : BaseFieldMapper | ||
134 | { | ||
135 | private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor; | ||
136 | private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor; | ||
137 | |||
138 | public override object GetParamValue(object obj) | ||
139 | { | ||
140 | return m_fieldGetAccessor((TObject)obj); | ||
141 | } | ||
142 | |||
143 | public override void SetPropertyFromReader(object obj, BaseDataReader reader) | ||
144 | { | ||
145 | object value; | ||
146 | |||
147 | value = GetValue(reader); | ||
148 | |||
149 | if (value == null) | ||
150 | { | ||
151 | m_fieldSetAccessor((TObject)obj, default(TField)); | ||
152 | } | ||
153 | else | ||
154 | { | ||
155 | m_fieldSetAccessor((TObject)obj, (TField)value); | ||
156 | } | ||
157 | } | ||
158 | |||
159 | |||
160 | public ObjectField(BaseTableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor, | ||
161 | ObjectSetAccessor<TObject, TField> rowMapperSetAccessor) | ||
162 | : base(tableMapper, fieldName, typeof(TField)) | ||
163 | { | ||
164 | m_fieldGetAccessor = rowMapperGetAccessor; | ||
165 | m_fieldSetAccessor = rowMapperSetAccessor; | ||
166 | } | ||
167 | } | ||
168 | } | ||
diff --git a/OpenSim/Data/Base/BaseRowMapper.cs b/OpenSim/Data/Base/BaseRowMapper.cs new file mode 100644 index 0000000..b008b86 --- /dev/null +++ b/OpenSim/Data/Base/BaseRowMapper.cs | |||
@@ -0,0 +1,61 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using OpenSim.Framework.Data.Base; | ||
29 | |||
30 | namespace OpenSim.Framework.Data.Base | ||
31 | { | ||
32 | public abstract class BaseRowMapper | ||
33 | { | ||
34 | public abstract void FillObject(BaseDataReader reader); | ||
35 | } | ||
36 | |||
37 | public class BaseRowMapper<TObj> : BaseRowMapper | ||
38 | { | ||
39 | private readonly BaseSchema m_schema; | ||
40 | private readonly TObj m_obj; | ||
41 | |||
42 | public TObj Object | ||
43 | { | ||
44 | get { return m_obj; } | ||
45 | } | ||
46 | |||
47 | public BaseRowMapper(BaseSchema schema, TObj obj) | ||
48 | { | ||
49 | m_schema = schema; | ||
50 | m_obj = obj; | ||
51 | } | ||
52 | |||
53 | public override void FillObject(BaseDataReader reader) | ||
54 | { | ||
55 | foreach (BaseFieldMapper fieldMapper in m_schema.Fields.Values) | ||
56 | { | ||
57 | fieldMapper.SetPropertyFromReader(this, reader); | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | } | ||
diff --git a/OpenSim/Data/Base/BaseSchema.cs b/OpenSim/Data/Base/BaseSchema.cs new file mode 100644 index 0000000..8a7ee71 --- /dev/null +++ b/OpenSim/Data/Base/BaseSchema.cs | |||
@@ -0,0 +1,69 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System.Collections.Generic; | ||
29 | using OpenSim.Framework.Data.Base; | ||
30 | |||
31 | namespace OpenSim.Framework.Data.Base | ||
32 | { | ||
33 | public class BaseSchema | ||
34 | { | ||
35 | protected BaseTableMapper m_tableMapper; | ||
36 | protected Dictionary<string, BaseFieldMapper> m_mappings; | ||
37 | |||
38 | public Dictionary<string, BaseFieldMapper> Fields | ||
39 | { | ||
40 | get { return m_mappings; } | ||
41 | } | ||
42 | |||
43 | public BaseSchema(BaseTableMapper tableMapper) | ||
44 | { | ||
45 | m_mappings = new Dictionary<string, BaseFieldMapper>(); | ||
46 | m_tableMapper = tableMapper; | ||
47 | } | ||
48 | } | ||
49 | |||
50 | public class BaseSchema<TObj> : BaseSchema | ||
51 | { | ||
52 | public BaseSchema(BaseTableMapper tableMapper) | ||
53 | : base(tableMapper) | ||
54 | { | ||
55 | } | ||
56 | |||
57 | public ObjectField<TObj, TField> AddMapping<TField>(string fieldName, | ||
58 | ObjectGetAccessor<TObj, TField> rowMapperGetAccessor, | ||
59 | ObjectSetAccessor<TObj, TField> rowMapperSetAccessor) | ||
60 | { | ||
61 | ObjectField<TObj, TField> rowMapperField = | ||
62 | new ObjectField<TObj, TField>(m_tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor); | ||
63 | |||
64 | m_mappings.Add(fieldName, rowMapperField); | ||
65 | |||
66 | return rowMapperField; | ||
67 | } | ||
68 | } | ||
69 | } | ||
diff --git a/OpenSim/Data/Base/BaseTableMapper.cs b/OpenSim/Data/Base/BaseTableMapper.cs new file mode 100644 index 0000000..cad4823 --- /dev/null +++ b/OpenSim/Data/Base/BaseTableMapper.cs | |||
@@ -0,0 +1,281 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Data; | ||
30 | using System.Data.Common; | ||
31 | using OpenSim.Framework.Data.Base; | ||
32 | |||
33 | namespace OpenSim.Framework.Data.Base | ||
34 | { | ||
35 | public abstract class BaseTableMapper | ||
36 | { | ||
37 | private readonly BaseDatabaseConnector m_database; | ||
38 | private readonly object m_syncRoot = new object(); | ||
39 | |||
40 | protected void WithConnection(Action<DbConnection> action) | ||
41 | { | ||
42 | lock (m_syncRoot) | ||
43 | { | ||
44 | DbConnection m_connection = m_database.GetNewConnection(); | ||
45 | |||
46 | if (m_connection.State != ConnectionState.Open) | ||
47 | { | ||
48 | m_connection.Open(); | ||
49 | } | ||
50 | |||
51 | action(m_connection); | ||
52 | |||
53 | if (m_connection.State == ConnectionState.Open) | ||
54 | { | ||
55 | m_connection.Close(); | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | |||
60 | private readonly string m_tableName; | ||
61 | public string TableName | ||
62 | { | ||
63 | get { return m_tableName; } | ||
64 | } | ||
65 | |||
66 | protected BaseSchema m_schema; | ||
67 | public BaseSchema Schema | ||
68 | { | ||
69 | get { return m_schema; } | ||
70 | } | ||
71 | |||
72 | protected BaseFieldMapper m_keyFieldMapper; | ||
73 | public BaseFieldMapper KeyFieldMapper | ||
74 | { | ||
75 | get { return m_keyFieldMapper; } | ||
76 | } | ||
77 | |||
78 | public BaseTableMapper(BaseDatabaseConnector database, string tableName) | ||
79 | { | ||
80 | m_database = database; | ||
81 | m_tableName = tableName.ToLower(); // Stupid MySQL hack. | ||
82 | } | ||
83 | |||
84 | public string CreateParamName(string fieldName) | ||
85 | { | ||
86 | return m_database.CreateParamName(fieldName); | ||
87 | } | ||
88 | |||
89 | protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey) | ||
90 | { | ||
91 | return m_database.CreateSelectCommand(this, connection, fieldName, primaryKey); | ||
92 | } | ||
93 | |||
94 | public string CreateCondition(DbCommand command, string fieldName, object key) | ||
95 | { | ||
96 | return m_database.CreateCondition(this, command, fieldName, key); | ||
97 | } | ||
98 | |||
99 | public DbCommand CreateInsertCommand(DbConnection connection, object obj) | ||
100 | { | ||
101 | return m_database.CreateInsertCommand(this, connection, obj); | ||
102 | } | ||
103 | |||
104 | public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey) | ||
105 | { | ||
106 | return m_database.CreateUpdateCommand(this, connection, rowMapper, primaryKey); | ||
107 | } | ||
108 | |||
109 | public object ConvertToDbType(object value) | ||
110 | { | ||
111 | return m_database.ConvertToDbType(value); | ||
112 | } | ||
113 | |||
114 | protected virtual BaseDataReader CreateReader(IDataReader reader) | ||
115 | { | ||
116 | return m_database.CreateReader(reader); | ||
117 | } | ||
118 | } | ||
119 | |||
120 | public abstract class BaseTableMapper<TRowMapper, TPrimaryKey> : BaseTableMapper | ||
121 | { | ||
122 | public BaseTableMapper(BaseDatabaseConnector database, string tableName) | ||
123 | : base(database, tableName) | ||
124 | { | ||
125 | } | ||
126 | |||
127 | // HACK: This is a temporary function used by TryGetValue(). | ||
128 | // Due to a bug in mono 1.2.6, delegate blocks cannot contain | ||
129 | // a using() block. This has been fixed in SVN, so the next | ||
130 | // mono release should work. | ||
131 | private void TryGetConnectionValue(DbConnection connection, TPrimaryKey primaryKey, ref TRowMapper result, ref bool success) | ||
132 | { | ||
133 | using ( | ||
134 | DbCommand command = | ||
135 | CreateSelectCommand(connection, KeyFieldMapper.FieldName, primaryKey)) | ||
136 | { | ||
137 | using (IDataReader reader = command.ExecuteReader()) | ||
138 | { | ||
139 | if (reader.Read()) | ||
140 | { | ||
141 | result = FromReader( CreateReader(reader)); | ||
142 | success = true; | ||
143 | } | ||
144 | else | ||
145 | { | ||
146 | success = false; | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | |||
152 | public bool TryGetValue(TPrimaryKey primaryKey, out TRowMapper value) | ||
153 | { | ||
154 | TRowMapper result = default(TRowMapper); | ||
155 | bool success = false; | ||
156 | |||
157 | WithConnection(delegate(DbConnection connection) | ||
158 | { | ||
159 | TryGetConnectionValue(connection, primaryKey, ref result, ref success); | ||
160 | }); | ||
161 | |||
162 | value = result; | ||
163 | |||
164 | return success; | ||
165 | } | ||
166 | |||
167 | // HACK: This is a temporary function used by Remove(). | ||
168 | // Due to a bug in mono 1.2.6, delegate blocks cannot contain | ||
169 | // a using() block. This has been fixed in SVN, so the next | ||
170 | // mono release should work. | ||
171 | protected virtual void TryDelete(DbConnection connection, TPrimaryKey id, ref int deleted) | ||
172 | { | ||
173 | using ( | ||
174 | DbCommand command = | ||
175 | CreateDeleteCommand(connection, KeyFieldMapper.FieldName, id)) | ||
176 | { | ||
177 | deleted = command.ExecuteNonQuery(); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | public virtual bool Remove(TPrimaryKey id) | ||
182 | { | ||
183 | int deleted = 0; | ||
184 | |||
185 | WithConnection(delegate(DbConnection connection) | ||
186 | { | ||
187 | TryDelete(connection, id, ref deleted); | ||
188 | }); | ||
189 | |||
190 | if (deleted == 1) | ||
191 | { | ||
192 | return true; | ||
193 | } | ||
194 | else | ||
195 | { | ||
196 | return false; | ||
197 | } | ||
198 | } | ||
199 | |||
200 | public DbCommand CreateDeleteCommand(DbConnection connection, string fieldName, TPrimaryKey primaryKey) | ||
201 | { | ||
202 | string table = TableName; | ||
203 | |||
204 | DbCommand command = connection.CreateCommand(); | ||
205 | |||
206 | string conditionString = CreateCondition(command, fieldName, primaryKey); | ||
207 | |||
208 | string query = | ||
209 | String.Format("delete from {0} where {1}", table, conditionString); | ||
210 | |||
211 | command.CommandText = query; | ||
212 | command.CommandType = CommandType.Text; | ||
213 | |||
214 | return command; | ||
215 | } | ||
216 | |||
217 | // HACK: This is a temporary function used by Update(). | ||
218 | // Due to a bug in mono 1.2.6, delegate blocks cannot contain | ||
219 | // a using() block. This has been fixed in SVN, so the next | ||
220 | // mono release should work. | ||
221 | protected void TryUpdate(DbConnection connection, TPrimaryKey primaryKey, TRowMapper value, ref int updated) | ||
222 | { | ||
223 | using (DbCommand command = CreateUpdateCommand(connection, value, primaryKey)) | ||
224 | { | ||
225 | updated = command.ExecuteNonQuery(); | ||
226 | } | ||
227 | } | ||
228 | |||
229 | public virtual bool Update(TPrimaryKey primaryKey, TRowMapper value) | ||
230 | { | ||
231 | int updated = 0; | ||
232 | |||
233 | WithConnection(delegate(DbConnection connection) | ||
234 | { | ||
235 | TryUpdate(connection, primaryKey, value, ref updated); | ||
236 | }); | ||
237 | |||
238 | if (updated == 1) | ||
239 | { | ||
240 | return true; | ||
241 | } | ||
242 | else | ||
243 | { | ||
244 | return false; | ||
245 | } | ||
246 | } | ||
247 | |||
248 | // HACK: This is a temporary function used by Add(). | ||
249 | // Due to a bug in mono 1.2.6, delegate blocks cannot contain | ||
250 | // a using() block. This has been fixed in SVN, so the next | ||
251 | // mono release should work. | ||
252 | protected void TryAdd(DbConnection connection, TRowMapper value, ref int added) | ||
253 | { | ||
254 | using (DbCommand command = CreateInsertCommand(connection, value)) | ||
255 | { | ||
256 | added = command.ExecuteNonQuery(); | ||
257 | } | ||
258 | } | ||
259 | |||
260 | public virtual bool Add(TRowMapper value) | ||
261 | { | ||
262 | int added = 0; | ||
263 | |||
264 | WithConnection(delegate(DbConnection connection) | ||
265 | { | ||
266 | TryAdd(connection, value, ref added); | ||
267 | }); | ||
268 | |||
269 | if (added == 1) | ||
270 | { | ||
271 | return true; | ||
272 | } | ||
273 | else | ||
274 | { | ||
275 | return false; | ||
276 | } | ||
277 | } | ||
278 | |||
279 | public abstract TRowMapper FromReader(BaseDataReader reader); | ||
280 | } | ||
281 | } | ||
diff --git a/OpenSim/Data/Base/OpenSim.Framework.Data.Base.snk b/OpenSim/Data/Base/OpenSim.Framework.Data.Base.snk new file mode 100644 index 0000000..fc71027 --- /dev/null +++ b/OpenSim/Data/Base/OpenSim.Framework.Data.Base.snk | |||
Binary files differ | |||
diff --git a/OpenSim/Data/Base/Properties/AssemblyInfo.cs b/OpenSim/Data/Base/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ab97490 --- /dev/null +++ b/OpenSim/Data/Base/Properties/AssemblyInfo.cs | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System.Reflection; | ||
29 | using System.Runtime.InteropServices; | ||
30 | using System.Security; | ||
31 | |||
32 | // General Information about an assembly is controlled through the following | ||
33 | // set of attributes. Change these attribute values to modify the information | ||
34 | // associated with an assembly. | ||
35 | |||
36 | [assembly : AssemblyTitle("OpenSim.Framework.Data.Base")] | ||
37 | [assembly : AssemblyDescription("Generic Database Abstraction Layer")] | ||
38 | [assembly : AssemblyConfiguration("")] | ||
39 | [assembly : AssemblyCompany("OpenSim Project (www.opensimulator.org)")] | ||
40 | [assembly: AssemblyProduct("OpenSim.Framework.Data.Base")] | ||
41 | [assembly: AssemblyCopyright("Copyright (c) 2007 OpenSim Project (www.opensimulator.org)")] | ||
42 | [assembly : AssemblyTrademark("")] | ||
43 | [assembly : AssemblyCulture("")] | ||
44 | |||
45 | // Setting ComVisible to false makes the types in this assembly not visible | ||
46 | // to COM components. If you need to access a type in this assembly from | ||
47 | // COM, set the ComVisible attribute to true on that type. | ||
48 | |||
49 | [assembly : ComVisible(false)] | ||
50 | |||
51 | // The following GUID is for the ID of the typelib if this project is exposed to COM | ||
52 | |||
53 | [assembly : Guid("9269f421-19d9-4eea-bfe3-c0ffe426fada")] | ||
54 | |||
55 | // Version information for an assembly consists of the following four values: | ||
56 | // | ||
57 | // Major Version | ||
58 | // Minor Version | ||
59 | // Build Number | ||
60 | // Revision | ||
61 | // | ||
62 | // You can specify all the values or you can default the Revision and Build Numbers | ||
63 | // by using the '*' as shown below: | ||
64 | |||
65 | [assembly : AssemblyVersion("1.0.0.0")] | ||
66 | [assembly : AssemblyFileVersion("1.0.0.0")] | ||
67 | [assembly : AllowPartiallyTrustedCallers] | ||