OciLobLocator.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // OciLobLocator.cs
  3. //
  4. // Part of managed C#/.NET library System.Data.OracleClient.dll
  5. //
  6. // Part of the Mono class libraries at
  7. // mcs/class/System.Data.OracleClient/System.Data.OracleClient.Oci
  8. //
  9. // Assembly: System.Data.OracleClient.dll
  10. // Namespace: System.Data.OracleClient.Oci
  11. //
  12. // Author:
  13. // Tim Coleman <[email protected]>
  14. //
  15. // Copyright (C) Tim Coleman, 2003
  16. //
  17. using System;
  18. using System.Data.OracleClient;
  19. using System.Runtime.InteropServices;
  20. namespace System.Data.OracleClient.Oci {
  21. internal sealed class OciLobLocator : OciDescriptorHandle, IDisposable
  22. {
  23. #region Fields
  24. bool disposed = false;
  25. OciErrorHandle errorHandle;
  26. OciServiceHandle service;
  27. OciDataType type;
  28. #endregion // Fields
  29. #region Constructors
  30. public OciLobLocator (OciHandle parent, IntPtr handle)
  31. : base (OciHandleType.LobLocator, parent, handle)
  32. {
  33. }
  34. #endregion // Constructors
  35. #region Properties
  36. public OciErrorHandle ErrorHandle {
  37. get { return errorHandle; }
  38. set { errorHandle = value; }
  39. }
  40. public OciServiceHandle Service {
  41. get { return service; }
  42. set { service = value; }
  43. }
  44. public OciDataType LobType {
  45. get { return type; }
  46. set { type = value; }
  47. }
  48. #endregion // Properties
  49. #region Methods
  50. public void BeginBatch (OracleLobOpenMode mode)
  51. {
  52. int status = 0;
  53. status = OciCalls.OCILobOpen (Service,
  54. ErrorHandle,
  55. Handle,
  56. (byte) mode);
  57. if (status != 0) {
  58. OciErrorInfo info = ErrorHandle.HandleError ();
  59. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  60. }
  61. }
  62. public uint Copy (OciLobLocator destination, uint amount, uint destinationOffset, uint sourceOffset)
  63. {
  64. OciCalls.OCILobCopy (Service,
  65. ErrorHandle,
  66. destination,
  67. Handle,
  68. amount,
  69. destinationOffset,
  70. sourceOffset);
  71. return amount;
  72. }
  73. protected override void Dispose (bool disposing)
  74. {
  75. if (!disposed) {
  76. disposed = true;
  77. base.Dispose ();
  78. }
  79. }
  80. public void EndBatch ()
  81. {
  82. int status = 0;
  83. status = OciCalls.OCILobClose (Service, ErrorHandle, this);
  84. if (status != 0) {
  85. OciErrorInfo info = ErrorHandle.HandleError ();
  86. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  87. }
  88. }
  89. public uint Erase (uint offset, uint amount)
  90. {
  91. int status = 0;
  92. uint output = amount;
  93. status = OciCalls.OCILobErase (Service,
  94. ErrorHandle,
  95. this,
  96. ref output,
  97. (uint) offset);
  98. if (status != 0) {
  99. OciErrorInfo info = ErrorHandle.HandleError ();
  100. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  101. }
  102. return output;
  103. }
  104. public int GetChunkSize ()
  105. {
  106. int status = 0;
  107. uint output;
  108. status = OciCalls.OCILobGetChunkSize (Service,
  109. ErrorHandle,
  110. this,
  111. out output);
  112. if (status != 0) {
  113. OciErrorInfo info = ErrorHandle.HandleError ();
  114. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  115. }
  116. return (int) output;
  117. }
  118. public long GetLength (bool binary)
  119. {
  120. int status = 0;
  121. uint output;
  122. status = OciCalls.OCILobGetLength (Service,
  123. ErrorHandle,
  124. this,
  125. out output);
  126. if (status != 0) {
  127. OciErrorInfo info = ErrorHandle.HandleError ();
  128. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  129. }
  130. if (!binary)
  131. output *= 2;
  132. return (long) output;
  133. }
  134. public int Read (byte[] buffer, uint offset, uint count, bool binary)
  135. {
  136. int status = 0;
  137. uint amount = count;
  138. // Character types are UTF-16, so amount of characters is 1/2
  139. // the amount of bytes
  140. if (!binary)
  141. amount /= 2;
  142. status = OciCalls.OCILobRead (Service,
  143. ErrorHandle,
  144. this,
  145. ref amount,
  146. offset,
  147. buffer,
  148. count,
  149. IntPtr.Zero,
  150. IntPtr.Zero,
  151. 1000, // OCI_UCS2ID
  152. 0); // Ignored if csid is specified as above
  153. if (status != 0) {
  154. OciErrorInfo info = ErrorHandle.HandleError ();
  155. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  156. }
  157. return (int) amount;
  158. }
  159. public void Trim (uint newlen)
  160. {
  161. int status = 0;
  162. status = OciCalls.OCILobTrim (Service,
  163. ErrorHandle,
  164. this,
  165. newlen);
  166. if (status != 0) {
  167. OciErrorInfo info = ErrorHandle.HandleError ();
  168. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  169. }
  170. }
  171. public int Write (byte[] buffer, uint offset, uint count, OracleType type)
  172. {
  173. int status = 0;
  174. uint amount = count;
  175. if (type == OracleType.Clob)
  176. amount /= 2;
  177. status = OciCalls.OCILobWrite (Service,
  178. ErrorHandle,
  179. this,
  180. ref amount,
  181. offset,
  182. buffer,
  183. count,
  184. 0, // OCI_ONE_PIECE
  185. IntPtr.Zero,
  186. IntPtr.Zero,
  187. 1000, // OCI_UCS2ID
  188. 0);
  189. if (status != 0) {
  190. OciErrorInfo info = ErrorHandle.HandleError ();
  191. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  192. }
  193. return (int) amount;
  194. }
  195. #endregion // Methods
  196. }
  197. }