| Line 1... |
Line -... |
| 1 |
#include <stdio.h>
|
- |
|
| 2 |
#include <string.h>
|
1 |
#include <string.h>
|
| 3 |
#include "sqlite_dataframe.h"
|
2 |
#include "sqlite_dataframe.h"
|
| 4 |
|
3 |
|
| 5 |
/****************************************************************************
|
4 |
/****************************************************************************
|
| 6 |
* UTILITY FUNCTIONS
|
5 |
* UTILITY FUNCTIONS
|
| Line 430... |
Line 429... |
| 430 |
char *iname;
|
429 |
char *iname;
|
| 431 |
int len;
|
430 |
int len;
|
| 432 |
sqlite3_stmt *stmt;
|
431 |
sqlite3_stmt *stmt;
|
| 433 |
int res;
|
432 |
int res;
|
| 434 |
|
433 |
|
| 435 |
iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
|
434 |
iname = SDF_INAME(sdf);
|
| 436 |
sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
|
435 |
sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
|
| 437 |
if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
|
436 |
if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
|
| 438 |
|
437 |
|
| 439 |
res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
|
438 |
res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
|
| 440 |
if (_sqlite_error(res)) return R_NilValue;
|
439 |
if (_sqlite_error(res)) return R_NilValue;
|
| Line 508... |
Line 507... |
| 508 |
|
507 |
|
| 509 |
/*
|
508 |
/*
|
| 510 |
* PROCESS COLUMN INDEX: set columns in the select statement
|
509 |
* PROCESS COLUMN INDEX: set columns in the select statement
|
| 511 |
*/
|
510 |
*/
|
| 512 |
if (col == R_NilValue) {
|
511 |
if (col == R_NilValue) {
|
| - |
|
512 |
int len;
|
| 513 |
for (i = 1; i < col_cnt+1; i++) {
|
513 |
for (i = 1; i < col_cnt+1; i++) {
|
| 514 |
colname = sqlite3_column_name(stmt, i);
|
514 |
colname = sqlite3_column_name(stmt, i);
|
| 515 |
buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
|
515 |
buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
|
| - |
|
516 |
_expand_buf(0, buflen + 100);
|
| 516 |
}
|
517 |
}
|
| 517 |
_expand_buf(0, buflen+20+strlen(iname));
|
- |
|
| 518 |
buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
|
518 |
buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
|
| 519 |
col_index_len = col_cnt;
|
519 |
col_index_len = col_cnt;
|
| 520 |
col_indices = (int *)R_alloc(col_cnt, sizeof(int));
|
520 |
col_indices = (int *)R_alloc(col_cnt, sizeof(int));
|
| 521 |
dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
|
521 |
dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
|
| 522 |
for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
|
522 |
for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
|
| Line 535... |
Line 535... |
| 535 |
sqlite3_finalize(stmt);
|
535 |
sqlite3_finalize(stmt);
|
| 536 |
error("undefined columns selected\n");
|
536 |
error("undefined columns selected\n");
|
| 537 |
} else if (index > 0) {
|
537 |
} else if (index > 0) {
|
| 538 |
colname = sqlite3_column_name(stmt,index);
|
538 |
colname = sqlite3_column_name(stmt,index);
|
| 539 |
buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
|
539 |
buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
|
| - |
|
540 |
_expand_buf(0, buflen + 100);
|
| 540 |
dup_indices[col_index_len] = 0;
|
541 |
dup_indices[col_index_len] = 0;
|
| 541 |
for (j = 0; j < col_index_len; j++) {
|
542 |
for (j = 0; j < col_index_len; j++) {
|
| 542 |
if (col_indices[j] == index) dup_indices[col_index_len]++;
|
543 |
if (col_indices[j] == index) dup_indices[col_index_len]++;
|
| 543 |
}
|
544 |
}
|
| 544 |
col_indices[col_index_len++] = index;
|
545 |
col_indices[col_index_len++] = index;
|
| Line 570... |
Line 571... |
| 570 |
sqlite3_finalize(stmt);
|
571 |
sqlite3_finalize(stmt);
|
| 571 |
error("undefined columns selected\n");
|
572 |
error("undefined columns selected\n");
|
| 572 |
} else if (index > 0) {
|
573 |
} else if (index > 0) {
|
| 573 |
colname = sqlite3_column_name(stmt,index);
|
574 |
colname = sqlite3_column_name(stmt,index);
|
| 574 |
buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
|
575 |
buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
|
| - |
|
576 |
_expand_buf(0, buflen + 100);
|
| 575 |
dup_indices[col_index_len] = 0;
|
577 |
dup_indices[col_index_len] = 0;
|
| 576 |
for (j = 0; j < col_index_len; j++) {
|
578 |
for (j = 0; j < col_index_len; j++) {
|
| 577 |
if (col_indices[j] == index) dup_indices[col_index_len]++;
|
579 |
if (col_indices[j] == index) dup_indices[col_index_len]++;
|
| 578 |
}
|
580 |
}
|
| 579 |
col_indices[col_index_len++] = index;
|
581 |
col_indices[col_index_len++] = index;
|
| Line 583... |
Line 585... |
| 583 |
}
|
585 |
}
|
| 584 |
}
|
586 |
}
|
| 585 |
|
587 |
|
| 586 |
if (col_index_len == 0) {
|
588 |
if (col_index_len == 0) {
|
| 587 |
sqlite3_finalize(stmt);
|
589 |
sqlite3_finalize(stmt);
|
| 588 |
Rprintf("Error: no indices detected??\n");
|
590 |
error("no valid indices detected");
|
| 589 |
return R_NilValue;
|
- |
|
| 590 |
} else {
|
591 |
} else {
|
| - |
|
592 |
/*Rprintf("debug: col_index_len: %d, idxlen: %d, buf 0: %s\n", col_index_len, idxlen, g_sql_buf[0]);*/
|
| 591 |
_expand_buf(0, buflen+20+strlen(iname));
|
593 |
_expand_buf(0, buflen+20+strlen(iname));
|
| 592 |
buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
|
594 |
buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
|
| 593 |
}
|
595 |
}
|
| 594 |
} else if (IS_LOGICAL(col)) {
|
596 |
} else if (IS_LOGICAL(col)) {
|
| 595 |
/* recycling stuff, so max column output is the # of cols in the df */
|
597 |
/* recycling stuff, so max column output is the # of cols in the df */
|
| Line 627... |
Line 629... |
| 627 |
if (row == R_NilValue) {
|
629 |
if (row == R_NilValue) {
|
| 628 |
if (col_index_len == 1 && !force_new_df) {
|
630 |
if (col_index_len == 1 && !force_new_df) {
|
| 629 |
ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
|
631 |
ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
|
| 630 |
sqlite3_finalize(stmt);
|
632 |
sqlite3_finalize(stmt);
|
| 631 |
return ret;
|
633 |
return ret;
|
| 632 |
} if (col_index_len > 1 || (force_new_df && col_index_len == 1)) {
|
634 |
} else if (col_index_len > 1 || (force_new_df && col_index_len == 1)) {
|
| 633 |
/* create a new SDF, logic similar to sdf_create_sdf */
|
635 |
/* create a new SDF, logic similar to sdf_create_sdf */
|
| 634 |
|
636 |
|
| 635 |
/* find a new name. data<n> ? */
|
637 |
/* find a new name. data<n> ? */
|
| 636 |
char *iname2;
|
638 |
char *iname2;
|
| 637 |
int namelen, sql_len, sql_len2;
|
639 |
int namelen, sql_len, sql_len2;
|
| Line 651... |
Line 653... |
| 651 |
/* un-duplicate col names by appending num, just like R */
|
653 |
/* un-duplicate col names by appending num, just like R */
|
| 652 |
sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
|
654 |
sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
|
| 653 |
dup_indices[i],
|
655 |
dup_indices[i],
|
| 654 |
sqlite3_column_decltype(stmt, col_indices[i]));
|
656 |
sqlite3_column_decltype(stmt, col_indices[i]));
|
| 655 |
}
|
657 |
}
|
| - |
|
658 |
_expand_buf(1, sql_len + 100);
|
| 656 |
|
659 |
|
| 657 |
/* deal with possibly factor columns */
|
660 |
/* deal with possibly factor columns */
|
| 658 |
if (_is_factor2(iname, "factor", colname)) {
|
661 |
if (_is_factor2(iname, "factor", colname)) {
|
| 659 |
/* found a factor column */
|
662 |
/* found a factor column */
|
| 660 |
|
663 |
|
| Line 689... |
Line 692... |
| 689 |
sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
|
692 |
sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
|
| 690 |
res = _sqlite_exec(g_sql_buf[1]);
|
693 |
res = _sqlite_exec(g_sql_buf[1]);
|
| 691 |
if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
|
694 |
if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
|
| 692 |
|
695 |
|
| 693 |
/* insert data (with row names). buf[0] contains a select */
|
696 |
/* insert data (with row names). buf[0] contains a select */
|
| - |
|
697 |
_expand_buf(1, g_sql_buf_sz[0] + 32);
|
| 694 |
sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
|
698 |
sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
|
| 695 |
g_sql_buf[0]);
|
699 |
g_sql_buf[0]);
|
| 696 |
res = _sqlite_exec(g_sql_buf[1]);
|
700 |
res = _sqlite_exec(g_sql_buf[1]);
|
| 697 |
if (_sqlite_error(res)) return R_NilValue;
|
701 |
if (_sqlite_error(res)) return R_NilValue;
|
| 698 |
|
702 |
|