| 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 */
|
| 4798 |
mrmanese |
44 |
int _is_r_sym(const char *sym);
|
| 3255 |
mrmanese |
45 |
int _file_exists(char *filename);
|
| 4798 |
mrmanese |
46 |
int _sdf_exists2(const char *iname);
|
| 3307 |
mrmanese |
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 */
|
| 4798 |
mrmanese |
52 |
int _add_sdf1(const char *filename, const 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);
|
| 4798 |
mrmanese |
57 |
char *_get_full_pathname2(const 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 */
|
| 4798 |
mrmanese |
74 |
SEXP _getListElement(SEXP list, const char *varname);
|
| 3307 |
mrmanese |
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 */
|
| 4590 |
mrmanese |
81 |
int _get_r_type(const char *decl_type); /* get r type based on sqlite decltype */
|
| 4798 |
mrmanese |
82 |
sqlite3* _is_sqlitedb(const char *filename);
|
| 3473 |
mrmanese |
83 |
void _init_sqlite_function_accumulator();
|
| 4590 |
mrmanese |
84 |
int *_make_row_index(SEXP idx, int *plen);
|
| 3307 |
mrmanese |
85 |
|
|
|
86 |
/* global buffer (g_sql_buf) utilities */
|
| 3821 |
mrmanese |
87 |
R_INLINE void _expand_buf(int i, int size); /* expand ith buf if size > buf[i].size */
|
| 3307 |
mrmanese |
88 |
|
|
|
89 |
|
| 3397 |
mrmanese |
90 |
/* workspace utilities */
|
|
|
91 |
int _prepare_attach2(); /* prepare workspace before attaching a sqlite db */
|
|
|
92 |
|
| 3684 |
mrmanese |
93 |
/* sqlite vector utilities */
|
|
|
94 |
SEXP _create_svector_sexp(const char *iname, const char *tblname, const char *varname, const char *type);
|
|
|
95 |
|
| 3307 |
mrmanese |
96 |
/* misc utilities */
|
|
|
97 |
char *_r2iname(char *internal_name, char *filename);
|
|
|
98 |
char *_fixname(char *rname);
|
| 3397 |
mrmanese |
99 |
char *_str_tolower(char *out, const char *ref);
|
| 3307 |
mrmanese |
100 |
|
|
|
101 |
/* register functions to sqlite */
|
|
|
102 |
void __register_vector_math();
|
|
|
103 |
|
| 4590 |
mrmanese |
104 |
#define _sqlite_exec(sql) sqlite3_exec(g_workspace, sql, NULL, NULL, NULL)
|
| 3419 |
mrmanese |
105 |
#define _sqlite_error(res) _sqlite_error_check((res), __FILE__, __LINE__)
|
|
|
106 |
|
|
|
107 |
#ifdef __SQLITE_DEBUG__
|
|
|
108 |
#define _sqlite_begin { _sqlite_error(_sqlite_exec("begin")); Rprintf("begin at " __FILE__ " line %d\n", __LINE__); }
|
|
|
109 |
#define _sqlite_commit { _sqlite_error(_sqlite_exec("commit")); Rprintf("commit at " __FILE__ " line %d\n", __LINE__); }
|
|
|
110 |
#else
|
|
|
111 |
#define _sqlite_begin _sqlite_error(_sqlite_exec("begin"))
|
|
|
112 |
#define _sqlite_commit _sqlite_error(_sqlite_exec("commit"))
|
|
|
113 |
#endif
|
|
|
114 |
|
| 3255 |
mrmanese |
115 |
/* R object accessors shortcuts */
|
|
|
116 |
#define CHAR_ELT(str, i) CHAR(STRING_ELT(str,i))
|
|
|
117 |
|
|
|
118 |
/* SDF object accessors shortcuts */
|
|
|
119 |
#define SDF_INAME(sdf) CHAR(STRING_ELT(_getListElement(sdf, "iname"),0))
|
| 3684 |
mrmanese |
120 |
#define SVEC_TBLNAME(sdf) CHAR(STRING_ELT(_getListElement(sdf, "tblname"),0))
|
| 3255 |
mrmanese |
121 |
#define SVEC_VARNAME(sdf) CHAR(STRING_ELT(_getListElement(sdf, "varname"),0))
|
|
|
122 |
|
|
|
123 |
/* possible var types when stored in sqlite as integer */
|
|
|
124 |
#define VAR_INTEGER 0
|
|
|
125 |
#define VAR_FACTOR 1
|
|
|
126 |
#define VAR_ORDERED 2
|
|
|
127 |
|
| 3307 |
mrmanese |
128 |
/* detail constants (see _get_sdf_detail2 in sqlite_workspace.c) */
|
|
|
129 |
#define SDF_DETAIL_EXISTS 0
|
|
|
130 |
#define SDF_DETAIL_FULLFILENAME 1
|
| 3255 |
mrmanese |
131 |
|
| 3358 |
mrmanese |
132 |
/* R SXP type constants */
|
|
|
133 |
#define FACTORSXP 11
|
|
|
134 |
#define ORDEREDSXP 12
|
|
|
135 |
|
| 4160 |
mrmanese |
136 |
/* additional column types */
|
|
|
137 |
#define SQLITEDF_BIT 100
|
| 3255 |
mrmanese |
138 |
|
|
|
139 |
#endif
|