/* setorder */ JNIEXPORT void JNICALL Java_tokyotyrant_RDBQRY_setorder (JNIEnv *env, jobject self, jstring name, jint type){ if(!name){ throwillarg(env); return; } RDBQRY *qry = (RDBQRY *)(intptr_t)(*env)->GetLongField(env, self, tcrdbqry_fid_ptr); jboolean icn; const char *tname = (*env)->GetStringUTFChars(env, name, &icn); tcrdbqrysetorder(qry, tname, type); if(icn) (*env)->ReleaseStringUTFChars(env, name, tname); }
static VALUE cQuery_setorder(VALUE vself, VALUE vname, VALUE vtype){ VALUE vqry; RDBQRY *qry; vname = StringValueEx(vname); if (TYPE(vtype) == T_SYMBOL) vtype = rb_str_new2(rb_id2name(SYM2ID(vtype))); if (TYPE(vtype) == T_STRING){ vtype = StringValueEx(vtype); vtype = tctdbqrystrtoordertype(RSTRING_PTR(toupper(vtype))); vtype = INT2NUM(vtype); } vqry = rb_iv_get(vself, RDBQRYVNDATA); Data_Get_Struct(vqry, RDBQRY, qry); tcrdbqrysetorder(qry, RSTRING_PTR(vname), NUM2INT(vtype)); return vself; }
static PyObject * RTDBQuery_sort(RTDBQuery *self, PyObject *args) { #if PY_MAJOR_VERSION >= 3 const char *format = "yi:sort"; #else const char *format = "si:sort"; #endif const char *column; int type; if (!PyArg_ParseTuple(args, format, &column, &type)) { return NULL; } Py_BEGIN_ALLOW_THREADS tcrdbqrysetorder(self->rqry, column, type); Py_END_ALLOW_THREADS Py_RETURN_NONE; }
void xtcrdb_qrysetorder(void* query, const char* column_name, int order) { tcrdbqrysetorder(query, column_name, order); }
void search_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx) { char *uri, *json; double lat, lng, distance, minlat, minlng, maxlat, maxlng, miles, lat2, lng2; int x1, x2, y1, y2, id, max; int total; struct evkeyvalq args; int ecode, pksiz, i, rsiz; char pkbuf[256]; char minx[8]; char miny[8]; char maxx[8]; char maxy[8]; const char *rbuf, *name, *buf; RDBQRY *query; TCLIST *result; TCMAP *cols; Geo_Result *georesultPtr, **georesults; struct json_object *jsobj, *jsobj2, *jsarr; if (rdb == NULL) { evhttp_send_error(req, 503, "database not connected"); return; } uri = evhttp_decode_uri(req->uri); evhttp_parse_query(uri, &args); free(uri); argtof(&args, "lat", &lat, 0); argtof(&args, "lng", &lng, 0); argtof(&args, "miles", &miles, 0); argtoi(&args, "max", &max, 1); geo_box(lat, lng, miles, &minlat, &minlng, &maxlat, &maxlng); x1 = (minlat * 10000) + 1800000; y1 = (minlng * 10000) + 1800000; x2 = (maxlat * 10000) + 1800000; y2 = (maxlng * 10000) + 1800000; sprintf(minx, "%d", x1); sprintf(miny, "%d", y1); sprintf(maxx, "%d", x2); sprintf(maxy, "%d", y2); query = tcrdbqrynew(rdb); tcrdbqryaddcond(query, "x", RDBQCNUMGT, minx); tcrdbqryaddcond(query, "x", RDBQCNUMLT, maxx); tcrdbqryaddcond(query, "y", RDBQCNUMGT, miny); tcrdbqryaddcond(query, "y", RDBQCNUMLT, maxy); tcrdbqrysetorder(query, "x", RDBQONUMASC); cols = tcmapnew(); result = tcrdbqrysearch(query); total = tclistnum(result); georesults = malloc(sizeof(Geo_Result *) * total); for(i = 0; i < total; i++){ rbuf = tclistval(result, i, &rsiz); cols = tcrdbtblget(rdb, rbuf, rsiz); if (cols) { georesultPtr = malloc(sizeof(*georesultPtr)); tcmapiterinit(cols); buf = tcmapget2(cols, "lat"); lat2 = atof(buf); georesultPtr->latitude = lat2; buf = tcmapget2(cols, "lng"); lng2 = atof(buf); georesultPtr->longitude = lng2; id = atoi(rbuf); georesultPtr->id = id; georesultPtr->data = strdup(tcmapget2(cols, "data")); distance = geo_distance(lat, lng, lat2, lng2); georesultPtr->distance = distance; georesults[i] = georesultPtr; tcmapdel(cols); } } tclistdel(result); tcrdbqrydel(query); qsort(georesults, total, sizeof(Geo_Result *), CmpElem); jsobj = json_object_new_object(); jsarr = json_object_new_array(); for(i = 0; i < total; i++){ georesultPtr = georesults[i]; if (i < max) { jsobj2 = json_object_new_object(); json_object_object_add(jsobj2, "id", json_object_new_int(georesultPtr->id)); json_object_object_add(jsobj2, "data", json_object_new_string(georesultPtr->data)); json_object_object_add(jsobj2, "latitude", json_object_new_double(georesultPtr->latitude)); json_object_object_add(jsobj2, "longitude", json_object_new_double(georesultPtr->longitude)); json_object_object_add(jsobj2, "distance", json_object_new_double(georesultPtr->distance)); json_object_array_add(jsarr, jsobj2); } free(georesultPtr->data); free(georesultPtr); } free(georesults); json_object_object_add(jsobj, "total", json_object_new_int(total)); json_object_object_add(jsobj, "results", jsarr); finalize_json(req, evb, &args, jsobj); }