Xmipp  v3.23.11-Nereus
xmipp_program_sql.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: J.M. de la Rosa Trevin (jmdelarosa@cnb.csic.es)
3  *
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include <sstream>
27 #include "xmipp_program_sql.h"
28 #include "metadata_label.h"
29 
30 void ProgramDb::init(const FileName &dbName)
31 {
32  rc = sqlite3_open(dbName.c_str(), &db);
33  sqlite3_exec(db, "PRAGMA temp_store=MEMORY",NULL, NULL, &errmsg);
34  sqlite3_exec(db, "PRAGMA synchronous=OFF",NULL, NULL, &errmsg);
35  sqlite3_exec(db, "PRAGMA count_changes=OFF",NULL, NULL, &errmsg);
36  sqlite3_exec(db, "PRAGMA page_size=4092",NULL, NULL, &errmsg);
37 }
38 
40 {
41  init(dbName);
42 }
43 
45 {
46  init(formatString("%s/.xmipp_programs.sqlite", getXmippPath()));
47 }
48 
49 bool ProgramDb::execStmt(const String &stmt, const String &error)
50 {
51  if (sqlite3_exec(db, stmt.c_str(), NULL, NULL, &errmsg) != SQLITE_OK)
52  {
53  std::cerr << error << ": " << errmsg << std::endl;
54  return false;
55  }
56  return true;
57 }
58 
60 {
61  return execStmt("BEGIN TRANSACTION", "Couldn't begin transaction: ");
62 }
63 
65 {
66  return execStmt("COMMIT TRANSACTION", "Couldn't commit transaction: ");
67 }
68 
71 {
72  const char * cmdStr =
73  "DROP TABLE IF EXISTS Category;"
74  "CREATE TABLE Category ("
75  " id INTEGER PRIMARY KEY ASC AUTOINCREMENT, "
76  " name TEXT UNIQUE, "
77  " desc TEXT, "
78  " prefixes TEXT);"
79  "INSERT INTO Category VALUES(NULL, 'Classification', NULL, 'classify_ ml_ mlf_');"
80  "INSERT INTO Category VALUES(NULL, 'CTF', NULL, 'ctf_');"
81  "INSERT INTO Category VALUES(NULL, 'Images', NULL, 'image_');"
82  "INSERT INTO Category VALUES(NULL, 'Metadatas', NULL, 'metadata_');"
83  "INSERT INTO Category VALUES(NULL, 'Phantoms', NULL, 'phantom_ pdb_');"
84  "INSERT INTO Category VALUES(NULL, 'Angular assignment', NULL, 'angular_');"
85  "INSERT INTO Category VALUES(NULL, 'Tomography', NULL, 'tomo_ xray_');"
86  "INSERT INTO Category VALUES(NULL, 'Transformations', NULL, 'transform_');"
87  "INSERT INTO Category VALUES(NULL, 'Volumes', NULL, 'volume_ reconstruct_ resolution_');"
88  "DROP TABLE IF EXISTS Program;"
89  "CREATE TABLE Program ("
90  "id INTEGER PRIMARY KEY ASC AUTOINCREMENT,"
91  "category_id INTEGER, name TEXT UNIQUE, usage TEXT, examples TEXT,"
92  "keywords TEXT);";
93 
94  return execStmt(cmdStr, "Couldn't create Program table: ");
95 }
96 
98 {
99  size_t pos = 0;
100  while ((pos = str.find_first_of("'", pos)) != String::npos)
101  {
102  str.replace(pos, 1, "''");
103  pos += 2;
104  }
105  str = "'" + str + "'";
106  return str;
107 }
108 
111 {
113  //deleteProgramByName(program["name"]);
114  std::stringstream ss;
115  String &progName = escapeSqliteStr(program["name"]);
116  ss
117  << "DELETE FROM Program WHERE name=" << progName
118  << ";INSERT INTO Program VALUES(NULL, NULL,"
119  << progName << ","
120  << escapeSqliteStr(program["usage"]) << ", "
121  << escapeSqliteStr(program["examples"]) << ", "
122  << escapeSqliteStr(program["keywords"]) << ");";
123  bool result = execStmt(ss.str(), "Couldn't insert program");
124  //program["id"] = sqlite3_last_insert_rowid(db);
125  return result;
126 }
127 
129 //bool ProgramDb::selectPrograms()
130 
131 
132 
134 //bool ProgramDb::updateProgram(DbProgram * program)
135 //{}
136 //
137 //bool ProgramDb::selectPrograms(std::vector<DbProgram*> &programs)
138 //{
139 //
140 // sqlite3_stmt *stmt;
141 // char * cmdStr = "SELECT * FROM Program ORDER BY name;";
142 // DbProgram * progData;
143 //
144 // rc = sqlite3_prepare_v2(db, cmdStr, -1, &stmt, NULL);
145 // programs.clear();
146 //
147 // while ((rc = sqlite3_step(stmt)) == SQLITE_ROW)
148 // {
149 // progData = new DbProgram();
150 // progData->id = sqlite3_column_int(stmt, 0);
151 // progData->cat_id = sqlite3_column_int(stmt, 1);
152 // progData->name.assign((char*)sqlite3_column_text(stmt, 2));
153 // progData->description.assign((char*)sqlite3_column_text(stmt, 3));
154 // progData->keywords.assign((char*)sqlite3_column_text(stmt, 4));
155 // programs.push_back(progData);
156 // }
157 // rc = sqlite3_finalize(stmt);
158 //
159 // return rc == SQLITE_OK;
160 //}
161 
163 {
164  sqlite3_stmt *stmt;
165  String aux = MDL::label2Str(label);
166  String cmd = formatString("SELECT * FROM Label WHERE name='%s';", aux.c_str());
167 
168  rc = sqlite3_prepare_v2(db, cmd.c_str(), -1, &stmt, NULL);
169 
170  if ((rc = sqlite3_step(stmt)) == SQLITE_ROW)
171  {
172  aux.assign((char*)sqlite3_column_text(stmt, 4));
173  }
174  rc = sqlite3_finalize(stmt);
175 
176  return aux;
177 }
178 
179 //--------- SQLITE PRINTER -----------------------
180 void ProgramDb::printProgram(const ProgramDef &program, int v)
181 {
182  //print program name and usage
183  String usage, examples;
184  DictDB dict;
185 
186  dict["name"] = program.name;
187  dict["usage"] = "";
188  dict["examples"] = "";
189  dict["keywords"] = program.keywords;
190  //print usage
191  if (program.usageComments.size() > 0)
192  {
193  for (size_t i = 0; i < program.usageComments.size(); ++i)
194  dict["usage"] += program.usageComments.comments[i] + '\n';
195  }
196  //print examples
197  if (program.examples.size() > 0)
198  {
199  for (size_t i = 0; i < program.examples.size(); ++i)
200  dict["examples"] += program.examples.comments[i] + '\n';
201  }
202  insertProgram(dict);
203  //std::cerr << "DEBUG_JM: program.name: " << program.name << std::endl;
204 
205  //print sections and params
206  if (program.sections.size() > 0)
207  {
208  for (size_t i = 0; i < program.sections.size(); ++i)
209  printSection(*program.sections[i], v);
210  }
211 }
212 
213 void ProgramDb::printSection(const SectionDef &section, int v)
214 {
215 }
216 
218 {
219 }
220 
221 void ProgramDb::printArgument(const ArgumentDef & argument, int v)
222 {
223 }
224 
225 void ProgramDb::printCommentList(const CommentList &comments, int v)
226 {
227 }
virtual void printCommentList(const CommentList &comments, int v=0)
bool createProgramTables()
void init(const FileName &dbName)
String name
Definition: argsparser.h:152
char * getXmippPath()
bool execStmt(const String &stmt, const String &error="")
std::vector< SectionDef * > sections
Definition: argsparser.h:241
virtual void printParam(const ParamDef &param, int v=0)
#define i
virtual void printArgument(const ArgumentDef &argument, int v=0)
virtual void printProgram(const ProgramDef &program, int v=0)
virtual void printSection(const SectionDef &section, int v=0)
size_t size() const
String keywords
Definition: argsparser.h:246
struct _parameter * param
StringVector comments
Definition: comment_list.h:35
CommentList examples
examples of use
Definition: argsparser.h:243
std::map< const char *, String > DictDB
CommentList usageComments
comments of usage
Definition: argsparser.h:242
std::string String
Definition: xmipp_strings.h:34
String getLabelComment(const MDLabel &label)
String formatString(const char *format,...)
bool insertProgram(DictDB &program)
String & escapeSqliteStr(String &str)
static String label2Str(const MDLabel &label)
MDLabel