Exemplo n.º 1
0
void
load_statement(sqlite3_stmt *stmt, Row_t *row, Table_t *table)
{
	guint32 i, max;

	if (table->header.list.count <= 0) /* Lazy header loading */
		load_table_header(stmt, table);

	if (!row->fields) /* Lazy memory allocation */
		row->fields = calloc(1, sizeof(struct RowFieldSequence));

	for (i=0,max=sqlite3_data_count(stmt); i<max ;i++) {
		struct RowField *rf = calloc(1, sizeof(*rf));
		asn_uint32_to_INTEGER(&(rf->pos), i);
		rf->value.present = RowFieldValue_PR_n;

		switch (sqlite3_column_type(stmt, i)) {
			case SQLITE_NULL:
				rf->value.present = RowFieldValue_PR_n;
				break;
			case SQLITE_INTEGER:
				do {
					gint64 i64 = sqlite3_column_int64(stmt, i);
					asn_int64_to_INTEGER(&(rf->value.choice.i), i64);
					rf->value.present = RowFieldValue_PR_i;
				} while (0);
				break;
			case SQLITE_FLOAT:
				do {
					gdouble d = sqlite3_column_double(stmt, i);
					asn_double2REAL(&(rf->value.choice.f), d);
					rf->value.present = RowFieldValue_PR_f;
				} while (0);
				break;
			case SQLITE_TEXT:
				do {
					const guint8 *t = sqlite3_column_text(stmt, i);
					gsize tsize = sqlite3_column_bytes(stmt, i);
					OCTET_STRING_fromBuf(&(rf->value.choice.s), (char*)t, tsize);
					rf->value.present = RowFieldValue_PR_s;
				} while (0);
				break;
			case SQLITE_BLOB:
				do {
					const void *b = sqlite3_column_blob(stmt, i);
					gsize bsize = sqlite3_column_bytes(stmt, i);
					OCTET_STRING_fromBuf(&(rf->value.choice.b), (char*)b, bsize);
					rf->value.present = RowFieldValue_PR_b;
				} while (0);
				break;
			default:
				rf->value.present = RowFieldValue_PR_n;
				break;
		}
		asn_sequence_add(&(row->fields->list), rf);
	}
}
Exemplo n.º 2
0
static void
load_table_header(sqlite3_stmt *stmt, Table_t *t)
{
	guint32 i, max;

	for (i=0,max=sqlite3_data_count(stmt); i<max ;i++) {
		const char *cname = sqlite3_column_name(stmt, i);
		struct RowName *rname = calloc(1, sizeof(*rname));
		asn_uint32_to_INTEGER(&(rname->pos), i);
		OCTET_STRING_fromBuf(&(rname->name), cname, strlen(cname));
		asn_sequence_add(&(t->header.list), rname);
	}
}
Exemplo n.º 3
0
std::vector<uint8_t> Buf1() {
  std::vector<uint8_t> buf(DEFAULT_BUF_SIZE);
  SequenceRecord_I_IO_IRef_t val1;
  memset(&val1, 0, sizeof(val1));
  val1.type = 1;
  val1.type3 = 3;
  SequenceRecord_I_IO_IRef_t val2;
  memset(&val2, 0, sizeof(val2));
  val2.type = -1;
  val2.type3 = -3;
  SequenceOfSequenceRecord_I_IO_I_t rec;
  memset(&rec, 0, sizeof(rec));
  asn_sequence_add(&rec.list, &val1);
  asn_sequence_add(&rec.list, &val2);
  asn_enc_rval_t rval;
  rval = der_encode_to_buffer(&asn_DEF_SequenceOfSequenceRecord_I_IO_I, &rec, (void*)buf.data(), buf.size());
  if (rval.encoded >= 0) {
    buf.resize(rval.encoded);
  } else {
    printf("cannot encode record in Buf1");
    exit(0);
  }
  return std::move(buf);
}
Exemplo n.º 4
0
GByteArray*
sqlx_pack_QUERY_single(struct sqlx_name_s *name, const gchar *query,
		gboolean autocreate)
{
	EXTRA_ASSERT(name != NULL);
	EXTRA_ASSERT(query != NULL);

	struct Table *t = calloc(1, sizeof(Table_t));
	OCTET_STRING_fromBuf(&(t->name), query, strlen(query));

	struct TableSequence *ts = calloc(1, sizeof(TableSequence_t));
	asn_sequence_add(&(ts->list), t);

	GByteArray *req = sqlx_pack_QUERY(name, query, ts, autocreate);
	asn_DEF_TableSequence.free_struct(&asn_DEF_TableSequence, ts, FALSE);
	return req;
}
Exemplo n.º 5
0
GByteArray*
version_encode(GTree *t)
{
	asn_enc_rval_t rv;
	GByteArray *encoded;
	struct BaseVersion bv;

	gboolean runner(gpointer _k, gpointer _v, gpointer _u) {
		(void) _u;
		if (_k && _v && hashstr_len(_k) > 0) {
			struct object_version_s *v = _v;
			struct TableVersion *tv = g_malloc0(sizeof(*tv));
			OCTET_STRING_fromBuf(&(tv->name), hashstr_str(_k), hashstr_len(_k));
			asn_int64_to_INTEGER(&(tv->version), v->version);
			asn_int64_to_INTEGER(&(tv->when), v->when);
			asn_sequence_add(&(bv.list), tv);
		}
		return FALSE;
	}
Exemplo n.º 6
0
GByteArray*
sqlx_pack_QUERY_single(struct sqlx_name_s *name, const gchar *query,
		gboolean autocreate)
{
	EXTRA_ASSERT(name != NULL);
	EXTRA_ASSERT(query != NULL);

	MESSAGE req = make_request(NAME_MSGNAME_SQLX_QUERY, name);
	guint8 ac = (guint8) autocreate;
	do {
		Table_t *t = g_malloc0(sizeof(Table_t));
		OCTET_STRING_fromBuf(&(t->name), query, strlen(query));

		TableSequence_t *ts = g_malloc0(sizeof(TableSequence_t));
		asn_sequence_add(&(ts->list), t);

		metautils_message_add_body_unref(req, sqlx_encode_TableSequence(ts, NULL));
		metautils_message_add_field(req, NAME_MSGKEY_AUTOCREATE, &ac, 1);

		asn_DEF_TableSequence.free_struct(&asn_DEF_TableSequence, ts, FALSE);
	} while (0);

	return message_marshall_gba_and_clean(req);
}
Exemplo n.º 7
0
GByteArray*
sqlx_pack_QUERY_single(struct sqlxsrv_name_s *name, const gchar *query,
		gboolean autocreate)
{
	struct message_s *req = NULL;
	guint8 ac = (guint8) autocreate;

	EXTRA_ASSERT(name != NULL);
	EXTRA_ASSERT(query != NULL);

	req = make_srv_request("SQLX_QUERY", name);
	g_assert(req != NULL);

	do {
		Table_t *t;
		TableSequence_t *ts;
		GByteArray *body;

		t = g_malloc0(sizeof(Table_t));
		g_assert(t != NULL);
		ts = g_malloc0(sizeof(TableSequence_t));
		g_assert(ts != NULL);

		OCTET_STRING_fromBuf(&(t->name), query, strlen(query));
		asn_sequence_add(&(ts->list), t);
		body = sqlx_encode_TableSequence(ts, NULL);
		g_assert(body != NULL);
		message_set_BODY(req, body->data, body->len, NULL);
		message_add_field(req, "AUTOCREATE", 10, &ac, 1, NULL);

		asn_DEF_TableSequence.free_struct(&asn_DEF_TableSequence, ts, FALSE);
		g_byte_array_free(body, TRUE);
	} while (0);

	return message_marshall_gba_and_clean(req);
}
static int libbdm_process_resourceDatapointsQuery(
        ResourceDatapointsQuery_t *rdpq,
        asn_app_consume_bytes_f *consume_cb, void *consume_data)
{
    GPtrArray *bdm_list;
    struct timeval datapoint_start, datapoint_end;
    int entry_start, entry_end;
    int r, bi;
    int ret = -1;

    BDM_S2C_Message_t reply;
    ResourceDatapointsReply_t *rdpr;
    asn_enc_rval_t asn_r;


    g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "    Resource Datapoints Query {");
    g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "        Resource Name Pattern: %s.%s.%s:%s", rdpq->habType.buf, rdpq->habId.buf, rdpq->nodeId.buf, rdpq->resourceId.buf);
    g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "        datapointStartTime=%s", rdpq->datapointStartTime.buf);
    g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "        datapointEndTime=%s", rdpq->datapointEndTime.buf);
    g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "        entryStart=%ld", rdpq->entryStart);
    g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "        entryEnd=%ld", rdpq->entryEnd);
    g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "    }");

    r = bionet_GeneralizedTime_to_timeval(&rdpq->datapointStartTime, &datapoint_start);
    if (r != 0) {
        g_log(
            BDM_LOG_DOMAIN,
            G_LOG_LEVEL_WARNING,
            "%s(): error converting GeneralizedTime '%s' to struct timeval: %s",
            __FUNCTION__,
            rdpq->datapointStartTime.buf,
            strerror(errno)
        );
        return -1;  // FIXME: return an error message to the client
    }

    r = bionet_GeneralizedTime_to_timeval(&rdpq->datapointEndTime, &datapoint_end);
    if (r != 0) {
        g_log(
            BDM_LOG_DOMAIN,
            G_LOG_LEVEL_WARNING,
            "%s(): error converting GeneralizedTime '%s' to struct timeval: %s", __FUNCTION__,
            rdpq->datapointEndTime.buf,
            strerror(errno)
        );
        return -1;  // FIXME: return an error message to the client
    }

    entry_start = rdpq->entryStart;

    entry_end = rdpq->entryEnd;
    if (entry_end == -1) {
	entry_end = db_get_latest_entry_seq(main_db);
    }

    // do the database lookup
    bdm_list = db_get_bdmlist(main_db, NULL, 
                                          (const char *)rdpq->habType.buf, 
					  (const char *)rdpq->habId.buf, 
					  (const char *)rdpq->nodeId.buf, 
					  (const char *)rdpq->resourceId.buf, 
                                          NULL, NULL,
					  entry_start, entry_end);
    if (NULL == bdm_list) {
	g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_ERROR,
	      "Failed to get a BDM list.");
	return -1;
    }

    memset(&reply, 0x00, sizeof(BDM_S2C_Message_t));
    reply.present = BDM_S2C_Message_PR_resourceDatapointsReply;
    rdpr = &reply.choice.resourceDatapointsReply;

    // build the reply message
    for (bi = 0; bi < bdm_list->len; bi++) {
        DataManager_t * asn_bdm;
	bionet_bdm_t * bdm = g_ptr_array_index(bdm_list, bi);
	if (NULL == bdm) {
	    g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
		  "Failed to get BDM %d from BDM list", bi);
	    goto cleanup;
	}

        //add the BDM to the message
        asn_bdm = bionet_bdm_to_asn(bdm);

        r = asn_sequence_add(&rdpr->bdms.list, asn_bdm);
        if (r != 0) {
            g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_WARNING, 
                  "sync_send_metadata(): error adding BDM to Sync Metadata: %s", strerror(errno));
            goto cleanup;
        }
    } //for (bi = 0; bi < bdm_list->len; bi++)

    //
    // Encode ASN message and send to requesting peer
    //
    asn_r = der_encode(&asn_DEF_BDM_S2C_Message, &reply, consume_cb, consume_data);
    if (asn_r.encoded == -1) {
        g_log(BDM_LOG_DOMAIN, G_LOG_LEVEL_WARNING, 
                "bdm_get_resource_datapoints(): error with der_encode(): %s", strerror(errno));
        goto cleanup;
    }

    ret = 0;


cleanup:
    if(bdm_list){
        bdm_list_free(bdm_list);
    }
    ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_BDM_S2C_Message, &reply);
    return ret;
}