gpt4 book ai didi

C++ 查询 oracle spatial (sdo_geomtery)

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:31 25 4
gpt4 key购买 nike

我目前正在编写一个简单的测试应用程序,仅使用 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++ 查询 oracle spatial (sdo_geomtery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23686004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com