Debian Codegraph @

biosig4c++/t210/sopen_sqlite.c

raw symbols SCIP
1 /*
2
3 Copyright (C) 2021 Alois Schloegl <[email protected]>
4
5 This file is part of the "BioSig for C/C++" repository
6 (biosig4c++) at http://biosig.sf.net/
7
8 BioSig is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 3
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 */
22
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef WITH_SQLITE3
31 #include <sqlite3.h>
32 #endif
33 #include "../biosig.h"
34
35
36 #ifdef WITH_SQLITE3
37 static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
38 for(int i=0; i<argc; i++)
39 printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
40 printf("\n");
41 return 0;
42 }
43
44 void printColumnValue(sqlite3_stmt* stmt, int col) {
45
46 int colType = sqlite3_column_type(stmt, col);
47
48 switch(colType) {
49
50 case SQLITE_INTEGER:
51 printf(" %3d ", sqlite3_column_int(stmt, col));
52 break;
53
54 case SQLITE_FLOAT:
55 printf(" %5.2f", sqlite3_column_double(stmt, col));
56 break;
57
58 case SQLITE_TEXT:
59 printf(" %-5s", sqlite3_column_text(stmt, col));
60 break;
61
62 case SQLITE_NULL:
63 printf(" null");
64 break;
65
66 case SQLITE_BLOB:
67 printf(" blob");
68 break;
69 }
70
71 }
72 #endif
73
74 int sopen_sqlite(HDRTYPE* hdr) {
75 #ifdef WITH_SQLITE3
76 sclose(hdr);
77
78 /* refererence to libsqlite3:
79 https://www.sqlite.org/cintro.html
80 */
81 const SIZE_zSql=10000;
82 char zErrMsg[SIZE_zSql];*zErrMsg=0;
83 int rc;
84 sqlite3 *db; /* Database handle */
85 sqlite3_blob *Blob;
86
87 if (VERBOSE_LEVEL>6) fprintf(stdout,"%s (line %d): %s(...) libsqlite v%s is used\n", __FILE__,__LINE__,__func__,sqlite3_libversion());
88
89 //sqlite3_initialize();
90 rc = sqlite3_open(hdr->FileName, &db);
91 if (rc) {
92 biosigERROR(hdr, B4C_CANNOT_OPEN_FILE, "can not open (sqlite) file.");
93 sqlite3_close(db);
94 return;
95 }
96
97 if (VERBOSE_LEVEL>6) fprintf(stdout,"%s (line %d): %s(...)rc=%d\n", __FILE__,__LINE__,__func__,rc);
98
99 /*
100 Identify whether it is an Cadwell EZDATA file or not
101 Cadwell has these tables:
102 */
103 char zSql[SIZE_zSql];
104 strcpy(zSql, "SELECT COUNT(*) AS x FROM FrameInfo;"
105 "SELECT COUNT(*) AS a FROM MiscInfo;"
106 "SELECT COUNT(*) AS b FROM TrackInfoSyncRowData;"
107 "SELECT COUNT(*) AS c FROM MaxUpdateTick;"
108 "SELECT COUNT(*) AS d FROM MiscInfoSyncRowData;"
109 "SELECT COUNT(*) AS e FROM syncrowdata;"
110 "SELECT COUNT(*) AS f FROM MediaHeader;"
111 "SELECT COUNT(*) AS g FROM SchemaUpdateLog;"
112 "SELECT COUNT(*) AS h FROM synctable;"
113 "SELECT COUNT(*) AS i FROM MediaHeaderSyncRowData;"
114 "SELECT COUNT(*) AS j FROM TrackInfo;");
115
116 rc = sqlite3_exec(db, zSql, callback, 0, zErrMsg);
117 if( rc != SQLITE_OK ) {
118 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format SQLite: not supported yet - not an ARC(Cadwell) file.");
119 fprintf(stderr, "SQL error: %s\n", zErrMsg);
120 sqlite3_free(zErrMsg);
121 }
122
123 if (VERBOSE_LEVEL>6) fprintf(stdout,"%s (line %d) %s(...) most likely a Cadwell file, because all tables are available.\n",__FILE__,__LINE__,__func__);
124 const char *ARC_TableList[]={"FrameInfo", "MiscInfo", "TrackInfoSyncRowData", "MaxUpdateTick", "MiscInfoSyncRowData", "syncrowdata", "MediaHeader", "SchemaUpdateLog", "synctable", "MediaHeaderSyncRowData", "TrackInfo", NULL};
125
126 // https://www.sqlite.org/c3ref/prepare.html
127 /* SQL statement, UTF-8 encoded */
128 unsigned int prepFlags = 0;
129 sqlite3_stmt *pStmt = NULL; /* OUT: Statement handle */
130 const char *zTail = NULL; /* OUT: Pointer to unused portion of zSql */
131
132 if (VERBOSE_LEVEL>6) fprintf(stdout,"%s (line %d): %s(...)rc=%d\n", __FILE__, __LINE__, __func__, rc);
133
134 for (int k=0; ARC_TableList[k]!=NULL; k++) {
135 snprintf(zSql, SIZE_zSql, "SELECT * FROM %s", ARC_TableList[k]);
136 rc = sqlite3_prepare_v2(db, zSql, sizeof(zSql), &pStmt, &zTail);
137
138 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %d): %s(...)rc=%d <-prepare(db,\"%s\",...)\n", __FILE__,__LINE__,__func__, rc, zSql);
139
140 int M = 0;
141 while (SQLITE_ROW==sqlite3_step(pStmt)) {
142 M++;
143
144 if (M==1) for (int col = 0; ; col++) {
145 int typ = sqlite3_column_type(pStmt, col);
146 if (typ==SQLITE_NULL) break;
147 char *DatabaseName = sqlite3_column_database_name(pStmt,col);
148 char *TableName = sqlite3_column_table_name(pStmt,col);
149 char *ColumnName = sqlite3_column_origin_name(pStmt,col);
150 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %d): %s(...) col%d: %s.%s.%s\n", \
151 __FILE__,__LINE__,__func__, col+1, DatabaseName, TableName, ColumnName);
152 }
153
154 for (int col = 0; ; col++) {
155 int T,I;
156 int64_t I64;
157 char *S;
158
159 if (M > 10) break; // this breaks only the inner loop and stopping the output/fprintf, only up to M rows are displayed
160
161 size_t sz = sqlite3_column_bytes(pStmt, col);
162 int typ = sqlite3_column_type(pStmt, col);
163
164 if (typ==SQLITE_NULL) break;
165
166 switch (typ) {
167 case SQLITE_INTEGER: { // 1
168 double V = sqlite3_column_int64(pStmt, col);
169 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %d): %s(...) r%d,c%d sz%d I<%ld>\n", \
170 __FILE__,__LINE__,__func__, M, col+1, sz, V);
171 break;
172 }
173 case SQLITE_FLOAT: { // 2
174 double V = sqlite3_column_double(pStmt, col);
175 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %d): %s(...) r%d,c%d sz%d F<%g>\n", \
176 __FILE__,__LINE__,__func__, M, col+1, sz, V);
177 break;
178 }
179 case SQLITE_TEXT: { // 3
180 char* V = sqlite3_column_text(pStmt, col);
181 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %d): %s(...) r%d,c%d sz%d T<%s>\n", \
182 __FILE__,__LINE__,__func__, M, col+1, sz, V);
183 break;
184 }
185 case SQLITE_BLOB: { // 4
186 void* V = sqlite3_column_blob(pStmt, col);
187 if (VERBOSE_LEVEL>7)
188 fprintf(stdout,"%s (line %d): %s(...) r%d,c%d sz%d 0x%016x\n", \
189 __FILE__,__LINE__,__func__, M, col+1, sz, leu64p(V));
190 if (VERBOSE_LEVEL>8) {
191 char A[33]; A[32]=0;
192 for (int k=0; k<sz; k++) {
193 int ks = k%32;
194 if (ks==0) fprintf(stdout,"\n%08x ",k);
195 fprintf(stdout,"%02x ",*(uint8_t*)(V+k));
196 A[ks] = *(uint8_t*)(V+k);
197 if (!isprint(A[ks])) A[ks]='.';
198 if (ks==31) fprintf(stdout," %s",A);
199 }
200 fprintf(stdout,"\n");
201 }
202 break;
203 }
204 case SQLITE_NULL: // 5
205 break;
206 otherwise:
207 ;
208 }
209 // if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %d): %s(...) rows=%d T%d %d %ld %s\n", __FILE__,__LINE__,__func__, M,T,I,I64,S);
210 } // for
211 } // while
212
213 if (VERBOSE_LEVEL>6) fprintf(stdout,"%s (line %d): %s(...) rows=%d\n", __FILE__,__LINE__,__func__, M);
214
215 // https://www.sqlite.org/c3ref/finalize.html
216 rc = sqlite3_finalize(pStmt);
217 if (VERBOSE_LEVEL>6) fprintf(stdout,"%s (line %d): %s(...)rc=%d <-finalize(...) \n", __FILE__,__LINE__,__func__, rc);
218 }
219
220 if (VERBOSE_LEVEL > 6) fprintf(stdout,"%s (line %d) %s(...) rc=%d\n", __FILE__,__LINE__,__func__,rc);
221
222 sqlite3_close(db);
223 sqlite3_shutdown();
224
225 /* TODO:
226 - check whether it is a recognized and supported data set (i.e. Cadwell/ARC format)
227 - fill in HDRstruct* hdr
228 - extract data samples and fill hdr->data
229 - fill event table if applicable
230 Once these steps are completed, the following line can be removed
231 */
232 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format SQLite: not supported yet.");
233
234 #else // WITH_SQLITE3
235
236 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "SOPEN(SQLite): - sqlite format not supported - libbiosig need to be recompiled with libsqlite3 support.");
237 #endif // WITH_SQLITE3
238 }
239