OracleLob.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using System;
  2. using System.Data.SqlTypes;
  3. using System.IO;
  4. using System.Text;
  5. namespace System.Data.OracleClient {
  6. public sealed class OracleLob : Stream, ICloneable, INullable {
  7. #region Fields
  8. public static readonly new OracleLob Null = new OracleLob ();
  9. internal OracleConnection connection;
  10. bool isBatched = false;
  11. bool isOpen = true;
  12. bool notNull = false;
  13. OracleType type;
  14. long length = -1;
  15. long position = 1;
  16. #endregion // Fields
  17. #region Constructors
  18. internal OracleLob () {
  19. }
  20. #endregion // Constructors
  21. #region Properties
  22. public override bool CanRead {
  23. get { return (IsNull || isOpen); }
  24. }
  25. public override bool CanSeek {
  26. get { return (IsNull || isOpen); }
  27. }
  28. public override bool CanWrite {
  29. get { return isOpen; }
  30. }
  31. public int ChunkSize {
  32. [MonoTODO]
  33. get {
  34. AssertConnectionIsOpen ();
  35. AssertObjectNotDisposed ();
  36. throw new NotImplementedException ();
  37. }
  38. }
  39. public OracleConnection Connection {
  40. get { return connection; }
  41. }
  42. public bool IsBatched {
  43. get { return isBatched; }
  44. }
  45. public bool IsNull {
  46. get { return !notNull; }
  47. }
  48. public bool IsTemporary {
  49. get {
  50. AssertConnectionIsOpen ();
  51. AssertObjectNotDisposed ();
  52. throw new NotImplementedException ();
  53. }
  54. }
  55. public override long Length {
  56. get {
  57. AssertConnectionIsOpen ();
  58. AssertObjectNotDisposed ();
  59. throw new NotImplementedException ();
  60. }
  61. }
  62. public OracleType LobType {
  63. get { return type; }
  64. }
  65. public override long Position {
  66. get {
  67. AssertConnectionIsOpen ();
  68. AssertObjectNotDisposed ();
  69. return position;
  70. }
  71. set {
  72. AssertConnectionIsOpen ();
  73. AssertObjectNotDisposed ();
  74. position = value;
  75. }
  76. }
  77. public object Value {
  78. get {
  79. AssertObjectNotDisposed ();
  80. if (IsNull)
  81. return DBNull.Value;
  82. byte[] buffer = null;
  83. int len = (int) Length;
  84. if (len == 0) {
  85. // LOB is not Null, but it is Empty
  86. if (LobType == OracleType.Clob)
  87. return "";
  88. else // OracleType.Blob
  89. return new byte[0];
  90. }
  91. if (LobType == OracleType.Clob) {
  92. buffer = new byte [len];
  93. Read (buffer, 0, len);
  94. UnicodeEncoding encoding = new UnicodeEncoding ();
  95. return encoding.GetString (buffer);
  96. }
  97. else {
  98. // OracleType.Blob
  99. buffer = new byte [len];
  100. Read (buffer, 0, len);
  101. return buffer;
  102. }
  103. }
  104. }
  105. #endregion // Properties
  106. #region Methods
  107. [MonoTODO]
  108. public void Append (OracleLob source) {
  109. if (source.IsNull)
  110. throw new ArgumentNullException ();
  111. if (Connection.State == ConnectionState.Closed)
  112. throw new InvalidOperationException ();
  113. throw new NotImplementedException ();
  114. }
  115. void AssertAmountIsEven (long amount, string argName) {
  116. if (amount % 2 == 1)
  117. throw new ArgumentOutOfRangeException ("CLOB and NCLOB parameters require even number of bytes for this argument.");
  118. }
  119. void AssertAmountIsValid (long amount, string argName) {
  120. if (amount > UInt32.MaxValue)
  121. throw new ArgumentOutOfRangeException ("Argument too big.");
  122. if (LobType == OracleType.Clob || LobType == OracleType.NClob)
  123. AssertAmountIsEven (amount, argName);
  124. }
  125. void AssertConnectionIsOpen () {
  126. if (connection.State == ConnectionState.Closed)
  127. throw new InvalidOperationException ("Invalid operation. The connection is closed.");
  128. }
  129. void AssertObjectNotDisposed () {
  130. if (!isOpen)
  131. throw new ObjectDisposedException ("OracleLob");
  132. }
  133. void AssertTransactionExists () {
  134. // if (connection.Transaction == null)
  135. // throw new InvalidOperationException ("Modifying a LOB requires that the connection be transacted.");
  136. throw new NotImplementedException ();
  137. }
  138. public void BeginBatch () {
  139. BeginBatch (OracleLobOpenMode.ReadOnly);
  140. }
  141. public void BeginBatch (OracleLobOpenMode mode) {
  142. AssertConnectionIsOpen ();
  143. AssertObjectNotDisposed ();
  144. isBatched = true;
  145. }
  146. [MonoTODO]
  147. public object Clone () {
  148. throw new NotImplementedException ();
  149. }
  150. [MonoTODO]
  151. public override void Close () {
  152. isOpen = false;
  153. }
  154. public long CopyTo (OracleLob destination) {
  155. return CopyTo (0, destination, 0, Length);
  156. }
  157. public long CopyTo (OracleLob destination, long destinationOffset) {
  158. return CopyTo (0, destination, destinationOffset, Length);
  159. }
  160. public long CopyTo (long sourceOffset, OracleLob destination, long destinationOffset, long amount) {
  161. if (destination.IsNull)
  162. throw new ArgumentNullException ();
  163. AssertAmountIsValid (sourceOffset, "sourceOffset");
  164. AssertAmountIsValid (destinationOffset, "destinationOffset");
  165. AssertAmountIsValid (amount, "amount");
  166. AssertTransactionExists ();
  167. AssertConnectionIsOpen ();
  168. throw new NotImplementedException ();
  169. }
  170. [MonoTODO]
  171. public void Dispose () {
  172. throw new NotImplementedException ();
  173. }
  174. public void EndBatch () {
  175. AssertConnectionIsOpen ();
  176. AssertObjectNotDisposed ();
  177. isBatched = false;
  178. }
  179. public long Erase () {
  180. return Erase (0, Length);
  181. }
  182. public long Erase (long offset, long amount) {
  183. if (offset < 0 || amount < 0)
  184. throw new ArgumentOutOfRangeException ("Must be a positive value.");
  185. if (offset + amount > Length)
  186. throw new ArgumentOutOfRangeException ();
  187. AssertAmountIsValid (offset, "offset");
  188. AssertAmountIsValid (amount, "amount");
  189. throw new NotImplementedException ();
  190. }
  191. public override void Flush () {
  192. // No-op
  193. }
  194. public override int Read (byte[] buffer, int offset, int count) {
  195. if (buffer == null)
  196. throw new ArgumentNullException ();
  197. AssertAmountIsValid (offset, "offset");
  198. AssertAmountIsValid (count, "count");
  199. AssertConnectionIsOpen ();
  200. AssertObjectNotDisposed ();
  201. throw new NotImplementedException ();
  202. int bytesRead;
  203. byte[] output = new byte[count];
  204. output.CopyTo (buffer, offset);
  205. position += bytesRead;
  206. return bytesRead;
  207. }
  208. [MonoTODO]
  209. public override long Seek (long offset, SeekOrigin origin) {
  210. long newPosition = position;
  211. switch (origin) {
  212. case SeekOrigin.Begin:
  213. newPosition = offset;
  214. break;
  215. case SeekOrigin.Current:
  216. newPosition += offset;
  217. break;
  218. case SeekOrigin.End:
  219. newPosition = Length - offset;
  220. break;
  221. }
  222. if (newPosition > Length)
  223. throw new ArgumentOutOfRangeException ();
  224. position = newPosition;
  225. return position;
  226. }
  227. [MonoTODO]
  228. public override void SetLength (long value) {
  229. AssertAmountIsValid (value, "value");
  230. AssertTransactionExists ();
  231. AssertConnectionIsOpen ();
  232. AssertObjectNotDisposed ();
  233. throw new NotImplementedException ();
  234. length = value;
  235. }
  236. public override void Write (byte[] buffer, int offset, int count) {
  237. if (buffer == null)
  238. throw new ArgumentNullException ("Buffer is null.");
  239. if (offset < 0 || count < 0)
  240. throw new ArgumentOutOfRangeException ("Must be a positive value.");
  241. if (offset + count > buffer.Length)
  242. throw new ArgumentOutOfRangeException ("The offset and count values specified exceed the buffer provided.");
  243. AssertAmountIsValid (offset, "offset");
  244. AssertAmountIsValid (count, "count");
  245. AssertTransactionExists ();
  246. AssertConnectionIsOpen ();
  247. AssertObjectNotDisposed ();
  248. byte[] value = null;
  249. if (offset + count == buffer.Length && offset == 0)
  250. value = buffer;
  251. else {
  252. value = new byte[count];
  253. Array.Copy (buffer, offset, value, 0, count);
  254. }
  255. throw new NotImplementedException ();
  256. // position += locator.Write (value, (uint) Position, (uint) value.Length, LobType);
  257. }
  258. #endregion // Methods
  259. }
  260. }