﻿// Decompiled with JetBrains decompiler
// Type: System.Data.Common.DateTimeOffsetStorage
// Assembly: System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: A0A8E655-5215-4802-816D-C9E5EBD7DA5D
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Data.dll

using System;
using System.Collections;
using System.Data;
using System.Xml;

namespace System.Data.Common
{
  internal sealed class DateTimeOffsetStorage : DataStorage
  {
    private static readonly DateTimeOffset defaultValue = DateTimeOffset.MinValue;
    private DateTimeOffset[] values;

    internal DateTimeOffsetStorage(DataColumn column)
      : base(column, typeof (DateTimeOffset), (object) DateTimeOffsetStorage.defaultValue, StorageType.DateTimeOffset)
    {
    }

    public override object Aggregate(int[] records, AggregateType kind)
    {
      bool flag = false;
      try
      {
        switch (kind)
        {
          case AggregateType.Min:
            DateTimeOffset second1 = DateTimeOffset.MaxValue;
            for (int index = 0; index < records.Length; ++index)
            {
              int recordNo = records[index];
              if (this.HasValue(recordNo))
              {
                second1 = DateTimeOffset.Compare(this.values[recordNo], second1) < 0 ? this.values[recordNo] : second1;
                flag = true;
              }
            }
            if (flag)
              return (object) second1;
            return this.NullValue;
          case AggregateType.Max:
            DateTimeOffset second2 = DateTimeOffset.MinValue;
            for (int index = 0; index < records.Length; ++index)
            {
              int recordNo = records[index];
              if (this.HasValue(recordNo))
              {
                second2 = DateTimeOffset.Compare(this.values[recordNo], second2) >= 0 ? this.values[recordNo] : second2;
                flag = true;
              }
            }
            if (flag)
              return (object) second2;
            return this.NullValue;
          case AggregateType.First:
            if (records.Length > 0)
              return (object) this.values[records[0]];
            return (object) null;
          case AggregateType.Count:
            int num = 0;
            for (int index = 0; index < records.Length; ++index)
            {
              if (this.HasValue(records[index]))
                ++num;
            }
            return (object) num;
        }
      }
      catch (OverflowException ex)
      {
        throw ExprException.Overflow(typeof (DateTimeOffset));
      }
      throw ExceptionBuilder.AggregateException(kind, this.DataType);
    }

    public override int Compare(int recordNo1, int recordNo2)
    {
      DateTimeOffset first = this.values[recordNo1];
      DateTimeOffset second = this.values[recordNo2];
      if (first == DateTimeOffsetStorage.defaultValue || second == DateTimeOffsetStorage.defaultValue)
      {
        int num = this.CompareBits(recordNo1, recordNo2);
        if (num != 0)
          return num;
      }
      return DateTimeOffset.Compare(first, second);
    }

    public override int CompareValueTo(int recordNo, object value)
    {
      if (this.NullValue == value)
        return !this.HasValue(recordNo) ? 0 : 1;
      DateTimeOffset first = this.values[recordNo];
      if (DateTimeOffsetStorage.defaultValue == first && !this.HasValue(recordNo))
        return -1;
      return DateTimeOffset.Compare(first, (DateTimeOffset) value);
    }

    public override object ConvertValue(object value)
    {
      if (this.NullValue != value)
        value = value == null ? this.NullValue : (object) (DateTimeOffset) value;
      return value;
    }

    public override void Copy(int recordNo1, int recordNo2)
    {
      this.CopyBits(recordNo1, recordNo2);
      this.values[recordNo2] = this.values[recordNo1];
    }

    public override object Get(int record)
    {
      DateTimeOffset dateTimeOffset = this.values[record];
      if (dateTimeOffset != DateTimeOffsetStorage.defaultValue || this.HasValue(record))
        return (object) dateTimeOffset;
      return this.NullValue;
    }

    public override void Set(int record, object value)
    {
      if (this.NullValue == value)
      {
        this.values[record] = DateTimeOffsetStorage.defaultValue;
        this.SetNullBit(record, true);
      }
      else
      {
        this.values[record] = (DateTimeOffset) value;
        this.SetNullBit(record, false);
      }
    }

    public override void SetCapacity(int capacity)
    {
      DateTimeOffset[] dateTimeOffsetArray = new DateTimeOffset[capacity];
      if (this.values != null)
        Array.Copy((Array) this.values, 0, (Array) dateTimeOffsetArray, 0, Math.Min(capacity, this.values.Length));
      this.values = dateTimeOffsetArray;
      base.SetCapacity(capacity);
    }

    public override object ConvertXmlToObject(string s)
    {
      return (object) XmlConvert.ToDateTimeOffset(s);
    }

    public override string ConvertObjectToXml(object value)
    {
      return XmlConvert.ToString((DateTimeOffset) value);
    }

    protected override object GetEmptyStorage(int recordCount)
    {
      return (object) new DateTimeOffset[recordCount];
    }

    protected override void CopyValue(int record, object store, BitArray nullbits, int storeIndex)
    {
      ((DateTimeOffset[]) store)[storeIndex] = this.values[record];
      nullbits.Set(storeIndex, !this.HasValue(record));
    }

    protected override void SetStorage(object store, BitArray nullbits)
    {
      this.values = (DateTimeOffset[]) store;
      this.SetNullStorage(nullbits);
    }
  }
}
