bool
mongoc_database_remove_all_users (mongoc_database_t *database,
                                  bson_error_t      *error)
{
   mongoc_collection_t *col;
   bson_error_t lerror;
   bson_t cmd;
   bool ret;

   ENTRY;

   bson_return_val_if_fail (database, false);

   bson_init (&cmd);
   BSON_APPEND_INT32 (&cmd, "dropAllUsersFromDatabase", 1);
   ret = mongoc_database_command_simple (database, &cmd, NULL, NULL, &lerror);
   bson_destroy (&cmd);

   if (!ret && (lerror.code == MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND)) {
      bson_init (&cmd);

      col = mongoc_client_get_collection (database->client, database->name,
                                          "system.users");
      BSON_ASSERT (col);

      ret = mongoc_collection_remove (col, MONGOC_REMOVE_NONE, &cmd, NULL,
                                      error);

      bson_destroy (&cmd);
      mongoc_collection_destroy (col);
   }

   RETURN (ret);
}
void Collection::CollectionImpl::Remove( tlib::bson::object doc )
{
	bson_error_t error;

	bool res = mongoc_collection_remove( m_collection.get(), MONGOC_REMOVE_NONE, doc.get(), NULL, &error );
	if(!res)
		throw MongoError( std::string(error.message).append(" in Collection::Remove") );
}
Exemple #3
0
SEXP R_mongo_collection_remove(SEXP ptr_col, SEXP ptr_bson, SEXP just_one){
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *b = r2bson(ptr_bson);
  bson_error_t err;
  mongoc_remove_flags_t flags = Rf_asLogical(just_one) ? MONGOC_REMOVE_SINGLE_REMOVE : MONGOC_REMOVE_NONE;

  if(!mongoc_collection_remove(col, flags, b, NULL, &err))
    stop(err.message);

  return Rf_ScalarLogical(1);
}
bool
mongoc_gridfs_file_remove (mongoc_gridfs_file_t *file,
                           bson_error_t         *error)
{
   bson_t sel = BSON_INITIALIZER;
   bool ret = false;

   BSON_ASSERT (file);

   BSON_APPEND_VALUE (&sel, "_id", &file->files_id);

   if (!mongoc_collection_remove (file->gridfs->files,
                                  MONGOC_REMOVE_SINGLE_REMOVE,
                                  &sel,
                                  NULL,
                                  error)) {
      goto cleanup;
   }

   bson_reinit (&sel);
   BSON_APPEND_VALUE (&sel, "files_id", &file->files_id);

   if (!mongoc_collection_remove (file->gridfs->chunks,
                                  MONGOC_REMOVE_NONE,
                                  &sel,
                                  NULL,
                                  error)) {
      goto cleanup;
   }

   ret = true;

cleanup:
   bson_destroy (&sel);

   return ret;
}
Exemple #5
0
/*
 * Delete MongoDB's document.
 */
bool
MongoDelete(MONGO_CONN* conn, char* database, char *collection, BSON* b)
{
	mongoc_collection_t *c = NULL;
	bson_error_t error;
	bool r = false;

	c = mongoc_client_get_collection (conn, database, collection);

	r = mongoc_collection_remove(c, MONGOC_DELETE_SINGLE_REMOVE, b, NULL, &error);
	mongoc_collection_destroy(c);
	if (!r)
		ereport(ERROR, (errmsg("failed to delete row"),
						errhint("Mongo error: \"%s\"", error.message)));
	return true;
}
bool
mongoc_database_remove_user (mongoc_database_t *database,
                             const char        *username,
                             bson_error_t      *error)
{
   mongoc_collection_t *col;
   bson_error_t lerror;
   bson_t cmd;
   bool ret;

   ENTRY;

   bson_return_val_if_fail (database, false);
   bson_return_val_if_fail (username, false);

   bson_init (&cmd);
   BSON_APPEND_UTF8 (&cmd, "dropUser", username);
   ret = mongoc_database_command_simple (database, &cmd, NULL, NULL, &lerror);
   bson_destroy (&cmd);

   if (!ret && (lerror.code == MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND)) {
      bson_init (&cmd);
      BSON_APPEND_UTF8 (&cmd, "user", username);

      col = mongoc_client_get_collection (database->client, database->name,
                                          "system.users");
      BSON_ASSERT (col);

      ret = mongoc_collection_remove (col,
                                      MONGOC_REMOVE_SINGLE_REMOVE,
                                      &cmd,
                                      NULL,
                                      error);

      bson_destroy (&cmd);
      mongoc_collection_destroy (col);
   } else if (error) {
      memcpy (error, &lerror, sizeof *error);
   }

   RETURN (ret);
}
Exemple #7
0
mrb_value mrb_mongo_collection_drop(mrb_state *mrb, mrb_value self) {
  mrb_mongo_collection_data *data = DATA_PTR(self);
  mrb_value drop_hash;
  bson_t *doc;
  bson_error_t error;

  mrb_get_args(mrb, "|H", &drop_hash);
  if (!mrb_hash_p(drop_hash)) {
    drop_hash = mrb_hash_new(mrb);
  }

  doc = bson_new();
  mrb_hash_to_bson(mrb, drop_hash, doc);

  if (!mongoc_collection_remove(data->collection, MONGOC_REMOVE_NONE, doc, NULL, &error)) {
    bson_destroy(doc);
    mrb_raise(mrb, E_RUNTIME_ERROR, error.message);
  }

  return mrb_nil_value();
}
bool TMongoDriver::remove(const QString &collection, const QVariantMap &object)
{
    if (!isOpen()) {
        return false;
    }

    errorCode = 0;
    errorString.clear();
    bson_error_t error;

    mongoc_collection_t *col = mongoc_client_get_collection(mongoClient, qPrintable(dbName), qPrintable(collection));
    bool res = mongoc_collection_remove(col, MONGOC_REMOVE_SINGLE_REMOVE,
                                        (bson_t *)TBson::toBson(object).constData(), nullptr, &error);

    setLastCommandStatus(mongoc_collection_get_last_error(col));
    mongoc_collection_destroy(col);

    if (!res) {
        tSystemError("MongoDB Remove Error: %s", error.message);
        errorCode = error.code;
        errorString = QLatin1String(error.message);
    }
    return res;
}
Exemple #9
0
/*
 * Delete a row from the specified table
 * _h: structure representing database connection
 * _k: key names
 * _o: operators
 * _v: values of the keys that must match
 * _n: number of key=value pairs
 */
int db_mongodb_delete(const db1_con_t* _h, const db_key_t* _k,
		const db_op_t* _o, const db_val_t* _v, const int _n)
{
	int i;
	km_mongodb_con_t *mgcon;
	mongoc_client_t *client;
	mongoc_collection_t *collection = NULL;
	bson_error_t error;
	bson_t *doc = NULL;
	char *cname;
	char *jstr;
	char b1;

	mgcon = MONGODB_CON(_h);
	if(mgcon==NULL || mgcon->id== NULL || mgcon->con==NULL) {
		LM_ERR("connection to server is null\n");
		return -1;
	}
	client = mgcon->con;
	if(CON_TABLE(_h)->s==NULL) {
		LM_ERR("collection (table) name not set\n");
		return -1;
	}
	b1 = '\0';
	if(CON_TABLE(_h)->s[CON_TABLE(_h)->len]!='\0') {
		b1 = CON_TABLE(_h)->s[CON_TABLE(_h)->len];
		CON_TABLE(_h)->s[CON_TABLE(_h)->len] = '\0';
	}
	cname = CON_TABLE(_h)->s;
	collection = mongoc_client_get_collection(client, mgcon->id->database,
			cname);
	if(collection==NULL) {
		LM_ERR("cannot get collection (table): %s\n", cname);
		if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;
		return -1;
	}
	if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;

	doc = bson_new();
	if(doc==NULL) {
		LM_ERR("cannot initialize bson document\n");
		goto error;
	}

	if(_o==NULL) {
		for(i = 0; i < _n; i++) {
			if(db_mongodb_bson_add(doc, _k[i], _v+i, i)<0)
				goto error;
		}
	} else {
		for(i = 0; i < _n; i++) {
			if(!strcmp(_o[i], OP_EQ)) {
				if(db_mongodb_bson_add(doc, _k[i], _v+i, i)<0)
					goto error;
			} else {
				if(db_mongodb_bson_filter_add(doc, _k, _o, _v, i)<0)
					goto error;
			}
		}
	}

	if(is_printable(L_DBG)) {
		jstr = bson_as_json (doc, NULL);
		LM_DBG("delete filter document: %s\n", jstr);
		bson_free (jstr);
	}
	if (!mongoc_collection_remove (collection, MONGOC_REMOVE_NONE,
				doc, NULL, &error)) {
		LM_ERR("failed to delete in collection: %s\n", error.message);
		goto error;
	}
	bson_destroy (doc);
	mongoc_collection_destroy (collection);
	
	return 0;
error:
	if(doc) bson_destroy (doc);
	if(collection) mongoc_collection_destroy (collection);
	return -1;
}
Exemple #10
0
bool dbproxy::remove(std::string collection, mongoc_remove_flags_t flags, const bson_t * selector, const mongoc_write_concern_t * write_concern, bson_error_t * error) {
    mongoc_collection_t * _c = get_collection(collection);
    return mongoc_collection_remove(_c, flags, selector, write_concern, error);
}
Exemple #11
0
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Event Function
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void gps_gateway_process(int x, short int y, void *pargs)
{
    unsigned int unFromAddrLen;
    int nByte = 0;
    int nSendByte = 0;
    unsigned char aReqBuffer[512];
    unsigned char aRespBuffer[512];
    struct sockaddr_in stFromAddr;
    bson_oid_t oid;
    bson_t *doc;
    const bson_t *result;

    bson_t *query;
    int i = 0;
    int command = 0;
    int monitor_type = 0;
    char telephone[10];
    char text_message[240];
    int length;

    char strBaseinfo[10];
    AGPS_info * agps;
    cell_info * cell;

    unFromAddrLen = sizeof(stFromAddr);
    memset(aReqBuffer,0x00,512);
    memset(aRespBuffer,0x00,512);

    if ((nByte = recvfrom(x, aReqBuffer, sizeof(aReqBuffer), 0,
        (struct sockaddr *)&stFromAddr, &unFromAddrLen)) == -1)
    {
        printf("error occured while receivingn");
    }

    //proc();
    data_frame_head *msg = (data_frame_head *) aReqBuffer;

    content_gps_up *g = (content_gps_up *)(aReqBuffer + sizeof(data_frame_head));

    //异或校验

    //异或校验
    unsigned char xor = *(aReqBuffer + nByte - 2);

    if( xor != check_xor(aReqBuffer, nByte - 2))
    {
        return ;//drop down BAD check xor.
    }

    switch (msg->cmd)
    {
        case CMD_TERMINAL_GPS_UP  :


            //解析位置信息,得到经纬度
            fprintf(stderr,"protocal head %x \n",msg->data_head[0] );
            fprintf(stderr,"cmd %x \n",       msg->cmd );
            fprintf(stderr,"length %d \n",   ntohs(msg->length) );
            //fprintf(stderr,"product_id %u\n",get_product_id(msg->terminal_id));
            fprintf(stderr,"product_id %u\n",get_product_id(g->terminal_id));
            fprintf(stderr,"flow_id %x \n",   ntohs(g->flow_id) );
            fprintf(stderr,"data_type %x \n",   g->data_type );
            fprintf(stderr," time " );

            for (  i = 0 ; i< 6 ;i++)
                fprintf(stderr," %x \n",   g->base_info.date_time[i] );
            /////////
            //经纬度
            fprintf(stderr," latitude " );
            for (  i = 0 ; i< 4 ;i++)
                fprintf(stderr,"%x \n",   g->base_info.latitude[i] );
            fprintf(stderr," longitude " );
            for (  i = 0 ; i< 4 ;i++)
                fprintf(stderr," %x \n",   g->base_info.longitude[i] );
            /////////
            fprintf(stderr,"speed %u \n", ntohs(g->base_info.speed));
            fprintf(stderr,"fangxiang %u \n", ntohs(g->base_info.direction));
            fprintf(stderr,"gaodu %u\n", ntohs(g->base_info.high));
            fprintf(stderr,"定位状态 %c\n", g->base_info.pos_station);
            fprintf(stderr,"是否应答 %x\n", g->base_info.need_response);

            if (ntohs(msg->length) > sizeof (content_gps_up)) {
                fprintf(stderr,"含有附带信息\n");
                unsigned char *attach_info = aReqBuffer + sizeof(data_frame_head) + sizeof(content_gps_up);
                fprintf(stderr,"附带信息类型:%x\n",*attach_info);
                fprintf(stderr,"附带信息长度:%d\n",*(attach_info+1));
                
                switch (*(attach_info))
                {
                    case 0x01:
                        break;
                    case 0x02:
                        fprintf(stderr,"基站信息\n");
                        agps = (AGPS_info * ) ( attach_info + 2);
                        cell = (cell_info * ) ( attach_info + 2 + sizeof(AGPS_info));
                        fprintf(stderr,"\n国家编码");

                        fprintf(stderr," %x ", ntohl(agps->country_code) );
                        fprintf(stderr,"\n运营编码");
                        
                        fprintf(stderr," %x ", ntohl(agps->mobile_code) );
                        fprintf(stderr,"\n运基站定位\n");

                        for (  i = 0 ; i< agps->base_count ;i++){
                            fprintf(stderr," 基站 %d 位置区编码 %d\n", i+1, ntohs(cell->lacN) );
                            fprintf(stderr," 基站 %d 小区编码 %d \n", i+1, ntohs(cell->cellN) );
                            fprintf(stderr," 基站 %d 信号强度 %x \n", i+1, cell->signalN );
                            cell++;
                        }
                        break;


                    case 0x0C:
                    case 0x1E:
                    case 0x1F:
                    case 0x28:
                    case 0x40:
                    case 0x41:
                    case 0x42:
                    case 0x43:
                    case 0x44:
                    case 0x45:

                        break;

                }

            }

            if( g->data_type == 0x80 || g->data_type == 0x82 || g->data_type == 0x8E )
            {

                //保存mongodb
                unsigned char timegps[15];
                unsigned char latitude[10];
                unsigned char longitude[10];
                double lat;
                double lng;
                doc = bson_new ();
                bson_oid_init (&oid, NULL);
                BSON_APPEND_OID (doc, "_id", &oid);
                BSON_APPEND_INT32 (doc, "id",get_product_id(g->terminal_id));

                get_gps_time(g->base_info.date_time, timegps );
                
                BSON_APPEND_UTF8 (doc, "timestamp", timegps);
                
                lat = bcd2longitude(g->base_info.latitude, latitude );
                lng = bcd2longitude(g->base_info.longitude, longitude );
               
                BSON_APPEND_UTF8 (doc, "latitude", latitude);
                BSON_APPEND_UTF8 (doc, "longitude", longitude);
                BSON_APPEND_DOUBLE (doc, "lat", lat);
                BSON_APPEND_DOUBLE (doc, "lng", lng);
                BSON_APPEND_INT32 (doc, "speed", ntohs(g->base_info.speed));
                BSON_APPEND_INT32 (doc, "direction", ntohs(g->base_info.direction));
                BSON_APPEND_INT32 (doc, "high", ntohs(g->base_info.high));
                BSON_APPEND_INT32 (doc, "pos_station", g->base_info.pos_station);

               if (ntohs(msg->length) > sizeof (content_gps_up)) {
                    unsigned char *attach_info = aReqBuffer + sizeof(data_frame_head) + sizeof(content_gps_up);
                    
                    switch (*(attach_info))
                    {
                        case 0x01:
                            break;
                        case 0x02:
                            agps = (AGPS_info * ) ( attach_info + 2);
                            cell = (cell_info * ) ( attach_info + 2 + sizeof(AGPS_info));
                            

                            fprintf(stderr,"\n国家编码");

                            fprintf(stderr," %x ", ntohl(agps->country_code) );
                            BSON_APPEND_INT32 (doc, "country_code", ntohl(agps->country_code));

                            fprintf(stderr,"\n运营编码");
                            
                            fprintf(stderr," %x ", ntohl(agps->mobile_code) );
                            BSON_APPEND_INT32 (doc, "mobile_code", ntohl(agps->mobile_code));

                            fprintf(stderr,"\n运基站定位\n");

                            for (  i = 0 ; i< agps->base_count ;i++){
                                fprintf(stderr," 基站 %d 位置区编码 %d\n", i+1, ntohs(cell->lacN) );
                                fprintf(stderr," 基站 %d 小区编码 %d \n", i+1, ntohs(cell->cellN) );
                                fprintf(stderr," 基站 %d 信号强度 %x \n", i+1, cell->signalN );
                                sprintf(strBaseinfo,"lac%d",i);
                                BSON_APPEND_INT32 (doc, strBaseinfo, ntohs(cell->lacN) );
                                sprintf(strBaseinfo,"cell%d",i);
                                BSON_APPEND_INT32 (doc, strBaseinfo, ntohs(cell->cellN) );
                                sprintf(strBaseinfo,"signal%d",i);
                                BSON_APPEND_INT32 (doc, strBaseinfo, cell->signalN );

                                cell++;
                            }
                            break;


                        case 0x0C:
                        case 0x1E:
                        case 0x1F:
                        case 0x28:
                        case 0x40:
                        case 0x41:
                        case 0x42:
                        case 0x43:
                        case 0x44:
                        case 0x45:

                            break;

                    }

                }

                if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
                    printf ("Insert failed: %s\n", error.message);
                }

                bson_destroy (doc);
            }
            else if( g->data_type == 0x85 )
            {

                //delete from mongo
                query = bson_new ();
                BSON_APPEND_INT32 (query, "id",get_product_id(g->terminal_id));

                if( ! mongoc_collection_remove(collection_cmd, MONGOC_DELETE_NONE, query, NULL, &error))
                {
                    fprintf(stderr,"remove command failed \n"   );
                }
                bson_destroy (query);

            }
            
            if (g->base_info.need_response == 0x01)
            {
                send_22_response( x,  aRespBuffer ,  g->flow_id, xor ,  g->data_type,
                          stFromAddr,  unFromAddrLen );
            }
            

            
            break;

        case CMD_TERMINAL_INFO_UP :
            fprintf(stderr,"CMD_TERMINAL_INFO_UP\n");
            content_info_up * t = (content_info_up *)(aReqBuffer + sizeof(data_frame_head));
            fprintf(stderr,"parameter %x \n",   t->parameter );

            if(t->parameter == 0x01)
            {

                sent_time_response( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen );
            }

            break;
        case CMD_VERSION_INFO_UP  :
            fprintf(stderr,"CMD_VERSION_INFO_UP");
            break;
        case CMD_VOICE_UP         :
            fprintf(stderr,"CMD_VOICE_UP");
            break;
        default:
            break;
    }


    //get command from mongo
    char *str;
    query = bson_new ();
    BSON_APPEND_INT32 (query, "id",get_product_id(g->terminal_id));



    cursor = mongoc_collection_find (collection_cmd, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);

    //while (mongoc_cursor_next (cursor, &doc)) {
    //   mongoc_cursor_next (cursor, &result);
    while (mongoc_cursor_next (cursor, (const  bson_t **) &doc)) {
        bson_iter_t iter;
        bson_iter_t sub_iter;
        str = bson_as_json (doc, NULL);
        fprintf (stderr, "%s\n", str);
        bson_free (str);

        if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "cmd", &sub_iter)) {
            fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
            printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter));
            command = (int)bson_iter_int32 (&sub_iter);
        }
        if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "parameter", &sub_iter)) {
            fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
            printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter));
            strcpy (telephone , bson_iter_utf8 (&sub_iter,&length));
        }
        if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "monitor_type", &sub_iter)) {
            fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
            printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter));
            monitor_type = (int)bson_iter_int32 (&sub_iter);
        }

        if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "text_message", &sub_iter)) {
            fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
            printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter));
            strcpy (text_message , bson_iter_utf8 (&sub_iter,&length));
        }
    }
    mongoc_cursor_destroy (cursor);

    bson_destroy (query);

    //send command to handring

    if (command == 1)
    {
        send_monitor_cmd( x,  aRespBuffer , g->terminal_id, stFromAddr,  unFromAddrLen ,monitor_type, telephone);
    }
    else if (command == 9)
    {
        send_sleep_cmd( x,  aRespBuffer , g->terminal_id, stFromAddr,  unFromAddrLen );
    }
    else if (command == 2)
    {
        send_set_cmd( x,  aRespBuffer , g->terminal_id, stFromAddr,  unFromAddrLen ,text_message);
    }
    else{
        //为了避免长时间使用网络。在通信6次发送一个休眠指令
        if( (msg->cmd == CMD_TERMINAL_GPS_UP) && (check_state(g->terminal_id) == MAX_STANDBY) )
        {
            send_sleep_cmd( x,  aRespBuffer , g->terminal_id, stFromAddr,  unFromAddrLen );
        }
    }

    //printf("Function called buffer is %sn",aReqBuffer);
    g_count++;

}