diff --git a/examples/advanced.cpp b/examples/advanced.cpp index c17bae1..1f7b85d 100644 --- a/examples/advanced.cpp +++ b/examples/advanced.cpp @@ -21,6 +21,17 @@ std::string MgValueToString(const mg::ConstValue &value) { value_str = std::to_string(value.ValueBool()); } else if (value.type() == mg::Value::Type::Double) { value_str = std::to_string(value.ValueDouble()); + } else if (value.type() == mg::Value::Type::Point2d) { + auto point2d = value.ValuePoint2d(); + value_str += "Point2D({ srid:" + std::to_string(point2d.srid()) + + ", x:" + std::to_string(point2d.x()) + + ", y:" + std::to_string(point2d.y()) + " })"; + } else if (value.type() == mg::Value::Type::Point3d) { + auto point3d = value.ValuePoint3d(); + value_str += "Point3D({ srid:" + std::to_string(point3d.srid()) + + ", x:" + std::to_string(point3d.x()) + + ", y:" + std::to_string(point3d.y()) + + ", z:" + std::to_string(point3d.z()) + " })"; } else if (value.type() == mg::Value::Type::List) { value_str += "["; for (auto item : value.ValueList()) { @@ -63,7 +74,9 @@ int main(int argc, char *argv[]) { if (!client->Execute( "CREATE (:Person:Entrepreneur {id: 0, age: 40, name: 'John', " - "isStudent: false, score: 5.0});")) { + "isStudent: false, score: 5.0, " + "position2D: point({x: 1, y: 2, srid: 4326}), " + "position3D: point({x: 8, y: 9, z: 10, srid: 9757}) });")) { std::cerr << "Failed to add data." << std::endl; return 1; } diff --git a/include/mgclient.h b/include/mgclient.h index 0c20ce4..be540ad 100644 --- a/include/mgclient.h +++ b/include/mgclient.h @@ -1010,6 +1010,11 @@ MGCLIENT_EXPORT mg_duration *mg_duration_copy(const mg_duration *duration); /// Destroy the given duration. MGCLIENT_EXPORT void mg_duration_destroy(mg_duration *duration); +/// Creates mg_point_2d from srid, x_longitude and y_latitude. +/// \return a pointer to mg_point_2d or NULL if an error occured. +MGCLIENT_EXPORT mg_point_2d *mg_point_2d_make(uint16_t srid, double x_longitude, + double y_latitude); + /// Returns SRID of the 2D point. MGCLIENT_EXPORT int64_t mg_point_2d_srid(const mg_point_2d *point_2d); @@ -1027,6 +1032,12 @@ MGCLIENT_EXPORT mg_point_2d *mg_point_2d_copy(const mg_point_2d *point_2d); /// Destroys the given 2D point. MGCLIENT_EXPORT void mg_point_2d_destroy(mg_point_2d *point_2d); +/// Creates mg_point_3d from srid, x_longitude, y_latitude and z_height. +/// \return a pointer to mg_point_3d or NULL if an error occured. +MGCLIENT_EXPORT mg_point_3d *mg_point_3d_make(uint16_t srid, double x_longitude, + double y_latitude, + double z_height); + /// Returns SRID of the 3D point. MGCLIENT_EXPORT int64_t mg_point_3d_srid(const mg_point_3d *point_3d); diff --git a/src/mgsession-encoder.c b/src/mgsession-encoder.c index 9a68ed1..800c22d 100644 --- a/src/mgsession-encoder.c +++ b/src/mgsession-encoder.c @@ -168,6 +168,27 @@ int mg_session_write_duration(mg_session *session, const mg_duration *dur) { return 0; } +int mg_session_write_point_2d(mg_session *session, const mg_point_2d *point) { + MG_RETURN_IF_FAILED( + mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT3))); + MG_RETURN_IF_FAILED(mg_session_write_uint8(session, MG_SIGNATURE_POINT_2D)); + MG_RETURN_IF_FAILED(mg_session_write_integer(session, point->srid)); + MG_RETURN_IF_FAILED(mg_session_write_float(session, point->x)); + MG_RETURN_IF_FAILED(mg_session_write_float(session, point->y)); + return 0; +} + +int mg_session_write_point_3d(mg_session *session, const mg_point_3d *point) { + MG_RETURN_IF_FAILED( + mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT4))); + MG_RETURN_IF_FAILED(mg_session_write_uint8(session, MG_SIGNATURE_POINT_3D)); + MG_RETURN_IF_FAILED(mg_session_write_integer(session, point->srid)); + MG_RETURN_IF_FAILED(mg_session_write_float(session, point->x)); + MG_RETURN_IF_FAILED(mg_session_write_float(session, point->y)); + MG_RETURN_IF_FAILED(mg_session_write_float(session, point->z)); + return 0; +} + int mg_session_write_value(mg_session *session, const mg_value *value) { switch (value->type) { case MG_VALUE_TYPE_NULL: @@ -219,11 +240,9 @@ int mg_session_write_value(mg_session *session, const mg_value *value) { case MG_VALUE_TYPE_DURATION: return mg_session_write_duration(session, value->duration_v); case MG_VALUE_TYPE_POINT_2D: - mg_session_set_error(session, "tried to send value of type 'point_2d'"); - return MG_ERROR_INVALID_VALUE; + return mg_session_write_point_2d(session, value->point_2d_v); case MG_VALUE_TYPE_POINT_3D: - mg_session_set_error(session, "tried to send value of type 'point_3d'"); - return MG_ERROR_INVALID_VALUE; + return mg_session_write_point_3d(session, value->point_3d_v); case MG_VALUE_TYPE_UNKNOWN: mg_session_set_error(session, "tried to send value of unknown type"); return MG_ERROR_INVALID_VALUE; diff --git a/src/mgvalue.c b/src/mgvalue.c index 183bf93..be19915 100644 --- a/src/mgvalue.c +++ b/src/mgvalue.c @@ -1681,6 +1681,32 @@ mg_duration *mg_duration_make(int64_t months, int64_t days, int64_t seconds, return dur; } +mg_point_2d *mg_point_2d_make(uint16_t srid, double x_longitude, + double y_latitude) { + mg_point_2d *point = mg_point_2d_alloc(&mg_system_allocator); + if (!point) { + return NULL; + } + point->srid = srid; + point->x = x_longitude; + point->y = y_latitude; + return point; +} + +mg_point_3d *mg_point_3d_make(uint16_t srid, double x_longitude, + double y_latitude, double z_height) { + mg_point_3d *point = mg_point_3d_alloc(&mg_system_allocator); + if (!point) { + return NULL; + } + point->srid = srid; + point->x = x_longitude; + point->y = y_latitude; + point->z = z_height; + ; + return point; +} + int mg_string_equal(const mg_string *lhs, const mg_string *rhs) { if (lhs->size != rhs->size) { return 0;