PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/source/Main/Driver/OracleTestPlugin2.cs

#
C# | 236 lines | 195 code | 31 blank | 10 comment | 26 complexity | 871699b262a515b6af1bb2404b139c7c MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, AGPL-1.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.Common;
  6. using Devart.Data.Oracle;
  7. using System.Data;
  8. using DbSharp.Utils.Db;
  9. using System.Globalization;
  10. using DbSharp.Framework.DbObject;
  11. using DbSharp.Framework.Common;
  12. using DbSharp.Framework.Driver.FieldPersistence;
  13. namespace DbSharp.Framework.Driver
  14. {
  15. class OracleTestPlugin2 : ITestDataSource
  16. {
  17. #region Private method
  18. /// <summary>
  19. /// remove all semicolon symbols tailed the sql statement
  20. /// </summary>
  21. /// <param name="sql"></param>
  22. /// <returns></returns>
  23. private string RemoveTailedSemiColons(string sql)
  24. {
  25. return SqlTextHelper.RightTrimCharacter(sql, ';');
  26. }
  27. private int GetArgumentType(string argumentTypeText)
  28. {
  29. OracleDbType parameterType;
  30. string upperTypeStr = argumentTypeText.Trim().ToUpper();
  31. switch (upperTypeStr)
  32. {
  33. case "ORA_CHAR":
  34. parameterType = OracleDbType.Char;
  35. break;
  36. case "ORA_VARCHAR":
  37. case "ORA_VARCHAR2":
  38. parameterType = OracleDbType.VarChar;
  39. break;
  40. case "ORA_NCHAR":
  41. parameterType = OracleDbType.NChar;
  42. break;
  43. case "ORA_NVARCHAR":
  44. case "ORA_NVARCHAR2":
  45. parameterType = OracleDbType.NVarChar;
  46. break;
  47. case "ORA_NUMBER":
  48. parameterType = OracleDbType.Number;
  49. break;
  50. case "ORA_DATETIME":
  51. parameterType = OracleDbType.Date;
  52. break;
  53. case "ORA_TIMESTAMP":
  54. parameterType = OracleDbType.TimeStamp;
  55. break;
  56. case "ORA_TIMESTAMPLOCAL":
  57. parameterType = OracleDbType.TimeStampLTZ;
  58. break;
  59. case "ORA_TIMESTAMPWITHTZ":
  60. parameterType = OracleDbType.TimeStampTZ;
  61. break;
  62. case "ORA_INTERVALDAYTOSECOND":
  63. parameterType = OracleDbType.IntervalDS;
  64. break;
  65. case "ORA_INTERVALYEARTOMONTH":
  66. parameterType = OracleDbType.IntervalYM;
  67. break;
  68. case "ORA_INT":
  69. parameterType = OracleDbType.Integer;
  70. break;
  71. case "ORA_FLOAT":
  72. case "ORA_REAL":
  73. parameterType = OracleDbType.Float;
  74. break;
  75. case "ORA_DOUBLE":
  76. parameterType = OracleDbType.Double;
  77. break;
  78. case "ORA_ROWID":
  79. parameterType = OracleDbType.RowId;
  80. break;
  81. case "ORA_CURSOR":
  82. parameterType = OracleDbType.Cursor;
  83. break;
  84. default:
  85. parameterType = OracleDbType.VarChar;
  86. break;
  87. }
  88. return (int)parameterType;
  89. }
  90. #endregion
  91. #region ITestDataSource Members
  92. public DateTime GetDbServerTime(DbConnection conn)
  93. {
  94. string sql = "Select sysdate from dual ";
  95. DbCommand cmd = new OracleCommand();
  96. cmd.Connection = conn;
  97. cmd.CommandType = CommandType.Text;
  98. cmd.CommandText = sql;
  99. DateTime result = (DateTime)cmd.ExecuteScalar();
  100. return result;
  101. }
  102. public bool IsRecordsetTypeArgument(string argumentTypeText)
  103. {
  104. if (GetArgumentType(argumentTypeText) == (int)OracleDbType.Cursor)
  105. return true;
  106. else
  107. return false;
  108. }
  109. public bool IsRecordsetTypeParameter(DbParameter param)
  110. {
  111. if (param == null)
  112. return false;
  113. else if (((OracleParameter)param).OracleDbType == OracleDbType.Cursor)
  114. return true;
  115. else
  116. return false;
  117. }
  118. public DbConnection CreateConnection(string connString)
  119. {
  120. var cnn = new OracleConnection();
  121. cnn.ConnectionString = connString;
  122. return cnn;
  123. }
  124. public DbCommand PrepareCommand(IStatementDeclaration statement)
  125. {
  126. DbCommand cmd = new OracleCommand();
  127. cmd.CommandType = statement.CommandType;
  128. IList<StatementArgument> listArgumentObj = statement.ListArgument;
  129. foreach (var argumentObj in listArgumentObj)
  130. {
  131. if (argumentObj.Direction == ArgumentDirection.ClientSide)
  132. {
  133. //Clientside parameter will not added into ParameterList for ever
  134. continue;
  135. }
  136. else if (argumentObj.IsSqlStmtResultArg)
  137. {
  138. //It means this argument is virtual argument, not the real argument
  139. // So the argument will not be added into ParameterList
  140. continue;
  141. }
  142. else
  143. {
  144. //Oracle always use named arguments to hold resultsets, so we need add these return type arguments into parameterlist
  145. }
  146. OracleParameter parameter = new OracleParameter();
  147. parameter.Direction = ArgumentDirectionHelper.ConvertToParamDirection(argumentObj.Direction);
  148. parameter.OracleDbType = (OracleDbType)GetArgumentType(argumentObj.TypeStr);
  149. parameter.Size = argumentObj.Size;
  150. parameter.ParameterName = NormalizeArgumentName(argumentObj.Name);
  151. cmd.Parameters.Add(parameter);
  152. }
  153. return cmd;
  154. }
  155. public void UpdateCommandText(DbCommand cmd, string newCmdText)
  156. {
  157. cmd.CommandText = newCmdText.Trim();
  158. //make sure the sql statement does not end with semicolon
  159. if (cmd.CommandType == CommandType.Text)
  160. {
  161. cmd.CommandText = RemoveTailedSemiColons(cmd.CommandText);
  162. }
  163. }
  164. public void AssignParameterValue(DbParameter param, string value, string dateTimeFormat)
  165. {
  166. OracleParameter parameter = (OracleParameter)param;
  167. if ((parameter.OracleDbType == OracleDbType.Date) ||
  168. (parameter.OracleDbType == OracleDbType.TimeStamp) ||
  169. (parameter.OracleDbType == OracleDbType.TimeStampLTZ) ||
  170. (parameter.OracleDbType == OracleDbType.TimeStampTZ))
  171. {
  172. if (string.IsNullOrEmpty(dateTimeFormat))
  173. parameter.Value = DateTimeParser.ParseUtc(value);
  174. else
  175. {
  176. parameter.Value = DateTimeParser.ParseExactUtc(value, dateTimeFormat);
  177. }
  178. }
  179. else if (parameter.OracleDbType == OracleDbType.IntervalDS)
  180. parameter.Value = OracleIntervalDS.Parse(value);
  181. else if (parameter.OracleDbType == OracleDbType.IntervalYM)
  182. parameter.Value = OracleIntervalYM.Parse(value);
  183. else
  184. parameter.Value = value;
  185. }
  186. public bool TransactionSupported
  187. {
  188. get { return true; }
  189. }
  190. public bool HoldResultsetMustWithNamedArgInSP
  191. {
  192. get { return true; }
  193. }
  194. public string NormalizeArgumentName(string argumentName)
  195. {
  196. return SqlTextHelper.LeftTrimCharacter(argumentName, ':');
  197. }
  198. private IFieldPersistence m_FieldPersistenceService = new OracleFieldPersistence2();
  199. public IFieldPersistence FieldPersistenceService
  200. {
  201. get { return m_FieldPersistenceService; }
  202. }
  203. #endregion
  204. }
  205. }