SEXP ConvertDate(bson_iter_t* iter){ SEXP list = PROTECT(allocVector(VECSXP, 1)); SET_VECTOR_ELT(list, 0, ScalarReal((double) bson_iter_date_time(iter))); setAttrib(list, R_NamesSymbol, mkString("$date")); UNPROTECT(1); return list; }
void bsonToMongoDate(bson_iter_t* iter, Array* output) { int64_t ts = bson_iter_date_time(iter); bsonToObject(iter, output, &s_MongoDate, make_packed_array(ts / 1000, (ts % 1000) * 1000) ); }
/** * _mongoc_gridfs_file_new_from_bson: * * creates a gridfs file from a bson object * * This is only really useful for instantiating a gridfs file from a server * side object */ mongoc_gridfs_file_t * _mongoc_gridfs_file_new_from_bson (mongoc_gridfs_t *gridfs, const bson_t *data) { mongoc_gridfs_file_t *file; const char *key; bson_iter_t iter; const uint8_t *buf; uint32_t buf_len; ENTRY; BSON_ASSERT (gridfs); BSON_ASSERT (data); file = bson_malloc0 (sizeof *file); file->gridfs = gridfs; bson_copy_to (data, &file->bson); bson_iter_init (&iter, &file->bson); while (bson_iter_next (&iter)) { key = bson_iter_key (&iter); if (0 == strcmp (key, "_id")) { bson_oid_copy (bson_iter_oid (&iter), &file->files_id); } else if (0 == strcmp (key, "length")) { file->length = bson_iter_int64 (&iter); } else if (0 == strcmp (key, "chunkSize")) { file->chunk_size = bson_iter_int32 (&iter); } else if (0 == strcmp (key, "uploadDate")) { file->upload_date = bson_iter_date_time (&iter); } else if (0 == strcmp (key, "md5")) { file->bson_md5 = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "filename")) { file->bson_filename = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "contentType")) { file->bson_content_type = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "aliases")) { bson_iter_array (&iter, &buf_len, &buf); bson_init_static (&file->bson_aliases, buf, buf_len); } else if (0 == strcmp (key, "metadata")) { bson_iter_document (&iter, &buf_len, &buf); bson_init_static (&file->bson_metadata, buf, buf_len); } } /* TODO: is there are a minimal object we should be verifying that we * actually have here? */ RETURN (file); }
static void test_bson_iter_mixed (void) { bson_iter_t iter; bson_decimal128_t iter_value; bson_decimal128_t value; bson_t *b; bson_t *b2; b = bson_new(); b2 = bson_new(); value.high = 0; value.low = 1; assert(bson_append_utf8(b2, "foo", -1, "bar", -1)); assert(bson_append_code(b, "0", -1, "var a = {};")); assert(bson_append_code_with_scope(b, "1", -1, "var b = {};", b2)); assert(bson_append_int32(b, "2", -1, 1234)); assert(bson_append_int64(b, "3", -1, 4567)); assert(bson_append_time_t(b, "4", -1, 123456)); assert(bson_append_decimal128(b, "5", -1, &value)); assert(bson_iter_init(&iter, b)); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_CODE(&iter)); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_CODEWSCOPE(&iter)); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_INT32(&iter)); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_INT64(&iter)); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_DATE_TIME(&iter)); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_DECIMAL128(&iter)); assert(!bson_iter_next(&iter)); assert(bson_iter_init_find(&iter, b, "3")); assert(!strcmp(bson_iter_key(&iter), "3")); assert(bson_iter_int64(&iter) == 4567); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_DATE_TIME(&iter)); assert(bson_iter_time_t(&iter) == 123456); assert(bson_iter_date_time(&iter) == 123456000); assert(bson_iter_next(&iter)); /* This test uses memcmp because libbson lacks decimal128 comparison. */ bson_iter_decimal128(&iter, &iter_value); assert(memcmp(&iter_value, &value, sizeof(value)) == 0); assert(!bson_iter_next(&iter)); bson_destroy(b); bson_destroy(b2); }
int monary_load_datetime_value(const bson_iter_t* bsonit, monary_column_item* citem, int idx) { int64_t value; if (BSON_ITER_HOLDS_DATE_TIME(bsonit)) { value = bson_iter_date_time(bsonit); memcpy(((int64_t*) citem->storage) + idx, &value, sizeof(int64_t)); return 1; } else { return 0; } }
static struct ofields* get_data(struct results* res) { struct ofields* fields; const char* msg; const char* prog; const char* syslog_tag; int64_t date_r; bson_iter_t c; fields = malloc (sizeof (struct ofields)); bson_iter_init_find (&c, res->result, "msg"); if (!(msg = bson_iter_utf8 (&c, NULL))) { perror ("bson_cursor_get_string()"); exit (1); } bson_iter_init_find (&c, res->result, "sys"); if (!(prog = bson_iter_utf8 (&c, NULL))) { perror ("bson_cursor_get_string()"); exit (1); } bson_iter_init_find (&c, res->result, "syslog_tag"); if (!(syslog_tag = bson_iter_utf8 (&c, NULL))) { perror ("bson_cursor_get_string()"); exit (1); } bson_iter_init_find (&c, res->result, "time_rcvd"); if (!(date_r = bson_iter_date_time (&c))) { perror ("bson_cursor_get_utc_datetime()"); exit (1); } fields->msg = msg; fields->prog = prog; fields->syslog_tag = syslog_tag; fields->date_r = date_r; return fields; }
/** * _mongoc_gridfs_file_new_from_bson: * * creates a gridfs file from a bson object * * This is only really useful for instantiating a gridfs file from a server * side object */ mongoc_gridfs_file_t * _mongoc_gridfs_file_new_from_bson (mongoc_gridfs_t *gridfs, const bson_t *data) { mongoc_gridfs_file_t *file; const bson_value_t *value; const char *key; bson_iter_t iter; const uint8_t *buf; uint32_t buf_len; ENTRY; BSON_ASSERT (gridfs); BSON_ASSERT (data); file = (mongoc_gridfs_file_t *)bson_malloc0 (sizeof *file); file->gridfs = gridfs; bson_copy_to (data, &file->bson); bson_iter_init (&iter, &file->bson); while (bson_iter_next (&iter)) { key = bson_iter_key (&iter); if (0 == strcmp (key, "_id")) { value = bson_iter_value (&iter); bson_value_copy (value, &file->files_id); } else if (0 == strcmp (key, "length")) { if (!BSON_ITER_HOLDS_INT32 (&iter) && !BSON_ITER_HOLDS_INT64 (&iter) && !BSON_ITER_HOLDS_DOUBLE (&iter)) { GOTO (failure); } file->length = bson_iter_as_int64 (&iter); } else if (0 == strcmp (key, "chunkSize")) { if (!BSON_ITER_HOLDS_INT32 (&iter) && !BSON_ITER_HOLDS_INT64 (&iter) && !BSON_ITER_HOLDS_DOUBLE (&iter)) { GOTO (failure); } if (bson_iter_as_int64 (&iter) > INT32_MAX) { GOTO (failure); } file->chunk_size = (int32_t)bson_iter_as_int64 (&iter); } else if (0 == strcmp (key, "uploadDate")) { if (!BSON_ITER_HOLDS_DATE_TIME (&iter)){ GOTO (failure); } file->upload_date = bson_iter_date_time (&iter); } else if (0 == strcmp (key, "md5")) { if (!BSON_ITER_HOLDS_UTF8 (&iter)) { GOTO (failure); } file->bson_md5 = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "filename")) { if (!BSON_ITER_HOLDS_UTF8 (&iter)) { GOTO (failure); } file->bson_filename = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "contentType")) { if (!BSON_ITER_HOLDS_UTF8 (&iter)) { GOTO (failure); } file->bson_content_type = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "aliases")) { if (!BSON_ITER_HOLDS_ARRAY (&iter)) { GOTO (failure); } bson_iter_array (&iter, &buf_len, &buf); bson_init_static (&file->bson_aliases, buf, buf_len); } else if (0 == strcmp (key, "metadata")) { if (!BSON_ITER_HOLDS_DOCUMENT (&iter)) { GOTO (failure); } bson_iter_document (&iter, &buf_len, &buf); bson_init_static (&file->bson_metadata, buf, buf_len); } } /* TODO: is there are a minimal object we should be verifying that we * actually have here? */ RETURN (file); failure: bson_destroy (&file->bson); RETURN (NULL); }
/*! * \brief Convert rows from mongodb to db API representation * \param _h database connection * \param _r database result set * \return 0 on success, negative on failure */ static int db_mongodb_convert_bson(const db1_con_t* _h, db1_res_t* _r, int _row, const bson_t *_rdoc) { static str dummy_string = {"", 0}; int col; db_mongodb_result_t *mgres; const char *colname; bson_type_t coltype; bson_iter_t riter; bson_iter_t citer; bson_iter_t *piter; db_val_t* dval; uint32_t i32tmp; bson_subtype_t subtype; bson_t *cdoc; mgres = (db_mongodb_result_t*)RES_PTR(_r); if(mgres->nrcols==0) { LM_ERR("no fields to convert\n"); return -1; } if(mgres->colsdoc==NULL) { cdoc = (bson_t*)_rdoc; } else { cdoc = (bson_t*)mgres->colsdoc; } if (!bson_iter_init (&citer, cdoc)) { LM_ERR("failed to initialize columns iterator\n"); return -3; } if(mgres->colsdoc) { if (!bson_iter_init (&riter, _rdoc)) { LM_ERR("failed to initialize result iterator\n"); return -3; } } if (db_allocate_row(_r, &(RES_ROWS(_r)[_row])) != 0) { LM_ERR("could not allocate row: %d\n", _row); return -2; } col = 0; while (bson_iter_next (&citer)) { if(col >= RES_COL_N(_r)) { LM_ERR("invalid number of columns (%d/%d)\n", col, RES_COL_N(_r)); return -4; } colname = bson_iter_key (&citer); LM_DBG("looking for field[%d] named: %s\n", col, colname); if(mgres->colsdoc) { if(!bson_iter_find(&riter, colname)) { LM_ERR("field [%s] not found in result iterator\n", colname); return -4; } piter = &riter; } else { piter = &citer; } coltype = bson_iter_type(piter); dval = &(ROW_VALUES(&(RES_ROWS(_r)[_row]))[col]); VAL_TYPE(dval) = RES_TYPES(_r)[col]; switch(coltype) { case BSON_TYPE_BOOL: VAL_INT(dval) = (int)bson_iter_bool (piter); break; case BSON_TYPE_INT32: VAL_INT(dval) = bson_iter_int32 (piter); break; case BSON_TYPE_TIMESTAMP: bson_iter_timestamp (piter, (uint32_t*)&VAL_INT(dval), &i32tmp); break; case BSON_TYPE_INT64: VAL_BIGINT(dval) = bson_iter_int64 (piter); break; case BSON_TYPE_DOUBLE: VAL_DOUBLE(dval) = bson_iter_double (piter); break; case BSON_TYPE_DATE_TIME: VAL_TIME(dval) = (time_t)(bson_iter_date_time (piter)/1000); break; case BSON_TYPE_BINARY: bson_iter_binary (piter, &subtype, (uint32_t*)&VAL_BLOB(dval).len, (const uint8_t**)&VAL_BLOB(dval).s); break; case BSON_TYPE_UTF8: VAL_STRING(dval) = (char*)bson_iter_utf8 (piter, &i32tmp); break; case BSON_TYPE_OID: break; case BSON_TYPE_NULL: memset(dval, 0, sizeof(db_val_t)); /* Initialize the string pointers to a dummy empty * string so that we do not crash when the NULL flag * is set but the module does not check it properly */ VAL_STRING(dval) = dummy_string.s; VAL_STR(dval) = dummy_string; VAL_BLOB(dval) = dummy_string; VAL_TYPE(dval) = RES_TYPES(_r)[col]; VAL_NULL(dval) = 1; break; #if 0 case BSON_TYPE_EOD: case BSON_TYPE_DOCUMENT: case BSON_TYPE_ARRAY: case BSON_TYPE_UNDEFINED: case BSON_TYPE_REGEX: case BSON_TYPE_DBPOINTER: case BSON_TYPE_CODE: case BSON_TYPE_SYMBOL: case BSON_TYPE_CODEWSCOPE: case BSON_TYPE_MAXKEY: case BSON_TYPE_MINKEY: #endif default: LM_WARN("unhandled data type column (%.*s) type id (%d), " "use DB1_STRING as default\n", RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s, coltype); RES_TYPES(_r)[col] = DB1_STRING; break; } LM_DBG("RES_NAMES(%p)[%d]=[%.*s] (%d)\n", RES_NAMES(_r)[col], col, RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s, coltype); col++; } return 0; }
types::b_date element::get_date() const { BSONCXX_TYPE_CHECK(k_date); BSONCXX_CITER; return types::b_date{std::chrono::milliseconds{bson_iter_date_time(&iter)}}; }
types::b_date element::get_date() const { BSONCXX_TYPE_CHECK(k_date); CITER; return types::b_date{bson_iter_date_time(&iter)}; }
types::b_date element::get_date() const { CITER; return types::b_date{bson_iter_date_time(&iter)}; }
time_t BsonIterDate(BSON_ITERATOR *it) { return bson_iter_date_time(it); }
QVariantMap TBson::fromBson(const TBsonObject *obj) { QVariantMap ret; bson_iter_t it; const bson_t *bson = (const bson_t *)obj; bson_iter_init(&it, bson); while (bson_iter_next(&it)) { bson_type_t t = bson_iter_type(&it); QString key(bson_iter_key(&it)); switch (t) { case BSON_TYPE_EOD: return ret; break; case BSON_TYPE_DOUBLE: ret[key] = bson_iter_double(&it); break; case BSON_TYPE_UTF8: ret[key] = QString::fromUtf8(bson_iter_utf8(&it, nullptr)); break; case BSON_TYPE_ARRAY: { const uint8_t *docbuf = nullptr; uint32_t doclen = 0; bson_t sub[1]; bson_iter_array(&it, &doclen, &docbuf); if (bson_init_static(sub, docbuf, doclen)) { ret[key] = fromBson(sub).values(); } break; } case BSON_TYPE_DOCUMENT: { const uint8_t *docbuf = nullptr; uint32_t doclen = 0; bson_t sub[1]; bson_iter_document(&it, &doclen, &docbuf); if (bson_init_static(sub, docbuf, doclen)) { ret[key] = fromBson(sub); } break; } case BSON_TYPE_BINARY: { const uint8_t *binary = nullptr; bson_subtype_t subtype = BSON_SUBTYPE_BINARY; uint32_t len = 0; bson_iter_binary(&it, &subtype, &len, &binary); if (binary) { ret[key] = QByteArray((char *)binary, len); } break; } case BSON_TYPE_UNDEFINED: ret[key] = QVariant(); break; case BSON_TYPE_OID: { char oidhex[25]; bson_oid_to_string(bson_iter_oid(&it), oidhex); ret[key] = QString(oidhex); break; } case BSON_TYPE_BOOL: ret[key] = (bool)bson_iter_bool(&it); break; case BSON_TYPE_DATE_TIME: { #if QT_VERSION >= 0x040700 QDateTime date; date.setMSecsSinceEpoch(bson_iter_date_time(&it)); #else qint64 val = bson_iter_date_time(&it); qint64 days = val / 86400000; // 24*60*60*1000 int msecs = val % 86400000; QDate dt = QDate(1970, 1, 1).addDays(days); QTime tm = QTime(0, 0, 0).addMSecs(msecs); QDateTime date(dt, tm, Qt::UTC); #endif ret[key] = date; break; } case BSON_TYPE_NULL: ret[key] = QVariant(); break; case BSON_TYPE_REGEX: ret[key] = QRegExp(QLatin1String(bson_iter_regex(&it, nullptr))); break; case BSON_TYPE_CODE: ret[key] = QString(bson_iter_code(&it, nullptr)); break; case BSON_TYPE_SYMBOL: ret[key] = QString(bson_iter_symbol(&it, nullptr)); break; case BSON_TYPE_INT32: ret[key] = bson_iter_int32(&it); break; case BSON_TYPE_INT64: ret[key] = (qint64)bson_iter_int64(&it); break; case BSON_TYPE_CODEWSCOPE: // FALL THROUGH case BSON_TYPE_TIMESTAMP: // FALL THROUGH (internal use) // do nothing break; default: tError("fromBson() unknown type: %d", t); break; } //tSystemDebug("fromBson : t:%d key:%s = %s", t, qPrintable(key), qPrintable(ret[key].toString())); } return ret; }