Created
May 30, 2018 16:49
-
-
Save librehat/89f4f7068fc3b824ffceb8aaecfaf2b6 to your computer and use it in GitHub Desktop.
Bind datetime param
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cdb2api.h> | |
#include <stdbool.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <strings.h> | |
#include <time.h> | |
void print_datetime(const cdb2_client_datetime_t* src) | |
{ | |
printf("%d-%d-%dT%d:%d:%d (%s)", | |
src->tm.tm_year + 1900, src->tm.tm_mon + 1, src->tm.tm_mday, | |
src->tm.tm_hour, src->tm.tm_min, src->tm.tm_sec, | |
src->tzname); | |
} | |
void print_cdb2_value(const void *src, int type) | |
{ | |
switch (type) { | |
case CDB2_INTEGER: | |
printf("%ld", *(int64_t*)src); | |
break; | |
case CDB2_REAL: | |
printf("%e", *(double*)src); | |
break; | |
case CDB2_CSTRING: | |
printf("%s", (char*)src); | |
break; | |
case CDB2_BLOB: | |
printf("<BLOB>"); | |
break; | |
case CDB2_DATETIME: | |
print_datetime((cdb2_client_datetime_t*)src); | |
break; | |
} | |
} | |
int main() | |
{ | |
cdb2_hndl_tp *handle; | |
int rc = cdb2_open(&handle, "testdb", "local", 0); | |
if (0 != rc) { | |
fprintf(stderr, "Failed to open connection to testdb: %s\n", | |
cdb2_errstr(handle)); | |
return rc; | |
} | |
cdb2_client_datetime_t value; | |
bzero(&value, sizeof(cdb2_client_datetime_t)); | |
time_t raw_now_time; | |
time(&raw_now_time); | |
struct tm* tm = gmtime(&raw_now_time); | |
printf("Current time=%s\n", asctime(tm)); | |
value.tm.tm_sec = tm->tm_sec; | |
value.tm.tm_min = tm->tm_min; | |
value.tm.tm_hour = tm->tm_hour; | |
value.tm.tm_mday = tm->tm_mday; | |
value.tm.tm_mon = tm->tm_mon; | |
value.tm.tm_year = tm->tm_year; | |
value.tm.tm_wday = tm->tm_wday; | |
value.tm.tm_yday = tm->tm_yday; | |
value.tm.tm_isdst = tm->tm_isdst; | |
value.msec = 0; | |
strcpy(value.tzname, "UTC"); | |
printf("Converted into CDB2 value="); | |
print_datetime(&value); | |
printf("\n"); | |
rc = cdb2_bind_param(handle, "val", CDB2_DATETIME, &value, sizeof(cdb2_client_datetime_t)); | |
if (0 != rc) { | |
fprintf(stderr, "Failed to bind: %s\n", | |
cdb2_errstr(handle)); | |
return rc; | |
} | |
const char* STATEMENT = "SELECT @val AS value"; | |
rc = cdb2_run_statement(handle, STATEMENT); | |
if (0 != rc) { | |
fprintf(stderr, "Failed to execute statement \"%s\": %s\n", | |
STATEMENT, cdb2_errstr(handle)); | |
return rc; | |
} | |
// Read results | |
while (cdb2_next_record(handle) == CDB2_OK) { | |
printf("Reading column, name=%s\n", cdb2_column_name(handle, 0)); | |
const size_t value_size = cdb2_column_size(handle, 0); | |
void* buffer = malloc(value_size); | |
memcpy(buffer, cdb2_column_value(handle, 0), value_size); | |
const int type = cdb2_column_type(handle, 0); | |
printf("Read %lu bytes, type=%d, value=", value_size, type); | |
print_cdb2_value(buffer, type); | |
printf("\n"); | |
free(buffer); | |
} | |
rc = cdb2_close(handle); | |
if (0 != rc) { | |
fprintf(stderr, "Failed to close CDB2 connection: %s, rc=%d\n", | |
cdb2_errstr(handle), rc); | |
return rc; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment