Pergunta

I am currently writing a simple testing application to would just query an sdo_geometry from a database using the oracle oci in c++. I am having some issues finding a good example of how exactly to accomplish since this the sdo_geometery is a complex type. There are some notes about using a collection SQL type but nothing specific.

My code is based off the adpoci project which can be found here: https://github.com/ReneNyffenegger/development_misc/tree/master/c%2B%2B/oci/adpoci

However I need to expand upon that example to query a sdo_geometry.

   select mdsys.sdo_geometry (2003,
        NULL,
        NULL,
        mdsys.sdo_elem_info_array (1, 1003, 1),
        mdsys.sdo_ordinate_array (1,1,2,1,2,2,1,2,1,1))
    from dual;

any help would be greatly appreciated.

Foi útil?

Solução

Yes, this is complex. I've done it a long time ago, but unfortunately I don't have it in a way I can easily share (it's all wrapped into a proprietary C++ layer on top of OCI). For any user-defined Oracle types (UDTs), you normally use the Object Type Translator (OTT) to generate the C structs (for values and indicators) that you bind and define. I've included below the ones I used at the time (11gR1), which I expect to be still valid, but ideally you'd read up on Object-Relational aspects of OCI and generate them yourself.

struct sdo_point_type {
    OCINumber x, y, z;
};
struct sdo_point_type_ind {
    OCIInd _atomic;
    OCIInd x, y, z;
};
struct sdo_geometry {
    OCINumber sdo_gtype;
    OCINumber sdo_srid;
    sdo_point_type sdo_point;
    OCIArray* sdo_elem_info;
    OCIArray* sdo_ordinates;
};
struct sdo_geometry_ind {
    OCIInd _atomic;
    OCIInd sdo_gtype;
    OCIInd sdo_srid;
    sdo_point_type_ind sdo_point;
    OCIInd sdo_elem_info;
    OCIInd sdo_ordinates;
};

To bind/define, you need the TDO for the UDTs. Here's code I used for that:

/*!
 * \brief Looks up raw OCI type descriptor for a given SQL type name.
 *
 * \param type_name the possibly qualified type name. Note that
 *        types are implicitly capitalized by Oracle Server when
 *        defined, and thus type_name should usually be uppercase only.
 *        \a type_name can be qualified, as in "MDSYS.SDO_GEOMETRY", or
 *        unqualified as in "MY_TYPE", in which case it is looked up in
 *        the current user's schema.
 * \return the object type, either obtained from the server when
 *        the type is looked up for the first time, or from a cache
 *        this connection keeps. Note that the return type is valid
 *        only for this connection, and only as long as this connection
 *        is valid itself.
 * \throw std::runtime_error if the type cannot be found.
 */
OCIType* Connection::get_object_type(const char* type_name) {
    typedef std::map<std::string, OCIType*> TdoMap;

    TdoMap::iterator tdo_iter = tdo_map_.find(type_name);
    if (tdo_iter != tdo_map_.end()) {
        // return type already in the cache
        return tdo_iter->second;
    }

    // lookup the type for the first time
    OCIType* tdo = 0;
    OraTextString tdo_name(type_name);
    const sword rc = OCITypeByName(
        env_->envhp(), errhp(), svchp(),
        0, 0, // schema name (default schema when 0)
        tdo_name.text, tdo_name.length,
        0, 0, // version name (ignored)
        OCI_DURATION_SESSION,
        OCI_TYPEGET_ALL,
        &tdo
    );
    checkerr(rc, errhp());

    // add it to the cache before returning it
    tdo_map_.insert(TdoMap::value_type(type_name, tdo));

    return tdo;
}

Then you bind/define using the SQLT_NTY type, and the OCI calls for objects. For example:

OCIType* tdo = conn.get_object_type(tdo_name);

// WARNING: Addresses passed to OCIBindObject *MUST* still be valid
// *after* OCIStmtExecute, so cannot be addresses to local(stack) vars.
checkerr(
    OCIBindObject(
        bind_hndl, errhp(), tdo,
        (void**)&obj_ref.p_value_,     0, // value/indicator sizes
        (void**)&obj_ref.p_indicator_, 0  // not used apparently...
    ),
    errhp()
);

Note that you need both a normal bind/define, and then a bind/define-object. Again, see the doc about Object-Relational OCI.

Then you need to dig into the OCIArray* instances. The SDO (Spatial) UDTs are actually not simple either... Here are definitions from my geometry wrapper:

/*!
 * \brief All possible geometries dimensions.
 */
enum Dimension {
    TWO_D = 2, //! a 2D geomertry
    THREE_D = 3, //! a 3D geomertry
    FOUR_D = 4 //! a 4D geomertry
};

/*!
 * \brief All possible types of geometries.
 */
enum Type {
    CUSTOM_GEOMETRY = 0,
    POINT = 1,
    LINE = 2,
    POLY = 3,
    COLLECTION = 4, // heterogeous group of all other geometry types
    MULTI_POINT = COLLECTION + POINT,
    MULTI_LINE  = COLLECTION + LINE,
    MULTI_POLY  = COLLECTION + POLY,
    SOLID = 8,
    MULTI_SOLID = 9
};

/*!
 * \brief All possible type of geometry elements.
 */
enum ElementType {
    CUSTOM_ELEMENT = 0,
    POINT_ELEMENT = 1,
    LINE_ELEMENT = 2,
    EXTERIOR_POLY = 1003,
    INTERIOR_POLY = 2003,
    COMPOUND_LINE = 4,
    EXTERIOR_COMPOUND_POLY = 1005,
    INTERIOR_COMPOUND_POLY = 2005,
    SURFACE = 1006,
    SURFACE_HOLE = 2006,
    SOLID_ELEMENT = 1007
};

/*!
 * Meaning of the interpretation field for ElementType LINE.
 */
enum LineType {
    SEG_LINE = 1,
    ARC_LINE = 2
};

/*!
 * Meaning of the interpretation field for ElementType
 * EXTERIOR_POLY or INTERIOR_POLY.
 */
enum PolygonType {
    SEG_POLY = 1,
    ARC_POLY = 2,
    RECTANGLE = 3,
    CIRCLE = 4
};

/*!
 * \brief A triplet describing a element (piece) of this geometry.
 *
 * A geometry is composed of elements which are described by one or
 * more info elements, each refering to zero or more ordinates.
 */
struct PDGM_OCI_API ElemInfo {
    /*!
     * \brief the element's type.
     *
     * Valid values are: 0, 1, 2, 1003, 2003, 4, 1005, 2005, 1006, 2006, 1007.
     */
    ub2 type;

    /*!
     * \brief The offset in the ordinate array
     *        at which this element's coordinates starts.
     *
     * The end offset is determined either via the element-specific
     * interpretation field, or the offset for the next ElemInfo.
     *
     * The maximum size of the ordinate array is 1,048,576 (1024^2).
     *
     * Note that persistent offsets are 1-based in Oracle, but this
     * offset is transparently adjusted to be 0-based.
     */
    ub4 offset;

    /*!
     * \brief The number of ordinates corresponding to this element.
     *
     * For example, a 2D rectangle has only 4 ordinate, for the x,y
     * of its lower-left and upper-right corners.
     *
     * Note that some elements have no ordinates, for example a compound
     * line string has an initial element info which introduces and precedes
     * the info elements each each line string, but itself has no ordinates
     * per se, only its composed line-strings refer to ordinates.
     */
    ub4 ordinate_count;

    /*!
     * \brief element-specific \em detail about this element.
     *
     * I'm assuming this is an integer, but could be any number in fact...
     *
     * For an ElemInfo of type 1 (point or vector or multi-point),
     * interpretation can be 0 (vector, and thus ordinates must contain
     * two points, the second normalized), 1 (single point), or N > 1
     * (multi-point) where N is the number of points.
     */
    ub4 interpretation;

    ElementType get_type() const {
        return ElementType(type);
    }
};

As you can see, it's definitely non-obvious. Oracle Spatial in C++ OCI too me a long time to get right, and a lots of documentation reading and experimentation.

Hopefully the above will help you a bit. Good luck, -DD

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top