/
BookHook.cpp.in
279 lines (258 loc) · 8.64 KB
/
BookHook.cpp.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <coopy/PolyBook.h>
#include <coopy/ShortTextBook.h>
#include <coopy/FormatDesc.h>
#include <fstream>
using namespace std;
using namespace coopy::store;
#cmakedefine USE_NONE
#ifndef USE_NONE
#cmakedefine USE_GNUMERIC
#cmakedefine USE_REMOTE_SQL
#cmakedefine USE_ACCESS
#cmakedefine USE_SOCIALCALC
#cmakedefine USE_JACKCESS
#cmakedefine USE_SQLITE
#cmakedefine USE_JSON
#cmakedefine USE_MYSQL
#cmakedefine USE_POSTGRES
#endif
#ifdef USE_GNUMERIC
#include <coopy/GnumericTextBookFactory.h>
using namespace coopy::store::gnumeric;
#endif
#ifdef USE_ACCESS
#include <coopy/AccessTextBook.h>
using namespace coopy::store::mdb;
#endif
#ifdef USE_SOCIALCALC
#include <coopy/SocialCalcTextBookFactory.h>
using namespace coopy::store::socialcalc;
#endif
#ifdef USE_JACKCESS
#include <coopy/JackTextBook.h>
using namespace coopy::store::mdb;
#endif
#ifdef USE_REMOTE_SQL
#include <coopy/RemoteSqlTextBook.h>
using namespace coopy::store::remotesql;
#endif
#ifdef USE_SQLITE
#include <coopy/SqliteTextBook.h>
using namespace coopy::store::sqlite;
#endif
#ifdef USE_JSON
#include <json/json.h>
#include <coopy/JsonProperty.h>
#include <coopy/JsonBook.h>
using namespace coopy::store::json;
#endif
#define STRVAL PolyValue::makeString
void gatherFactories(vector<TextBookFactory *>& lst,
vector<FormatDesc>& descs,
bool preview) {
#ifdef USE_SQLITE
if (preview) {
FormatDesc sqlite("SQLITE: file-based database");
sqlite.addExtension(".sqlite","Sqlite database file");
sqlite.addDbi("dbi:sqlite:fname.db","Force sqlite interpretation");
sqlite.addOption("type",STRVAL("sqlite"),"Sqlite family",true);
sqlite.addOption("file",STRVAL("fname.db"),"File name",true);
descs.push_back(sqlite);
FormatDesc sqlitext("SQLITEXT: sqlite-format sql dump");
sqlitext.addExtension(".sqlitext","SQL dump of Sqlite database");
sqlitext.addDbi("dbi:sqlitext:fname.sql","Force sqlitext interpretation");
sqlitext.addOption("type",STRVAL("sqlitext"),"Sqlitext family",true);
sqlitext.addOption("file",STRVAL("fname.sql"),"File name",true);
descs.push_back(sqlitext);
} else {
lst.push_back(new SqliteTextBookFactory());
lst.push_back(new SqliteTextBookFactory(true));
}
#endif
#ifdef USE_JSON
if (preview) {
FormatDesc desc("JSONBOOK: Spreadsheet formats in json");
desc.addExtension(".jsonbook","Json spreadsheet");
desc.addDbi("dbi:jsonbook:fname.sheet",
"Force Json spreadsheet interpretation");
desc.addOption("type",STRVAL("jsonbook"),"Json spreadsheet",true);
desc.addOption("file",STRVAL("fname.sheet"),"File name",true);
desc.addOption("hash",STRVAL("1"),"Generate hash/dict/objects for each row when schema available",true);
descs.push_back(desc);
} else {
lst.push_back(new JsonBookFactory());
}
#endif
#ifdef USE_GNUMERIC
if (preview) {
FormatDesc desc("GNUMERIC: Spreadsheet formats (via gnumeric)");
desc.addExtension(".xls","Excel spreadsheet");
desc.addExtension(".xlsx","Excel spreadsheet");
desc.addExtension(".gnumeric","Gnumeric spreadsheet");
desc.addExtension(".ods","OpenOffice/LibreOffice spreadsheet");
desc.addDbi("dbi:gnumeric:fname.sheet","Force Gnumeric interpretation");
desc.addOption("type",STRVAL("gnumeric"),"Gnumeric family",true);
desc.addOption("file",STRVAL("fname.sheet"),"File name",true);
descs.push_back(desc);
} else {
lst.push_back(new GnumericTextBookFactory());
}
#endif
#ifdef USE_ACCESS
if (preview) {
FormatDesc desc("MDB: Access database format (via Mdbtools, READ-ONLY)");
desc.addExtension(".mdb","Access database file");
desc.addDbi("dbi:access:fname.db","Force Access interpretation");
desc.addOption("type",STRVAL("access"),"Access family",true);
desc.addOption("file",STRVAL("fname.db"),"File name",true);
descs.push_back(desc);
} else {
lst.push_back(new AccessTextBookFactory());
}
#endif
#ifdef USE_JACKCESS
if (preview) {
FormatDesc desc("JMDB: Access database format (via Jackcess)");
desc.addExtension(".mdb","Access database file");
desc.addDbi("dbi:jackess:fname.db","Force Access interpretation");
desc.addOption("type",STRVAL("access"),"Jackcess/access family",true);
desc.addOption("file",STRVAL("fname.db"),"File name",true);
descs.push_back(desc);
} else {
lst.push_back(new JackTextBookFactory());
}
#endif
#ifdef USE_MYSQL
if (preview) {
FormatDesc desc("MYSQL: database connector");
desc.addDbi("dbi:mysql:database_name","Use default port, username, etc");
desc.addDbi("dbi:mysql:database_name:username=USER:password=PASS","");
desc.addDbi("dbi:mysql:database_name:host=HOST:port=PORT","");
desc.addOption("type",STRVAL("mysql"),"MYSQL connector",true);
desc.addOption("database",STRVAL("db_name"),"Database name",true);
desc.addOption("host",STRVAL("localhost"),"Host name",true);
desc.addOption("port",STRVAL("1111"),"Port number",true);
desc.addOption("username",STRVAL("root"),"Username",true);
desc.addOption("password",STRVAL("****"),"Password",true);
descs.push_back(desc);
} else {
lst.push_back(new RemoteSqlTextBookFactory("mysql"));
}
#endif
#ifdef USE_POSTGRES
if (preview) {
FormatDesc desc("PostgreSQL: database connector");
desc.addDbi("dbi:pg:database_name","Use default port, username, etc");
desc.addDbi("dbi:pg:database_name:username=USER:password=PASS","");
desc.addDbi("dbi:pg:database_name:host=HOST:port=PORT","");
desc.addOption("type",STRVAL("pg"),"PostgreSQL connector",true);
desc.addOption("database",STRVAL("db_name"),"Database name",true);
desc.addOption("host",STRVAL("localhost"),"Host name",true);
desc.addOption("port",STRVAL("1111"),"Port number",true);
desc.addOption("username",STRVAL("root"),"Username",true);
desc.addOption("password",STRVAL("****"),"Password",true);
descs.push_back(desc);
} else {
lst.push_back(new RemoteSqlTextBookFactory("pg"));
lst.push_back(new RemoteSqlTextBookFactory("postgres"));
}
#endif
#ifdef USE_SOCIALCALC
if (preview) {
FormatDesc desc("SOCIALCALC: SocialCalc format (via mozjs)");
desc.addExtension(".socialcalc","SocialCalc spreadsheet file");
desc.addExtension(".sc","SocialCalc spreadsheet file");
desc.addDbi("dbi:socialcalc:sheet.txt","Force SocialCalc interpretation");
desc.addOption("type",STRVAL("socialcalc"),"SocialCalc family",true);
desc.addOption("file",STRVAL("sheet.txt"),"File name",true);
descs.push_back(desc);
} else {
lst.push_back(new SocialCalcTextBookFactory());
}
#endif
}
void getFactories(vector<TextBookFactory *>& lst) {
vector<FormatDesc> descs;
gatherFactories(lst,descs,false);
}
void getFactoriesList(vector<FormatDesc>& descs) {
vector<TextBookFactory *> lst;
gatherFactories(lst,descs,true);
}
TextBook *readHelper(const char *fname,
const char *ext,
const char *data) {
dbg_printf("Trying plugin data formats...\n");
TextBook *result = NULL;
if (string(ext)==".json") {
Property p;
#ifdef USE_JSON
dbg_printf("Trying with JSON...\n");
if (!JsonProperty::add(p,fname)) return NULL;
#else
fprintf(stderr,"Compiled without support for JSON\n");
return NULL;
#endif
TextBook *book = NULL;
string key = p.get("type",PolyValue::makeString("none")).asString();
if (key=="mysql") {
#ifdef USE_MYSQL
book = new RemoteSqlTextBook("mysql");
#endif
} else if (key=="pg"||key=="postgres") {
#ifdef USE_POSTGRES
book = new RemoteSqlTextBook("pg");
#endif
} else if (key=="csv") {
book = new ShortTextBook();
} else if (key=="sqlite") {
#ifdef USE_SQLITE
dbg_printf("Trying sqlite\n");
book = new SqliteTextBook();
#endif
} else if (key=="sqlitext") {
#ifdef USE_SQLITE
dbg_printf("Trying sqlitext\n");
book = new SqliteTextBook(true);
#endif
} else {
fprintf(stderr,"Type not recognized: %s\n", key.c_str());
return NULL;
}
if (book!=NULL) {
if (!book->open(p)) {
fprintf(stderr,"Failed to read data source\n");
delete book;
} else {
result = book;
return result;
}
}
}
#ifdef USE_GNUMERIC
/*
dbg_printf("Trying with Gnumeric...\n");
GnumericTextBookFactory fact;
if (fact.check(fname,ext,data)) {
result = fact.load(fname);
}
if (result!=NULL) {
dbg_printf("Got a Gnumeric-readable file\n");
return result;
} else {
dbg_printf("Not a Gnumeric-readable file\n");
}
*/
#endif
return result;
}
bool readHelperJson(coopy::store::Property& config,
const char *filename) {
dbg_printf("Asked to attach, with json configuration\n");
#ifdef USE_JSON
return JsonProperty::add(config,filename);
#else
fprintf(stderr,"Compiled without support for JSON\n");
return false;
#endif
}