YARP
Yet Another Robot Platform
TripleSourceCreator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
11 
12 #include <yarp/conf/compiler.h>
14 
15 #if !defined(_WIN32)
16 #include <unistd.h>
17 #else
18 #include <io.h>
19 #define access(f,a) _access(f,a)
20 #endif
21 
22 #ifndef F_OK
23 #define F_OK 0
24 #endif
25 
26 #include <string>
27 using namespace yarp::serversql::impl;
28 using namespace std;
29 
30 
31 static bool sql_enact(sqlite3 *db, const char *cmd) {
32  //printf("ISSUE %s\n", cmd);
33  int result = sqlite3_exec(db, cmd, nullptr, nullptr, nullptr);
34  if (result!=SQLITE_OK) {
35  const char *msg = sqlite3_errmsg(db);
36  if (msg != nullptr) {
37  fprintf(stderr,"Database error: %s\n", msg);
38  }
39  sqlite3_close(db);
40  fprintf(stderr,"Failed to set up database tables\n");
41  std::exit(1);
42  }
43  return true;
44 }
45 
46 
48  bool cautious,
49  bool fresh) {
50  sqlite3 *db = nullptr;
51  if (fresh) {
52  int result = access(filename,F_OK);
53  if (result==0) {
54  fprintf(stderr,"Database needs to be recreated.\n");
55  fprintf(stderr,"Please move %s out of the way.\n", filename);
56  return nullptr;
57  }
58 
59  }
60  int result = sqlite3_open_v2(filename,
61  &db,
62  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_NOMUTEX,
63  nullptr);
64  if (result!=SQLITE_OK) {
65  fprintf(stderr,"Failed to open database %s\n", filename);
66  if (db != nullptr) {
67  sqlite3_close(db);
68  }
69  return nullptr;
70  }
71 
72 
73  string create_main_table = "CREATE TABLE IF NOT EXISTS tags (\n\
74  id INTEGER PRIMARY KEY,\n\
75  rid INTEGER,\n\
76  ns TEXT,\n\
77  name TEXT,\n\
78  value TEXT);";
79 
80  result = sqlite3_exec(db, create_main_table.c_str(), nullptr, nullptr, nullptr);
81  if (result!=SQLITE_OK) {
82  const char *msg = sqlite3_errmsg(db);
83  if (msg != nullptr) {
84  fprintf(stderr,"Error in %s: %s\n", filename, msg);
85  }
86  sqlite3_close(db);
87  fprintf(stderr,"Failed to set up database tables\n");
88  std::exit(1);
89  }
90 
91  string cmd_synch = string("PRAGMA synchronous=") + (cautious?"FULL":"OFF") + ";";
92  sql_enact(db,cmd_synch.c_str());
93 
94  sql_enact(db,"CREATE INDEX IF NOT EXISTS tagsRidNameValue on tags(rid,name,value);");
95 
96  implementation = db;
97  accessor = new SqliteTripleSource(db);
98  return accessor;
99 }
100 
101 
103  if (accessor != nullptr) {
104  delete accessor;
105  accessor = nullptr;
106  }
107  if (implementation != nullptr) {
108  auto* db = (sqlite3 *)implementation;
109  sqlite3_close(db);
110  implementation = nullptr;
111  }
112  return true;
113 }
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
static bool sql_enact(sqlite3 *db, const char *cmd)
#define F_OK
Sqlite database, viewed as a collection of triples.
TripleSource * open(const char *filename, bool cautious=false, bool fresh=false)
Abstract view of a database as a collection of triples.
Definition: TripleSource.h:44