Skip to content

Commit 7dbffd6

Browse files
committed
Add more available functions
1 parent edf537c commit 7dbffd6

File tree

2 files changed

+324
-1
lines changed

2 files changed

+324
-1
lines changed

src/KuzuGD/KuzuGD.cpp

Lines changed: 312 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ Array KuzuGD::execute_query(const String &query) {
236236

237237
UtilityFunctions::push_error("KuzuGD ERROR | Query - " + String(error_msg));
238238
result_array.append("ERROR: " + String(error_msg));
239+
240+
//CleanUp
241+
kuzu_query_result_destroy(&result);
242+
kuzu_destroy_string(error_msg);
243+
239244
return result_array;
240245
}
241246

@@ -255,18 +260,26 @@ Array KuzuGD::execute_query(const String &query) {
255260
row_dict[String(column_name)] = kuzu_value_to_string(&value);
256261
}
257262
}
263+
kuzu_destroy_string(column_name);
258264
}
259265

260266
result_array.append(row_dict);
261267
}
262268

269+
//Clean Up
270+
kuzu_query_result_destroy(&result);
271+
263272
return result_array;
264273
}
265274

266275
Array KuzuGD::execute_prepared_query(const String &query, const Dictionary &params) {
267276
kuzu_prepared_statement stmt;
268277
if (kuzu_connection_prepare(dbConnection, query.utf8().get_data(), &stmt) != KuzuSuccess) {
269278
UtilityFunctions::push_error("KuzuGD ERROR | Prepared Query - FAILED to prepare query");
279+
280+
//CleanUp
281+
kuzu_prepared_statement_destroy(&stmt);
282+
270283
return Array();
271284
}
272285

@@ -402,6 +415,10 @@ Array KuzuGD::execute_prepared_query(const String &query, const Dictionary &para
402415

403416
result_array.append("ERROR: " + String(error_msg));
404417

418+
//Cleanup
419+
kuzu_query_result_destroy(&result);
420+
kuzu_destroy_string(error_msg);
421+
405422
return Array(); // Return empty array if execution fails
406423
}
407424

@@ -421,13 +438,285 @@ Array KuzuGD::execute_prepared_query(const String &query, const Dictionary &para
421438
row_dict[String(column_name)] = kuzu_value_to_string(&value);
422439
}
423440
}
441+
kuzu_destroy_string(column_name);
424442
}
425443
result_array.append(row_dict);
426444
}
427445

446+
//Clean Up
447+
kuzu_query_result_destroy(&result);
448+
kuzu_prepared_statement_destroy(&stmt);
449+
428450
return result_array;
429451
}
430452

453+
int KuzuGD::query_columns_count(const String &query) {
454+
kuzu_query_result result;
455+
kuzu_state state = kuzu_connection_query(dbConnection, query.utf8().get_data(), &result);
456+
457+
if (state != KuzuSuccess) {
458+
char *error_msg = kuzu_query_result_get_error_message(&result);
459+
460+
UtilityFunctions::push_error("KuzuGD ERROR | Query Columns Count - " + String(error_msg));
461+
462+
kuzu_query_result_destroy(&result);
463+
kuzu_destroy_string(error_msg);
464+
465+
return -1;
466+
}
467+
468+
int columnCount = kuzu_query_result_get_num_columns(&result);
469+
470+
//Clean Up
471+
kuzu_query_result_destroy(&result);
472+
473+
return columnCount;
474+
}
475+
476+
String KuzuGD::query_column_name(const String &query, int colIndex) {
477+
kuzu_query_result result;
478+
kuzu_state stateSuccess = kuzu_connection_query(dbConnection, query.utf8().get_data(), &result);
479+
480+
if (stateSuccess != KuzuSuccess) {
481+
char *error_msg = kuzu_query_result_get_error_message(&result);
482+
483+
UtilityFunctions::push_error("KuzuGD ERROR | Query Columns Count - " + String(error_msg));
484+
485+
kuzu_query_result_destroy(&result);
486+
kuzu_destroy_string(error_msg);
487+
488+
return "UNDEFINED";
489+
}
490+
491+
char **column_name;
492+
kuzu_state stateSuccess2 = kuzu_query_result_get_column_name(&result, colIndex, column_name);
493+
494+
if (stateSuccess2 != KuzuSuccess) {
495+
UtilityFunctions::push_error("KuzuGD ERROR | Query Columns Count - Column Name NOT FOUND");
496+
497+
kuzu_query_result_destroy(&result);
498+
499+
return "UNDEFINED";
500+
}
501+
502+
String gd_column_name = String::utf8(*column_name);
503+
504+
//Clean Up
505+
kuzu_query_result_destroy(&result);
506+
kuzu_destroy_string(*column_name);
507+
508+
return gd_column_name;
509+
}
510+
511+
String KuzuGD::query_column_data_type(const String &query, int colIndex) {
512+
kuzu_query_result result;
513+
kuzu_state stateSuccess = kuzu_connection_query(dbConnection, query.utf8().get_data(), &result);
514+
515+
if (stateSuccess != KuzuSuccess) {
516+
char *error_msg = kuzu_query_result_get_error_message(&result);
517+
518+
UtilityFunctions::push_error("KuzuGD ERROR | Query Column Data Type - " + String(error_msg));
519+
520+
kuzu_query_result_destroy(&result);
521+
kuzu_destroy_string(error_msg);
522+
523+
return "UNDEFINED";
524+
}
525+
526+
kuzu_logical_type column_data_type;
527+
kuzu_state stateSuccess2 = kuzu_query_result_get_column_data_type(&result, colIndex, &column_data_type);
528+
529+
if (stateSuccess2 != KuzuSuccess) {
530+
UtilityFunctions::push_error("KuzuGD ERROR | Query Column Data Type - Column Name NOT FOUND");
531+
532+
kuzu_query_result_destroy(&result);
533+
534+
return "UNDEFINED";
535+
}
536+
537+
String column_type;
538+
539+
switch (kuzu_data_type_get_id(&column_data_type)) {
540+
case KUZU_ANY:
541+
column_type = "ANY";
542+
case KUZU_NODE:
543+
column_type = "NODE";
544+
case KUZU_REL:
545+
column_type = "REL";
546+
case KUZU_RECURSIVE_REL:
547+
column_type = "RECURSIVE_REL";
548+
case KUZU_SERIAL:
549+
column_type = "SERIAL";
550+
case KUZU_BOOL:
551+
column_type = "BOOL";
552+
case KUZU_INT64:
553+
column_type = "INT64";
554+
case KUZU_INT32:
555+
column_type = "INT32";
556+
case KUZU_INT16:
557+
column_type = "INT16";
558+
case KUZU_INT8:
559+
column_type = "INT8";
560+
case KUZU_UINT64:
561+
column_type = "UINT64";
562+
case KUZU_UINT32:
563+
column_type = "UINT32";
564+
case KUZU_UINT16:
565+
column_type = "UINT16";
566+
case KUZU_UINT8:
567+
column_type = "UINT8";
568+
case KUZU_INT128:
569+
column_type = "INT128";
570+
case KUZU_DOUBLE:
571+
column_type = "DOUBLE";
572+
case KUZU_FLOAT:
573+
column_type = "FLOAT";
574+
case KUZU_DATE:
575+
column_type = "DATE";
576+
case KUZU_TIMESTAMP:
577+
column_type = "TIMESTAMP";
578+
case KUZU_TIMESTAMP_SEC:
579+
column_type = "TIMESTAMP_SEC";
580+
case KUZU_TIMESTAMP_MS:
581+
column_type = "TIMESTAMP_MS";
582+
case KUZU_TIMESTAMP_NS:
583+
column_type = "TIMESTAMP_NS";
584+
case KUZU_TIMESTAMP_TZ:
585+
column_type = "TIMESTAMP_TZ";
586+
case KUZU_INTERVAL:
587+
column_type = "INTERVAL";
588+
case KUZU_DECIMAL:
589+
column_type = "DECIMAL";
590+
case KUZU_INTERNAL_ID:
591+
column_type = "INTERNAL_ID";
592+
case KUZU_STRING:
593+
column_type = "STRING";
594+
case KUZU_BLOB:
595+
column_type = "BLOB";
596+
case KUZU_LIST:
597+
column_type = "LIST";
598+
case KUZU_ARRAY:
599+
column_type = "ARRAY";
600+
case KUZU_STRUCT:
601+
column_type = "STRUCT";
602+
case KUZU_MAP:
603+
column_type = "MAP";
604+
case KUZU_UNION:
605+
column_type = "UNION";
606+
case KUZU_POINTER:
607+
column_type = "POINTER";
608+
case KUZU_UUID:
609+
column_type = "UUID";
610+
default:
611+
column_type = "UNDEFINED";
612+
}
613+
614+
//Clean Up
615+
kuzu_query_result_destroy(&result);
616+
617+
return column_type;
618+
}
619+
620+
int KuzuGD::query_num_tuples(const String &query) {
621+
kuzu_query_result result;
622+
kuzu_state stateSuccess = kuzu_connection_query(dbConnection, query.utf8().get_data(), &result);
623+
624+
if (stateSuccess != KuzuSuccess) {
625+
char *error_msg = kuzu_query_result_get_error_message(&result);
626+
627+
UtilityFunctions::push_error("KuzuGD ERROR | Query Tuple Count - " + String(error_msg));
628+
629+
//CleanUp
630+
kuzu_query_result_destroy(&result);
631+
kuzu_destroy_string(error_msg);
632+
633+
return 0;
634+
}
635+
636+
int tupleCount = kuzu_query_result_get_num_tuples(&result);
637+
638+
//Clean Up
639+
kuzu_query_result_destroy(&result);
640+
641+
return tupleCount;
642+
}
643+
644+
float KuzuGD::query_summary_compile_time(const String &query) {
645+
kuzu_query_result result;
646+
kuzu_query_summary out_query_summary;
647+
648+
kuzu_state stateSuccess = kuzu_connection_query(dbConnection, query.utf8().get_data(), &result);
649+
650+
if (stateSuccess != KuzuSuccess) {
651+
char *error_msg = kuzu_query_result_get_error_message(&result);
652+
653+
UtilityFunctions::push_error("KuzuGD ERROR | Query Summary Compile Time - " + String(error_msg));
654+
655+
//CleanUp
656+
kuzu_destroy_string(error_msg);
657+
kuzu_query_result_destroy(&result);
658+
kuzu_query_summary_destroy(&out_query_summary);
659+
660+
return 0;
661+
}
662+
663+
kuzu_state stateSuccess2 = kuzu_query_result_get_query_summary(&result, &out_query_summary);
664+
665+
if (stateSuccess2 != KuzuSuccess) {
666+
UtilityFunctions::push_error("KuzuGD ERROR | Query Summary Compile Time - Get Query Summary FAILED");
667+
668+
//CleanUp
669+
kuzu_query_result_destroy(&result);
670+
kuzu_query_summary_destroy(&out_query_summary);
671+
672+
return 0;
673+
}
674+
675+
//Clean Up
676+
kuzu_query_result_destroy(&result);
677+
kuzu_query_summary_destroy(&out_query_summary);
678+
679+
return kuzu_query_summary_get_compiling_time(&out_query_summary);
680+
}
681+
682+
float KuzuGD::query_summary_execution_time(const String &query) {
683+
kuzu_query_result result;
684+
kuzu_query_summary out_query_summary;
685+
686+
kuzu_state stateSuccess = kuzu_connection_query(dbConnection, query.utf8().get_data(), &result);
687+
688+
if (stateSuccess != KuzuSuccess) {
689+
char *error_msg = kuzu_query_result_get_error_message(&result);
690+
691+
UtilityFunctions::push_error("KuzuGD ERROR | Query Summary Execution Time - " + String(error_msg));
692+
693+
//CleanUp
694+
kuzu_destroy_string(error_msg);
695+
kuzu_query_result_destroy(&result);
696+
kuzu_query_summary_destroy(&out_query_summary);
697+
698+
return 0;
699+
}
700+
701+
kuzu_state stateSuccess2 = kuzu_query_result_get_query_summary(&result, &out_query_summary);
702+
703+
if (stateSuccess2 != KuzuSuccess) {
704+
UtilityFunctions::push_error("KuzuGD ERROR | Query Summary Execution Time - Get Query Summary FAILED");
705+
706+
//CleanUp
707+
kuzu_query_result_destroy(&result);
708+
kuzu_query_summary_destroy(&out_query_summary);
709+
710+
return 0;
711+
}
712+
713+
//Clean Up
714+
kuzu_query_result_destroy(&result);
715+
kuzu_query_summary_destroy(&out_query_summary);
716+
717+
return kuzu_query_summary_get_execution_time(&out_query_summary);
718+
}
719+
431720
/******************************************************************
432721
433722
@@ -489,7 +778,6 @@ void KuzuGD::string_to_tm(const string &time_str, ParsedTime &timeStruct) {
489778
nanoseconds = stoll(nano_str);
490779
} else if (regex_match(time_str, timestamp_tz_regex)) {
491780
ss >> out_tm.tm_year >> dash1 >> out_tm.tm_mon >> dash2 >> out_tm.tm_mday >> space >> out_tm.tm_hour >> dash1 >> out_tm.tm_min >> dash2 >> out_tm.tm_sec >> space >> tz_offset_str;
492-
493781
char sign = tz_offset_str[0];
494782
int tz_hour = stoi(tz_offset_str.substr(1, 2));
495783
int tz_minute = stoi(tz_offset_str.substr(4, 2));
@@ -588,6 +876,16 @@ void KuzuGD::_bind_methods() {
588876

589877
ClassDB::bind_method(D_METHOD("get_config"), &KuzuGD::get_config);
590878

879+
/********************************************
880+
881+
Management Functions
882+
883+
********************************************/
884+
885+
ClassDB::bind_method(D_METHOD("query_timeout", "timeout_millis"), &KuzuGD::query_timeout);
886+
887+
ClassDB::bind_method(D_METHOD("interrupt_connection"), &KuzuGD::interrupt_connection);
888+
591889
/********************************************
592890
593891
Initialization
@@ -611,5 +909,18 @@ void KuzuGD::_bind_methods() {
611909
********************************************/
612910

613911
ClassDB::bind_method(D_METHOD("execute_query", "query"), &KuzuGD::execute_query);
912+
614913
ClassDB::bind_method(D_METHOD("execute_prepared_query", "query", "params"), &KuzuGD::execute_prepared_query);
914+
915+
ClassDB::bind_method(D_METHOD("query_columns_count", "query"), &KuzuGD::query_columns_count);
916+
917+
ClassDB::bind_method(D_METHOD("query_column_name", "query", "colIndex"), &KuzuGD::query_column_name);
918+
919+
ClassDB::bind_method(D_METHOD("query_column_data_type", "query", "colIndex"), &KuzuGD::query_column_data_type);
920+
921+
ClassDB::bind_method(D_METHOD("query_num_tuples", "query"), &KuzuGD::query_num_tuples);
922+
923+
ClassDB::bind_method(D_METHOD("query_summary_compile_time", "query"), &KuzuGD::query_summary_compile_time);
924+
925+
ClassDB::bind_method(D_METHOD("query_summary_execution_time", "query"), &KuzuGD::query_summary_execution_time);
615926
}

0 commit comments

Comments
 (0)