public static MeasurementCharacteristic GetOrAdd(this TableOperations <MeasurementCharacteristic> measurementCharacteristicTable, string name, string description = null)
        {
            MeasurementCharacteristic measurementCharacteristic = measurementCharacteristicTable.QueryRecordWhere("Name = {0}", name);

            if ((object)measurementCharacteristic == null)
            {
                measurementCharacteristic             = new MeasurementCharacteristic();
                measurementCharacteristic.Name        = name;
                measurementCharacteristic.Description = description ?? name;

                try
                {
                    measurementCharacteristicTable.AddNewRecord(measurementCharacteristic);
                }
                catch (Exception ex)
                {
                    // Ignore errors regarding unique key constraints
                    // which can occur as a result of a race condition
                    bool isUniqueViolation = ExceptionHandler.IsUniqueViolation(ex);

                    if (!isUniqueViolation)
                    {
                        throw;
                    }

                    return(measurementCharacteristicTable.QueryRecordWhere("Name = {0}", name));
                }

                measurementCharacteristic.ID = measurementCharacteristicTable.Connection.ExecuteScalar <int>("SELECT @@IDENTITY");
            }

            return(measurementCharacteristic);
        }
Example #2
0
        public MeasurementCharacteristic GetMeasurementCharacteristic(MeasurementCharacteristic measurementCharacteristic)
        {
            MeasurementCharacteristic cachedMeasurementCharacteristicLine;

            if ((object)measurementCharacteristic == null)
            {
                return(null);
            }

            if (measurementCharacteristic.ID == 0)
            {
                return(measurementCharacteristic);
            }

            if (m_measurementCharacteristics.TryGetValue(measurementCharacteristic.ID, out cachedMeasurementCharacteristicLine))
            {
                return(cachedMeasurementCharacteristicLine);
            }

            m_measurementCharacteristics.Add(measurementCharacteristic.ID, measurementCharacteristic);
            return(measurementCharacteristic);
        }