Moved ProviderCollectionTest.cs from System assembly to System.Configuration.
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient.Oci / OciRowIdDescriptor.cs
1 //
2 // OciRowIdDescriptor.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 //     Daniel Morgan <monodanmorg@yahoo.com>
15 //
16 // Copyright (C) Tim Coleman, 2003
17 // Copyright (C) Daniel Morgan, 2008
18 //
19
20 using System;
21 using System.Data.OracleClient;
22 using System.Runtime.InteropServices;
23 using System.Text;
24
25 namespace System.Data.OracleClient.Oci {
26         internal sealed class OciRowIdDescriptor : OciDescriptorHandle, IDisposable
27         {
28                 #region Fields
29
30                 bool disposed = false;
31
32                 #endregion // Fields
33
34                 #region Constructors
35
36                 public OciRowIdDescriptor (OciHandle parent, IntPtr newHandle)
37                         : base (OciHandleType.RowId, parent, newHandle)
38                 {
39                 }
40
41                 #endregion // Constructors
42
43                 #region Methods
44
45                 protected override void Dispose (bool disposing)
46                 {
47                         if (!disposed) {
48                                 disposed = true;
49                                 base.Dispose (disposing);
50                         }
51                 }
52
53                 //FIXME: This method only exists in Oracle 9i client and above
54                 [DllImport ("oci")]
55                 static extern int OCIRowidToChar (IntPtr rowidDesc,
56                                                 IntPtr outbfp,
57                                                 ref ushort outbflp,
58                                                 IntPtr errhp);
59
60                 [MonoTODO ("Only will work with 9i and above. Get it to work for 8i as well.")]
61                 internal string GetRowIdToString (OciErrorHandle errorHandle)
62                 {
63                         string output = String.Empty;
64
65                         int len = 18; // Universal ROWID has a length of 18
66                         int maxByteCount = Encoding.UTF8.GetMaxByteCount (len);
67                         IntPtr outputPtr = OciCalls.AllocateClear (maxByteCount); 
68
69                         int status = 0;
70
71                         ushort u = (ushort) maxByteCount;
72
73                         status = OCIRowidToChar (Handle,
74                                                 outputPtr,
75                                                 ref u,
76                                                 errorHandle);
77
78                         if (status != 0) {
79                                 OciErrorInfo info = errorHandle.HandleError ();
80                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
81                         }
82
83                         if (outputPtr != IntPtr.Zero && maxByteCount > 0) {
84                                 object str = Marshal.PtrToStringAnsi (outputPtr, len);
85                                 if (str != null)
86                                         output = String.Copy ((string) str);
87                         }
88
89                         return output;
90                 }
91
92                 #endregion // Methods
93         }
94 }
95
96