我目前正在编写一个简单的测试应用程序,以使用c++中的oracle oci从数据库查询sdo_geometry。我有一些问题正在寻找一个很好的例子说明如何精确完成,因为sdo_geometery是一个复杂的类型。关于使用集合SQL类型,有一些注意事项,但没有具体说明。

我的代码基于adpoci项目,可以在这里找到:https://github.com/ReneNyffenegger/development_misc/tree/master/c%2B%2B/oci/adpoci

但是,我需要扩展该示例以查询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;

任何帮助将不胜感激。

最佳答案

是的,这很复杂。我已经做很久了,但是不幸的是,我没有一种可以轻松共享的方式(它们全部封装在OCI之上的专有C++层中)。对于任何用户定义的Oracle类型(UDT),通常使用对象类型转换器(OTT)来生成要绑定(bind)和定义的C结构(用于值和指示符)。我在下面包括了我当时使用的那些(11gR1),我希望它仍然有效,但是理想情况下,您应该阅读OCI的对象关系方面并自己生成。

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;
};

要绑定(bind)/定义,您需要UDT的TDO。这是我用于此的代码:
/*!
 * \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;
}

然后,使用SQLT_NTY类型进行绑定(bind)/定义,并且OCI调用对象。例如:
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()
);

请注意,您既需要一个普通的绑定(bind)/定义对象,又需要一个绑定(bind)/定义对象对象。再次,请参阅有关对象关系OCI的文档。

然后,您需要深入研究OCIArray *实例。 SDO(空间)UDT实际上也不简单...这是我的几何包装程序的定义:
/*!
 * \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);
    }
};

如您所见,这绝对不是显而易见的。我对C++ OCI中的Oracle Spatial的理解也花了很长时间,并且阅读和实验了很多文档。

希望以上内容对您有所帮助。祝你好运-DD

关于c++ - C++查询Oracle空间(sdo_geomtery),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23686004/

10-13 04:24