/* {{{ proto array WriteResult::getUpsertedIds() Returns the identifiers generated by the server for upsert operations. */ PHP_METHOD(WriteResult, getUpsertedIds) { bson_iter_t iter, child; php_phongo_writeresult_t *intern; SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(return_value_used) intern = Z_WRITERESULT_OBJ_P(getThis()); if (zend_parse_parameters_none() == FAILURE) { return; } array_init(return_value); if (bson_iter_init_find(&iter, intern->reply, "upserted") && BSON_ITER_HOLDS_ARRAY(&iter) && bson_iter_recurse(&iter, &child)) { while (bson_iter_next(&child)) { int32_t index; bson_iter_t outer; if (!BSON_ITER_HOLDS_DOCUMENT(&child) || !bson_iter_recurse(&child, &outer)) { continue; } if (!bson_iter_find(&outer, "index") || !BSON_ITER_HOLDS_INT32(&outer)) { continue; } index = bson_iter_int32(&outer); if (!bson_iter_find(&outer, "_id")) { continue; } if (BSON_ITER_HOLDS_OID(&outer)) { #if PHP_VERSION_ID >= 70000 zval zid; php_phongo_objectid_new_from_oid(&zid, bson_iter_oid(&outer) TSRMLS_CC); add_index_zval(return_value, index, &zid); #else zval *zid = NULL; MAKE_STD_ZVAL(zid); php_phongo_objectid_new_from_oid(zid, bson_iter_oid(&outer) TSRMLS_CC); add_index_zval(return_value, index, zid); #endif } else if (BSON_ITER_HOLDS_INT32(&outer)) { int32_t val = bson_iter_int32(&outer); add_index_long(return_value, index, val); } else if (BSON_ITER_HOLDS_INT64(&outer)) { int64_t val = bson_iter_int64(&outer); ADD_INDEX_INT64(return_value, index, val); } } } }
types::b_oid element::get_oid() const { CITER; const bson_oid_t* boid = bson_iter_oid(&iter); oid v(reinterpret_cast<const char*>(boid->bytes), sizeof(boid->bytes)); return types::b_oid{v}; }
void bsonToMongoId(bson_iter_t* iter, Array* output) { const bson_oid_t* oid = bson_iter_oid(iter); char id[25]; bson_oid_to_string(oid, id); bsonToObject(iter, output, &s_MongoId, make_packed_array(String(id)) ); }
/** * _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); }
int monary_load_objectid_value(const bson_iter_t* bsonit, monary_column_item* citem, int idx) { const bson_oid_t* oid; uint8_t* dest; if (BSON_ITER_HOLDS_OID(bsonit)) { oid = bson_iter_oid(bsonit); dest = ((uint8_t*) citem->storage) + (idx * sizeof(bson_oid_t)); memcpy(dest, oid->bytes, sizeof(bson_oid_t)); return 1; } else { return 0; } }
/* {{{ proto mixed BulkWrite::insert(array|object $document) Adds an insert operation to the bulk */ PHP_METHOD(BulkWrite, insert) { php_phongo_bulkwrite_t *intern; zval *document; bson_t *bson; bson_t *bson_out = NULL; int bson_flags = PHONGO_BSON_ADD_ID; DECLARE_RETURN_VALUE_USED SUPPRESS_UNUSED_WARNING(return_value_ptr) intern = Z_BULKWRITE_OBJ_P(getThis()); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "A", &document) == FAILURE) { return; } if (return_value_used) { bson_flags |= PHONGO_BSON_RETURN_ID; } bson = bson_new(); phongo_zval_to_bson(document, bson_flags, bson, &bson_out TSRMLS_CC); mongoc_bulk_operation_insert(intern->bulk, bson); bson_clear(&bson); intern->num_ops++; if (bson_out && return_value_used) { bson_iter_t iter; if (bson_iter_init_find(&iter, bson_out, "_id")) { php_phongo_objectid_new_from_oid(return_value, bson_iter_oid(&iter) TSRMLS_CC); bson_clear(&bson_out); return; } bson_clear(&bson_out); } }
SEXP ConvertValue(bson_iter_t* iter){ if(BSON_ITER_HOLDS_INT32(iter)){ return ScalarInteger(bson_iter_int32(iter)); } else if(BSON_ITER_HOLDS_NULL(iter)){ return R_NilValue; } else if(BSON_ITER_HOLDS_BOOL(iter)){ return ScalarLogical(bson_iter_bool(iter)); } else if(BSON_ITER_HOLDS_DOUBLE(iter)){ return ScalarReal(bson_iter_double(iter)); } else if(BSON_ITER_HOLDS_INT64(iter)){ return ScalarReal((double) bson_iter_int64(iter)); } else if(BSON_ITER_HOLDS_UTF8(iter)){ return mkStringUTF8(bson_iter_utf8(iter, NULL)); } else if(BSON_ITER_HOLDS_CODE(iter)){ return mkStringUTF8(bson_iter_code(iter, NULL)); } else if(BSON_ITER_HOLDS_BINARY(iter)){ return ConvertBinary(iter); } else if(BSON_ITER_HOLDS_DATE_TIME(iter)){ return ConvertDate(iter); } else if(BSON_ITER_HOLDS_OID(iter)){ const bson_oid_t *val = bson_iter_oid(iter); char str[25]; bson_oid_to_string(val, str); return mkString(str); } else if(BSON_ITER_HOLDS_ARRAY(iter)){ bson_iter_t child1; bson_iter_t child2; bson_iter_recurse (iter, &child1); bson_iter_recurse (iter, &child2); return ConvertArray(&child1, &child2); } else if(BSON_ITER_HOLDS_DOCUMENT(iter)){ bson_iter_t child1; bson_iter_t child2; bson_iter_recurse (iter, &child1); bson_iter_recurse (iter, &child2); return ConvertObject(&child1, &child2); } else { stop("Unimplemented BSON type %d\n", bson_iter_type(iter)); } }
SEXP ConvertValue(bson_iter_t* iter){ if(BSON_ITER_HOLDS_INT32(iter)){ return ScalarInteger(bson_iter_int32(iter)); } else if(BSON_ITER_HOLDS_NULL(iter)){ return R_NilValue; } else if(BSON_ITER_HOLDS_BOOL(iter)){ return ScalarLogical(bson_iter_bool(iter)); } else if(BSON_ITER_HOLDS_DOUBLE(iter)){ return ScalarReal(bson_iter_double(iter)); } else if(BSON_ITER_HOLDS_INT64(iter)){ return ScalarReal((double) bson_iter_int64(iter)); } else if(BSON_ITER_HOLDS_UTF8(iter)){ return mkStringUTF8(bson_iter_utf8(iter, NULL)); } else if(BSON_ITER_HOLDS_CODE(iter)){ return mkStringUTF8(bson_iter_code(iter, NULL)); } else if(BSON_ITER_HOLDS_BINARY(iter)){ return ConvertBinary(iter); } else if(BSON_ITER_HOLDS_DATE_TIME(iter)){ return ConvertDate(iter); } else if(BSON_ITER_HOLDS_OID(iter)){ //not sure if this casting works return mkRaw((unsigned char *) bson_iter_oid(iter), 12); } else if(BSON_ITER_HOLDS_ARRAY(iter)){ bson_iter_t child1; bson_iter_t child2; bson_iter_recurse (iter, &child1); bson_iter_recurse (iter, &child2); return ConvertArray(&child1, &child2); } else if(BSON_ITER_HOLDS_DOCUMENT(iter)){ bson_iter_t child1; bson_iter_t child2; bson_iter_recurse (iter, &child1); bson_iter_recurse (iter, &child2); return ConvertObject(&child1, &child2); } else { stop("Unimplemented BSON type %d\n", bson_iter_type(iter)); } }
int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, const char *topic, int acc) { struct mongo_backend *handle = (struct mongo_backend *) conf; mongoc_collection_t *collection; mongoc_cursor_t *cursor; bson_error_t error; const bson_t *doc; bson_iter_t iter; int match = 0; const bson_oid_t *topic_lookup_oid = NULL; const char *topic_lookup_utf8 = NULL; int64_t topic_lookup_int64 = 0; bson_t query; bson_init(&query); bson_append_utf8(&query, handle->user_username_prop, -1, username, -1); collection = mongoc_client_get_collection(handle->client, handle->database, handle->user_coll); cursor = mongoc_collection_find_with_opts(collection, &query, NULL, NULL); if (!mongoc_cursor_error (cursor, &error) && mongoc_cursor_next (cursor, &doc)) { // First find any user[handle->user_topiclist_fk_prop] if (bson_iter_init_find(&iter, doc, handle->user_topiclist_fk_prop)) { bson_type_t loc_id_type = bson_iter_type(&iter); if (loc_id_type == BSON_TYPE_OID) { topic_lookup_oid = bson_iter_oid(&iter); } else if (loc_id_type == BSON_TYPE_INT32 || loc_id_type == BSON_TYPE_INT64) { topic_lookup_int64 = bson_iter_as_int64(&iter); } else if (loc_id_type == BSON_TYPE_UTF8) { topic_lookup_utf8 = bson_iter_utf8(&iter, NULL); } } // Look through the props from the beginning for user[handle->user_topics_prop] if (bson_iter_init_find(&iter, doc, handle->user_topics_prop)) { bson_type_t embedded_prop_type = bson_iter_type(&iter); if (embedded_prop_type == BSON_TYPE_ARRAY) { match = be_mongo_check_acl_topics_array(&iter, topic, clientid, username); } else if (embedded_prop_type == BSON_TYPE_DOCUMENT) { match = be_mongo_check_acl_topics_map(&iter, topic, acc, clientid, username); } } } if ((mongoc_cursor_error (cursor, &error)) && (match != 1)) { fprintf (stderr, "Cursor Failure: %s\n", error.message); } bson_destroy(&query); mongoc_cursor_destroy (cursor); mongoc_collection_destroy(collection); if (!match && (topic_lookup_oid != NULL || topic_lookup_int64 != 0 || topic_lookup_utf8 != NULL)) { bson_init(&query); if (topic_lookup_oid != NULL) { bson_append_oid(&query, handle->topiclist_key_prop, -1, topic_lookup_oid); } else if (topic_lookup_int64 != 0) { bson_append_int64(&query, handle->topiclist_key_prop, -1, topic_lookup_int64); } else if (topic_lookup_utf8 != NULL) { bson_append_utf8(&query, handle->topiclist_key_prop, -1, topic_lookup_utf8, -1); } collection = mongoc_client_get_collection(handle->client, handle->database, handle->topiclist_coll); cursor = mongoc_collection_find_with_opts(collection, &query, NULL, NULL); if (!mongoc_cursor_error (cursor, &error) && mongoc_cursor_next(cursor, &doc)) { bson_iter_init(&iter, doc); if (bson_iter_find(&iter, handle->topiclist_topics_prop)) { bson_type_t loc_prop_type = bson_iter_type(&iter); if (loc_prop_type == BSON_TYPE_ARRAY) { match = be_mongo_check_acl_topics_array(&iter, topic, clientid, username); } else if (loc_prop_type == BSON_TYPE_DOCUMENT) { match = be_mongo_check_acl_topics_map(&iter, topic, acc, clientid, username); } } else { _log(LOG_NOTICE, "[mongo] ACL check error - no topic list found for user (%s) in collection (%s)", username, handle->topiclist_coll); } } if ((mongoc_cursor_error (cursor, &error)) && (match != 1)) { fprintf (stderr, "Cursor Failure: %s\n", error.message); } bson_destroy(&query); mongoc_cursor_destroy(cursor); mongoc_collection_destroy(collection); } return (match) ? BACKEND_ALLOW : BACKEND_DEFER; }
bson_oid_t * BsonIterOid(BSON_ITERATOR *it) { return bson_iter_oid(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; }