509
508
/* SQL Execute! */
510
509
if ((res = SQLExecute (db_handle->jobrep_hstmt)) != SQL_SUCCESS) {
511
510
ODBC_Errors(db_handle, res, "SQLExecute");
511
goto sql_query_cleanup;
516
514
/* Got a Query, clean the ResultSet, if any is set. */
517
515
if (db_handle->resultset != NULL) {
518
516
lcmaps_log(LOG_ERR, "%s: The memory for the SQL resultset was not cleaned.\n", __func__);
518
goto sql_query_cleanup;
523
521
/* New resultset */
524
522
db_handle->resultset = malloc(sizeof(TResultSet));
525
523
if (db_handle->resultset == NULL) {
526
524
lcmaps_log(LOG_ERR, "%s: Unable to allocate the resultset\n", __func__);
526
goto sql_query_cleanup;
531
529
/* Get column count */
532
530
res = SQLNumResultCols(db_handle->jobrep_hstmt, &(db_handle->resultset->colCnt));
533
if (res != SQL_SUCCESS) {
531
if (res != SQL_SUCCESS || db_handle->resultset->colCnt<0) {
534
532
ODBC_Errors(db_handle, res, "SQLNumResultCols");
533
goto sql_query_cleanup;
539
536
/* Get the row count */
540
537
res = SQLRowCount(db_handle->jobrep_hstmt, &(db_handle->resultset->rowCnt));
541
if (res != SQL_SUCCESS) {
538
if (res != SQL_SUCCESS || db_handle->resultset->rowCnt<0) {
542
539
ODBC_Errors(db_handle, res, "SQLRowCount");
540
goto sql_query_cleanup;
547
/* Build the ResultSet Grid */
548
db_handle->resultset->columns = malloc(sizeof(TColumn) * db_handle->resultset->colCnt);
543
/* Build the ResultSet Grid *
544
* Note: colCnt is successfully retrieved, hence will be non-negative so
545
* cast explicitly to unsigned */
546
colCnt=(SQLUSMALLINT)db_handle->resultset->colCnt;
547
rowCnt=(SQLULEN)db_handle->resultset->rowCnt;
549
db_handle->resultset->columns = malloc(sizeof(TColumn) * colCnt);
549
550
if (db_handle->resultset->columns == NULL) {
550
551
lcmaps_log(LOG_ERR, "%s: Error in allocating %u bytes\n",
551
__func__, sizeof(TColumn) * db_handle->resultset->colCnt);
552
goto done_with_filling_result_set;
552
__func__, sizeof(TColumn) * colCnt);
554
goto sql_query_cleanup;
554
memset(db_handle->resultset->columns, 0, sizeof(TColumn) * db_handle->resultset->colCnt);
556
db_handle->resultset->data = malloc(sizeof(TRow) * db_handle->resultset->rowCnt);
556
memset(db_handle->resultset->columns, 0, sizeof(TColumn) * colCnt);
559
db_handle->resultset->data = malloc(sizeof(TRow) * rowCnt);
557
560
if (db_handle->resultset->data == NULL) {
558
561
lcmaps_log(LOG_ERR, "%s: Error in allocating %u bytes\n",
559
__func__, sizeof(TRow) * db_handle->resultset->rowCnt);
560
goto done_with_filling_result_set;
562
__func__, sizeof(TRow) * rowCnt);
564
goto sql_query_cleanup;
562
memset(db_handle->resultset->data, 0, sizeof(TRow) * db_handle->resultset->rowCnt);
566
memset(db_handle->resultset->data, 0, sizeof(TRow) * rowCnt);
564
for (i = 0; i < db_handle->resultset->rowCnt; i++) {
565
db_handle->resultset->data[i] = malloc(sizeof(TField) * db_handle->resultset->colCnt);
568
for (i = 0; i < rowCnt; i++) {
569
db_handle->resultset->data[i] = malloc(sizeof(TField) * colCnt);
566
570
if (db_handle->resultset->data[i] == NULL) {
567
571
lcmaps_log(LOG_ERR, "%s: Error in allocating %u bytes\n",
568
__func__, sizeof(TField) * db_handle->resultset->colCnt);
569
goto done_with_filling_result_set;
572
__func__, sizeof(TField) * colCnt);
574
goto sql_query_cleanup;
571
memset(db_handle->resultset->data[i], 0, sizeof(TField) * db_handle->resultset->colCnt);
576
memset(db_handle->resultset->data[i], 0, sizeof(TField) * colCnt);
574
579
/* Let's get us a dataset */
577
sts = SQLFetch (db_handle->jobrep_hstmt);
578
if (sts == SQL_NO_DATA_FOUND) {
579
goto done_with_filling_result_set;
581
if (sts != SQL_SUCCESS) {
582
ODBC_Errors(db_handle, sts, "SQLFetch");
583
goto done_with_filling_result_set;
586
/* Read a row, column by column */
587
for (colNum = 0; colNum < db_handle->resultset->colCnt; colNum++) {
588
sts = SQLDescribeCol(db_handle->jobrep_hstmt, colNum + 1, (UCHAR *) colName, sizeof (colName),
589
NULL, &colType, &colPrecision,
590
&colScale, &colNullable);
591
if (sts != SQL_SUCCESS) {
592
ODBC_Errors(db_handle, sts, "SQLDescribeCol");
593
goto done_with_filling_result_set;
597
unsigned long bufsize_bytes;
602
case SQL_UNKNOWN_TYPE :
607
colCType = SQL_C_CHAR;
608
bufsize_bytes = sizeof(char) * 2;
612
colCType = SQL_C_LONG;
613
bufsize_bytes = sizeof(long);
616
colCType = SQL_C_SHORT;
617
bufsize_bytes = sizeof(short);
621
colCType = SQL_C_FLOAT;
622
bufsize_bytes = sizeof(float);
625
colCType = SQL_C_DOUBLE;
626
bufsize_bytes = sizeof(double);
580
for (rowNum=0; rowNum<rowCnt; rowNum++) {
581
res = SQLFetch (db_handle->jobrep_hstmt);
582
if (res == SQL_NO_DATA_FOUND) {
583
goto sql_query_cleanup;
585
if (res != SQL_SUCCESS) {
586
ODBC_Errors(db_handle, res, "SQLFetch");
587
goto sql_query_cleanup;
590
/* Read a row, column by column */
591
for (colNum = 0; colNum < colCnt; colNum++) {
592
res = SQLDescribeCol(db_handle->jobrep_hstmt, (SQLUSMALLINT)(colNum + 1),
593
(UCHAR *) colName, sizeof (colName),
594
NULL, &colType, &colPrecision,
595
&colScale, &colNullable);
596
if (res != SQL_SUCCESS) {
597
ODBC_Errors(db_handle, res, "SQLDescribeCol");
598
goto sql_query_cleanup;
602
SQLLEN bufsize_bytes;
607
case SQL_UNKNOWN_TYPE :
612
colCType = SQL_C_CHAR;
613
bufsize_bytes = sizeof(char) * 2;
617
colCType = SQL_C_LONG;
618
bufsize_bytes = sizeof(long);
621
colCType = SQL_C_SHORT;
622
bufsize_bytes = sizeof(short);
626
colCType = SQL_C_FLOAT;
627
bufsize_bytes = sizeof(float);
630
colCType = SQL_C_DOUBLE;
631
bufsize_bytes = sizeof(double);
629
#if (ODBCVER >= 0x0300)
634
#if (ODBCVER >= 0x0300)
637
colCType = SQL_C_CHAR;
638
bufsize_bytes = sizeof(char) * 50;
641
lcmaps_log(LOG_ERR, "%s: Unknown datatype of value %d\n", __func__, colType);
647
/********************************
648
Start of getting data with SQLGetData() The data will be read and made
649
available in the 'data_final' field. This can be casted to what ever
650
the colCType indicates.
651
********************************/
652
data = malloc(bufsize_bytes);
654
void *data_final = NULL;
660
int needs_concat = 0;
664
/* Fetch data for this field */
665
memset(data, 0, bufsize_bytes);
666
res = SQLGetData (db_handle->jobrep_hstmt,
668
colCType, data, bufsize_bytes,
670
if (res == SQL_SUCCESS) {
672
/* If there is more data available for a given field, continue here */
673
total_buf += bufsize_bytes;
675
/* Expand memory into a new buffer and copy the current buffer in it. */
677
data3 = malloc(sizeof(char) * total_buf);
679
data3 = realloc(data2, sizeof(char) * total_buf);
682
/* Allocation error */
684
goto done_with_filling_result_set;
687
memcpy((char*)data2+offset, data, bufsize_bytes);
689
/* Move the offset with the buffer size minus 1, because the last
691
offset += bufsize_bytes - 1;
696
} else if (res != SQL_SUCCESS_WITH_INFO) {
697
ODBC_Errors(db_handle, res, "SQLGetData");
698
goto done_with_filling_result_set;
701
/* Signal that the last read buffer needs to be concattinated as a final step */
704
/* If there is more data available for a given field, continue here */
705
total_buf += bufsize_bytes;
707
/* Expand memory into a new buffer and copy the current buffer in it. */
709
data3 = malloc(sizeof(char) * total_buf);
711
data3 = realloc(data2, sizeof(char) * total_buf);
714
/* Allocation error */
716
goto done_with_filling_result_set;
719
memcpy((char*)data2+offset, data, bufsize_bytes);
720
/* Move the offset with the buffer size minus 1, because the last
722
offset += bufsize_bytes - 1;
725
} while (res == SQL_SUCCESS_WITH_INFO);
727
/* Hook up the fetched results into the TResultSet */
728
db_handle->resultset->columns[colNum].columnname = strdup(colName);
729
db_handle->resultset->columns[colNum].type = colType;
730
db_handle->resultset->data[rowNum][colNum].fieldname = strdup(colName);
731
db_handle->resultset->data[rowNum][colNum].type = colCType;
732
db_handle->resultset->data[rowNum][colNum].byte_size = total_buf;
735
db_handle->resultset->data[rowNum][colNum].v_string = (char *)data_final;
738
db_handle->resultset->data[rowNum][colNum].v_long = *((long *)data_final);
741
db_handle->resultset->data[rowNum][colNum].v_short = *((short *)data_final);
744
db_handle->resultset->data[rowNum][colNum].v_float = *((float *)data_final);
747
db_handle->resultset->data[rowNum][colNum].v_double = *((double *)data_final);
754
goto loop_for_a_new_row;
756
done_with_filling_result_set:
757
res = db_handle->resultset->rowCnt;
642
colCType = SQL_C_CHAR;
643
bufsize_bytes = sizeof(char) * 50;
646
lcmaps_log(LOG_ERR, "%s: Unknown datatype of value %d\n", __func__, colType);
648
goto sql_query_cleanup;
652
/********************************
653
Start of getting data with SQLGetData() The data will be read and made
654
available in the 'data_final' field. This can be casted to what ever
655
the colCType indicates.
656
********************************/
657
data = malloc((size_t)bufsize_bytes);
659
void *data_final = NULL;
663
size_t total_buf = 0;
665
int needs_concat = 0;
669
/* Fetch data for this field */
670
memset(data, 0, (size_t)bufsize_bytes);
671
res = SQLGetData (db_handle->jobrep_hstmt,
672
(SQLUSMALLINT)(colNum + 1),
673
colCType, data, bufsize_bytes,
675
if (res == SQL_SUCCESS) {
677
/* If there is more data available for a given field, continue here */
678
total_buf += (size_t)bufsize_bytes;
680
/* Expand memory into a new buffer and copy the current buffer in it. */
682
data3 = malloc(sizeof(char) * total_buf);
684
data3 = realloc(data2, sizeof(char) * total_buf);
687
/* Allocation error */
690
goto sql_query_cleanup;
693
memcpy((char*)data2+offset, data, (size_t)bufsize_bytes);
695
/* Move the offset with the buffer size minus 1, because the last
697
offset += bufsize_bytes - 1;
702
} else if (res != SQL_SUCCESS_WITH_INFO) {
703
ODBC_Errors(db_handle, res, "SQLGetData");
704
goto sql_query_cleanup;
707
/* Signal that the last read buffer needs to be concattinated as a final step */
710
/* If there is more data available for a given field, continue here */
711
total_buf += (size_t)bufsize_bytes;
713
/* Expand memory into a new buffer and copy the current buffer in it. */
715
data3 = malloc(sizeof(char) * total_buf);
717
data3 = realloc(data2, sizeof(char) * total_buf);
720
/* Allocation error */
723
goto sql_query_cleanup;
726
memcpy((char*)data2+offset, data, (size_t)bufsize_bytes);
727
/* Move the offset with the buffer size minus 1, because the last
729
offset += bufsize_bytes - 1;
732
} while (res == SQL_SUCCESS_WITH_INFO);
734
/* Hook up the fetched results into the TResultSet */
735
db_handle->resultset->columns[colNum].columnname = strdup(colName);
736
db_handle->resultset->columns[colNum].type = colType;
737
db_handle->resultset->data[rowNum][colNum].fieldname = strdup(colName);
738
db_handle->resultset->data[rowNum][colNum].type = colCType;
739
db_handle->resultset->data[rowNum][colNum].byte_size = total_buf;
742
db_handle->resultset->data[rowNum][colNum].v_string = (char *)data_final;
745
db_handle->resultset->data[rowNum][colNum].v_long = *((long *)data_final);
748
db_handle->resultset->data[rowNum][colNum].v_short = *((short *)data_final);
751
db_handle->resultset->data[rowNum][colNum].v_float = *((float *)data_final);
754
db_handle->resultset->data[rowNum][colNum].v_double = *((double *)data_final);
760
761
SQL_QueryClose(db_handle);