DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
RowId.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.Tables {
24  public struct RowId : IEquatable<RowId> {
28  public static readonly RowId Null = new RowId(true);
29 
39  public RowId(int tableId, int rowNumber)
40  : this(false) {
41  RowNumber = rowNumber;
42  TableId = tableId;
43  }
44 
45  private RowId(bool isNull)
46  : this() {
47  IsNull = isNull;
48  }
49 
53  public int TableId { get; private set; }
54 
58  public int RowNumber { get; private set; }
59 
64  public bool IsNull { get; private set; }
65 
66  public bool Equals(RowId other) {
67  if (IsNull && other.IsNull)
68  return true;
69 
70  return TableId.Equals(other.TableId) &&
71  RowNumber.Equals(other.RowNumber);
72  }
73 
74  public override bool Equals(object obj) {
75  if (!(obj is RowId))
76  return false;
77 
78  return Equals((RowId) obj);
79  }
80 
81  public override int GetHashCode() {
82  if (IsNull)
83  return 0;
84 
85  return unchecked (TableId.GetHashCode() ^ RowNumber.GetHashCode());
86  }
87 
88  public override string ToString() {
89  return String.Format("{0}-{1}", TableId, RowNumber);
90  }
91 
103  public static bool TryParse(string s, out RowId value) {
104  value = Null;
105 
106  if (String.IsNullOrEmpty(s))
107  return false;
108 
109  var index = s.IndexOf("-", StringComparison.Ordinal);
110  if (index == -1)
111  return false;
112 
113  var s1 = s.Substring(0, index);
114  var s2 = s.Substring(index + 1);
115 
116  int v1;
117  int v2;
118  if (!Int32.TryParse(s1, out v1))
119  return false;
120  if (!Int32.TryParse(s2, out v2))
121  return false;
122 
123  value = new RowId(v1, v2);
124  return true;
125  }
126 
138  public static RowId Parse(string s) {
139  RowId rowId;
140  if (!TryParse(s, out rowId))
141  throw new FormatException("Unable to parse the given string into a valid ROWID.");
142 
143  return rowId;
144  }
145  }
146 }
override bool Equals(object obj)
Definition: RowId.cs:74
bool IsNull
Gets a boolean value indicating if the object equivales to a NULL.
Definition: RowId.cs:64
override int GetHashCode()
Definition: RowId.cs:81
bool Equals(RowId other)
Definition: RowId.cs:66
int RowNumber
Gets the number of the column within the table referenced.
Definition: RowId.cs:58
static RowId Parse(string s)
Parses the given input string into an instance of RowId.
Definition: RowId.cs:138
RowId(int tableId, int rowNumber)
Constructs the object with the references to the given table unique ID and the number of the row with...
Definition: RowId.cs:39
RowId(bool isNull)
Definition: RowId.cs:45
Defines the value of a ROWID object, that is a unique reference within a database system to a single ...
Definition: RowId.cs:24
static bool TryParse(string s, out RowId value)
Attempts to parse the input string given into a valid instance of RowId.
Definition: RowId.cs:103
override string ToString()
Definition: RowId.cs:88
int TableId
Gets the unique identifier of the table the row is contained.
Definition: RowId.cs:53