This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient.Oci / OciLobLocator.cs
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 <tim@timcoleman.com>
14 //         
15 // Copyright (C) Tim Coleman, 2003
16 // 
17
18 using System;
19 using System.Data.OracleClient;
20 using System.Runtime.InteropServices;
21
22 namespace System.Data.OracleClient.Oci {
23         internal sealed class OciLobLocator : OciDescriptorHandle, IDisposable
24         {
25                 #region Fields
26
27                 bool disposed = false;
28                 OciErrorHandle errorHandle;
29                 OciServiceHandle service;
30                 OciDataType type;
31
32                 #endregion // Fields
33
34                 #region Constructors
35
36                 public OciLobLocator (OciHandle parent, IntPtr handle)
37                         : base (OciHandleType.LobLocator, parent, handle)
38                 {
39                 }
40
41                 #endregion // Constructors
42
43                 #region Properties 
44
45                 public OciErrorHandle ErrorHandle {
46                         get { return errorHandle; }
47                         set { errorHandle = value; }
48                 }
49
50                 public OciServiceHandle Service {
51                         get { return service; }
52                         set { service = value; }
53                 }
54
55                 public OciDataType LobType {    
56                         get { return type; }
57                         set { type = value; }
58                 }
59                 
60                 #endregion // Properties
61
62                 #region Methods
63
64                 public void BeginBatch (OracleLobOpenMode mode)
65                 {
66                         int status = 0;
67                         status = OciCalls.OCILobOpen (Service, 
68                                                 ErrorHandle,
69                                                 Handle,
70                                                 (byte) mode);
71
72                         if (status != 0) {
73                                 OciErrorInfo info = ErrorHandle.HandleError ();
74                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
75                         }
76                 }
77
78                 public uint Copy (OciLobLocator destination, uint amount, uint destinationOffset, uint sourceOffset)
79                 {
80                         OciCalls.OCILobCopy (Service,
81                                         ErrorHandle,
82                                         destination,
83                                         Handle,
84                                         amount,
85                                         destinationOffset,
86                                         sourceOffset);
87                         return amount;
88                 }
89
90                 protected override void Dispose (bool disposing)
91                 {
92                         if (!disposed) {
93                                 disposed = true;
94                                 base.Dispose ();
95                         }
96                 }
97
98                 public void EndBatch ()
99                 {
100                         int status = 0;
101                         status = OciCalls.OCILobClose (Service, ErrorHandle, this);
102
103                         if (status != 0) {
104                                 OciErrorInfo info = ErrorHandle.HandleError ();
105                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
106                         }
107                 }
108
109                 public uint Erase (uint offset, uint amount)
110                 {
111                         int status = 0;
112                         uint output = amount;
113                         status = OciCalls.OCILobErase (Service,
114                                                 ErrorHandle,
115                                                 this,
116                                                 ref output,
117                                                 (uint) offset);
118
119                         if (status != 0) {
120                                 OciErrorInfo info = ErrorHandle.HandleError ();
121                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
122                         }
123
124                         return output;
125                 }
126
127                 public int GetChunkSize ()
128                 {
129                         int status = 0;
130                         uint output;
131                         status = OciCalls.OCILobGetChunkSize (Service,
132                                                         ErrorHandle,
133                                                         this,
134                                                         out output);
135
136                         if (status != 0) {
137                                 OciErrorInfo info = ErrorHandle.HandleError ();
138                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
139                         }
140
141                         return (int) output;
142                 }
143
144                 public long GetLength (bool binary)
145                 {
146                         int status = 0;
147                         uint output;
148                         status = OciCalls.OCILobGetLength (Service, 
149                                                 ErrorHandle,
150                                                 this,
151                                                 out output);
152                         if (status != 0) {
153                                 OciErrorInfo info = ErrorHandle.HandleError ();
154                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
155                         }
156
157                         if (!binary)
158                                 output *= 2;
159
160                         return (long) output;
161                 }
162
163                 public int Read (byte[] buffer, uint offset, uint count, bool binary)
164                 {
165                         int status = 0;
166                         uint amount = count;
167
168                         // Character types are UTF-16, so amount of characters is 1/2
169                         // the amount of bytes
170                         if (!binary) 
171                                 amount /= 2;
172
173                         status = OciCalls.OCILobRead (Service,
174                                                 ErrorHandle,
175                                                 this,
176                                                 ref amount,
177                                                 offset,
178                                                 buffer,
179                                                 count,
180                                                 IntPtr.Zero,
181                                                 IntPtr.Zero,
182                                                 1000,  // OCI_UCS2ID
183                                                 0);    // Ignored if csid is specified as above
184
185                         if (status != 0) {
186                                 OciErrorInfo info = ErrorHandle.HandleError ();
187                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
188                         }
189
190                         return (int) amount;
191                 }
192
193                 public void Trim (uint newlen)
194                 {
195                         int status = 0;
196                         status = OciCalls.OCILobTrim (Service,
197                                                 ErrorHandle,
198                                                 this,
199                                                 newlen);
200
201                         if (status != 0) {
202                                 OciErrorInfo info = ErrorHandle.HandleError ();
203                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
204                         }
205
206                 }
207
208                 public int Write (byte[] buffer, uint offset, uint count, OracleType type)
209                 {
210                         int status = 0;
211                         uint amount = count;
212
213                         if (type == OracleType.Clob)
214                                 amount /= 2;
215
216                         status = OciCalls.OCILobWrite (Service,
217                                                 ErrorHandle,
218                                                 this,
219                                                 ref amount,
220                                                 offset,
221                                                 buffer,
222                                                 count,
223                                                 0,    // OCI_ONE_PIECE
224                                                 IntPtr.Zero,
225                                                 IntPtr.Zero,
226                                                 1000, // OCI_UCS2ID
227                                                 0);
228
229                         if (status != 0) {
230                                 OciErrorInfo info = ErrorHandle.HandleError ();
231                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
232                         }
233
234                         return (int) amount;
235                 }
236
237                 #endregion // Methods
238         }
239 }