| 3255 |
mrmanese |
1 |
#include "R.h"
|
|
|
2 |
#include "Rdefines.h"
|
|
|
3 |
#include "Rinternals.h"
|
|
|
4 |
#include "sqlite3.h"
|
| 4160 |
mrmanese |
5 |
#include "exports.h"
|
| 3255 |
mrmanese |
6 |
|
|
|
7 |
#ifndef __SQLITE_DATAFRAME__
|
|
|
8 |
#define __SQLITE_DATAFRAME__
|
|
|
9 |
|
| 3446 |
mrmanese |
10 |
/* # of sql buffers */
|
|
|
11 |
#define NBUFS 4
|
|
|
12 |
|
|
|
13 |
#ifdef __SQLITE_WORKSPACE__
|
|
|
14 |
SEXP SDF_RowNamesSymbol;
|
| 3456 |
mrmanese |
15 |
SEXP SDF_VectorTypeSymbol;
|
| 3446 |
mrmanese |
16 |
SEXP SDF_DimSymbol;
|
|
|
17 |
SEXP SDF_DimNamesSymbol;
|
|
|
18 |
#else
|
|
|
19 |
extern SEXP SDF_RowNamesSymbol;
|
| 3456 |
mrmanese |
20 |
extern SEXP SDF_VectorTypeSymbol;
|
| 3446 |
mrmanese |
21 |
extern SEXP SDF_DimSymbol;
|
|
|
22 |
extern SEXP SDF_DimNamesSymbol;
|
|
|
23 |
|
|
|
24 |
extern sqlite3 *g_workspace;
|
|
|
25 |
extern char *g_sql_buf[NBUFS];
|
|
|
26 |
extern int g_sql_buf_sz[NBUFS];
|
|
|
27 |
#endif
|
|
|
28 |
|
| 3456 |
mrmanese |
29 |
/* sdf type attributes */
|
|
|
30 |
#define GET_SDFVECTORTYPE(x) getAttrib(x, SDF_VectorTypeSymbol)
|
|
|
31 |
#define SET_SDFVECTORTYPE(x,t) setAttrib(x, SDF_VectorTypeSymbol, t)
|
|
|
32 |
#define TEST_SDFVECTORTYPE(x, t) (strcmp(CHAR(asChar(GET_SDFVECTORTYPE(x))), t) == 0)
|
|
|
33 |
#define GET_SDFROWNAMES(x) getAttrib(x, SDF_RowNamesSymbol)
|
| 3457 |
mrmanese |
34 |
#define SET_SDFROWNAMES(x, r) setAttrib(x, SDF_RowNamesSymbol, r)
|
| 3456 |
mrmanese |
35 |
|
| 3457 |
mrmanese |
36 |
#ifndef SET_ROWNAMES
|
|
|
37 |
#define SET_ROWNAMES(x, r) setAttrib(x, R_RowNamesSymbol, r)
|
|
|
38 |
#endif
|
|
|
39 |
|
| 3419 |
mrmanese |
40 |
#define WORKSPACE_COLUMNS 6
|
| 3308 |
mrmanese |
41 |
#define MAX_ATTACHED 30 /* 31 including workspace.db */
|
| 3255 |
mrmanese |
42 |
|
| 3307 |
mrmanese |
43 |
/* utilities for checking characteristics of arg */
|
| 3255 |
mrmanese |
44 |
int _is_r_sym(char *sym);
|
|
|
45 |
int _file_exists(char *filename);
|
| 3307 |
mrmanese |
46 |
int _sdf_exists2(char *iname);
|
|
|
47 |
|
|
|
48 |
/* sdf utilities */
|
| 3419 |
mrmanese |
49 |
int USE_SDF1(const char *iname, int exists, int protect); /* call this before doing anything on an SDF */
|
|
|
50 |
int UNUSE_SDF2(const char *iname); /* somewhat like UNPROTECT */
|
| 3307 |
mrmanese |
51 |
SEXP _create_sdf_sexp(const char *iname); /* create a SEXP for an SDF */
|
|
|
52 |
int _add_sdf1(char *filename, char *internal_name); /* add SDF to workspace */
|
| 3324 |
mrmanese |
53 |
void _delete_sdf2(const char *iname); /* remove SDF from workspace */
|
| 4160 |
mrmanese |
54 |
int _get_factor_levels1(const char *iname, const char *varname, SEXP var, int set_class);
|
| 3358 |
mrmanese |
55 |
int _get_row_count2(const char *table, int quote);
|
| 3307 |
mrmanese |
56 |
SEXP _get_rownames(const char *sdf_iname);
|
|
|
57 |
char *_get_full_pathname2(char *relpath); /* get full path given relpath, used in workspace mgmt */
|
| 3419 |
mrmanese |
58 |
int _is_factor2(const char *iname, const char *factor_type, const char *colname);
|
|
|
59 |
SEXP _get_rownames2(const char *sdf_iname);
|
| 3255 |
mrmanese |
60 |
|
| 4160 |
mrmanese |
61 |
/* utilities for creating SDFs */
|
| 3419 |
mrmanese |
62 |
char *_create_sdf_skeleton1(SEXP name, int *o_namelen, int protect);
|
| 3358 |
mrmanese |
63 |
int _copy_factor_levels2(const char *factor_type, const char *iname_src,
|
|
|
64 |
const char *colname_src, const char *iname_dst, const char *colname_dst);
|
| 3397 |
mrmanese |
65 |
int _create_factor_table2(const char *iname, const char *factor_type,
|
|
|
66 |
const char *colname);
|
| 3419 |
mrmanese |
67 |
char *_create_svector1(SEXP name, const char *type, int * _namelen, int protect);
|
| 3307 |
mrmanese |
68 |
|
| 4160 |
mrmanese |
69 |
/* utilities for creating SVecs */
|
|
|
70 |
int _get_vector_index_typed_result(sqlite3_stmt *stmt, SEXP *ret, int colidx,
|
|
|
71 |
int idx_or_len, int *coltype);
|
|
|
72 |
|
| 3307 |
mrmanese |
73 |
/* R utilities */
|
|
|
74 |
SEXP _getListElement(SEXP list, char *varname);
|
|
|
75 |
SEXP _shrink_vector(SEXP vec, int len); /* shrink vector size */
|
|
|
76 |
|
|
|
77 |
/* sqlite utilities */
|
|
|
78 |
int _empty_callback(void *data, int ncols, char **row, char **cols);
|
| 3397 |
mrmanese |
79 |
int _sqlite_error_check(int res, const char *file, int line);
|
| 3307 |
mrmanese |
80 |
const char *_get_column_type(const char *class, int type); /* get sqlite type corresponding to R class & type */
|
| 3397 |
mrmanese |
81 |
sqlite3* _is_sqlitedb(char *filename);
|
| 3473 |
mrmanese |
82 |
void _init_sqlite_function_accumulator();
|
| 3307 |
mrmanese |
83 |
|
|
|
84 |
/* global buffer (g_sql_buf) utilities */
|
| 3821 |
mrmanese |
85 |
R_INLINE void _expand_buf(int i, int size); /* expand ith buf if size > buf[i].size */
|
| 3307 |
mrmanese |
86 |
|
|
|
87 |
|
| 3397 |
mrmanese |
88 |
/* workspace utilities */
|
|
|
89 |
int _prepare_attach2(); /* prepare workspace before attaching a sqlite db */
|
|
|
90 |
|
| 3684 |
mrmanese |
91 |
/* sqlite vector utilities */
|
|
|
92 |
SEXP _create_svector_sexp(const char *iname, const char *tblname, const char *varname, const char *type);
|
|
|
93 |
|
| 3307 |
mrmanese |
94 |
/* misc utilities */
|
|
|
95 |
char *_r2iname(char *internal_name, char *filename);
|
|
|
96 |
char *_fixname(char *rname);
|
| 3397 |
mrmanese |
97 |
char *_str_tolower(char *out, const char *ref);
|
| 3307 |
mrmanese |
98 |
|
|
|
99 |
/* register functions to sqlite */
|
|
|
100 |
void __register_vector_math();
|
|
|
101 |
|
| 3255 |
mrmanese |
102 |
#define _sqlite_exec(sql) sqlite3_exec(g_workspace, sql, _empty_callback, NULL, NULL)
|
| 3419 |
mrmanese |
103 |
#define _sqlite_error(res) _sqlite_error_check((res), __FILE__, __LINE__)
|
|
|
104 |
|
|
|
105 |
#ifdef __SQLITE_DEBUG__
|
|
|
106 |
#define _sqlite_begin { _sqlite_error(_sqlite_exec("begin")); Rprintf("begin at " __FILE__ " line %d\n", __LINE__); }
|
|
|
107 |
#define _sqlite_commit { _sqlite_error(_sqlite_exec("commit")); Rprintf("commit at " __FILE__ " line %d\n", __LINE__); }
|
|
|
108 |
#else
|
|
|
109 |
#define _sqlite_begin _sqlite_error(_sqlite_exec("begin"))
|
|
|
110 |
#define _sqlite_commit _sqlite_error(_sqlite_exec("commit"))
|
|
|
111 |
#endif
|
|
|
112 |
|
| 3255 |
mrmanese |
113 |
/* R object accessors shortcuts */
|
|
|
114 |
#define CHAR_ELT(str, i) CHAR(STRING_ELT(str,i))
|
|
|
115 |
|
|
|
116 |
/* SDF object accessors shortcuts */
|
|
|
117 |
#define SDF_INAME(sdf) CHAR(STRING_ELT(_getListElement(sdf, "iname"),0))
|
| 3684 |
mrmanese |
118 |
#define SVEC_TBLNAME(sdf) CHAR(STRING_ELT(_getListElement(sdf, "tblname"),0))
|
| 3255 |
mrmanese |
119 |
#define SVEC_VARNAME(sdf) CHAR(STRING_ELT(_getListElement(sdf, "varname"),0))
|
|
|
120 |
|
|
|
121 |
/* possible var types when stored in sqlite as integer */
|
|
|
122 |
#define VAR_INTEGER 0
|
|
|
123 |
#define VAR_FACTOR 1
|
|
|
124 |
#define VAR_ORDERED 2
|
|
|
125 |
|
| 3307 |
mrmanese |
126 |
/* detail constants (see _get_sdf_detail2 in sqlite_workspace.c) */
|
|
|
127 |
#define SDF_DETAIL_EXISTS 0
|
|
|
128 |
#define SDF_DETAIL_FULLFILENAME 1
|
| 3255 |
mrmanese |
129 |
|
| 3358 |
mrmanese |
130 |
/* R SXP type constants */
|
|
|
131 |
#define FACTORSXP 11
|
|
|
132 |
#define ORDEREDSXP 12
|
|
|
133 |
|
| 4160 |
mrmanese |
134 |
/* additional column types */
|
|
|
135 |
#define SQLITEDF_BIT 100
|
| 3255 |
mrmanese |
136 |
|
|
|
137 |
#endif
|