DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlArray.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.Collections;
19 using System.Collections.Generic;
20 
22 
23 namespace Deveel.Data.Sql.Objects {
28  public sealed class SqlArray : ISqlObject, IEnumerable<SqlExpression> {
29  private readonly SqlExpression[] expressions;
30 
34  public static readonly SqlArray Null = new SqlArray(null);
35 
42  public SqlArray(SqlExpression[] expressions) {
43  if (expressions == null) {
44  this.expressions = null;
45  IsNull = true;
46  } else {
47  this.expressions = new SqlExpression[expressions.Length];
48  Array.Copy(expressions, this.expressions, expressions.Length);
49  }
50  }
51 
52  int IComparable.CompareTo(object obj) {
53  throw new NotSupportedException();
54  }
55 
56  int IComparable<ISqlObject>.CompareTo(ISqlObject other) {
57  throw new NotSupportedException();
58  }
59 
60  public bool IsNull { get; private set; }
61 
68  public int Length {
69  get {
70  AssertNotNull();
71  return expressions.Length;
72  }
73  }
74 
76  return false;
77  }
78 
79  private void AssertNotNull() {
80  if (IsNull)
81  throw new NullReferenceException("The array is null");
82  }
83 
101  public SqlExpression this[int index] {
102  get { return GetValue(index); }
103  }
104 
122  public SqlExpression GetValue(int index) {
123  AssertNotNull();
124 
125  if (index < 0 || index >= Length)
126  throw new ArgumentOutOfRangeException("index");
127 
128  return expressions[index];
129  }
130 
131  public IEnumerator<SqlExpression> GetEnumerator() {
132  AssertNotNull();
133  return new Enumerator(this);
134  }
135 
136  IEnumerator IEnumerable.GetEnumerator() {
137  return GetEnumerator();
138  }
139 
140  #region Enumerator
141 
142  class Enumerator : IEnumerator<SqlExpression> {
143  private int index;
144  private int length;
145  private readonly SqlArray array;
146 
147  public Enumerator(SqlArray array) {
148  this.array = array;
149  length = array.Length;
150  index = -1;
151  }
152 
153  public void Dispose() {
154  }
155 
156  public bool MoveNext() {
157  array.AssertNotNull();
158  return ++index < length;
159  }
160 
161  public void Reset() {
162  array.AssertNotNull();
163 
164  index = -1;
165  length = array.Length;
166  }
167 
168  public SqlExpression Current {
169  get {
170  array.AssertNotNull();
171  return array.GetValue(index);
172  }
173  }
174 
175  object IEnumerator.Current {
176  get { return Current; }
177  }
178  }
179 
180  #endregion
181  }
182 }
IEnumerator< SqlExpression > GetEnumerator()
Definition: SqlArray.cs:131
int Length
Gets the length of the array.
Definition: SqlArray.cs:68
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
readonly SqlExpression[] expressions
Definition: SqlArray.cs:29
bool IsComparableTo(ISqlObject other)
Checks if the current object is comparable with the given one.
SqlExpression GetValue(int index)
Gets the expression at the given index of the array.
Definition: SqlArray.cs:122
Defines the base class for instances that represent SQL expression tree nodes.
An object that provides methods for accessing a finite collection of SQL expressions.
Definition: SqlArray.cs:28
SqlArray(SqlExpression[] expressions)
Constructs a new SqlArray on the given array of expressions.
Definition: SqlArray.cs:42