int main(int argc, char* argv[]) { CassCluster* cluster = NULL; CassSession* session = cass_session_new(); CassFuture* close_future = NULL; char* hosts = "127.0.0.1"; if (argc > 1) { hosts = argv[1]; } cluster = create_cluster(hosts); if (connect_session(session, cluster) != CASS_OK) { cass_cluster_free(cluster); cass_session_free(session); return -1; } execute_query(session, "CREATE KEYSPACE examples WITH replication = { \ 'class': 'SimpleStrategy', 'replication_factor': '3' };"); execute_query(session, "CREATE TABLE examples.date_time (key text PRIMARY KEY, d date, t time)"); insert_into(session, "test"); select_from(session, "test"); close_future = cass_session_close(session); cass_future_wait(close_future); cass_future_free(close_future); cass_cluster_free(cluster); cass_session_free(session); return 0; }
static int select_and_print(DB * db, const char *tname, col_t * cols, int ncol, cond_t * conds, int ncond) { table_t *t; cursor_t *cur; record_t *r; int count; int ret = 0; int error, xerror; if ((t = db_find_table(db, tname)) == NULL) { xerrno = ERR_NOTABLE; return -1; } for (int i = 0; i < ncol; i++) { int k; if ((k = table_find_col(t, cols[i].name)) < 0) { xerrno = ERR_NOCOL; return -1; } cols[i].type = t->cols[k].type; cols[i].size = t->cols[k].size; } if (ncol == 0) { ncol = t->ncols; cols = t->cols; } int *width; if ((width = calloc(ncol, sizeof(int))) == NULL) { perror("out of memory"); return -1; } for (int i = 0; i < ncol; i++) { int k = table_find_col(t, cols[i].name); switch (t->cols[k].type) { case TYPE_INT: width[i] = _max(INT_P_WIDTH, strlen(t->cols[k].name) + 1); break; case TYPE_FLOAT: width[i] = _max(FLOAT_P_WIDTH, strlen(t->cols[k].name) + 1); break; case TYPE_STRING: width[i] = _max(t->cols[k].size, strlen(t->cols[k].name) + 1); break; } } if ((cur = select_from(db, tname, conds, ncond)) == NULL) { preserve_errno(free(width)); return -1; } print_thead(cols, ncol, width); count = 0; while ((r = select_next(db, cur)) != NULL) { print_record(t, cols, ncol, r, width); free_record(r); count++; } if (select_error(cur)) { error = errno; xerror = xerrno; ret = -1; } printf("Total %d %s\n\n", count, count > 1 ? "records" : "record"); free_cursor(cur); free(width); errno = error; xerrno = xerror; return ret; }