Skip to content

Commit 316e0cc

Browse files
authored
Add make and encoder functions for Point2&3D (#65)
1 parent 60d05ee commit 316e0cc

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

examples/advanced.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ std::string MgValueToString(const mg::ConstValue &value) {
2121
value_str = std::to_string(value.ValueBool());
2222
} else if (value.type() == mg::Value::Type::Double) {
2323
value_str = std::to_string(value.ValueDouble());
24+
} else if (value.type() == mg::Value::Type::Point2d) {
25+
auto point2d = value.ValuePoint2d();
26+
value_str += "Point2D({ srid:" + std::to_string(point2d.srid()) +
27+
", x:" + std::to_string(point2d.x()) +
28+
", y:" + std::to_string(point2d.y()) + " })";
29+
} else if (value.type() == mg::Value::Type::Point3d) {
30+
auto point3d = value.ValuePoint3d();
31+
value_str += "Point3D({ srid:" + std::to_string(point3d.srid()) +
32+
", x:" + std::to_string(point3d.x()) +
33+
", y:" + std::to_string(point3d.y()) +
34+
", z:" + std::to_string(point3d.z()) + " })";
2435
} else if (value.type() == mg::Value::Type::List) {
2536
value_str += "[";
2637
for (auto item : value.ValueList()) {
@@ -63,7 +74,9 @@ int main(int argc, char *argv[]) {
6374

6475
if (!client->Execute(
6576
"CREATE (:Person:Entrepreneur {id: 0, age: 40, name: 'John', "
66-
"isStudent: false, score: 5.0});")) {
77+
"isStudent: false, score: 5.0, "
78+
"position2D: point({x: 1, y: 2, srid: 4326}), "
79+
"position3D: point({x: 8, y: 9, z: 10, srid: 9757}) });")) {
6780
std::cerr << "Failed to add data." << std::endl;
6881
return 1;
6982
}

include/mgclient.h

+11
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,11 @@ MGCLIENT_EXPORT mg_duration *mg_duration_copy(const mg_duration *duration);
10101010
/// Destroy the given duration.
10111011
MGCLIENT_EXPORT void mg_duration_destroy(mg_duration *duration);
10121012

1013+
/// Creates mg_point_2d from srid, x_longitude and y_latitude.
1014+
/// \return a pointer to mg_point_2d or NULL if an error occured.
1015+
MGCLIENT_EXPORT mg_point_2d *mg_point_2d_make(uint16_t srid, double x_longitude,
1016+
double y_latitude);
1017+
10131018
/// Returns SRID of the 2D point.
10141019
MGCLIENT_EXPORT int64_t mg_point_2d_srid(const mg_point_2d *point_2d);
10151020

@@ -1027,6 +1032,12 @@ MGCLIENT_EXPORT mg_point_2d *mg_point_2d_copy(const mg_point_2d *point_2d);
10271032
/// Destroys the given 2D point.
10281033
MGCLIENT_EXPORT void mg_point_2d_destroy(mg_point_2d *point_2d);
10291034

1035+
/// Creates mg_point_3d from srid, x_longitude, y_latitude and z_height.
1036+
/// \return a pointer to mg_point_3d or NULL if an error occured.
1037+
MGCLIENT_EXPORT mg_point_3d *mg_point_3d_make(uint16_t srid, double x_longitude,
1038+
double y_latitude,
1039+
double z_height);
1040+
10301041
/// Returns SRID of the 3D point.
10311042
MGCLIENT_EXPORT int64_t mg_point_3d_srid(const mg_point_3d *point_3d);
10321043

src/mgsession-encoder.c

+23-4
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,27 @@ int mg_session_write_duration(mg_session *session, const mg_duration *dur) {
168168
return 0;
169169
}
170170

171+
int mg_session_write_point_2d(mg_session *session, const mg_point_2d *point) {
172+
MG_RETURN_IF_FAILED(
173+
mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT3)));
174+
MG_RETURN_IF_FAILED(mg_session_write_uint8(session, MG_SIGNATURE_POINT_2D));
175+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, point->srid));
176+
MG_RETURN_IF_FAILED(mg_session_write_float(session, point->x));
177+
MG_RETURN_IF_FAILED(mg_session_write_float(session, point->y));
178+
return 0;
179+
}
180+
181+
int mg_session_write_point_3d(mg_session *session, const mg_point_3d *point) {
182+
MG_RETURN_IF_FAILED(
183+
mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT4)));
184+
MG_RETURN_IF_FAILED(mg_session_write_uint8(session, MG_SIGNATURE_POINT_3D));
185+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, point->srid));
186+
MG_RETURN_IF_FAILED(mg_session_write_float(session, point->x));
187+
MG_RETURN_IF_FAILED(mg_session_write_float(session, point->y));
188+
MG_RETURN_IF_FAILED(mg_session_write_float(session, point->z));
189+
return 0;
190+
}
191+
171192
int mg_session_write_value(mg_session *session, const mg_value *value) {
172193
switch (value->type) {
173194
case MG_VALUE_TYPE_NULL:
@@ -219,11 +240,9 @@ int mg_session_write_value(mg_session *session, const mg_value *value) {
219240
case MG_VALUE_TYPE_DURATION:
220241
return mg_session_write_duration(session, value->duration_v);
221242
case MG_VALUE_TYPE_POINT_2D:
222-
mg_session_set_error(session, "tried to send value of type 'point_2d'");
223-
return MG_ERROR_INVALID_VALUE;
243+
return mg_session_write_point_2d(session, value->point_2d_v);
224244
case MG_VALUE_TYPE_POINT_3D:
225-
mg_session_set_error(session, "tried to send value of type 'point_3d'");
226-
return MG_ERROR_INVALID_VALUE;
245+
return mg_session_write_point_3d(session, value->point_3d_v);
227246
case MG_VALUE_TYPE_UNKNOWN:
228247
mg_session_set_error(session, "tried to send value of unknown type");
229248
return MG_ERROR_INVALID_VALUE;

src/mgvalue.c

+26
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,32 @@ mg_duration *mg_duration_make(int64_t months, int64_t days, int64_t seconds,
16811681
return dur;
16821682
}
16831683

1684+
mg_point_2d *mg_point_2d_make(uint16_t srid, double x_longitude,
1685+
double y_latitude) {
1686+
mg_point_2d *point = mg_point_2d_alloc(&mg_system_allocator);
1687+
if (!point) {
1688+
return NULL;
1689+
}
1690+
point->srid = srid;
1691+
point->x = x_longitude;
1692+
point->y = y_latitude;
1693+
return point;
1694+
}
1695+
1696+
mg_point_3d *mg_point_3d_make(uint16_t srid, double x_longitude,
1697+
double y_latitude, double z_height) {
1698+
mg_point_3d *point = mg_point_3d_alloc(&mg_system_allocator);
1699+
if (!point) {
1700+
return NULL;
1701+
}
1702+
point->srid = srid;
1703+
point->x = x_longitude;
1704+
point->y = y_latitude;
1705+
point->z = z_height;
1706+
;
1707+
return point;
1708+
}
1709+
16841710
int mg_string_equal(const mg_string *lhs, const mg_string *rhs) {
16851711
if (lhs->size != rhs->size) {
16861712
return 0;

0 commit comments

Comments
 (0)