Ejemplo n.º 1
0
Archivo: grid.c Proyecto: Maxime2/ccan
static int grid_edge_info_r(const struct agar_graph *gr,
			    const void *nr, const void *e,
			    struct agar_edge_info *eir)
{
	struct grid_graphr *ggr = container_of(gr, struct grid_graphr, gr);
	int ni = ptr2int(nr);
	int x = ((ni - 1) % ggr->nx) + 1;
	int y = ((ni - 1) / ggr->nx) + 1;
	int i = ptr2int(e);

	assert((x >= 1) && (x <= ggr->nx));
	assert((y >= 1) && (y <= ggr->ny));

	switch (i) {
	case 1: /* right */
		if (ggr->right && (x != ggr->nx))
			eir->to = int2ptr(ni + 1);
		break;

	case 2: /* down */
		if (ggr->down && (y != ggr->ny))
			eir->to = int2ptr(ni + ggr->nx);
		break;
		
	case 3: /* left */
		if (ggr->left && (x != 1))
			eir->to = int2ptr(ni - 1);
		break;

	case 4: /* up */
		if (ggr->up && (y != 1))
			eir->to = int2ptr(ni - ggr->nx);
		break;

	default:
		assert(0);
	}
	return 0;
}
Ejemplo n.º 2
0
int
ht_inc_u64_from_int_key (GHashTable * ht, int data_nkey, uint64_t inc)
{
  int ret, *key;
  if (ht == NULL)
    return (EINVAL);

  key = int2ptr (data_nkey);
  if ((ret = ht_inc_u64_from_key (ht, key, inc)) != 0)
    free (key);

  return ret;
}
Ejemplo n.º 3
0
int
ht_insert_host_agent (TCADB * adb, int data_nkey, int agent_nkey)
{
  GSLList *list, *match;
  int sp = 0;

  if (adb == NULL)
    return (EINVAL);

  if ((list = tcadbget (adb, &data_nkey, sizeof (int), &sp)) != NULL) {
    if ((match = list_find (list, find_host_agent_in_list, &agent_nkey)))
      goto out;
    list = list_insert_prepend (list, int2ptr (agent_nkey));
  } else {
    list = list_create (int2ptr (agent_nkey));
  }
  tcadbput (adb, &data_nkey, sizeof (int), list, sizeof (GSLList));
out:

  free (list);

  return 0;
}
Ejemplo n.º 4
0
static void test_chain(void)
{
	struct chain_graphr cgr;
	int i, j;

	chain_graphr_init(&cgr, CHAIN_LEN);

	for (i = 1; i <= CHAIN_LEN; i++) {
		struct agar_state *sr;

		ok1(sr = agar_dijkstra_new(NULL, &cgr.fgr.gr, int2ptr(i)));

		for (j = 1; j <= CHAIN_LEN; j++) {
			aga_icost_t cost;

			ok1(agar_dijkstra_path(sr, int2ptr(j),
					      &cost, NULL, NULL));
			ok1(cost == labs(i - j));
		}

		tal_free(sr);
	}
}
Ejemplo n.º 5
0
static void test_adjacency(const char *name,
			   const struct agar_graph *gr,
			   const struct adjacency_listr *atr)
{
	int i;

	for (i = 0; atr[i].from != 0; i++) {
		const void *e;
		struct agar_edge_info eir;
		int j = 0;
		int err;
		ptrint_t *from = int2ptr(atr[i].from);

		agar_for_each_edge_info(e, eir, err, gr, from) {
			const void *cmpto;

			assert(j < MAX_EDGES);
			cmpto = int2ptr(atr[i].to[j]);
			ok(cmpto == eir.to,
			   "%s: %p #%d -> #%ld (expected #%d -> #%d)", name, e,
			   atr[i].from, ptr2int(eir.to),
			   atr[i].from, atr[i].to[j]);

			j++;
		}
		if (atr[i].to[j] < 0) {
			ok(err == atr[i].to[j], "%s: %p #%d -> ERROR %d",
			   name, e, atr[i].from, atr[i].to[j]);
			continue; /* Move onto next node on errors */
		}
		assert(j < MAX_EDGES);
		ok(atr[i].to[j] == 0,
		   "%s: %p #%d -> --- (expected #%d -> #%d)", name, e,
		   atr[i].from, atr[i].from, atr[i].to[j]);
	}
}
Ejemplo n.º 6
0
static TDS_THREAD_PROC_DECLARE(signal_proc, arg)
{
	tds_condition *cond = (tds_condition *) arg;

	/* success */
	int res = 0;

	tds_mutex_lock(&mtx);
	if (tds_cond_signal(cond)) {
		/* failure */
		res = 1;
	}
	tds_mutex_unlock(&mtx);
	return int2ptr(res);
}
Ejemplo n.º 7
0
static void test_parallel(void)
{
	struct parallel_graphr pgr;
	struct agar_state *sr;
	aga_icost_t cost;
	const void *node, *edge;

	parallel_graphr_init(&pgr, 3, 0);

	ok1(sr = agar_dijkstra_new(NULL, &pgr.gr, int2ptr(1)));
	ok1(agar_dijkstra_step(sr, &node));
	ok1(ptr2int(node) == 1);
	ok1(agar_dijkstra_step(sr, &node));
	ok1(ptr2int(node) == 2);
	ok1(!agar_dijkstra_step(sr, &node));
	ok1(agar_dijkstra_path(sr, int2ptr(1), &cost, NULL, NULL));
	ok1(cost == 0);
	ok1(agar_dijkstra_path(sr, int2ptr(2), &cost, &node, NULL));
	ok1(cost == 2);
	ok1(node == int2ptr(1));
	tal_free(sr);

	ok1(sr = agar_dijkstra_new(NULL, &pgr.gr, int2ptr(2)));
	ok1(agar_dijkstra_step(sr, &node));
	ok1(ptr2int(node) == 2);
	ok1(!agar_dijkstra_step(sr, &node));
	ok1(agar_dijkstra_path(sr, int2ptr(2), &cost, NULL, NULL));
	ok1(cost == 0);
	ok1(!agar_dijkstra_path(sr, int2ptr(1), NULL, NULL, NULL));
	tal_free(sr);

	parallel_graphr_init(&pgr, 3, 2);
	ok1(sr = agar_dijkstra_new(NULL, &pgr.gr, int2ptr(1)));
	ok1(agar_dijkstra_path(sr, int2ptr(2), &cost, &node, &edge));
	ok1(cost == 1);
	ok1(ptr2int(node) == 1);
	ok1(ptr2int(edge) == 2);
	tal_free(sr);
}
Ejemplo n.º 8
0
static const void *shortcut1_first_edge_r(const struct agar_graph *gr,
        const void *nr)
{
    int ni = ptr2int(nr);

    switch (ni) {
    case 1:
    case 2:
        return int2ptr(1);

    case 3:
        return NULL;

    default:
        assert(0);
    }
}
Ejemplo n.º 9
0
static ptrint_t *traversal1_next_edge(const struct aga_graph *g,
				      const struct aga_node *n,
				      ptrint_t *e)
{
	struct traversal1_graph *t1g = container_of(g, struct traversal1_graph,
						    sg.g);
	int ni = n - t1g->sg.nodes;
	int index = ptr2int(e);

	assert((ni < 4) || (ni > 6));
	if (index == 1)
		return int2ptr(2);
	else if (index == 2)
		return NULL;
	else
		assert(0);
}
Ejemplo n.º 10
0
static void test_error(void)
{
	struct agar_state *sr;
	aga_icost_t cost;

	ok1(sr = agar_dijkstra_new(NULL, &error_graphr.gr, int2ptr(1)));
	ok1(agar_dijkstra_path(sr, int2ptr(1), &cost, NULL, NULL));
	ok1(cost == 0);
	ok1(agar_dijkstra_path(sr, int2ptr(2), &cost, NULL, NULL));
	ok1(cost == 1);
	ok1(!agar_dijkstra_path(sr, int2ptr(3), &cost, NULL, NULL));
	ok1(!agar_dijkstra_path(sr, int2ptr(4), &cost, NULL, NULL));
	tal_free(sr);

	ok1(sr = agar_dijkstra_new(NULL, &error_graphr.gr, int2ptr(3)));
	ok1(agar_dijkstra_path(sr, int2ptr(3), &cost, NULL, NULL));
	ok1(cost == 0);
	ok1(!agar_dijkstra_path(sr, int2ptr(4), &cost, NULL, NULL));
	ok1(agar_error(sr) == -1);
	tal_free(sr);
}
Ejemplo n.º 11
0
int
ht_insert_uniqmap (GHashTable * ht, char *uniq_key)
{
  int nkey = 0, size = 0;

  if (ht == NULL)
    return (EINVAL);

  if ((g_hash_table_lookup (ht, uniq_key)) != NULL)
    return 0;

  size = get_ht_size (ht);
  /* the auto increment value starts at SIZE (hash table) + 1 */
  nkey = size > 0 ? size + 1 : 1;

  g_hash_table_replace (ht, uniq_key, int2ptr (nkey));

  return nkey;
}
Ejemplo n.º 12
0
void queue_pkt_htlc_fulfill(struct peer *peer,
		      const struct htlc_progress *htlc_prog)
{
	UpdateFulfillHtlc *f = tal(peer, UpdateFulfillHtlc);
	size_t n;

	update_fulfill_htlc__init(f);
	assert(htlc_prog->stage.type == HTLC_FULFILL);

	f->id = htlc_prog->stage.fulfill.id;
	f->r = sha256_to_proto(f, &htlc_prog->stage.fulfill.r);

	/* We're about to send this, so their side will have it from now on. */
	n = funding_htlc_by_id(&peer->them.staging_cstate->a, f->id);
	funding_a_fulfill_htlc(peer->them.staging_cstate, n);

	queue_pkt_with_ack(peer, PKT__PKT_UPDATE_FULFILL_HTLC, f,
			   fulfill_their_htlc_ourside, int2ptr(f->id));
}
Ejemplo n.º 13
0
static int
ht_inc_int_from_key (GHashTable * ht, void *key, int inc)
{
  gpointer value_ptr;
  int add_value;

  if (ht == NULL)
    return (EINVAL);

  value_ptr = g_hash_table_lookup (ht, key);
  if (value_ptr != NULL) {
    add_value = (*(int *) value_ptr) + inc;
  } else {
    add_value = 0 + inc;
  }
  g_hash_table_replace (ht, key, int2ptr (add_value));

  return 0;
}
Ejemplo n.º 14
0
void queue_pkt_htlc_fail(struct peer *peer,
		   const struct htlc_progress *htlc_prog)
{
	UpdateFailHtlc *f = tal(peer, UpdateFailHtlc);
	size_t n;

	update_fail_htlc__init(f);
	assert(htlc_prog->stage.type == HTLC_FAIL);

	f->id = htlc_prog->stage.fail.id;
	/* FIXME: reason! */
	f->reason = tal(f, FailReason);
	fail_reason__init(f->reason);

	/* We're about to send this, so their side will have it from now on. */
	n = funding_htlc_by_id(&peer->them.staging_cstate->a, f->id);
	funding_a_fail_htlc(peer->them.staging_cstate, n);

	queue_pkt_with_ack(peer, PKT__PKT_UPDATE_FAIL_HTLC, f,
			   fail_their_htlc_ourside, int2ptr(f->id));
}
Ejemplo n.º 15
0
/* store generic data into the given hash table */
int
ht_insert_hit (GHashTable * ht, int data_nkey, int uniq_nkey, int root_nkey)
{
  GDataMap *map;

  if (ht == NULL)
    return (EINVAL);

  map = g_hash_table_lookup (ht, &data_nkey);
  if (map != NULL) {
    map->data++;
  } else {
    map = xcalloc (1, sizeof (GDataMap));
    map->data = 1;
    map->root = root_nkey;
    map->uniq = uniq_nkey;
  }
  g_hash_table_replace (ht, int2ptr (data_nkey), map);

  return 0;
}
Ejemplo n.º 16
0
static const void *shortcut1_next_edge_r(const struct agar_graph *gr,
        const void *nr, const void *e)
{
    int ni = ptr2int(nr);
    int index = ptr2int(e);

    switch (ni) {
    case 1:
        if (index == 1)
            return int2ptr(2);
        assert(index == 2);
        return NULL;

    case 2:
        assert(index == 1);
        return NULL;

    default:
        assert(0);
    }
}
Ejemplo n.º 17
0
int
ht_insert_keymap (GHashTable * ht, const char *value)
{
  gpointer value_ptr;
  int nkey = 0, size = 0;

  if (ht == NULL)
    return (EINVAL);

  value_ptr = g_hash_table_lookup (ht, value);
  if (value_ptr != NULL)
    return (*(int *) value_ptr);

  size = get_ht_size (ht);
  /* the auto increment value starts at SIZE (hash table) + 1 */
  nkey = size > 0 ? size + 1 : 1;

  g_hash_table_replace (ht, g_strdup (value), int2ptr (nkey));

  return nkey;
}
Ejemplo n.º 18
0
int
ht_max_u64_from_int_key (GHashTable * ht, int data_nkey, uint64_t newval)
{
  gpointer value_ptr;
  int *key;
  uint64_t curval = 0;

  if (ht == NULL)
    return (EINVAL);

  key = int2ptr (data_nkey);
  value_ptr = g_hash_table_lookup (ht, key);
  if (value_ptr != NULL)
    curval = (*(uint64_t *) value_ptr);

  if (curval < newval) {
    g_hash_table_replace (ht, key, uint642ptr (newval));
  } else {
    free (key);
  }

  return 0;
}
Ejemplo n.º 19
0
JNIEXPORT void JNICALL Java_com_tetrark_ganesh_MyRenderer_nativeTouch(JNIEnv*, jobject,
                                      jint id, jint type, jfloat x, jfloat y) {
    get_state()->handleTouch(int2ptr(id), (TouchState)type, x, y);
}
Ejemplo n.º 20
0
int main(int argc, char *argv[])
{
  struct dlist *node, list, *trav;
  struct list list2;
  cat_time_t ct;

  int arr1[] = { 10, 15, 5, 7, 12, 2, 7 },
      arr2[] = { 9, 1, 6, 10, 6 }, 
      s1 = sizeof(arr1) / sizeof(arr1[0]),
      s2 = sizeof(arr2) / sizeof(arr1[0]), i, j = 0;

  printf("Initial: ");
  for ( i = 0 ; i < s1 ; ++i )
    printf("%u/%u ", ++j, arr1[i]);
  printf("  |  ");
  for ( i = 0 ; i < s2 ; ++i )
    printf("%u/%u ", ++j, arr2[i]);
  printf("\n");

  j=0;
  dl_init(&list, tm_zero);
  for ( i = 0 ; i < s1 ; ++i )
  {
    ++j;
    node = cdl_new(tm_lset(arr1[i], 0), int2ptr(j));
    dl_ins(&list, node);
  }

  dl_first(&list, &ct);
  printf("The first is at %u\n", (uint)tm_sec(ct));
  node = dl_deq(&list);
  printf("The first was %u at %u\n\n", (uint)ptr2uint(cdl_data(node)),
         (uint)tm_sec(node->ttl));
  cdl_free(node);

  printf("Nodes from advance 10:  ");
  l_init(&list2);
  dl_adv(&list, tm_lset(10, 0), &list2);

  while ( ! l_isempty(&list2) )
  {
    trav = container(l_head(&list2), struct dlist, entry);
    printf("%u/%u ", (uint)ptr2uint(cdl_data(trav)), (uint)tm_sec(trav->ttl));
    l_rem(&trav->entry);
    cdl_free(trav);
  }
  printf("\n\n");

  for ( i = 0 ; i < s2 ; ++i ) 
  {
    ++j;
    node = cdl_new(tm_lset(arr2[i], 0), int2ptr(j));
    dl_ins(&list, node);
  }

  printf("After inserting arr2 array is :\n\t");
  while ( node = dl_deq(&list) )
  {
    printf("%u/", (uint)ptr2uint(cdl_data(node)));
    printf("%u ", (uint)tm_sec(node->ttl));
    cdl_free(node);
  }
  printf("\n");

  return 0;
}
Ejemplo n.º 21
0
static void
query_test(const char* expected, const char *expected_status)
{
#define ARRAY_SIZE 10

    ODBC_BUF *odbc_buf = NULL;
    SQLUINTEGER *ids;
    SQLCHAR *descs;
    SQLLEN *id_lens, *desc_lens;
    SQLULEN processed;
    SQLUSMALLINT i, statuses[ARRAY_SIZE];
    int desc_len = trunc ? 4 : 51;
    int rec_size = 0;
    Record *rec = NULL;
    char status[20];

    assert(odbc_stmt != SQL_NULL_HSTMT);
    odbc_reset_statement();

    SQLSetStmtAttr(odbc_stmt, SQL_ATTR_ROW_STATUS_PTR, statuses, 0);
    SQLSetStmtAttr(odbc_stmt, SQL_ATTR_ROW_ARRAY_SIZE, (void *) ARRAY_SIZE, 0);
    SQLSetStmtAttr(odbc_stmt, SQL_ATTR_ROWS_FETCHED_PTR, &processed, 0);
    SQLSetStmtAttr(odbc_stmt, SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN, 0);

    if (!record_bind) {
        ids = (SQLUINTEGER *) ODBC_GET(sizeof(SQLUINTEGER) * ARRAY_SIZE);
        descs = ODBC_GET(sizeof(SQLCHAR) * ARRAY_SIZE * desc_len);
        desc_lens = (SQLLEN *) ODBC_GET(sizeof(SQLLEN) * ARRAY_SIZE);
        id_lens = (SQLLEN *) ODBC_GET(sizeof(SQLLEN) * ARRAY_SIZE);
        assert(descs && ids && desc_lens && id_lens);
    } else {
        rec_size = (sizeof(Record) + (sizeof(SQLCHAR) * desc_len + sizeof(SQLLEN) - 1)) & ~(sizeof(SQLLEN) - 1);
        SQLSetStmtAttr(odbc_stmt, SQL_ATTR_ROW_BIND_TYPE, int2ptr(rec_size), 0);
        rec = (Record *) ODBC_GET(rec_size * ARRAY_SIZE);
        ids = &rec->id;
        id_lens = &rec->id_len;
        desc_lens = &rec->desc_len;
        descs = (SQLCHAR *) (((char *) rec) + sizeof(Record));
    }
#define REC(f,n) (((char*)f)+rec_size*(n))
#define DESCS(n) (rec ? (SQLCHAR*)REC(descs,n): (descs+(n)*desc_len))
#define IDS(n) *(rec ? (SQLUINTEGER*)REC(ids,n) : &ids[n])
#define ID_LENS(n) *(rec ? (SQLLEN*)REC(id_lens,n) : &id_lens[n])
#define DESC_LENS(n) *(rec ? (SQLLEN*)REC(desc_lens,n) : &desc_lens[n])

    processed = ARRAY_SIZE + 1;
    for (i = 0; i < ARRAY_SIZE; i++) {
        statuses[i] = SQL_ROW_UPDATED;
        IDS(i) = i * 132;
        sprintf((char *) DESCS(i), "aaa");
        ID_LENS(i) = 0;
        DESC_LENS(i) = -i;
    }

    SQLBindCol(odbc_stmt, 1, SQL_C_ULONG, &IDS(0), 0, &ID_LENS(0));
    SQLBindCol(odbc_stmt, 2, SQL_C_CHAR, DESCS(0), desc_len, &DESC_LENS(0));

    CHKExecDirect(T(test_query), SQL_NTS, "S");

    CHKFetch(expected);

    assert(processed <= ARRAY_SIZE);

    for (i = 0; i < processed; ++i) {
        char buf[128];

        sprintf(buf, "%crow number %d", 'a' + i, i * 13);
        if (trunc)
            buf[3] = 0;
        if (IDS(i) != i + 1 || strcmp((char *) DESCS(i), buf) != 0) {
            fprintf(stderr, "Invalid result\n\tgot '%d|%s'\n\texpected '%d|%s'\n", (int) IDS(i), DESCS(i), i + 1, buf);
            exit(1);
        }

        switch (statuses[i]) {
        case SQL_ROW_SUCCESS:
            status[i] = 'V';
            break;

        case SQL_ROW_SUCCESS_WITH_INFO:
            status[i] = 'v';
            break;

        case SQL_ROW_ERROR:
            status[i] = '!';
            break;

        case SQL_ROW_NOROW:
            status[i] = ' ';
            break;

        default:
            fprintf(stderr, "Invalid status returned\n");
            exit(1);
        }
    }
    status[i] = 0;

    if (expected_status && strcmp(expected_status, status) != 0) {
        fprintf(stderr, "Invalid status\n\tgot '%s'\n\texpected '%s'\n", status, expected_status);
        exit(1);
    }

    ODBC_FREE();
}
Ejemplo n.º 22
0
static void
my_attrs(void)
{
	CHKSetConnectAttr(SQL_ATTR_TXN_ISOLATION, int2ptr(global_txn), 0, "S");
	AutoCommit(SQL_AUTOCOMMIT_OFF);
}
Ejemplo n.º 23
0
static void
test_rows(void)
{
	const rows_set_t *p;
	SQLULEN len;
	SQLUINTEGER *ids = MALLOC_N(SQLUINTEGER,ARRAY_SIZE);
	SQLLEN *id_lens = MALLOC_N(SQLLEN,ARRAY_SIZE);
	unsigned long int h, l;
	unsigned int n;

	for (n = 0; n < ARRAY_SIZE; ++n) {
		ids[n] = n;
		id_lens[n] = 0;
	}

	/* test setting just some test pointers */
	set_ird_params1(int2ptr(0x01020304));
	check_ird_params();
	set_ird_params2(int2ptr(0xabcdef12));
	check_ird_params();

	/* now see results */
	for (p = row_set; ; ++p) {
		const char *test_name = NULL;

		odbc_reset_statement();
		len = 0xdeadbeef;
		len <<= 16;
		len <<= 16;
		len |= 12345678;
		if (*p)
			(*p)(&len);
		check_ird_params();

#if 0
		CHKSetStmtAttr(SQL_ATTR_PARAMSET_SIZE, (void *) int2ptr(ARRAY_SIZE), 0, "S");
		CHKBindParameter(1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 5, 0, ids, 0, id_lens, "S");
#endif

		CHKBindCol(1, SQL_C_ULONG, ids, 0, id_lens, "S");
		if (*p) {
			CHKSetStmtAttr(SQL_ATTR_ROW_ARRAY_SIZE, (void *) int2ptr(ARRAY_SIZE), 0, "S");

			odbc_command("SELECT DISTINCT i FROM #tmp1");
			SQLFetch(odbc_stmt);
			test_name = "SQLSetStmtAttr";
		} else {
			CHKSetStmtAttr(SQL_ROWSET_SIZE, (void *) int2ptr(ARRAY_SIZE), 0, "S");
			odbc_command("SELECT DISTINCT i FROM #tmp1");
			CHKExtendedFetch(SQL_FETCH_NEXT, 0, &len, NULL, "S");
			test_name = "SQLExtendedFetch";
		}
		SQLMoreResults(odbc_stmt);

		l = len;
		len >>= 16;
		h = len >> 16;
		l &= 0xfffffffflu;
		if (h != 0 || l != 2) {
			fprintf(stderr, "Wrong number returned in rows high %lu(0x%lx) low %lu(0x%lx) test %s\n", h, h, l, l, test_name);
			exit(1);
		}

		if (!*p)
			break;
	}

	free(ids);
	free(id_lens);
}
Ejemplo n.º 24
0
int
main(int argc, char *argv[])
{
#define TEST_FILE "describecol.in"
    const char *in_file = FREETDS_SRCDIR "/" TEST_FILE;
    FILE *f;
    char buf[256];
    SQLINTEGER i;
    SQLLEN len;
    check_attr_t check_attr_p = check_attr_none;

    odbc_connect();
    odbc_command("SET TEXTSIZE 4096");

    SQLBindCol(odbc_stmt, 1, SQL_C_SLONG, &i, sizeof(i), &len);

    f = fopen(in_file, "r");
    if (!f)
        fopen(TEST_FILE, "r");
    if (!f) {
        fprintf(stderr, "error opening test file\n");
        exit(1);
    }

    line_num = 0;
    while (fgets(buf, sizeof(buf), f)) {
        char *p = buf, *cmd;

        ++line_num;

        while (isspace((unsigned char) *p))
            ++p;
        cmd = strtok(p, SEP);

        /* skip comments */
        if (!cmd || cmd[0] == '#' || cmd[0] == 0 || cmd[0] == '\n')
            continue;

        if (strcmp(cmd, "odbc") == 0) {
            int odbc3 = get_int(strtok(NULL, SEP)) == 3 ? 1 : 0;

            if (odbc_use_version3 != odbc3) {
                odbc_use_version3 = odbc3;
                odbc_disconnect();
                odbc_connect();
                odbc_command("SET TEXTSIZE 4096");
                SQLBindCol(odbc_stmt, 1, SQL_C_SLONG, &i, sizeof(i), &len);
            }
        }

        /* select type */
        if (strcmp(cmd, "select") == 0) {
            const char *type = strtok(NULL, SEP);
            const char *value = strtok(NULL, SEP);
            char sql[sizeof(buf) + 40];

            SQLMoreResults(odbc_stmt);
            odbc_reset_statement();

            sprintf(sql, "SELECT CONVERT(%s, %s) AS col", type, value);

            /* ignore error, we only need precision of known types */
            check_attr_p = check_attr_none;
            if (odbc_command_with_result(odbc_stmt, sql) != SQL_SUCCESS) {
                odbc_reset_statement();
                SQLBindCol(odbc_stmt, 1, SQL_C_SLONG, &i, sizeof(i), &len);
                continue;
            }

            CHKFetch("SI");
            SQLBindCol(odbc_stmt, 1, SQL_C_SLONG, &i, sizeof(i), &len);
            check_attr_p = check_attr_ird;
        }

        /* set attribute */
        if (strcmp(cmd, "set") == 0) {
            const struct attribute *attr = lookup_attr(strtok(NULL, SEP));
            char *value = strtok(NULL, SEP);
            SQLHDESC desc;
            SQLRETURN ret;
            SQLINTEGER ind;

            if (!value)
                fatal("Line %u: value not defined\n", line_num);

            /* get ARD */
            SQLGetStmtAttr(odbc_stmt, SQL_ATTR_APP_ROW_DESC, &desc, sizeof(desc), &ind);

            ret = SQL_ERROR;
            switch (attr->type) {
            case type_INTEGER:
                ret = SQLSetDescField(desc, 1, attr->value, int2ptr(lookup(value, attr->lookup)),
                                      sizeof(SQLINTEGER));
                break;
            case type_SMALLINT:
                ret = SQLSetDescField(desc, 1, attr->value, int2ptr(lookup(value, attr->lookup)),
                                      sizeof(SQLSMALLINT));
                break;
            case type_LEN:
                ret = SQLSetDescField(desc, 1, attr->value, int2ptr(lookup(value, attr->lookup)),
                                      sizeof(SQLLEN));
                break;
            case type_CHARP:
                ret = SQLSetDescField(desc, 1, attr->value, (SQLPOINTER) value, SQL_NTS);
                break;
            }
            if (!SQL_SUCCEEDED(ret))
                fatal("Line %u: failure not expected setting ARD attribute\n", line_num);
            check_attr_p = check_attr_ard;
        }

        /* test attribute */
        if (strcmp(cmd, "attr") == 0) {
            const struct attribute *attr = lookup_attr(strtok(NULL, SEP));
            char *expected = strtok(NULL, SEP);

            if (!expected)
                fatal("Line %u: value not defined\n", line_num);

            check_attr_p(attr, expected);
        }
    }

    fclose(f);
    odbc_disconnect();

    printf("Done.\n");
    return g_result;
}
Ejemplo n.º 25
0
int
main(int argc, char *argv[])
{
	int i;
	SQLLEN len;
#ifdef HAVE_SQLROWSETSIZE
	SQLROWSETSIZE row_count;
#else
	SQLULEN row_count;
#endif
	SQLUSMALLINT statuses[10];
	char buf[32];

	odbc_use_version3 = 1;
	odbc_connect();

	/* initial value should be 1 */
	CHKGetStmtAttr(SQL_ROWSET_SIZE, &len, sizeof(len), NULL, "S");
	if (len != 1) {
		fprintf(stderr, "len should be 1\n");
		odbc_disconnect();
		return 1;
	}

	/* check invalid parameter values */
	test_err(-123);
	test_err(-1);
	test_err(0);

	odbc_check_cursor();

	/* set some correct values */
	CHKSetStmtAttr(SQL_ROWSET_SIZE, (SQLPOINTER) int2ptr(2), 0, "S");
	CHKSetStmtAttr(SQL_ROWSET_SIZE, (SQLPOINTER) int2ptr(1), 0, "S");

	/* now check that SQLExtendedFetch works as expected */
	odbc_command("CREATE TABLE #rowset(n INTEGER, c VARCHAR(20))");
	for (i = 0; i < 10; ++i) {
		char s[10];
		char sql[128];

		memset(s, 'a' + i, 9);
		s[9] = 0;
		sprintf(sql, "INSERT INTO #rowset(n,c) VALUES(%d,'%s')", i+1, s);
		odbc_command(sql);
	}

	odbc_reset_statement();
	CHKSetStmtOption(SQL_ATTR_CURSOR_TYPE, SQL_CURSOR_DYNAMIC, "S");
	CHKExecDirect((SQLCHAR *) "SELECT * FROM #rowset ORDER BY n", SQL_NTS, "SI");

	CHKBindCol(2, SQL_C_CHAR, buf, sizeof(buf), &len, "S");

	row_count = 0xdeadbeef;
	memset(statuses, 0x55, sizeof(statuses));
	CHKExtendedFetch(SQL_FETCH_NEXT, 1, &row_count, statuses, "S");

	if (row_count != 1 || statuses[0] != SQL_ROW_SUCCESS || strcmp(buf, "aaaaaaaaa") != 0) {
		fprintf(stderr, "Invalid result\n");
		odbc_disconnect();
		return 1;
	}

	odbc_disconnect();

	printf("Done.\n");
	return 0;
}
Ejemplo n.º 26
0
int
main(int argc, char **argv)
{
	int i;
	pthread_t th[NUM_THREAD];
	DBPROCESS *dbproc;
	LOGINREC *login;

	read_login_info(argc, argv);

	fprintf(stdout, "Start\n");

	dbinit();

	dberrhandle(syb_err_handler);
	dbmsghandle(syb_msg_handler);

	fprintf(stdout, "About to logon\n");

	login = dblogin();
	DBSETLPWD(login, PASSWORD);
	DBSETLUSER(login, USER);
	DBSETLAPP(login, "thread");

	fprintf(stdout, "About to open \"%s\"\n", SERVER);

	dbproc = dbopen(login, SERVER);
	if (!dbproc) {
		fprintf(stderr, "Unable to connect to %s\n", SERVER);
		return 1;
	}

	dbloginfree(login);

	if (strlen(DATABASE))
		dbuse(dbproc, DATABASE);

	fprintf(stdout, "Dropping table\n");
	dbcmd(dbproc, "if object_id('dblib_thread') is not null drop table dblib_thread");
	dbsqlexec(dbproc);
	while (dbresults(dbproc) == SUCCEED) {
		/* nop */
	}

	fprintf(stdout, "creating table\n");
	dbcmd(dbproc, "create table dblib_thread (i int not null, s char(10) not null)");
	dbsqlexec(dbproc);
	while (dbresults(dbproc) == SUCCEED) {
		/* nop */
	}

	fprintf(stdout, "insert\n");
	for (i = 0; i < ROWS; i++) {
		char cmd[128];

		sprintf(cmd, "insert into dblib_thread values (%d, 'row %d')", i, i);
		dbcmd(dbproc, cmd);
		dbsqlexec(dbproc);
		while (dbresults(dbproc) == SUCCEED) {
			/* nop */
		}
	}

	for (i = 0; i < NUM_THREAD; ++i) {
		int err = pthread_create(&th[i], NULL, thread_test, int2ptr(i));
		if (err != 0)
		{
			fprintf(stderr, "Error %d (%s) creating thread\n", err, strerror(err));
			return 1;
		}
		/* MSSQL rejects the connections if they come in too fast */
		sleep(1);
	}

	for (i = 0; i < NUM_THREAD; ++i) {
		pthread_join(th[i], NULL);
		fprintf(stdout, "thread: %d exited\n", i + 1);
	}

	fprintf(stdout, "Dropping table\n");
	dbcmd(dbproc, "drop table dblib_thread");
	dbsqlexec(dbproc);
	while (dbresults(dbproc) == SUCCEED) {
		/* nop */
	}

	dbexit();

	return result;
}
Ejemplo n.º 27
0
static ptrint_t *lazytrie_first_edge(const struct aga_graph *g,
				     const struct aga_node *n)
{
	return int2ptr(1);
}
Ejemplo n.º 28
0
int
main(int argc, char *argv[])
{
#define TEST_FILE "attributes.in"
	const char *in_file = FREETDS_SRCDIR "/" TEST_FILE;
	FILE *f;
	char buf[256];
	SQLINTEGER i;
	SQLLEN len;
	get_attr_t get_attr_p = get_attr_stmt;

	odbc_connect();
	/* TODO find another way */
	odbc_check_cursor();
	odbc_command("SET TEXTSIZE 4096");

	SQLBindCol(odbc_stmt, 1, SQL_C_SLONG, &i, sizeof(i), &len);

	f = fopen(in_file, "r");
	if (!f)
		f = fopen(TEST_FILE, "r");
	if (!f) {
		fprintf(stderr, "error opening test file\n");
		exit(1);
	}

	line_num = 0;
	while (fgets(buf, sizeof(buf), f)) {
		char *p = buf, *cmd;

		++line_num;

		while (isspace((unsigned char) *p))
			++p;
		cmd = strtok(p, SEP);

		/* skip comments */
		if (!cmd || cmd[0] == '#' || cmd[0] == 0 || cmd[0] == '\n')
			continue;

		if (strcmp(cmd, "odbc") == 0) {
			int odbc3 = get_int(strtok(NULL, SEP)) == 3 ? 1 : 0;

			if (odbc_use_version3 != odbc3) {
				odbc_use_version3 = odbc3;
				odbc_disconnect();
				odbc_connect();
				odbc_command("SET TEXTSIZE 4096");
				SQLBindCol(odbc_stmt, 1, SQL_C_SLONG, &i, sizeof(i), &len);
			}
			continue;
		}

		/* set attribute */
		if (strcmp(cmd, "set") == 0) {
			const struct attribute *attr = lookup_attr(strtok(NULL, SEP));
			char *value = strtok(NULL, SEP);
			SQLRETURN ret;

			if (!value)
				fatal("Line %u: value not defined\n", line_num);

			ret = SQL_ERROR;
			switch (attr->type) {
			case type_UINTEGER:
			case type_INTEGER:
				ret = SQLSetStmtAttr(odbc_stmt, attr->value, int2ptr(lookup(value, attr->lookup)),
						      sizeof(SQLINTEGER));
				break;
			case type_SMALLINT:
				ret = SQLSetStmtAttr(odbc_stmt, attr->value, int2ptr(lookup(value, attr->lookup)),
						      sizeof(SQLSMALLINT));
				break;
			case type_LEN:
				ret = SQLSetStmtAttr(odbc_stmt, attr->value, int2ptr(lookup(value, attr->lookup)),
						      sizeof(SQLLEN));
				break;
			case type_CHARP:
				ret = SQLSetStmtAttr(odbc_stmt, attr->value, (SQLPOINTER) value, SQL_NTS);
				break;
			case type_VOIDP:
			case type_DESC:
				fatal("Line %u: not implemented\n");
			}
			if (!SQL_SUCCEEDED(ret))
				fatal("Line %u: failure not expected setting statement attribute\n", line_num);
			get_attr_p = get_attr_stmt;
			continue;
		}

		/* test attribute */
		if (strcmp(cmd, "attr") == 0) {
			const struct attribute *attr = lookup_attr(strtok(NULL, SEP));
			char *value = strtok(NULL, SEP);
			int i, expected = lookup(value, attr->lookup);

			if (!value)
				fatal("Line %u: value not defined\n", line_num);

			i = get_attr_p(attr, expected);
			if (i != expected) {
				g_result = 1;
				fprintf(stderr, "Line %u: invalid %s got %d(%s) expected %s\n", line_num, attr->name, i, unlookup(i, attr->lookup), value);
			}
			continue;
		}

		if (strcmp(cmd, "reset") == 0) {
			odbc_reset_statement();
			continue;
		}

		fatal("Line %u: command '%s' not handled\n", line_num, cmd);
	}

	fclose(f);
	odbc_disconnect();

	printf("Done.\n");
	return g_result;
}
Ejemplo n.º 29
0
static void
AutoCommit(int onoff)
{
	CHKSetConnectAttr(SQL_ATTR_AUTOCOMMIT, int2ptr(onoff), 0, "S");
}
Ejemplo n.º 30
0
int
main(int argc, char *argv[])
{
	odbc_use_version3 = 1;
	odbc_connect();

	/* Invalid argument value */
	CHKSetConnectAttr(SQL_ATTR_TXN_ISOLATION, int2ptr(SQL_TXN_REPEATABLE_READ | SQL_TXN_READ_COMMITTED), 0, "E");
	ReadErrorConn();
	if (strcmp(odbc_sqlstate, "HY024") != 0) {
		odbc_disconnect();
		fprintf(stderr, "Unexpected success\n");
		return 1;
	}

	/* here we can't use temporary table cause we use two connection */
	odbc_command("IF OBJECT_ID('test_transaction') IS NOT NULL DROP TABLE test_transaction");
	odbc_command("CREATE TABLE test_transaction(n NUMERIC(18,0) PRIMARY KEY, t VARCHAR(30))");

	CHKSetStmtAttr(SQL_ATTR_QUERY_TIMEOUT, (SQLPOINTER) 2, 0, "S");

	AutoCommit(SQL_AUTOCOMMIT_OFF);
	odbc_command("INSERT INTO test_transaction(n, t) VALUES(1, 'initial')");

#ifdef ENABLE_DEVELOPING
	/* test setting with active transaction "Operation invalid at this time" */
	CHKSetConnectAttr(SQL_ATTR_TXN_ISOLATION, int2ptr(SQL_TXN_REPEATABLE_READ), 0, "E");
	ReadErrorConn();
	if (strcmp(odbc_sqlstate, "HY011") != 0) {
		odbc_disconnect();
		fprintf(stderr, "Unexpected success\n");
		return 1;
	}
#endif

	EndTransaction(SQL_COMMIT);

	odbc_command("SELECT * FROM test_transaction");

	/* test setting with pending data */
	CHKSetConnectAttr(SQL_ATTR_TXN_ISOLATION, int2ptr(SQL_TXN_REPEATABLE_READ), 0, "E");
	ReadErrorConn();
	if (strcmp(odbc_sqlstate, "HY011") != 0) {
		odbc_disconnect();
		fprintf(stderr, "Unexpected success\n");
		return 1;
	}

	SQLMoreResults(odbc_stmt);

	EndTransaction(SQL_COMMIT);


	/* save this connection and do another */
	SWAP_CONN();

	odbc_connect();

	CHKSetStmtAttr(SQL_ATTR_QUERY_TIMEOUT, (SQLPOINTER) 2, 0, "S");
	AutoCommit(SQL_AUTOCOMMIT_OFF);

	SWAP_CONN();

	for (test_with_connect = 0; test_with_connect <= 1; ++test_with_connect) {
		Test(SQL_TXN_READ_UNCOMMITTED, "dirty 1 non repeatable 1 phantom 1");
		Test(SQL_TXN_READ_COMMITTED, "dirty 0 non repeatable 1 phantom 1");
		if (odbc_db_is_microsoft()) {
			Test(SQL_TXN_REPEATABLE_READ, "dirty 0 non repeatable 0 phantom 1");
		} else {
			hide_error = 1;
			if (!Test(SQL_TXN_REPEATABLE_READ, "dirty 0 non repeatable 0 phantom 1"))
				Test(SQL_TXN_REPEATABLE_READ, "dirty 0 non repeatable 0 phantom 0");
		}
		Test(SQL_TXN_SERIALIZABLE, "dirty 0 non repeatable 0 phantom 0");
	}

	odbc_disconnect();

	SWAP_CONN();

	EndTransaction(SQL_COMMIT);

	/* Sybase do not accept DROP TABLE during a transaction */
	AutoCommit(SQL_AUTOCOMMIT_ON);
	odbc_command("DROP TABLE test_transaction");

	odbc_disconnect();
	return 0;
}