Datei: NDODLL/NDOMappingTableHandler.cs
Last Commit (bbf1d57)
| 1 | // |
| 2 | // Copyright (c) 2002-2016 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | using System; |
| 23 | using System.Data; |
| 24 | using System.Data.Common; |
| 25 | using System.Linq; |
| 26 | using System.Threading; |
| 27 | using System.Threading.Tasks; |
| 28 | using Microsoft.Extensions.Logging; |
| 29 | using NDO.Mapping; |
| 30 | using NDOInterfaces; |
| 31 | using NDO.SqlPersistenceHandling; |
| 32 | |
| 33 | namespace NDO |
| 34 | { |
| 35 | ····/// <summary> |
| 36 | ····/// This class supports reading and writing of relation mapping tables. |
| 37 | ····/// </summary> |
| 38 | ····internal class NDOMappingTableHandler : IMappingTableHandler |
| 39 | ····{ |
| 40 | ········private Relation relation; |
| 41 | ········private DbCommand selectCommand; |
| 42 | ········private DbCommand insertCommand; |
| 43 | ········private DbCommand deleteCommand; |
| 44 | ········private DbConnection connection; |
| 45 | ········private DbRowUpdater rowUpdater; |
| 46 | ········private IProvider provider; |
| 47 | ········private readonly ILogger logger; |
| 48 | ········private readonly ILoggerFactory loggerFactory; |
| 49 | |
| 50 | ········public NDOMappingTableHandler(ILoggerFactory loggerFactory) |
| 51 | ········{ |
| 52 | ············this.logger = loggerFactory.CreateLogger<NDOMappingTableHandler>(); |
| 53 | ············this.loggerFactory = loggerFactory; |
| 54 | ········} |
| 55 | |
| 56 | ········private string GetParameter(IProvider provider, string name) |
| 57 | ········{ |
| 58 | ············return provider.UseNamedParams ? provider.GetNamedParameter(name) : "?"; |
| 59 | ········} |
| 60 | |
| 61 | ········public void Initialize(NDOMapping mappings, Relation r) |
| 62 | ········{ |
| 63 | ············Connection con = mappings.FindConnection(r.MappingTable.ConnectionId); |
| 64 | ············this.provider = mappings.GetProvider(con); |
| 65 | |
| 66 | ············// The connection object will be initialized in the pm, to |
| 67 | ············// enable the callback for getting the real connection string. |
| 68 | ············// CheckTransaction is the place, where this happens. |
| 69 | ············this.connection = null;·· |
| 70 | |
| 71 | ············selectCommand = provider.NewSqlCommand(connection); |
| 72 | ············insertCommand = provider.NewSqlCommand(connection); |
| 73 | ············deleteCommand = provider.NewSqlCommand(connection); |
| 74 | ············rowUpdater = new DbRowUpdater(insertCommand, null, deleteCommand); |
| 75 | ············this.relation = r; |
| 76 | |
| 77 | ············// |
| 78 | ············// select |
| 79 | ············// |
| 80 | ············string sql = string.Format("SELECT * FROM {0} WHERE ", provider.GetQualifiedTableName(r.MappingTable.TableName)); |
| 81 | ············selectCommand.CommandText = sql; |
| 82 | |
| 83 | ············// |
| 84 | ············// insert |
| 85 | ············// |
| 86 | ············sql = "INSERT INTO {0}({1}) VALUES ({2})"; |
| 87 | ············//{0} = TableName: Mitarbeiter············ |
| 88 | ············//{1} = FieldList: vorname, nachname |
| 89 | ············//{2} = FieldList mit @: |
| 90 | |
| 91 | ············// IDOwn1, IDOwn2 |
| 92 | ············string columns = string.Empty; |
| 93 | ············string namedParameters = string.Empty; |
| 94 | |
| 95 | ············new ForeignKeyIterator(r).Iterate |
| 96 | ············( |
| 97 | ················delegate(ForeignKeyColumn fkColumn, bool isLastElement) |
| 98 | ················{ |
| 99 | ····················columns += provider.GetQuotedName(fkColumn.Name); |
| 100 | ····················namedParameters += GetParameter(provider, fkColumn.Name); |
| 101 | ····················provider.AddParameter(insertCommand, provider.GetNamedParameter(fkColumn.Name), provider.GetDbType(fkColumn.SystemType), provider.GetDefaultLength(fkColumn.SystemType), fkColumn.Name); |
| 102 | ····················columns += ", "; |
| 103 | ····················namedParameters += ", "; |
| 104 | ················} |
| 105 | ············); |
| 106 | |
| 107 | |
| 108 | ············// IDOther1, IDOther2 |
| 109 | ············new ForeignKeyIterator(r.MappingTable).Iterate |
| 110 | ············( |
| 111 | ················delegate(ForeignKeyColumn fkColumn, bool isLastElement) |
| 112 | ················{ |
| 113 | ····················columns += provider.GetQuotedName(fkColumn.Name); |
| 114 | ····················namedParameters += GetParameter(provider, fkColumn.Name); |
| 115 | ····················provider.AddParameter(insertCommand, provider.GetNamedParameter(fkColumn.Name), provider.GetDbType(fkColumn.SystemType), provider.GetDefaultLength(fkColumn.SystemType), fkColumn.Name); |
| 116 | ····················if (!isLastElement) |
| 117 | ····················{ |
| 118 | ························columns += ", "; |
| 119 | ························namedParameters += ", "; |
| 120 | ····················} |
| 121 | ················} |
| 122 | ············); |
| 123 | |
| 124 | |
| 125 | ············// TCOwn |
| 126 | ············if (r.ForeignKeyTypeColumnName != null) { |
| 127 | ················columns += ", " + provider.GetQuotedName(r.ForeignKeyTypeColumnName); |
| 128 | ················namedParameters += ", " + GetParameter(provider, r.ForeignKeyTypeColumnName); |
| 129 | ················provider.AddParameter(insertCommand, provider.GetNamedParameter(r.ForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), provider.GetDefaultLength(typeof(int)), r.ForeignKeyTypeColumnName); |
| 130 | ············} |
| 131 | ············// TCOther |
| 132 | ············if (r.MappingTable.ChildForeignKeyTypeColumnName != null) { |
| 133 | ················columns += ", " + provider.GetQuotedName(r.MappingTable.ChildForeignKeyTypeColumnName); |
| 134 | ················namedParameters += ", " + GetParameter(provider, r.MappingTable.ChildForeignKeyTypeColumnName); |
| 135 | ················provider.AddParameter(insertCommand, provider.GetNamedParameter(r.MappingTable.ChildForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), provider.GetDefaultLength(typeof(int)), r.MappingTable.ChildForeignKeyTypeColumnName); |
| 136 | ············} |
| 137 | ············insertCommand.CommandText = string.Format(sql, provider.GetQualifiedTableName(r.MappingTable.TableName), columns, namedParameters); |
| 138 | |
| 139 | ············Class relatedClass = mappings.FindClass(r.ReferencedTypeName); |
| 140 | |
| 141 | ············// |
| 142 | ············// delete |
| 143 | ············// |
| 144 | ············sql = "DELETE FROM {0} WHERE ({1})"; |
| 145 | ············//{0} = Tabellenname: Mitarbeiter |
| 146 | ············//{1} = Where-Bedingung: id = @Original_id |
| 147 | ············string where = string.Empty; |
| 148 | ············new ForeignKeyIterator(r).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastIndex) |
| 149 | ················{ |
| 150 | ····················where += provider.GetQuotedName(fkColumn.Name) + " = " + GetParameter(provider, "Original_" + fkColumn.Name) + " AND "; |
| 151 | ····················provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + fkColumn.Name), provider.GetDbType(fkColumn.SystemType), fkColumn.Size, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), fkColumn.Name, System.Data.DataRowVersion.Original, null); |
| 152 | ················} |
| 153 | ············); |
| 154 | ············new ForeignKeyIterator(r.MappingTable).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastIndex) |
| 155 | ················{ |
| 156 | ····················where += provider.GetQuotedName(fkColumn.Name) + " = " + GetParameter(provider, "Original_" + fkColumn.Name); |
| 157 | ····················if (!isLastIndex) |
| 158 | ························where += " AND "; |
| 159 | ····················provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + fkColumn.Name), provider.GetDbType(fkColumn.SystemType), fkColumn.Size, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), fkColumn.Name, System.Data.DataRowVersion.Original, null); |
| 160 | ················} |
| 161 | ············); |
| 162 | |
| 163 | ············if (r.ForeignKeyTypeColumnName != null) |
| 164 | ············{ |
| 165 | ················where += " AND " + provider.GetQuotedName(r.ForeignKeyTypeColumnName) + " = " + GetParameter(provider, "Original_" + r.ForeignKeyTypeColumnName); |
| 166 | ················provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + r.ForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), r.ForeignKeyTypeColumnName, System.Data.DataRowVersion.Original, null); |
| 167 | ············} |
| 168 | ············if (r.MappingTable.ChildForeignKeyTypeColumnName != null) |
| 169 | ············{ |
| 170 | ················where += " AND " + provider.GetQuotedName(r.MappingTable.ChildForeignKeyTypeColumnName) + " = " + GetParameter(provider, "Original_" + r.MappingTable.ChildForeignKeyTypeColumnName); |
| 171 | ················provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + r.MappingTable.ChildForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), r.MappingTable.ChildForeignKeyTypeColumnName, System.Data.DataRowVersion.Original, null); |
| 172 | ············} |
| 173 | |
| 174 | ············deleteCommand.CommandText = string.Format(sql, provider.GetQualifiedTableName(r.MappingTable.TableName), where); |
| 175 | |
| 176 | ········} |
| 177 | |
| 178 | ········private void Dump(DataRow[] rows) |
| 179 | ········{ |
| 180 | ············new SqlDumper(this.loggerFactory, this.provider, insertCommand, selectCommand, null, deleteCommand).Dump(rows); |
| 181 | ········} |
| 182 | |
| 183 | ········private DataTable GetTableTemplate(DataSet templateDataset, string name) |
| 184 | ········{ |
| 185 | ············DataTable dt = templateDataset.Tables[name]; |
| 186 | ············if (dt == null) |
| 187 | ················throw new NDOException(24, "Can't find mapping table '" + name + "' in the schema. Check your mapping file."); |
| 188 | ············return dt; |
| 189 | ········} |
| 190 | |
| 191 | ········ |
| 192 | ········public async Task<DataTable> FindRelatedObjectsAsync( ObjectId oid, DataSet templateDataset, CancellationToken cancellationToken = default ) |
| 193 | ········{ |
| 194 | ············DataTable table = GetTableTemplate(templateDataset, relation.MappingTable.TableName).Clone(); |
| 195 | ············string sql = "SELECT * FROM " + provider.GetQualifiedTableName(relation.MappingTable.TableName) + " WHERE "; |
| 196 | ············selectCommand.Parameters.Clear(); |
| 197 | |
| 198 | ············int i = 0; |
| 199 | ············new ForeignKeyIterator(relation).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastEntry) |
| 200 | ············{···················· |
| 201 | ················string parName = "p" + i; |
| 202 | ················sql += provider.GetQuotedName(fkColumn.Name) + " = " + provider.GetNamedParameter(parName); |
| 203 | ················IDataParameter dataParameter = provider.AddParameter(selectCommand, provider.GetNamedParameter(parName), provider.GetDbType(fkColumn.SystemType), fkColumn.Size, parName); |
| 204 | ················object o = oid.Id[i]; |
| 205 | ················if ( o is Guid && !provider.SupportsNativeGuidType ) |
| 206 | ····················o = ((Guid) o).ToString(); |
| 207 | ················dataParameter.Value = o; |
| 208 | ················dataParameter.Direction = ParameterDirection.Input; |
| 209 | ················if (!isLastEntry) |
| 210 | ····················sql += " AND "; |
| 211 | ················i++; |
| 212 | ············}); |
| 213 | ············ |
| 214 | ············if (relation.ForeignKeyTypeColumnName != null) |
| 215 | ············{ |
| 216 | ················//sql += " AND " + provider.GetQuotedName(r.ForeignKeyTypeColumnName) + " = " + oid.Id.TypeId; |
| 217 | ················sql += " AND " + provider.GetQuotedName(relation.ForeignKeyTypeColumnName) + " = " + provider.GetNamedParameter("typeCode"); |
| 218 | ················IDataParameter dataParameter = provider.AddParameter(selectCommand, provider.GetNamedParameter("typeCode"), provider.GetDbType(typeof(int)), provider.GetDefaultLength(typeof(int)), "typeCode"); |
| 219 | ················dataParameter.Value = oid.Id.TypeId; |
| 220 | ················dataParameter.Direction = ParameterDirection.Input;················ |
| 221 | ············} |
| 222 | |
| 223 | ············selectCommand.CommandText = sql; |
| 224 | ············try |
| 225 | ············{ |
| 226 | ················Dump(null); |
| 227 | ················await DbDataTableFiller.FillAsync( selectCommand, table, cancellationToken ).ConfigureAwait( false ); |
| 228 | ············} |
| 229 | ············catch (OperationCanceledException) |
| 230 | ············{ |
| 231 | ················throw; |
| 232 | ············} |
| 233 | ············catch (System.Exception ex) |
| 234 | ············{ |
| 235 | ················string text = "Exception while reading the mapping table: " + ex.Message + "\n"; |
| 236 | ················text += "Sql-Anweisung: " + selectCommand.CommandText + "\n"; |
| 237 | throw new NDOException( 25, text) ; |
| 238 | ············} |
| 239 | |
| 240 | ············return table;········ |
| 241 | ········} |
| 242 | |
| 243 | ········public async Task UpdateAsync( DataSet ds, CancellationToken cancellationToken = default ) |
| 244 | ········{ |
| 245 | ············DataTable dt = ds.Tables[relation.MappingTable.TableName]; |
| 246 | ············try |
| 247 | ············{ |
| 248 | ················DataRow[] rows = dt.Select(null, null, DataViewRowState.Added |
| 249 | ····················| DataViewRowState.ModifiedCurrent |
| 250 | ····················| DataViewRowState.Deleted); |
| 251 | |
| 252 | ················if (rows.Length > 0) |
| 253 | ················{ |
| 254 | ····················Dump(rows); |
| 255 | ····················// Like DbDataAdapter.Update(DataTable) the rows are processed in the order of the table. |
| 256 | ····················DataRow[] changedRows = dt.Rows.Cast<DataRow>() |
| 257 | ························.Where( r => r.RowState == DataRowState.Added || r.RowState == DataRowState.Modified || r.RowState == DataRowState.Deleted ) |
| 258 | ························.ToArray(); |
| 259 | ····················await rowUpdater.UpdateAsync( changedRows, cancellationToken ).ConfigureAwait( false ); |
| 260 | ················} |
| 261 | ············} |
| 262 | ············catch (OperationCanceledException) |
| 263 | ············{ |
| 264 | ················throw; |
| 265 | ············} |
| 266 | ············catch (System.Exception ex) |
| 267 | ············{ |
| 268 | throw new NDOException( 26, "Exception while updating the mapping table: " + ex. Message + "\n") ; |
| 269 | ············} |
| 270 | ········} |
| 271 | |
| 272 | ········public void Dispose() |
| 273 | ········{ |
| 274 | ········} |
| 275 | |
| 276 | ········public DbConnection Connection |
| 277 | ········{ |
| 278 | ············get { return this.selectCommand.Connection; } |
| 279 | ············set |
| 280 | ············{ |
| 281 | ················this.selectCommand.Connection = value; |
| 282 | ················this.deleteCommand.Connection = value; |
| 283 | ················this.insertCommand.Connection = value; |
| 284 | ············} |
| 285 | ········} |
| 286 | ···· |
| 287 | ········public DbTransaction Transaction |
| 288 | ········{ |
| 289 | ············get { return this.selectCommand.Transaction; } |
| 290 | ············set |
| 291 | ············{ |
| 292 | ················this.selectCommand.Transaction = value; |
| 293 | ················this.deleteCommand.Transaction = value; |
| 294 | ················this.insertCommand.Transaction = value; |
| 295 | ············} |
| 296 | ········} |
| 297 | |
| 298 | ········public Relation Relation |
| 299 | ········{ |
| 300 | ············get { return this.relation; } |
| 301 | ········} |
| 302 | ····} |
| 303 | } |
| 304 |
New Commit (4a40b5b)
| 1 | // |
| 2 | // Copyright (c) 2002-2016 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | using System; |
| 23 | using System.Data; |
| 24 | using System.Data.Common; |
| 25 | using System.Linq; |
| 26 | using System.Threading; |
| 27 | using System.Threading.Tasks; |
| 28 | using Microsoft.Extensions.Logging; |
| 29 | using NDO.Mapping; |
| 30 | using NDOInterfaces; |
| 31 | using NDO.SqlPersistenceHandling; |
| 32 | |
| 33 | namespace NDO |
| 34 | { |
| 35 | ····/// <summary> |
| 36 | ····/// This class supports reading and writing of relation mapping tables. |
| 37 | ····/// </summary> |
| 38 | ····internal class NDOMappingTableHandler : IMappingTableHandler |
| 39 | ····{ |
| 40 | ········private Relation relation; |
| 41 | ········private DbCommand selectCommand; |
| 42 | ········private DbCommand insertCommand; |
| 43 | ········private DbCommand deleteCommand; |
| 44 | ········private DbConnection connection; |
| 45 | ········private DbRowUpdater rowUpdater; |
| 46 | ········private IProvider provider; |
| 47 | ········private readonly ILogger logger; |
| 48 | ········private readonly ILoggerFactory loggerFactory; |
| 49 | |
| 50 | ········public NDOMappingTableHandler(ILoggerFactory loggerFactory) |
| 51 | ········{ |
| 52 | ············this.logger = loggerFactory.CreateLogger<NDOMappingTableHandler>(); |
| 53 | ············this.loggerFactory = loggerFactory; |
| 54 | ········} |
| 55 | |
| 56 | ········private string GetParameter(IProvider provider, string name) |
| 57 | ········{ |
| 58 | ············return provider.UseNamedParams ? provider.GetNamedParameter(name) : "?"; |
| 59 | ········} |
| 60 | |
| 61 | ········public void Initialize(NDOMapping mappings, Relation r) |
| 62 | ········{ |
| 63 | ············Connection con = mappings.FindConnection(r.MappingTable.ConnectionId); |
| 64 | ············this.provider = mappings.GetProvider(con); |
| 65 | |
| 66 | ············// The connection object will be initialized in the pm, to |
| 67 | ············// enable the callback for getting the real connection string. |
| 68 | ············// CheckTransaction is the place, where this happens. |
| 69 | ············this.connection = null;·· |
| 70 | |
| 71 | ············selectCommand = provider.NewSqlCommand(connection); |
| 72 | ············insertCommand = provider.NewSqlCommand(connection); |
| 73 | ············deleteCommand = provider.NewSqlCommand(connection); |
| 74 | ············rowUpdater = new DbRowUpdater(insertCommand, null, deleteCommand); |
| 75 | ············this.relation = r; |
| 76 | |
| 77 | ············// |
| 78 | ············// select |
| 79 | ············// |
| 80 | ············string sql = string.Format("SELECT * FROM {0} WHERE ", provider.GetQualifiedTableName(r.MappingTable.TableName)); |
| 81 | ············selectCommand.CommandText = sql; |
| 82 | |
| 83 | ············// |
| 84 | ············// insert |
| 85 | ············// |
| 86 | ············sql = "INSERT INTO {0}({1}) VALUES ({2})"; |
| 87 | ············//{0} = TableName: Mitarbeiter············ |
| 88 | ············//{1} = FieldList: vorname, nachname |
| 89 | ············//{2} = FieldList mit @: |
| 90 | |
| 91 | ············// IDOwn1, IDOwn2 |
| 92 | ············string columns = string.Empty; |
| 93 | ············string namedParameters = string.Empty; |
| 94 | |
| 95 | ············new ForeignKeyIterator(r).Iterate |
| 96 | ············( |
| 97 | ················delegate(ForeignKeyColumn fkColumn, bool isLastElement) |
| 98 | ················{ |
| 99 | ····················columns += provider.GetQuotedName(fkColumn.Name); |
| 100 | ····················namedParameters += GetParameter(provider, fkColumn.Name); |
| 101 | ····················provider.AddParameter(insertCommand, provider.GetNamedParameter(fkColumn.Name), provider.GetDbType(fkColumn.SystemType), provider.GetDefaultLength(fkColumn.SystemType), fkColumn.Name); |
| 102 | ····················columns += ", "; |
| 103 | ····················namedParameters += ", "; |
| 104 | ················} |
| 105 | ············); |
| 106 | |
| 107 | |
| 108 | ············// IDOther1, IDOther2 |
| 109 | ············new ForeignKeyIterator(r.MappingTable).Iterate |
| 110 | ············( |
| 111 | ················delegate(ForeignKeyColumn fkColumn, bool isLastElement) |
| 112 | ················{ |
| 113 | ····················columns += provider.GetQuotedName(fkColumn.Name); |
| 114 | ····················namedParameters += GetParameter(provider, fkColumn.Name); |
| 115 | ····················provider.AddParameter(insertCommand, provider.GetNamedParameter(fkColumn.Name), provider.GetDbType(fkColumn.SystemType), provider.GetDefaultLength(fkColumn.SystemType), fkColumn.Name); |
| 116 | ····················if (!isLastElement) |
| 117 | ····················{ |
| 118 | ························columns += ", "; |
| 119 | ························namedParameters += ", "; |
| 120 | ····················} |
| 121 | ················} |
| 122 | ············); |
| 123 | |
| 124 | |
| 125 | ············// TCOwn |
| 126 | ············if (r.ForeignKeyTypeColumnName != null) { |
| 127 | ················columns += ", " + provider.GetQuotedName(r.ForeignKeyTypeColumnName); |
| 128 | ················namedParameters += ", " + GetParameter(provider, r.ForeignKeyTypeColumnName); |
| 129 | ················provider.AddParameter(insertCommand, provider.GetNamedParameter(r.ForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), provider.GetDefaultLength(typeof(int)), r.ForeignKeyTypeColumnName); |
| 130 | ············} |
| 131 | ············// TCOther |
| 132 | ············if (r.MappingTable.ChildForeignKeyTypeColumnName != null) { |
| 133 | ················columns += ", " + provider.GetQuotedName(r.MappingTable.ChildForeignKeyTypeColumnName); |
| 134 | ················namedParameters += ", " + GetParameter(provider, r.MappingTable.ChildForeignKeyTypeColumnName); |
| 135 | ················provider.AddParameter(insertCommand, provider.GetNamedParameter(r.MappingTable.ChildForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), provider.GetDefaultLength(typeof(int)), r.MappingTable.ChildForeignKeyTypeColumnName); |
| 136 | ············} |
| 137 | ············insertCommand.CommandText = string.Format(sql, provider.GetQualifiedTableName(r.MappingTable.TableName), columns, namedParameters); |
| 138 | |
| 139 | ············Class relatedClass = mappings.FindClass(r.ReferencedTypeName); |
| 140 | |
| 141 | ············// |
| 142 | ············// delete |
| 143 | ············// |
| 144 | ············sql = "DELETE FROM {0} WHERE ({1})"; |
| 145 | ············//{0} = Tabellenname: Mitarbeiter |
| 146 | ············//{1} = Where-Bedingung: id = @Original_id |
| 147 | ············string where = string.Empty; |
| 148 | ············new ForeignKeyIterator(r).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastIndex) |
| 149 | ················{ |
| 150 | ····················where += provider.GetQuotedName(fkColumn.Name) + " = " + GetParameter(provider, "Original_" + fkColumn.Name) + " AND "; |
| 151 | ····················provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + fkColumn.Name), provider.GetDbType(fkColumn.SystemType), fkColumn.Size, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), fkColumn.Name, System.Data.DataRowVersion.Original, null); |
| 152 | ················} |
| 153 | ············); |
| 154 | ············new ForeignKeyIterator(r.MappingTable).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastIndex) |
| 155 | ················{ |
| 156 | ····················where += provider.GetQuotedName(fkColumn.Name) + " = " + GetParameter(provider, "Original_" + fkColumn.Name); |
| 157 | ····················if (!isLastIndex) |
| 158 | ························where += " AND "; |
| 159 | ····················provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + fkColumn.Name), provider.GetDbType(fkColumn.SystemType), fkColumn.Size, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), fkColumn.Name, System.Data.DataRowVersion.Original, null); |
| 160 | ················} |
| 161 | ············); |
| 162 | |
| 163 | ············if (r.ForeignKeyTypeColumnName != null) |
| 164 | ············{ |
| 165 | ················where += " AND " + provider.GetQuotedName(r.ForeignKeyTypeColumnName) + " = " + GetParameter(provider, "Original_" + r.ForeignKeyTypeColumnName); |
| 166 | ················provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + r.ForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), r.ForeignKeyTypeColumnName, System.Data.DataRowVersion.Original, null); |
| 167 | ············} |
| 168 | ············if (r.MappingTable.ChildForeignKeyTypeColumnName != null) |
| 169 | ············{ |
| 170 | ················where += " AND " + provider.GetQuotedName(r.MappingTable.ChildForeignKeyTypeColumnName) + " = " + GetParameter(provider, "Original_" + r.MappingTable.ChildForeignKeyTypeColumnName); |
| 171 | ················provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + r.MappingTable.ChildForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), r.MappingTable.ChildForeignKeyTypeColumnName, System.Data.DataRowVersion.Original, null); |
| 172 | ············} |
| 173 | |
| 174 | ············deleteCommand.CommandText = string.Format(sql, provider.GetQualifiedTableName(r.MappingTable.TableName), where); |
| 175 | |
| 176 | ········} |
| 177 | |
| 178 | ········private void Dump(DataRow[] rows) |
| 179 | ········{ |
| 180 | ············new SqlDumper(this.loggerFactory, this.provider, insertCommand, selectCommand, null, deleteCommand).Dump(rows); |
| 181 | ········} |
| 182 | |
| 183 | ········private DataTable GetTableTemplate(DataSet templateDataset, string name) |
| 184 | ········{ |
| 185 | ············DataTable dt = templateDataset.Tables[name]; |
| 186 | ············if (dt == null) |
| 187 | ················throw new NDOException(24, "Can't find mapping table '" + name + "' in the schema. Check your mapping file."); |
| 188 | ············return dt; |
| 189 | ········} |
| 190 | |
| 191 | ········ |
| 192 | ········public async Task<DataTable> FindRelatedObjectsAsync( ObjectId oid, DataSet templateDataset, CancellationToken cancellationToken = default ) |
| 193 | ········{ |
| 194 | ············DataTable table = GetTableTemplate(templateDataset, relation.MappingTable.TableName).Clone(); |
| 195 | ············string sql = "SELECT * FROM " + provider.GetQualifiedTableName(relation.MappingTable.TableName) + " WHERE "; |
| 196 | ············selectCommand.Parameters.Clear(); |
| 197 | |
| 198 | ············int i = 0; |
| 199 | ············new ForeignKeyIterator(relation).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastEntry) |
| 200 | ············{···················· |
| 201 | ················string parName = "p" + i; |
| 202 | ················sql += provider.GetQuotedName(fkColumn.Name) + " = " + provider.GetNamedParameter(parName); |
| 203 | ················IDataParameter dataParameter = provider.AddParameter(selectCommand, provider.GetNamedParameter(parName), provider.GetDbType(fkColumn.SystemType), fkColumn.Size, parName); |
| 204 | ················object o = oid.Id[i]; |
| 205 | ················if ( o is Guid && !provider.SupportsNativeGuidType ) |
| 206 | ····················o = ((Guid) o).ToString(); |
| 207 | ················dataParameter.Value = o; |
| 208 | ················dataParameter.Direction = ParameterDirection.Input; |
| 209 | ················if (!isLastEntry) |
| 210 | ····················sql += " AND "; |
| 211 | ················i++; |
| 212 | ············}); |
| 213 | ············ |
| 214 | ············if (relation.ForeignKeyTypeColumnName != null) |
| 215 | ············{ |
| 216 | ················//sql += " AND " + provider.GetQuotedName(r.ForeignKeyTypeColumnName) + " = " + oid.Id.TypeId; |
| 217 | ················sql += " AND " + provider.GetQuotedName(relation.ForeignKeyTypeColumnName) + " = " + provider.GetNamedParameter("typeCode"); |
| 218 | ················IDataParameter dataParameter = provider.AddParameter(selectCommand, provider.GetNamedParameter("typeCode"), provider.GetDbType(typeof(int)), provider.GetDefaultLength(typeof(int)), "typeCode"); |
| 219 | ················dataParameter.Value = oid.Id.TypeId; |
| 220 | ················dataParameter.Direction = ParameterDirection.Input;················ |
| 221 | ············} |
| 222 | |
| 223 | ············selectCommand.CommandText = sql; |
| 224 | ············try |
| 225 | ············{ |
| 226 | ················Dump(null); |
| 227 | ················await DbDataTableFiller.FillAsync( selectCommand, table, cancellationToken ).ConfigureAwait( false ); |
| 228 | ············} |
| 229 | ············catch (OperationCanceledException) |
| 230 | ············{ |
| 231 | ················throw; |
| 232 | ············} |
| 233 | ············catch (System.Exception ex) |
| 234 | ············{ |
| 235 | ················string text = "Exception while reading the mapping table: " + ex.Message + "\n"; |
| 236 | ················text += "Sql-Anweisung: " + selectCommand.CommandText + "\n"; |
| 237 | throw new NDOException( 25, text, ex) ; |
| 238 | ············} |
| 239 | |
| 240 | ············return table;········ |
| 241 | ········} |
| 242 | |
| 243 | ········public async Task UpdateAsync( DataSet ds, CancellationToken cancellationToken = default ) |
| 244 | ········{ |
| 245 | ············DataTable dt = ds.Tables[relation.MappingTable.TableName]; |
| 246 | ············try |
| 247 | ············{ |
| 248 | ················DataRow[] rows = dt.Select(null, null, DataViewRowState.Added |
| 249 | ····················| DataViewRowState.ModifiedCurrent |
| 250 | ····················| DataViewRowState.Deleted); |
| 251 | |
| 252 | ················if (rows.Length > 0) |
| 253 | ················{ |
| 254 | ····················Dump(rows); |
| 255 | ····················// Like DbDataAdapter.Update(DataTable) the rows are processed in the order of the table. |
| 256 | ····················DataRow[] changedRows = dt.Rows.Cast<DataRow>() |
| 257 | ························.Where( r => r.RowState == DataRowState.Added || r.RowState == DataRowState.Modified || r.RowState == DataRowState.Deleted ) |
| 258 | ························.ToArray(); |
| 259 | ····················await rowUpdater.UpdateAsync( changedRows, cancellationToken ).ConfigureAwait( false ); |
| 260 | ················} |
| 261 | ············} |
| 262 | ············catch (OperationCanceledException) |
| 263 | ············{ |
| 264 | ················throw; |
| 265 | ············} |
| 266 | ············catch (System.Exception ex) |
| 267 | ············{ |
| 268 | throw new NDOException( 26, "Exception while updating the mapping table: " + ex. Message + "\n", ex) ; |
| 269 | ············} |
| 270 | ········} |
| 271 | |
| 272 | ········public void Dispose() |
| 273 | ········{ |
| 274 | ········} |
| 275 | |
| 276 | ········public DbConnection Connection |
| 277 | ········{ |
| 278 | ············get { return this.selectCommand.Connection; } |
| 279 | ············set |
| 280 | ············{ |
| 281 | ················this.selectCommand.Connection = value; |
| 282 | ················this.deleteCommand.Connection = value; |
| 283 | ················this.insertCommand.Connection = value; |
| 284 | ············} |
| 285 | ········} |
| 286 | ···· |
| 287 | ········public DbTransaction Transaction |
| 288 | ········{ |
| 289 | ············get { return this.selectCommand.Transaction; } |
| 290 | ············set |
| 291 | ············{ |
| 292 | ················this.selectCommand.Transaction = value; |
| 293 | ················this.deleteCommand.Transaction = value; |
| 294 | ················this.insertCommand.Transaction = value; |
| 295 | ············} |
| 296 | ········} |
| 297 | |
| 298 | ········public Relation Relation |
| 299 | ········{ |
| 300 | ············get { return this.relation; } |
| 301 | ········} |
| 302 | ····} |
| 303 | } |
| 304 |