DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ObjectName.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 using System.Diagnostics;
19 using System.IO;
20 using System.Text;
21 
23 
24 namespace Deveel.Data.Sql {
42  [DebuggerDisplay("{FullName}")]
43  [Serializable]
44  public sealed class ObjectName : IEquatable<ObjectName>, IComparable<ObjectName>, ISerializable {
49  public const string GlobName = "*";
50 
54  public const char Separator = '.';
55 
71  public ObjectName(string name)
72  : this(null, name) {
73  }
74 
83  public ObjectName(ObjectName parent, string name) {
84  if (String.IsNullOrEmpty(name))
85  throw new ArgumentNullException("name");
86 
87  Name = name;
88  Parent = parent;
89  }
90 
91  private ObjectName(ObjectData graph) {
92  Name = graph.GetString("Name");
93  Parent = graph.GetValue<ObjectName>("Parent");
94  }
95 
99  public ObjectName Parent { get; private set; }
100 
101  public string ParentName {
102  get { return Parent == null ? null : Parent.FullName; }
103  }
104 
108  public string Name { get; private set; }
109 
114  public string FullName {
115  get { return ToString(); }
116  }
117 
121  public bool IsGlob {
122  get { return Name.Equals(GlobName); }
123  }
124 
139  public static ObjectName Parse(string s) {
140  if (String.IsNullOrEmpty(s))
141  throw new ArgumentNullException("s");
142 
143  var sp = s.Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
144  if (sp.Length == 0)
145  throw new FormatException("At least one part of the name must be provided");
146 
147  if (sp.Length == 1)
148  return new ObjectName(sp[0]);
149 
150  ObjectName finalName = null;
151  for (int i = 0; i < sp.Length; i++) {
152  if (finalName == null) {
153  finalName = new ObjectName(sp[i]);
154  } else {
155  finalName = new ObjectName(finalName, sp[i]);
156  }
157  }
158 
159  return finalName;
160  }
161 
172  public static ObjectName ResolveSchema(string schemaName, string name) {
173  var sb = new StringBuilder();
174  if (!String.IsNullOrEmpty(schemaName))
175  sb.Append(schemaName).Append('.');
176  sb.Append(name);
177 
178  return Parse(sb.ToString());
179  }
180 
189  public ObjectName Child(string name) {
190  return new ObjectName(this, name);
191  }
192 
193  public ObjectName Child(ObjectName childName) {
194  var baseName = this;
195  ObjectName parent = childName.Parent;
196  while (parent != null) {
197  baseName = baseName.Child(parent.Name);
198  parent = parent.Parent;
199  }
200 
201  baseName = baseName.Child(childName.Name);
202  return baseName;
203  }
204 
211  public int CompareTo(ObjectName other) {
212  if (other == null)
213  return -1;
214 
215  int v = 0;
216  if (Parent != null)
217  v = Parent.CompareTo(other.Parent);
218 
219  if (v == 0)
220  v = String.Compare(Name, other.Name, StringComparison.Ordinal);
221 
222  return v;
223  }
224 
225  public override string ToString() {
226  var sb = new StringBuilder();
227  if (Parent != null) {
228  sb.Append(Parent);
229  sb.Append('.');
230  }
231 
232  sb.Append(Name);
233  return sb.ToString();
234  }
235 
237  graph.SetValue("Name", Name);
238  graph.SetValue("Parent", Parent);
239  }
240 
241  public override bool Equals(object obj) {
242  var other = obj as ObjectName;
243  if (other == null)
244  return false;
245 
246  return Equals(other);
247  }
248 
249  public bool Equals(ObjectName other) {
250  return Equals(other, true);
251  }
252 
263  public bool Equals(ObjectName other, bool ignoreCase) {
264  if (other == null)
265  return false;
266 
267  if (Parent != null && other.Parent == null)
268  return false;
269  if (Parent == null && other.Parent != null)
270  return false;
271 
272  if (Parent != null && !Parent.Equals(other.Parent, ignoreCase))
273  return false;
274 
275  var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
276  return String.Equals(Name, other.Name, comparison);
277  }
278 
279  public override int GetHashCode() {
280  var code = Name.GetHashCode() ^ 5623;
281  if (Parent != null)
282  code ^= Parent.GetHashCode();
283 
284  return code;
285  }
286 
287  public static void Serialize(ObjectName objectName, Stream stream) {
288  using (var writer = new BinaryWriter(stream, Encoding.Unicode)) {
289  Serialize(objectName, writer);
290  }
291  }
292 
293  public static void Serialize(ObjectName objectName, BinaryWriter writer) {
294  if (objectName == null) {
295  writer.Write((byte) 0);
296  return;
297  }
298 
299  writer.Write((byte)1);
300 
301  if (objectName.Parent != null) {
302  writer.Write((byte) 1);
303  Serialize(objectName.Parent, writer);
304  } else {
305  writer.Write((byte)0);
306  }
307 
308  writer.Write(objectName.Name);
309  }
310 
311  public static ObjectName Deserialize(Stream stream) {
312  using (var reader = new BinaryReader(stream, Encoding.Unicode)) {
313  return Deserialize(reader);
314  }
315  }
316 
317  public static ObjectName Deserialize(BinaryReader reader) {
318  var status = reader.ReadByte();
319  if (status == 0)
320  return null;
321 
322  ObjectName parent = null;
323 
324  var parentStatus = reader.ReadByte();
325  if (parentStatus == 1)
326  parent = Deserialize(reader);
327 
328  var name = reader.ReadString();
329  return new ObjectName(parent, name);
330  }
331  }
332 }
void GetData(SerializeData data)
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
override string ToString()
Definition: ObjectName.cs:225
ObjectName(ObjectData graph)
Definition: ObjectName.cs:91
static void Serialize(ObjectName objectName, BinaryWriter writer)
Definition: ObjectName.cs:293
ObjectName Child(ObjectName childName)
Definition: ObjectName.cs:193
static void Serialize(ObjectName objectName, Stream stream)
Definition: ObjectName.cs:287
int CompareTo(ObjectName other)
Compares this instance of the object reference to a given one and returns a value indicating if the t...
Definition: ObjectName.cs:211
ObjectName(string name)
Constructs a name reference without a parent.
Definition: ObjectName.cs:71
void SetValue(string key, Type type, object value)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
static ObjectName Deserialize(Stream stream)
Definition: ObjectName.cs:311
ObjectName Child(string name)
Creates a reference what is the child of the current one.
Definition: ObjectName.cs:189
override bool Equals(object obj)
Definition: ObjectName.cs:241
override int GetHashCode()
Definition: ObjectName.cs:279
string FullName
Gets the full reference name formatted.
Definition: ObjectName.cs:114
static ObjectName ResolveSchema(string schemaName, string name)
Creates a new reference to a table, given a schema and a table name.
Definition: ObjectName.cs:172
ObjectName Parent
Gets the parent reference of the current one, if any or null if none.
Definition: ObjectName.cs:99
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
static ObjectName Deserialize(BinaryReader reader)
Definition: ObjectName.cs:317
ObjectName(ObjectName parent, string name)
Constructs a name reference with a given parent.
Definition: ObjectName.cs:83
bool Equals(ObjectName other)
Definition: ObjectName.cs:249
bool Equals(ObjectName other, bool ignoreCase)
Compares this object name with the other one given, according to the case sensitivity specified...
Definition: ObjectName.cs:263