示例#1
0
result_t MongoCollection::update(v8::Local<v8::Object> query,
    v8::Local<v8::Object> document, bool upsert, bool multi)
{
    obj_ptr<MongoDB> db(m_db);
    if (!db)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    bson bbq, bbd;
    int32_t flags = (upsert ? MONGO_UPDATE_UPSERT : 0)
        + (multi ? MONGO_UPDATE_MULTI : 0);

    Isolate* isolate = holder();
    result_t hr;

    hr = encodeObject(isolate, &bbq, query);
    if (hr < 0)
        return hr;

    hr = encodeObject(isolate, &bbd, document);
    if (hr < 0) {
        bson_destroy(&bbq);
        return hr;
    }

    int32_t result = MONGO_ERROR;
    ac__update(&bbq, &bbd, flags, result);

    bson_destroy(&bbq);
    bson_destroy(&bbd);

    if (result != MONGO_OK)
        return CHECK_ERROR(db->error());

    return 0;
}
示例#2
0
文件: MongoCursor.cpp 项目: ror/fibjs
MongoCursor::MongoCursor(MongoDB *db, const std::string &ns,
                         const std::string &name, v8::Local<v8::Object> query,
                         v8::Local<v8::Object> projection)
{
    m_state = CUR_NONE;

    m_ns = ns;
    m_name = name;

    m_cursor = new cursor;
    m_cursor->m_db = db;

    mongo_cursor_init(m_cursor, &db->m_conn, ns.c_str());

    v8::Local<v8::Value> _query;
    util_base::clone(query, _query);
    m_query.Reset(Isolate::now()->m_isolate, v8::Local<v8::Object>::Cast(_query)->Clone());

    mongo_cursor_set_query(m_cursor, &m_bbq);

    encodeObject(&m_bbp, projection);

    mongo_cursor_set_fields(m_cursor, &m_bbp);

    m_bInit = false;
    m_bSpecial = false;
}
示例#3
0
result_t MongoDB::runCommand(v8::Local<v8::Object> cmd,
                             v8::Local<v8::Object> &retVal)
{
    bson bbq;
    result_t hr;

    hr = encodeObject(holder(), &bbq, cmd);
    if (hr < 0)
        return hr;

    return bsonHandler(&bbq, retVal);
}
示例#4
0
result_t encodeObject(bson *bb, v8::Local<v8::Value> element)
{
    bson_init(bb);
    if (!encodeObject(bb, NULL, element, true))
    {
        bson_destroy(bb);
        return CHECK_ERROR(CALL_E_INVALIDARG);
    }
    bson_finish(bb);

    return 0;
}
示例#5
0
result_t encoding_base::bsonEncode(v8::Local<v8::Object> data,
                                   obj_ptr<Buffer_base> &retVal)
{
    bson bb;
    result_t hr;

    hr = encodeObject(&bb, data);
    if (hr < 0)
        return hr;

    std::string strBuffer(bson_data(&bb), bson_size(&bb));
    retVal = new Buffer(strBuffer);

    bson_destroy(&bb);

    return 0;
}
示例#6
0
result_t MongoCollection::insert(v8::Local<v8::Array> documents)
{
    obj_ptr<MongoDB> db(m_db);
    if (!db)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    std::vector<bson> bbs;
    std::vector<const bson*> pbbs;
    int32_t n = documents->Length();
    int32_t i;
    result_t hr;

    if (n > 0) {
        Isolate* isolate = holder();

        bbs.resize(n);
        pbbs.resize(n);

        for (i = 0; i < n; i++) {
            hr = encodeObject(isolate, &bbs[i], documents->Get(i));
            if (hr < 0) {
                n = i;
                for (i = 0; i < n; i++)
                    bson_destroy(&bbs[i]);
                return hr;
            }

            pbbs[i] = &bbs[i];
        }

        int32_t result = MONGO_ERROR;
        ac__batchInsert(pbbs, n, result);

        for (i = 0; i < n; i++)
            bson_destroy(&bbs[i]);

        if (result != MONGO_OK)
            return CHECK_ERROR(db->error());
    }

    return 0;
}
示例#7
0
MongoCursor::MongoCursor(MongoDB *db, const std::string &ns,
                         const std::string &name, v8::Local<v8::Object> query,
                         v8::Local<v8::Object> projection)
{
    m_state = CUR_NONE;

    m_ns = ns;
    m_name = name;

    v8::Local<v8::Value> _query;
    util_base::clone(query, _query);
    m_query.Reset(Isolate::now()->m_isolate, v8::Local<v8::Object>::Cast(_query)->Clone());

    encodeObject(&m_bbp, projection);

    cc__initCursor(db);

    m_bInit = false;
    m_bSpecial = false;
}
示例#8
0
QByteArray QPpsObjectPrivate::encode(const QVariantMap &ppsData, bool *ok)
{
    pps_encoder_t encoder;
    pps_encoder_initialize(&encoder, false);

    encodeObject(&encoder, ppsData, ok);
    const char *rawData = 0;
    if (*ok) {
        // rawData points to a memory owned by encoder.
        // The memory will be freed when pps_encoder_cleanup is called.
        rawData = pps_encoder_buffer(&encoder);
        if (!rawData) {
            qWarning() << "QPpsObjectPrivate::encode: pps_encoder_buffer failed";
            *ok = false;
        }
    }

    pps_encoder_cleanup(&encoder);
    return QByteArray(rawData);
}
示例#9
0
文件: MongoCursor.cpp 项目: ror/fibjs
result_t MongoCursor::hasNext(bool &retVal)
{
    if (!m_bInit)
    {
        result_t hr;

        hr = encodeObject(&m_bbq, v8::Local<v8::Object>::New(Isolate::now()->m_isolate, m_query));
        if (hr < 0)
            return hr;

        m_bInit = true;
    }

    if (m_state == CUR_NONE)
        m_state =
            mongo_cursor_next(m_cursor) == MONGO_OK ?
            CUR_DATA : CUR_NODATA;

    retVal = m_state == CUR_DATA;

    return CHECK_ERROR(m_cursor->m_db->error());
}
示例#10
0
result_t MongoCollection::remove(v8::Local<v8::Object> query)
{
    obj_ptr<MongoDB> db(m_db);
    if (!db)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    bson bbq;
    result_t hr;

    hr = encodeObject(holder(), &bbq, query);
    if (hr < 0)
        return hr;

    int32_t result = MONGO_ERROR;
    ac__remove(&bbq, result);

    bson_destroy(&bbq);

    if (result != MONGO_OK)
        return CHECK_ERROR(db->error());

    return 0;
}
示例#11
0
result_t MongoCollection::insert(v8::Local<v8::Object> document)
{
    obj_ptr<MongoDB> db(m_db);
    if (!db)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    bson bb;
    result_t hr;

    hr = encodeObject(holder(), &bb, document);
    if (hr < 0)
        return hr;

    int32_t result = MONGO_ERROR;
    ac__insert(&bb, result);

    bson_destroy(&bb);

    if (result != MONGO_OK)
        return CHECK_ERROR(db->error());

    return 0;
}
示例#12
0
void QPpsObjectPrivate::encodeData(pps_encoder_t *encoder, const char *name, const QVariant &data,
                                   bool *ok)
{
    QString errorFunction;
    pps_encoder_error_t error = PPS_ENCODER_OK;
    switch (data.type()) {
    case QVariant::Bool:
        error = pps_encoder_add_bool(encoder, name, data.toBool());
        errorFunction = QStringLiteral("pps_encoder_add_bool");
        break;
    // We want to support encoding uint even though libpps doesn't support it directly.
    // We can't encode uint as an int since that will lose precision (e.g. 2^31+1 can't be
    // encoded that way). However, we can convert uint to double without losing precision.
    // QVariant.toDouble() conveniently takes care of the conversion for us.
    case QVariant::UInt:
    case QVariant::Double:
        error = pps_encoder_add_double(encoder, name, data.toDouble());
        errorFunction = QStringLiteral("pps_encoder_add_double");
        break;
    case QVariant::Int:
        error = pps_encoder_add_int(encoder, name, data.toInt());
        errorFunction = QStringLiteral("pps_encoder_add_int");
        break;
    case QVariant::LongLong:
        error = pps_encoder_add_int64(encoder, name, data.toLongLong());
        errorFunction = QStringLiteral("pps_encoder_add_int64");
        break;
    case QVariant::String:
        error = pps_encoder_add_string(encoder, name, data.toString().toUtf8().constData());
        errorFunction = QStringLiteral("pps_encoder_add_string");
        break;
    case QVariant::List:
        error = pps_encoder_start_array(encoder, name);
        errorFunction = QStringLiteral("pps_encoder_start_array");
        if (error == PPS_ENCODER_OK) {
            encodeArray(encoder, data.toList(), ok);
            error = pps_encoder_end_array(encoder);
            errorFunction = QStringLiteral("pps_encoder_end_array");
        }
        break;
    case QVariant::Map:
        error = pps_encoder_start_object(encoder, name);
        errorFunction = QStringLiteral("pps_encoder_start_object");
        if (error == PPS_ENCODER_OK) {
            encodeObject(encoder, data.toMap(), ok);
            error = pps_encoder_end_object(encoder);
            errorFunction = QStringLiteral("pps_encoder_end_object");
        }
        break;
    case QVariant::Invalid:
        error = pps_encoder_add_null(encoder, name);
        errorFunction = QStringLiteral("pps_encoder_add_null");
        break;
    default:
        qWarning() << "QPpsObjectPrivate::encodeData: the type of the parameter data is invalid";
        *ok = false;
        return;
    }

    if (error != PPS_ENCODER_OK) {
        qWarning() << "QPpsObjectPrivate::encodeData: " << errorFunction << " failed";
        *ok = false;
    } else {
        *ok = true;
    }
}
示例#13
0
void encodeValue(bson *bb, const char *name, v8::Local<v8::Value> element,
                 bool doJson)
{
    if (element.IsEmpty() || element->IsUndefined() || element->IsFunction())
        ;
    else if (element->IsNull())
        bson_append_null(bb, name);
    else if (element->IsDate())
        bson_append_date(bb, name, (bson_date_t) element->NumberValue());
    else if (element->IsBoolean())
        bson_append_bool(bb, name, element->IsTrue());
    else if (element->IsNumber() || element->IsNumberObject())
    {
        double value = element->NumberValue();
        int64_t num = (int64_t) value;

        if (value == (double) num)
        {
            if (num >= -2147483648ll && num <= 2147483647ll)
                bson_append_int(bb, name, (int) num);
            else
                bson_append_long(bb, name, num);
        }
        else
            bson_append_double(bb, name, value);
    }
    else if (element->IsArray())
        encodeArray(bb, name, element);
    else if (element->IsRegExp())
    {
        v8::Local<v8::RegExp> re = v8::Local<v8::RegExp>::Cast(element);
        v8::Local<v8::String> src = re->GetSource();
        v8::RegExp::Flags flgs = re->GetFlags();
        char flgStr[4];
        char *p = flgStr;

        if (flgs & v8::RegExp::kIgnoreCase)
            *p++ = 'i';
        if (flgs & v8::RegExp::kGlobal)
            *p++ = 'g';
        if (flgs & v8::RegExp::kMultiline)
            *p++ = 'm';

        *p = 0;

        bson_append_regex(bb, name, *v8::String::Utf8Value(src), flgStr);
    }
    else if (element->IsObject())
    {
        {
            obj_ptr<Int64> num = (Int64 *)Int64_base::getInstance(element);

            if (num)
            {
                if (num->m_num >= -2147483648ll && num->m_num <= 2147483647ll)
                    bson_append_int(bb, name, (int) num->m_num);
                else
                    bson_append_long(bb, name, num->m_num);
                return;
            }
        }

        {
            obj_ptr<Buffer_base> buf = Buffer_base::getInstance(element);

            if (buf)
            {
                std::string strBuf;

                buf->toString(strBuf);
                bson_append_binary(bb, name, BSON_BIN_BINARY, strBuf.c_str(),
                                   (int) strBuf.length());

                return;
            }
        }

        {
            obj_ptr<MongoID> oid = (MongoID *) MongoID_base::getInstance(
                                       element);

            if (oid)
            {
                bson_append_oid(bb, name, &oid->m_id);
                return;
            }
        }

        encodeObject(bb, name, element, doJson);
    }
    else
    {
        v8::String::Utf8Value v(element);
        bson_append_string(bb, name, ToCString(v));
    }
}
示例#14
0
bool appendObject(bson *bb, v8::Local<v8::Value> element)
{
    return encodeObject(bb, NULL, element, true);
}