public DataRow GetRow(IdentityManagementOptions options, object[] identityValues, object[] additionalValues) { if (!_preloaded) { throw new InvalidOperationException("Preload must be called before any IDs can be retrieved."); } // Must be synchronized to avoid adding the same row twice DataRow returnRow = null; lock (_lock) { Identity identity = new Identity(identityValues); bool result = _identityIndex.ContainsKey(identity); if (_identityIndex.TryGetValue(identity, out returnRow)) { if ((options & IdentityManagementOptions.UpdateWhenExisting) != 0) { // Check if anything needs modification bool modified = false; for (int i = 0; i < _additionalColumns.Length && i < additionalValues.Length; i++) { if (additionalValues[i] != null && !Object.Equals(additionalValues[i], returnRow[_additionalColumns[i]])) { modified = true; break; } } // Row found in the hash, update its values if necessary if (modified) { SyncRow(returnRow, identityValues, additionalValues); } } } else if ((options & IdentityManagementOptions.InsertWhenUndefined) != 0) { // Nothing found so far, generate a new row with a new ID returnRow = SyncRow(null, identityValues, additionalValues); } } return(returnRow); }
public long GetID(IdentityManagementOptions options, object[] identityValues, object[] additionalValues) { if (!_preloaded) { throw new InvalidOperationException("Preload must be called before any IDs can be retrieved."); } DataRow row = GetRow(options, identityValues, additionalValues); if (row != null) { return((long)row[_idColumn]); } else { return(-1); } }
/// <summary> /// Retrieves an ID (either existing or new) of the DataItem with the specified identity. /// </summary> /// <typeparam name="DataItemT">The type of DataItem for which to retrieve an ID.</typeparam> /// <param name="values"> /// The identity values followed by additional values required for new items. /// The order of the values must match the order of the columns. /// </param> /// <returns>The unique ID that matches the identity.</returns> public long GetID(Type dataItemType, IdentityManagementOptions options, params object[] values) { IdentityTable table; if (!_types.TryGetValue(dataItemType, out table)) { throw new KeyNotFoundException("The DataItem type {0} does not have an identity management table."); } if (values.Length < table.IdentityColumns.Length) { throw new ArgumentException("The number of values must be at least the number of identity columns.", "values"); } // Retrieve the identity values from the values array object[] identityValues = new object[table.IdentityColumns.Length]; for (int i = 0; i < table.IdentityColumns.Length; i++) { identityValues[i] = values[i].ToString(); } //identityValues[i] = values[i]; // Changed by Yaniv // Retrieve additional values from the values array (if avaiable) object[] additionalValues = null; if (table.AdditionalColumns != null) { additionalValues = new object[table.AdditionalColumns.Length]; for (int i = 0; i < table.AdditionalColumns.Length; i++) { // Get the value if specified, otherwise null additionalValues[i] = i + table.IdentityColumns.Length < values.Length ? values[i + table.IdentityColumns.Length] : null; } } return(table.GetID(options, identityValues, additionalValues)); }
/*=========================*/ #endregion #region Methods /*=========================*/ public long GetID(IdentityManagementOptions options, object[] identityValues, object[] additionalValues) { // Try to get a current version DataTable data = new DataTable(); DataRow returnRow = null; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = DataManager.CreateCommand(_selectCmdText); for (int i = 0; i < _identityColumns.Length; i++) { string paramName = String.Format("@{0}", i + 1); if (adapter.SelectCommand.Parameters.Contains(paramName)) { adapter.SelectCommand.Parameters[paramName].Value = identityValues[i]; } } using (DataManager.Current.OpenConnection()) { // Serializable ensures key range locking DataManager.Current.StartTransaction(IsolationLevel.Serializable); DataManager.Current.AssociateCommands(adapter.SelectCommand); adapter.Fill(data); if (data.Rows.Count > 0) { // Found a match returnRow = data.Rows[0]; if ((options & IdentityManagementOptions.UpdateWhenExisting) != 0) { // Check if anything needs modification bool modified = false; for (int i = 0; i < _additionalColumns.Length && i < additionalValues.Length; i++) { if (additionalValues[i] != null && !Object.Equals(additionalValues[i], returnRow[_additionalColumns[i]])) { modified = true; break; } } // Row found in the hash, update its values if necessary if (modified) { SyncRow(data, identityValues, additionalValues); } } } else if ((options & IdentityManagementOptions.InsertWhenUndefined) != 0) { // Nothing found so far, generate a new row with a new ID returnRow = SyncRow(data, identityValues, additionalValues); } DataManager.Current.CommitTransaction(); } return(Convert.ToInt64(returnRow[_idColumn])); }