DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlYearToMonth.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 
19 namespace Deveel.Data.Sql.Objects {
23  public struct SqlYearToMonth : ISqlObject, IComparable<SqlYearToMonth> {
24  private int? months;
25 
26  public static readonly SqlYearToMonth Null = new SqlYearToMonth(true);
27 
28  public SqlYearToMonth(int months)
29  : this() {
30  this.months = months;
31  }
32 
33  public SqlYearToMonth(int years, int months)
34  : this((years*12) + months) {
35  }
36 
37  private SqlYearToMonth(bool isNull)
38  : this() {
39  if (isNull)
40  months = null;
41  }
42 
43  int IComparable.CompareTo(object obj) {
44  return CompareTo((ISqlObject) obj);
45  }
46 
47  public int CompareTo(ISqlObject other) {
48  if (other is SqlYearToMonth)
49  return CompareTo((SqlYearToMonth) other);
50 
51  throw new NotSupportedException();
52  }
53 
55  public bool IsNull {
56  get { return months == null; }
57  }
58 
62  public int TotalMonths {
63  get {
64  if (months == null)
65  throw new NullReferenceException();
66 
67  return months.Value;
68  }
69  }
70 
74  public double TotalYears {
75  get {
76  if (months == null)
77  throw new NullReferenceException();
78 
79  return (months.Value/12);
80  }
81  }
82 
84  return other is SqlYearToMonth ||
85  other is SqlNumber ||
86  other is SqlDayToSecond;
87  }
88 
90  public int CompareTo(SqlYearToMonth other) {
91  if (other.IsNull && IsNull)
92  return 0;
93  if (IsNull && !other.IsNull)
94  return 1;
95  if (!IsNull && other.IsNull)
96  return -1;
97 
98  return months.Value.CompareTo(other.months.Value);
99  }
100  }
101 }
SqlYearToMonth(int years, int months)
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
bool IsComparableTo(ISqlObject other)
Checks if the current object is comparable with the given one.
int CompareTo(SqlYearToMonth other)
A month span representation of time.