DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlLongString.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 using System.IO;
21 using System.Text;
22 
23 using Deveel.Data.Store;
24 
25 namespace Deveel.Data.Sql.Objects {
26  public sealed class SqlLongString : ISqlString, IObjectRef, IDisposable {
28 
29  public static readonly SqlLongString Null = new SqlLongString(null, null, true);
30 
31  private SqlLongString(ILargeObject largeObject, Encoding encoding, bool isNull) {
32  if (!isNull && largeObject == null)
33  throw new ArgumentNullException("largeObject");
34 
35  this.largeObject = largeObject;
36  Encoding = encoding;
37  IsNull = isNull;
38 
39  if (!isNull) {
40  Length = largeObject.RawSize;
41  }
42  }
43 
44  public SqlLongString(ILargeObject largeObject, Encoding encoding)
45  : this(largeObject, encoding, false) {
46  }
47 
49  Dispose(false);
50  }
51 
52  public Encoding Encoding { get; private set; }
53 
54  public void Dispose() {
55  Dispose(true);
56  GC.SuppressFinalize(this);
57  }
58 
59  private void Dispose(bool disposing) {
60  if (disposing) {
61  if (largeObject != null)
62  largeObject.Dispose();
63  }
64 
65  Encoding = null;
66  largeObject = null;
67  }
68 
69  int IComparable.CompareTo(object obj) {
70  throw new NotSupportedException();
71  }
72 
73  int IComparable<ISqlObject>.CompareTo(ISqlObject other) {
74  throw new NotSupportedException();
75  }
76 
77  public ObjectId ObjectId {
78  get { return largeObject.Id; }
79  }
80 
81  public bool IsNull { get; private set; }
82 
84  return false;
85  }
86 
87  public char this[long offset] {
88  get {
89  if (offset > Length)
90  throw new ArgumentOutOfRangeException("offset");
91  if (largeObject == null)
92  return '\0';
93 
94  throw new NotImplementedException();
95  }
96  }
97 
98  int IComparable<ISqlString>.CompareTo(ISqlString other) {
99  throw new NotSupportedException();
100  }
101 
102  public IEnumerator<char> GetEnumerator() {
103  return new Enumerator(this);
104  }
105 
106  IEnumerator IEnumerable.GetEnumerator() {
107  return GetEnumerator();
108  }
109 
110  public long Length { get; private set; }
111 
112  public TextReader GetInput(Encoding encoding) {
113  if (largeObject == null)
114  return TextReader.Null;
115 
116  return new StreamReader(new ObjectStream(largeObject), encoding);
117  }
118 
119  public static SqlLongString Unicode(ILargeObject largeObject) {
120  return new SqlLongString(largeObject, Encoding.Unicode);
121  }
122 
123 #if !PCL
124  public static SqlLongString Ascii(ILargeObject largeObject) {
125  return new SqlLongString(largeObject, Encoding.ASCII);
126  }
127 #endif
128 
129  #region Enumerator
130 
131  class Enumerator : IEnumerator<char> {
132  private TextReader reader;
133  private readonly SqlLongString longString;
134  private int curChar;
135 
136  public Enumerator(SqlLongString longString) {
137  this.longString = longString;
138  reader = longString.GetInput(longString.Encoding);
139  }
140 
141  public void Dispose() {
142  if (reader != null) {
143  reader.Dispose();
144  reader = null;
145  }
146  }
147 
148  public bool MoveNext() {
149  curChar = reader.Read();
150  return curChar != -1;
151  }
152 
153  public void Reset() {
154  reader = longString.GetInput(longString.Encoding);
155  }
156 
157  public char Current {
158  get {
159  if (curChar == -1)
160  throw new EndOfStreamException();
161 
162  return (char) curChar;
163  }
164  }
165 
166  object IEnumerator.Current {
167  get { return Current; }
168  }
169  }
170 
171  #endregion
172  }
173 }
Defines a referenced object that can be accessed on a multi-phase level.
Definition: ILargeObject.cs:35
static SqlLongString Unicode(ILargeObject largeObject)
TextReader GetInput(Encoding encoding)
long RawSize
Gets the raw byte size of the object.
Definition: ILargeObject.cs:56
static SqlLongString Ascii(ILargeObject largeObject)
SqlLongString(ILargeObject largeObject, Encoding encoding)
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
A unique identifier of an object within a database system, that is composed by a reference to the sto...
Definition: ObjectId.cs:31
bool IsComparableTo(ISqlObject other)
Checks if the current object is comparable with the given one.
ObjectId Id
Gets the unique identifier of the object within the system.
Definition: ILargeObject.cs:39
SqlLongString(ILargeObject largeObject, Encoding encoding, bool isNull)