Getting OCIError: ORA-00932: inconsistent datatypes From Rails 3

怎甘沉沦 提交于 2019-12-04 17:07:09

Finally, I managed to fix the problem.
Enhanced adapter uses bind_param method to perform binding variables to their appropriate oracle types. Here was the problem and error.
I had to override some codes in oracle_enhanced_adpter.rb.
I add a method to this file to manually build a SDO_GEOMETRY object.
Then I bind this returned object to OCI8::Object::Mdsys::SdoGeometry type.

My method contains these codes (Now, only for creating SDO_GEOMETRY Points):

#I needed a connection to my oracle database. Connection placeholder is conn.
def create_sdo_geometry_object(conn, gtype, srid, point, x, y)
    local_cursor = conn.parse("BEGIN :geom := SDO_GEOMETRY(:sdo_gtype, :sdo_srid, :sdo_point, :sdo_elem_info_array, :sdo_ordinate_array); END;")
    local_cursor.bind_param(:sdo_gtype, OraNumber)
    local_cursor.bind_param(:sdo_srid, OraNumber) 
    local_cursor.bind_param(:sdo_point, OCI8::Object::Mdsys::SdoPointType) 
    local_cursor.bind_param(:sdo_elem_info_array, OCI8::Object::Mdsys::SdoElemInfoArray)
    local_cursor.bind_param(:sdo_ordinate_array, OCI8::Object::Mdsys::SdoOrdinateArray)

    local_cursor.bind_param(:geom, OCI8::Object::Mdsys::SdoGeometry)

    sdo_gtype = OraNumber.new(gtype)
    sdo_srid = OraNumber.new(srid)
    sdo_point = nil #Temporarily I set it to nil


    #sdo_elem_info_array must be [1,1,1] for creating points.
    sdo_elem_info_array = OCI8::Object::Mdsys::SdoElemInfoArray.new(conn,OraNumber.new(1),OraNumber.new(1),OraNumber.new(1))
    #I want to create a point with x and y coordinates
    sdo_ordinate_array = OCI8::Object::Mdsys::SdoOrdinateArray.new(conn,OraNumber.new(x),OraNumber.new(y))

    local_cursor[:sdo_gtype] = sdo_gtype
    local_cursor[:sdo_srid] = sdo_srid
    local_cursor[:sdo_point] = sdo_point
    local_cursor[:sdo_elem_info_array] = sdo_elem_info_array
    local_cursor[:sdo_ordinate_array] = sdo_ordinate_array
    local_cursor.exec

    local_cursor[:geom]
end

Then when I wanted to bind this parameter I used the following code:

cursor.bind_param(i + 1, create_sdo_geometry_object("PARAMS HERE"), OCI8::Object::Mdsys::SdoGeometry)

It worked like a charm!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!