The R Project SVN R-packages

Rev

Rev 3471 | Rev 3700 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3471 Rev 3473
Line 132... Line 132...
132
    sqlite3_stmt *stmt;
132
    sqlite3_stmt *stmt;
133
    int res;
133
    int res;
134
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
134
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
135
    if (res == SQLITE_OK) {
135
    if (res == SQLITE_OK) {
136
        sprintf(g_sql_buf[2], "insert into [%s].[%s %s] select * from [%s].[%s %s]",
136
        sprintf(g_sql_buf[2], "insert into [%s].[%s %s] select * from [%s].[%s %s]",
137
                iname_src, factor_type, colname_src, iname_dst, factor_type,
137
                iname_dst, factor_type, colname_dst, iname_src, factor_type,
138
                colname_dst);
138
                colname_src);
139
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
139
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
140
        if (res == SQLITE_OK) sqlite3_step(stmt);
140
        if (res == SQLITE_OK) res = sqlite3_step(stmt);
141
        sqlite3_finalize(stmt);
141
        sqlite3_finalize(stmt);
142
    }
142
    }
143
    return res; /* error on dup name? */
143
    return res; /* error on dup name? */
144
}
144
}
145
 
145
 
Line 482... Line 482...
482
    sqlite3_stmt *stmt;
482
    sqlite3_stmt *stmt;
483
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
483
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
484
    int i, j,res;
484
    int i, j,res;
485
    int *col_indices, col_index_len, *dup_indices;
485
    int *col_indices, col_index_len, *dup_indices;
486
    int row_index_len = 0, force_new_df = LOGICAL(new_sdf)[0];
486
    int row_index_len = 0, force_new_df = LOGICAL(new_sdf)[0];
-
 
487
    const char *colname;
-
 
488
 
-
 
489
    /* dup_indices is used to handle when same column chosen more than once.
-
 
490
     * e.g. iris[,c(1,1)] have names Sepal.Length, Sepal.Length.1 *
-
 
491
     * col_indices is used to store the column index of selected columns
-
 
492
     * in the sdf_data table */
487
 
493
 
488
    if (!USE_SDF1(iname, TRUE, TRUE)) return R_NilValue;
494
    if (!USE_SDF1(iname, TRUE, TRUE)) return R_NilValue;
489
 
495
 
490
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
496
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
491
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
497
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
Line 498... Line 504...
498
    /*
504
    /*
499
     * PROCESS COLUMN INDEX: set columns in the select statement
505
     * PROCESS COLUMN INDEX: set columns in the select statement
500
     */
506
     */
501
    if (col == R_NilValue) {
507
    if (col == R_NilValue) {
502
        for (i = 1; i < col_cnt+1; i++) {
508
        for (i = 1; i < col_cnt+1; i++) {
503
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
509
            colname = sqlite3_column_name(stmt, i);
504
                    sqlite3_column_name(stmt, i)); 
510
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
505
        }
511
        }
506
        _expand_buf(0, buflen+20+strlen(iname));
512
        _expand_buf(0, buflen+20+strlen(iname));
507
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
513
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
508
        col_index_len = col_cnt;
514
        col_index_len = col_cnt;
509
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
515
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
510
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
516
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
511
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
517
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
512
    } else if (col == R_NilValue || idxlen < 1) {
518
    } else if (col == R_NilValue || idxlen < 1) {
513
        sqlite3_finalize(stmt);
519
        sqlite3_finalize(stmt);
514
        return R_NilValue;
520
        return R_NilValue;
515
    } else if (IS_NUMERIC(col)) {
521
    } else if (IS_NUMERIC(col)) {
Line 520... Line 526...
520
        for (i = 0; i < idxlen; i++) {
526
        for (i = 0; i < idxlen; i++) {
521
            /* no need to correct for 0 base because col 0 is [row name] */
527
            /* no need to correct for 0 base because col 0 is [row name] */
522
            index = ((int) REAL(col)[i]); 
528
            index = ((int) REAL(col)[i]); 
523
            if (index > col_cnt) {
529
            if (index > col_cnt) {
524
                sqlite3_finalize(stmt);
530
                sqlite3_finalize(stmt);
525
                Rprintf("Error: undefined columns selected\n");
531
                error("undefined columns selected\n");
526
                return R_NilValue;
-
 
527
            } else if (index > 0) {
532
            } else if (index > 0) {
528
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
533
                colname = sqlite3_column_name(stmt,index);
529
                        sqlite3_column_name(stmt,index));
534
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
530
                dup_indices[col_index_len] = 0;
535
                dup_indices[col_index_len] = 0;
531
                for (j = 0; j < col_index_len; j++) {
536
                for (j = 0; j < col_index_len; j++) {
532
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
537
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
533
                }
538
                }
534
                col_indices[col_index_len++] = index;
539
                col_indices[col_index_len++] = index;
535
            } else if (index < 0) {
540
            } else if (index < 0) {
536
                sqlite3_finalize(stmt);
541
                sqlite3_finalize(stmt);
537
                Rprintf("Error: negative indices not supported.\n");
542
                error("negative indices not supported.\n");
538
                return R_NilValue;
-
 
539
            }
543
            }
540
        }
544
        }
541
 
545
 
542
        if (col_index_len == 0) {
546
        if (col_index_len == 0) {
543
            sqlite3_finalize(stmt);
547
            sqlite3_finalize(stmt);
Line 557... Line 561...
557
        for (i = 0; i < idxlen; i++) {
561
        for (i = 0; i < idxlen; i++) {
558
            /* no need to correct for 0 base because col 0 is [row name] */
562
            /* no need to correct for 0 base because col 0 is [row name] */
559
            index = INTEGER(col)[i];
563
            index = INTEGER(col)[i];
560
            if (index > col_cnt) {
564
            if (index > col_cnt) {
561
                sqlite3_finalize(stmt);
565
                sqlite3_finalize(stmt);
562
                Rprintf("Error: undefined columns selected\n");
566
                error("undefined columns selected\n");
563
                return R_NilValue;
-
 
564
            } else if (index > 0) {
567
            } else if (index > 0) {
565
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
568
                colname = sqlite3_column_name(stmt,index);
566
                        sqlite3_column_name(stmt,index));
569
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
567
                dup_indices[col_index_len] = 0;
570
                dup_indices[col_index_len] = 0;
568
                for (j = 0; j < col_index_len; j++) {
571
                for (j = 0; j < col_index_len; j++) {
569
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
572
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
570
                }
573
                }
571
                col_indices[col_index_len++] = index;
574
                col_indices[col_index_len++] = index;
572
            } else if (index < 0) {
575
            } else if (index < 0) {
573
                sqlite3_finalize(stmt);
576
                sqlite3_finalize(stmt);
574
                Rprintf("Error: negative indices not supported.\n");
577
                error("negative indices not supported.\n");
575
                return R_NilValue;
-
 
576
            }
578
            }
577
        }
579
        }
578
 
580
 
579
        if (col_index_len == 0) {
581
        if (col_index_len == 0) {
580
            sqlite3_finalize(stmt);
582
            sqlite3_finalize(stmt);
Line 591... Line 593...
591
        col_index_len = 0;
593
        col_index_len = 0;
592
        for (i = 0; i < col_cnt; i++) {
594
        for (i = 0; i < col_cnt; i++) {
593
            if (LOGICAL(col)[i%idxlen]) {
595
            if (LOGICAL(col)[i%idxlen]) {
594
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
596
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
595
                        sqlite3_column_name(stmt,i+1));
597
                        sqlite3_column_name(stmt,i+1));
-
 
598
 
596
                dup_indices[col_index_len] = 0;
599
                dup_indices[col_index_len] = 0;
597
                col_indices[col_index_len++] = i;
600
                col_indices[col_index_len++] = i;
598
            }
601
            }
-
 
602
 
599
        }
603
        }
600
 
604
 
601
        if (col_index_len == 0) {
605
        if (col_index_len == 0) {
602
            sqlite3_finalize(stmt);
606
            sqlite3_finalize(stmt);
603
            Rprintf("Warning: no column selected.\n");
607
            warning("no column selected.\n");
604
            return R_NilValue;
608
            return R_NilValue;
605
        } else { 
609
        } else { 
606
            _expand_buf(0, buflen+20+strlen(iname));
610
            _expand_buf(0, buflen+20+strlen(iname));
607
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
611
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
608
        }
612
        }
609
    } else {
613
    } else {
610
        sqlite3_finalize(stmt);
614
        sqlite3_finalize(stmt);
611
        Rprintf("Error: don't know how to handle column index.\n");
615
        error("don't know how to handle column index.\n");
612
        return R_NilValue;
-
 
613
    }
616
    }
614
 
617
 
615
    /* 
618
    /* 
616
     * PROCESS ROW INDEX: setup limit or where clause
619
     * PROCESS ROW INDEX: setup limit or where clause
617
     */
620
     */
Line 624... Line 627...
624
        } if (col_index_len > 1 || (force_new_df && col_index_len == 1)) {
627
        } if (col_index_len > 1 || (force_new_df && col_index_len == 1)) {
625
            /* create a new SDF, logic similar to sdf_create_sdf */
628
            /* create a new SDF, logic similar to sdf_create_sdf */
626
 
629
 
627
            /* find a new name. data<n> ? */
630
            /* find a new name. data<n> ? */
628
            char *iname2;
631
            char *iname2;
629
            const char *colname;
-
 
630
            int namelen, sql_len, sql_len2;
632
            int namelen, sql_len, sql_len2;
631
 
633
 
632
            iname2 = _create_sdf_skeleton1(R_NilValue, &namelen, TRUE);
634
            iname2 = _create_sdf_skeleton1(R_NilValue, &namelen, TRUE);
633
 
635
 
634
            /* create sdf_data table */
636
            /* create sdf_data table */