76 lines
2.0 KiB
C
76 lines
2.0 KiB
C
#include <duckdb.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int main(int argc, char** argv){
|
|
printf("Hello from HardCompound!\n");
|
|
|
|
if(argc < 2){
|
|
printf("You need to specify a duckdb file path! Terminating!\n");
|
|
exit(1);
|
|
}
|
|
printf("First argument: %s\n", argv[1]);
|
|
|
|
duckdb_database db;
|
|
if(duckdb_open(argv[1], &db) == DuckDBError){
|
|
printf("Error opening duckdb file, terminating!\n");
|
|
exit(2);
|
|
}
|
|
|
|
duckdb_connection conn;
|
|
if(duckdb_connect(db, &conn) == DuckDBError){
|
|
printf("Error connecting to the duckdb database! Terminating!\n");
|
|
exit(3);
|
|
}
|
|
|
|
|
|
//Test query
|
|
duckdb_result result;
|
|
duckdb_query(conn, "SELECT * from \"Engine RPM\";\n", &result);
|
|
int32_t iterations = 0;
|
|
int32_t number_of_data_points = 0;
|
|
|
|
while(true){
|
|
iterations++;
|
|
|
|
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(result);
|
|
|
|
if(!data_chunk){
|
|
break;
|
|
}
|
|
|
|
idx_t row_count = duckdb_data_chunk_get_size(data_chunk); //number of rows from the data chunk
|
|
|
|
//first column
|
|
duckdb_vector col1 = duckdb_data_chunk_get_vector(data_chunk, 0);
|
|
int32_t *col1_data = (int32_t *) duckdb_vector_get_data(col1);
|
|
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
|
|
|
for (idx_t row = 0; row < row_count; row++) {
|
|
number_of_data_points++;
|
|
if(duckdb_validity_row_is_valid(col1_validity, row)){
|
|
printf("%d, ", col1_data[row]);
|
|
}else{
|
|
printf("NULL");
|
|
}
|
|
}
|
|
|
|
duckdb_destroy_data_chunk(&data_chunk);
|
|
}
|
|
|
|
printf("\nNumber of iterations: %d, number of data points: %d\n", iterations, number_of_data_points);
|
|
|
|
printf("Shutting down HardCompound!\n");
|
|
|
|
printf("Destroying result!\n");
|
|
duckdb_destroy_result(&result);
|
|
|
|
printf("Closing database and any connection.\n");
|
|
duckdb_disconnect(&conn);
|
|
duckdb_close(&db);
|
|
|
|
|
|
return 0;
|
|
}
|