[ruby-oci8-commit] [458] trunk/ruby-oci8: change the super class of OCINoData from OCIException to OCIError and fix testcases for OCIError and its subclasses .

nobody at rubyforge.org nobody at rubyforge.org
Sat Oct 22 07:21:46 EDT 2011


Revision: 458
Author:   kubo
Date:     2011-10-22 07:21:44 -0400 (Sat, 22 Oct 2011)

Log Message:
-----------
change the super class of OCINoData from OCIException to OCIError and fix testcases for OCIError and its subclasses.

Modified Paths:
--------------
    trunk/ruby-oci8/ChangeLog
    trunk/ruby-oci8/ext/oci8/error.c
    trunk/ruby-oci8/test/test_break.rb
    trunk/ruby-oci8/test/test_error.rb

Modified: trunk/ruby-oci8/ChangeLog
===================================================================
--- trunk/ruby-oci8/ChangeLog	2011-10-22 05:11:51 UTC (rev 457)
+++ trunk/ruby-oci8/ChangeLog	2011-10-22 11:21:44 UTC (rev 458)
@@ -1,4 +1,9 @@
 2011-10-22  KUBO Takehiro  <kubo at jiubao.org>
+	* ext/oci8/error.c, test/test_break.rb, test/test_error.rb: change the super
+	    class of OCINoData from OCIException to OCIError and fix testcases for
+	    OCIError and its subclasses.
+
+2011-10-22  KUBO Takehiro  <kubo at jiubao.org>
 	* ext/oci8/lob.c, ext/oci8/oci8.c, ext/oci8/ocinumber.c:
 	    delete code for Oracle 8 and Oracle 8i.
 

Modified: trunk/ruby-oci8/ext/oci8/error.c
===================================================================
--- trunk/ruby-oci8/ext/oci8/error.c	2011-10-22 05:11:51 UTC (rev 457)
+++ trunk/ruby-oci8/ext/oci8/error.c	2011-10-22 11:21:44 UTC (rev 458)
@@ -84,35 +84,23 @@
     VALUE parse_error_offset = Qnil;
     VALUE sql = Qnil;
     int rv;
+    int numarg = 1;
 
     switch (status) {
     case OCI_ERROR:
+        exc = eOCIError;
+        msg = get_error_msg(errhp, type, "Error", &errcode);
+        numarg = 4;
+        break;
     case OCI_SUCCESS_WITH_INFO:
-        /* get error string */
+        exc = eOCISuccessWithInfo;
         msg = get_error_msg(errhp, type, "Error", &errcode);
-        if (status == OCI_ERROR) {
-            exc = eOCIError;
-        } else {
-            exc = eOCISuccessWithInfo;
-        }
-        if (stmthp != NULL) {
-            ub2 offset;
-            text *text;
-            ub4 size;
-
-            rv = OCIAttrGet(stmthp, OCI_HTYPE_STMT, &offset, 0, OCI_ATTR_PARSE_ERROR_OFFSET, errhp);
-            if (rv == OCI_SUCCESS) {
-                parse_error_offset = INT2FIX(offset);
-            }
-            rv = OCIAttrGet(stmthp, OCI_HTYPE_STMT, &text, &size, OCI_ATTR_STATEMENT, errhp);
-            if (rv == OCI_SUCCESS) {
-                sql = rb_external_str_new_with_enc(TO_CHARPTR(text), size, oci8_encoding);
-            }
-        }
+        numarg = 4;
         break;
     case OCI_NO_DATA:
         exc = eOCINoData;
         msg = get_error_msg(errhp, type, "No Data", &errcode);
+        numarg = 4;
         break;
     case OCI_INVALID_HANDLE:
         exc = eOCIInvalidHandle;
@@ -135,7 +123,21 @@
         exc = eOCIException;
         msg = rb_usascii_str_new_cstr(errmsg);
     }
-    exc = rb_funcall(exc, oci8_id_new, 4, msg, INT2FIX(errcode), sql, parse_error_offset);
+    if (stmthp != NULL) {
+        ub2 offset;
+        text *text;
+        ub4 size;
+
+        rv = OCIAttrGet(stmthp, OCI_HTYPE_STMT, &offset, 0, OCI_ATTR_PARSE_ERROR_OFFSET, errhp);
+        if (rv == OCI_SUCCESS) {
+            parse_error_offset = INT2FIX(offset);
+        }
+        rv = OCIAttrGet(stmthp, OCI_HTYPE_STMT, &text, &size, OCI_ATTR_STATEMENT, errhp);
+        if (rv == OCI_SUCCESS) {
+            sql = rb_external_str_new_with_enc(TO_CHARPTR(text), size, oci8_encoding);
+        }
+    }
+    exc = rb_funcall(exc, oci8_id_new, numarg, msg, INT2FIX(errcode), sql, parse_error_offset);
     return set_backtrace(exc, file, line);
 }
 
@@ -162,7 +164,7 @@
  * call-seq:
  *    OCI8.new(message, code = nil, sql = nil, parse_error_offset = nil)
  *
- * Creates a new OCIException object.
+ * Creates a new OCIError object.
  */
 static VALUE oci8_error_initialize(int argc, VALUE *argv, VALUE self)
 {
@@ -200,19 +202,19 @@
     eOCIException = rb_define_class("OCIException", rb_eStandardError);
     eOCIBreak = rb_define_class("OCIBreak", eOCIException);
 
-    eOCINoData = rb_define_class("OCINoData", eOCIException);
     eOCIError = rb_define_class("OCIError", eOCIException);
+    eOCINoData = rb_define_class("OCINoData", eOCIError);
     eOCIInvalidHandle = rb_define_class("OCIInvalidHandle", eOCIException);
     eOCINeedData = rb_define_class("OCINeedData", eOCIException);
     eOCIStillExecuting = rb_define_class("OCIStillExecuting", eOCIException);
     eOCIContinue = rb_define_class("OCIContinue", eOCIException);
     eOCISuccessWithInfo = rb_define_class("OCISuccessWithInfo", eOCIError);
 
-    rb_define_method(eOCIException, "initialize", oci8_error_initialize, -1);
-    rb_define_attr(eOCIException, "code", 1, 0);
-    rb_define_attr(eOCIException, "sql", 1, 0);
-    rb_define_attr(eOCIException, "parse_error_offset", 1, 0);
-    rb_define_alias(eOCIException, "parseErrorOffset", "parse_error_offset");
+    rb_define_method(eOCIError, "initialize", oci8_error_initialize, -1);
+    rb_define_attr(eOCIError, "code", 1, 0);
+    rb_define_attr(eOCIError, "sql", 1, 0);
+    rb_define_attr(eOCIError, "parse_error_offset", 1, 0);
+    rb_define_alias(eOCIError, "parseErrorOffset", "parse_error_offset");
 }
 
 void oci8_do_raise(OCIError *errhp, sword status, OCIStmt *stmthp, const char *file, int line)
@@ -273,18 +275,18 @@
  *
  * The superclass for all exceptions raised by ruby-oci8.
  *
- * The following exceptions are defined as subclasses of OCIError.
+ * The following exceptions are defined as subclasses of OCIException
  * These exceptions are raised when Oracle Call Interface functions
  * return with an error status.
  *
  * - OCIBreak
- * - OCINoData
+ * - OCIContinue
  * - OCIError
+ *   - OCISuccessWithInfo
+ *   - OCINoData (It had been a subclass of OCIException, not OCIError, until ruby-oci8 2.0)
  * - OCIInvalidHandle
  * - OCINeedData
  * - OCIStillExecuting
- * - OCIContinue
- * - OCISuccessWithInfo
  */
 
 /*
@@ -292,14 +294,16 @@
  *
  * Subclass of OCIException
  *
- *
+ * Raised when a SQL execution is cancelled by OCI8#break.
  */
 
 /*
  * Document-class: OCINoData
  *
- * Subclass of OCIException
+ * Subclass of OCIError from ruby-oci8 2.1.
+ * It had been a subclass of OCIException until ruby-oci8 2.0.
  *
+ * Raised when PL/SQL NO_DATA_FOUND exception is got.
  */
 
 /*
@@ -307,6 +311,13 @@
  *
  * Subclass of OCIException
  *
+ * The following exceptions are defined as subclasses of OCIError.
+ *
+ * - OCISuccessWithInfo
+ * - OCINoData (It had been a subclass of OCIException, not OCIError, until ruby-oci8 2.0)
+ *
+ * Raised when underlying Oracle Call Interface failed with an Oracle error code
+ * such as ORA-00001.
  */
 
 /*
@@ -314,6 +325,8 @@
  *
  * Subclass of OCIException
  *
+ * Raised when an invalid handle is passed to underlying Oracle Call Interface.
+ * Report to the ruby-oci8 author if it is raised.
  */
 
 /*
@@ -321,13 +334,15 @@
  *
  * Subclass of OCIException
  *
+ * Report to the ruby-oci8 author if it is raised.
  */
 
 /*
  * Document-class: OCIStillExecuting
  *
- * Subclass of OCIException
+ * Subclass of OCIError
  *
+ * Report to the ruby-oci8 author if it is raised.
  */
 
 /*
@@ -335,11 +350,12 @@
  *
  * Subclass of OCIException
  *
+ * Report to the ruby-oci8 author if it is raised.
  */
 
 /*
  * Document-class: OCISuccessWithInfo
  *
- * Subclass of OCIException
+ * Subclass of OCIError
  *
  */

Modified: trunk/ruby-oci8/test/test_break.rb
===================================================================
--- trunk/ruby-oci8/test/test_break.rb	2011-10-22 05:11:51 UTC (rev 457)
+++ trunk/ruby-oci8/test/test_break.rb	2011-10-22 11:21:44 UTC (rev 458)
@@ -34,6 +34,7 @@
         assert_equal(expect[PLSQL_DONE], (Time.now - $start_time).round, 'PLSQL_DONE')
       rescue OCIBreak
         assert_equal(expect[OCIBREAK], (Time.now - $start_time).round, 'OCIBREAK')
+        assert_equal('Canceled by user request.', $!.to_s)
       end
     end
 

Modified: trunk/ruby-oci8/test/test_error.rb
===================================================================
--- trunk/ruby-oci8/test/test_error.rb	2011-10-22 05:11:51 UTC (rev 457)
+++ trunk/ruby-oci8/test/test_error.rb	2011-10-22 11:21:44 UTC (rev 458)
@@ -11,18 +11,78 @@
     @conn.logoff
   end
 
-  attr_reader :code, :sql, :parse_error_offset
-
-  def test_error
+  def test_sql_parse_error
+    sql = 'select * from'
     begin
-      @conn.exec('select * from') # raises "ORA-00903: invalid table name"
+      @conn.exec(sql) # raises "ORA-00903: invalid table name"
     rescue OCIException
       assert_instance_of(OCIError, $!)
       assert_match(/^ORA-00903: /, $!.to_s)
       assert_equal(903, $!.code)
       assert_equal(13, $!.parse_error_offset)
       assert_equal(13, $!.parseErrorOffset) # barkward compatibility
-      assert_equal('select * from', $!.sql)
+      assert_equal(sql, $!.sql)
     end
   end
+
+  def test_plsql_parse_error
+    sql = <<EOS
+BEGIN
+  SELECT dummy INTO l_dummy FROM dual WHERE 1=2;
+END;
+EOS
+    begin
+      @conn.exec(sql) # raises "ORA-06550: line 2, column 21"
+    rescue OCIException
+      assert_instance_of(OCIError, $!)
+      assert_match(/^ORA-06550: /, $!.to_s)
+      assert_equal(6550, $!.code)
+      assert_equal(26, $!.parse_error_offset)
+      assert_equal(26, $!.parseErrorOffset) # barkward compatibility
+      assert_equal(sql, $!.sql)
+    end
+  end
+
+  def test_plsql_error_in_execution
+    sql = <<EOS
+DECLARE
+  l_dummy VARCHAR2(50);
+BEGIN
+  SELECT * INTO l_dummy
+    FROM (SELECT DUMMY FROM DUAL
+          UNION ALL
+          SELECT DUMMY FROM DUAL);
+END;
+EOS
+    begin
+      @conn.exec(sql) # raises "ORA-01422: exact fetch returns more than requested number of rows"
+    rescue OCIException
+      assert_instance_of(OCIError, $!)
+      assert_match(/^ORA-01422: /, $!.to_s)
+      assert_equal(1422, $!.code)
+      assert_equal(0, $!.parse_error_offset)
+      assert_equal(0, $!.parseErrorOffset) # barkward compatibility
+      assert_equal(sql, $!.sql)
+    end
+  end
+
+  def test_nodata
+    sql = <<EOS
+DECLARE
+  l_dummy VARCHAR2(50);
+BEGIN
+  SELECT dummy INTO l_dummy FROM dual WHERE 1=2;
+END;
+EOS
+    begin
+      @conn.exec(sql) # raises "ORA-01403: no data found"
+    rescue OCIException
+      assert_instance_of(OCINoData, $!)
+      assert_match(/^ORA-01403: /, $!.to_s)
+      assert_equal(1403, $!.code)
+      assert_equal(0, $!.parse_error_offset)
+      assert_equal(0, $!.parseErrorOffset) # barkward compatibility
+      assert_equal(sql, $!.sql)
+    end
+  end
 end




More information about the ruby-oci8-commit mailing list