OciDefineHandle.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. //
  2. // OciDefineHandle.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. // Authors:
  13. // Tim Coleman <[email protected]>
  14. // Daniel Morgan <[email protected]>
  15. //
  16. // Copyright (C) Tim Coleman, 2003
  17. // Copyright (C) Daniel Morgan, 2004
  18. //
  19. using System;
  20. using System.Data.OracleClient;
  21. using System.Runtime.InteropServices;
  22. using System.Text;
  23. namespace System.Data.OracleClient.Oci
  24. {
  25. internal sealed class OciDefineHandle : OciHandle, IDisposable
  26. {
  27. #region Fields
  28. bool disposed = false;
  29. //IntPtr handle;
  30. IntPtr value;
  31. short indicator;
  32. //OracleType type;
  33. OciDataType ociType;
  34. OciDataType definedType;
  35. int definedSize;
  36. short rlenp = 0;
  37. //short precision;
  38. short scale;
  39. Type fieldType;
  40. //string name;
  41. // Oracle defines the LONG VARCHAR have a size of 2 to the 31 power - 5
  42. // maybe this should settable via a config file for System.Data.OracleClient.dll
  43. // see DefineLong. Or if the a truncate sql error occurs, then retry?
  44. // or maybe allocate the memory yourself and then let oracle return error more data,
  45. // then try to get some more data
  46. internal static int LongVarCharMaxValue = (int) Int16.MaxValue - 5;
  47. OciErrorHandle errorHandle;
  48. OciLobLocator lobLocator;
  49. OciDateTimeDescriptor dateTimeDesc;
  50. OciIntervalDescriptor intervalDesc;
  51. #endregion // Fields
  52. #region Constructors
  53. internal OciDefineHandle (OciHandle parent, IntPtr newHandle)
  54. : base (OciHandleType.Define, parent, newHandle)
  55. {
  56. }
  57. internal void DefineByPosition (int position, OracleConnection connection)
  58. {
  59. OciParameterDescriptor parameter = ((OciStatementHandle) Parent).GetParameter (position);
  60. //name = parameter.GetName ();
  61. definedType = parameter.GetDataType ();
  62. definedSize = parameter.GetDataSize ();
  63. //precision = parameter.GetPrecision ();
  64. scale = parameter.GetScale ();
  65. Define (position, connection);
  66. parameter.Dispose ();
  67. }
  68. #endregion // Constructors
  69. #region Properties
  70. internal OciDataType DataType {
  71. get { return definedType; }
  72. }
  73. internal Type FieldType {
  74. get { return fieldType; }
  75. }
  76. internal int DefinedSize {
  77. get { return definedSize; }
  78. }
  79. internal OciErrorHandle ErrorHandle {
  80. get { return errorHandle; }
  81. set { errorHandle = value; }
  82. }
  83. internal bool IsNull {
  84. get { return (indicator == -1); }
  85. }
  86. internal short Scale {
  87. get { return scale; }
  88. }
  89. internal short Size {
  90. get { return rlenp; }
  91. }
  92. internal IntPtr Value {
  93. get { return value; }
  94. }
  95. #endregion
  96. #region Methods
  97. void Define (int position, OracleConnection connection)
  98. {
  99. switch (definedType) {
  100. case OciDataType.Date:
  101. DefineDate (position, connection);
  102. return;
  103. case OciDataType.TimeStamp:
  104. DefineTimeStamp (position, connection);
  105. return;
  106. case OciDataType.Clob:
  107. case OciDataType.Blob:
  108. DefineLob (position, definedType, connection);
  109. return;
  110. case OciDataType.Raw:
  111. DefineRaw( position, connection);
  112. return;
  113. case OciDataType.RowIdDescriptor:
  114. definedSize = 10;
  115. DefineChar (position, connection);
  116. return;
  117. case OciDataType.Integer:
  118. case OciDataType.Number:
  119. case OciDataType.Float:
  120. DefineNumber (position, connection);
  121. return;
  122. case OciDataType.Long:
  123. case OciDataType.LongVarChar:
  124. DefineLongVarChar (position, connection);
  125. return;
  126. case OciDataType.IntervalDayToSecond:
  127. case OciDataType.IntervalYearToMonth:
  128. DefineInterval (position, definedType, connection);
  129. return;
  130. default:
  131. DefineChar (position, connection); // HANDLE ALL OTHERS AS CHAR FOR NOW
  132. return;
  133. }
  134. }
  135. void DefineTimeStamp (int position, OracleConnection connection)
  136. {
  137. definedSize = -1;
  138. ociType = OciDataType.TimeStamp;
  139. fieldType = typeof(System.DateTime);
  140. dateTimeDesc = (OciDateTimeDescriptor) connection.Environment.Allocate (OciHandleType.TimeStamp);
  141. if (dateTimeDesc == null) {
  142. OciErrorInfo info = connection.ErrorHandle.HandleError ();
  143. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  144. }
  145. value = dateTimeDesc.Handle;
  146. dateTimeDesc.ErrorHandle = ErrorHandle;
  147. int status = 0;
  148. status = OciCalls.OCIDefineByPosPtr (Parent,
  149. out handle,
  150. ErrorHandle,
  151. position + 1,
  152. ref value,
  153. definedSize,
  154. ociType,
  155. ref indicator,
  156. ref rlenp,
  157. IntPtr.Zero,
  158. 0);
  159. definedSize = 11;
  160. if (status != 0) {
  161. OciErrorInfo info = connection.ErrorHandle.HandleError ();
  162. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  163. }
  164. }
  165. void DefineDate (int position, OracleConnection connection)
  166. {
  167. definedSize = 7;
  168. ociType = OciDataType.Date;
  169. fieldType = typeof(System.DateTime);
  170. value = OciCalls.AllocateClear (definedSize);
  171. int status = 0;
  172. status = OciCalls.OCIDefineByPos (Parent,
  173. out handle,
  174. ErrorHandle,
  175. position + 1,
  176. value,
  177. definedSize,
  178. ociType,
  179. ref indicator,
  180. ref rlenp,
  181. IntPtr.Zero,
  182. 0);
  183. if (status != 0) {
  184. OciErrorInfo info = ErrorHandle.HandleError ();
  185. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  186. }
  187. }
  188. void DefineLongVarChar (int position, OracleConnection connection)
  189. {
  190. fieldType = typeof (System.String);
  191. // LONG VARCHAR max length is 2 to the 31 power - 5
  192. // the first 4 bytes of a LONG VARCHAR value contains the length
  193. // Int32.MaxValue - 5 causes out of memory in mono on win32
  194. // because I do not have 2GB of memory available
  195. // so Int16.MaxValue - 5 is used instead.
  196. // LAMESPEC for Oracle OCI - you can not get the length of the LONG VARCHAR value
  197. // until after you get the value. This could be why Oracle deprecated LONG VARCHAR.
  198. // If you specify a definedSize less then the length of the column value,
  199. // then you will get an OCI_ERROR ORA-01406: fetched column value was truncated
  200. definedSize = LongVarCharMaxValue;
  201. value = OciCalls.AllocateClear (definedSize);
  202. ociType = OciDataType.LongVarChar;
  203. int status = 0;
  204. status = OciCalls.OCIDefineByPos (Parent,
  205. out handle,
  206. ErrorHandle,
  207. position + 1,
  208. value,
  209. definedSize,
  210. ociType,
  211. ref indicator,
  212. ref rlenp,
  213. IntPtr.Zero, 0);
  214. rlenp = (short) definedSize;
  215. if (status != 0) {
  216. OciErrorInfo info = ErrorHandle.HandleError ();
  217. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  218. }
  219. }
  220. void DefineChar (int position, OracleConnection connection)
  221. {
  222. fieldType = typeof (System.String);
  223. int maxByteCount = Encoding.UTF8.GetMaxByteCount (definedSize);
  224. value = OciCalls.AllocateClear (maxByteCount);
  225. ociType = OciDataType.Char;
  226. int status = 0;
  227. status = OciCalls.OCIDefineByPos (Parent,
  228. out handle,
  229. ErrorHandle,
  230. position + 1,
  231. value,
  232. maxByteCount,
  233. ociType,
  234. ref indicator,
  235. ref rlenp,
  236. IntPtr.Zero,
  237. 0);
  238. OciErrorHandle.ThrowExceptionIfError (ErrorHandle, status);
  239. }
  240. void DefineNumber (int position, OracleConnection connection)
  241. {
  242. fieldType = typeof (System.Decimal);
  243. value = OciCalls.AllocateClear (definedSize);
  244. ociType = OciDataType.Char;
  245. int status = 0;
  246. status = OciCalls.OCIDefineByPos (Parent,
  247. out handle,
  248. ErrorHandle,
  249. position + 1,
  250. value,
  251. definedSize * 2,
  252. ociType,
  253. ref indicator,
  254. ref rlenp,
  255. IntPtr.Zero,
  256. 0);
  257. if (status != 0) {
  258. OciErrorInfo info = ErrorHandle.HandleError ();
  259. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  260. }
  261. }
  262. void DefineLob (int position, OciDataType type, OracleConnection connection)
  263. {
  264. ociType = type;
  265. if (ociType == OciDataType.Clob)
  266. fieldType = typeof(System.String);
  267. else if (ociType == OciDataType.Blob)
  268. fieldType = typeof(byte[]);
  269. int status = 0;
  270. definedSize = -1;
  271. lobLocator = (OciLobLocator) connection.Environment.Allocate (OciHandleType.LobLocator);
  272. if (lobLocator == null) {
  273. OciErrorInfo info = connection.ErrorHandle.HandleError ();
  274. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  275. }
  276. value = lobLocator.Handle;
  277. lobLocator.ErrorHandle = connection.ErrorHandle;
  278. lobLocator.Service = connection.ServiceContext;
  279. status = OciCalls.OCIDefineByPosPtr (Parent,
  280. out handle,
  281. ErrorHandle,
  282. position + 1,
  283. ref value,
  284. definedSize,
  285. ociType,
  286. ref indicator,
  287. ref rlenp,
  288. IntPtr.Zero,
  289. 0);
  290. definedSize = Int32.MaxValue;
  291. if (status != 0) {
  292. OciErrorInfo info = connection.ErrorHandle.HandleError ();
  293. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  294. }
  295. }
  296. void DefineRaw (int position, OracleConnection connection)
  297. {
  298. ociType = OciDataType.Raw;
  299. fieldType = typeof (byte[]);
  300. value = OciCalls.AllocateClear (definedSize);
  301. int status = 0;
  302. status = OciCalls.OCIDefineByPos (Parent,
  303. out handle,
  304. ErrorHandle,
  305. position + 1,
  306. value,
  307. definedSize * 2,
  308. ociType,
  309. ref indicator,
  310. ref rlenp,
  311. IntPtr.Zero, 0);
  312. if (status != 0) {
  313. OciErrorInfo info = ErrorHandle.HandleError ();
  314. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  315. }
  316. }
  317. void DefineInterval (int position, OciDataType type, OracleConnection connection)
  318. {
  319. ociType = type;
  320. fieldType = typeof(System.TimeSpan);
  321. switch (type) {
  322. case OciDataType.IntervalDayToSecond:
  323. definedSize = 11;
  324. intervalDesc = (OciIntervalDescriptor) connection.Environment.Allocate (OciHandleType.IntervalDayToSecond);
  325. break;
  326. case OciDataType.IntervalYearToMonth:
  327. intervalDesc = (OciIntervalDescriptor) connection.Environment.Allocate (OciHandleType.IntervalYearToMonth);
  328. definedSize = 5;
  329. break;
  330. }
  331. if (intervalDesc == null) {
  332. OciErrorInfo info = connection.ErrorHandle.HandleError ();
  333. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  334. }
  335. value = intervalDesc.Handle;
  336. intervalDesc.ErrorHandle = ErrorHandle;
  337. int status = 0;
  338. status = OciCalls.OCIDefineByPosPtr (Parent,
  339. out handle,
  340. ErrorHandle,
  341. position + 1,
  342. ref value,
  343. definedSize,
  344. ociType,
  345. ref indicator,
  346. ref rlenp,
  347. IntPtr.Zero,
  348. 0);
  349. if (status != 0) {
  350. OciErrorInfo info = connection.ErrorHandle.HandleError ();
  351. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  352. }
  353. }
  354. protected override void Dispose (bool disposing)
  355. {
  356. if (!disposed) {
  357. try {
  358. switch (definedType) {
  359. case OciDataType.Clob:
  360. case OciDataType.Blob:
  361. case OciDataType.TimeStamp:
  362. break;
  363. default:
  364. Marshal.FreeHGlobal (value);
  365. break;
  366. }
  367. disposed = true;
  368. } finally {
  369. base.Dispose (disposing);
  370. value = IntPtr.Zero;
  371. }
  372. }
  373. }
  374. internal OracleLob GetOracleLob ()
  375. {
  376. return new OracleLob (lobLocator, ociType);
  377. }
  378. internal object GetValue (IFormatProvider formatProvider, OracleConnection conn)
  379. {
  380. object tmp;
  381. byte [] buffer = null;
  382. switch (DataType) {
  383. case OciDataType.VarChar2:
  384. case OciDataType.String:
  385. case OciDataType.VarChar:
  386. case OciDataType.Char:
  387. case OciDataType.CharZ:
  388. case OciDataType.OciString:
  389. case OciDataType.RowIdDescriptor:
  390. buffer = new byte [Size];
  391. Marshal.Copy (Value, buffer, 0, Size);
  392. // Get length of returned string
  393. int rsize = 0;
  394. //IntPtr env = Parent.Parent; // Parent is statement, grandparent is environment
  395. IntPtr env = conn.Environment;
  396. int status = OciCalls.OCICharSetToUnicode (env, null, buffer, out rsize);
  397. OciErrorHandle.ThrowExceptionIfError (ErrorHandle, status);
  398. // Get string
  399. StringBuilder ret = new StringBuilder(rsize);
  400. status = OciCalls.OCICharSetToUnicode (env, ret, buffer, out rsize);
  401. OciErrorHandle.ThrowExceptionIfError (ErrorHandle, status);
  402. return ret.ToString ();
  403. case OciDataType.LongVarChar:
  404. case OciDataType.Long:
  405. buffer = new byte [LongVarCharMaxValue];
  406. Marshal.Copy (Value, buffer, 0, buffer.Length);
  407. int longSize = 0;
  408. if (BitConverter.IsLittleEndian)
  409. longSize = BitConverter.ToInt32 (new byte[]{buffer[0], buffer[1], buffer[2], buffer[3]}, 0);
  410. else
  411. longSize = BitConverter.ToInt32 (new byte[]{buffer[3], buffer[2], buffer[1], buffer[0]}, 0);
  412. ASCIIEncoding encoding = new ASCIIEncoding ();
  413. string e = encoding.GetString (buffer, 4, longSize);
  414. return e;
  415. case OciDataType.Integer:
  416. case OciDataType.Number:
  417. case OciDataType.Float:
  418. tmp = Marshal.PtrToStringAnsi (Value, Size);
  419. if (tmp != null)
  420. return Decimal.Parse (String.Copy ((string) tmp), formatProvider);
  421. break;
  422. case OciDataType.TimeStamp:
  423. return dateTimeDesc.GetDateTime (conn.Environment, dateTimeDesc.ErrorHandle);
  424. case OciDataType.Date:
  425. return UnpackDate ();
  426. case OciDataType.Raw:
  427. byte [] raw_buffer = new byte [Size];
  428. Marshal.Copy (Value, raw_buffer, 0, Size);
  429. return raw_buffer;
  430. case OciDataType.Blob:
  431. case OciDataType.Clob:
  432. return GetOracleLob ();
  433. case OciDataType.IntervalDayToSecond:
  434. long ticks;
  435. tmp = Marshal.PtrToStringAnsi (Value, Size);
  436. if (tmp != null) {
  437. //TimeSpan ts = new TimeSpan (
  438. Console.WriteLine ("IntervalDayToSecond value: {0}", (string)tmp);
  439. ticks = long.Parse (String.Copy ((string)tmp));
  440. return new OracleTimeSpan (ticks);
  441. }
  442. break;
  443. case OciDataType.IntervalYearToMonth:
  444. int months;
  445. tmp = Marshal.PtrToStringAnsi (Value, Size);
  446. if (tmp != null) {
  447. Console.WriteLine ("IntervalDayToSecond value: {0}", (string)tmp);
  448. months = int.Parse (String.Copy ((string)tmp));
  449. return new OracleMonthSpan (months);
  450. }
  451. break;
  452. default:
  453. throw new Exception("OciDataType not implemented: " + DataType.ToString ());
  454. }
  455. return DBNull.Value;
  456. }
  457. internal object GetOracleValue (IFormatProvider formatProvider, OracleConnection conn)
  458. {
  459. object ovalue = GetValue (formatProvider, conn);
  460. switch (DataType) {
  461. case OciDataType.Raw:
  462. return new OracleBinary ((byte[]) ovalue);
  463. case OciDataType.Date:
  464. return new OracleDateTime ((DateTime) ovalue);
  465. case OciDataType.Blob:
  466. case OciDataType.Clob:
  467. OracleLob lob = (OracleLob) ovalue;
  468. return lob;
  469. case OciDataType.Integer:
  470. case OciDataType.Number:
  471. case OciDataType.Float:
  472. return new OracleNumber ((decimal) ovalue);
  473. case OciDataType.VarChar2:
  474. case OciDataType.String:
  475. case OciDataType.VarChar:
  476. case OciDataType.Char:
  477. case OciDataType.CharZ:
  478. case OciDataType.OciString:
  479. case OciDataType.LongVarChar:
  480. case OciDataType.Long:
  481. case OciDataType.RowIdDescriptor:
  482. return new OracleString ((string) ovalue);
  483. case OciDataType.IntervalDayToSecond:
  484. return new OracleTimeSpan ((OracleTimeSpan) ovalue);
  485. case OciDataType.IntervalYearToMonth:
  486. return new OracleMonthSpan ((OracleMonthSpan) ovalue);
  487. default:
  488. // TODO: do other types
  489. throw new NotImplementedException ();
  490. }
  491. }
  492. [MonoTODO ("Be able to handle negative dates... i.e. BCE.")]
  493. internal DateTime UnpackDate ()
  494. {
  495. byte century = Marshal.ReadByte (value, 0);
  496. byte year = Marshal.ReadByte (value, 1);
  497. byte month = Marshal.ReadByte (value, 2);
  498. byte day = Marshal.ReadByte (value, 3);
  499. byte hour = Marshal.ReadByte (value, 4);
  500. byte minute = Marshal.ReadByte (value, 5);
  501. byte second = Marshal.ReadByte (value, 6);
  502. if (hour == 0)
  503. hour ++;
  504. if (minute == 0)
  505. minute ++;
  506. if (second == 0)
  507. second ++;
  508. return new DateTime ((century - 100) * 100 + (year - 100),
  509. month,
  510. day,
  511. hour - 1,
  512. minute - 1,
  513. second - 1);
  514. }
  515. #endregion // Methods
  516. }
  517. }