Xmipp  v3.23.11-Nereus
Public Member Functions | List of all members
ProgramDb Class Reference

#include <xmipp_program_sql.h>

Inheritance diagram for ProgramDb:
Inheritance graph
[legend]
Collaboration diagram for ProgramDb:
Collaboration graph
[legend]

Public Member Functions

void init (const FileName &dbName)
 
 ProgramDb ()
 
 ProgramDb (const FileName &dbName)
 
virtual ~ProgramDb ()
 
bool execStmt (const String &stmt, const String &error="")
 
bool beginTrans ()
 
bool commitTrans ()
 
bool createProgramTables ()
 
bool insertProgram (DictDB &program)
 
String getLabelComment (const MDLabel &label)
 
virtual void printProgram (const ProgramDef &program, int v=0)
 
virtual void printSection (const SectionDef &section, int v=0)
 
virtual void printParam (const ParamDef &param, int v=0)
 
virtual void printArgument (const ArgumentDef &argument, int v=0)
 
virtual void printCommentList (const CommentList &comments, int v=0)
 
- Public Member Functions inherited from Printer
virtual ~Printer ()
 
virtual void printToken (ArgToken *token)
 

Detailed Description

Class that will encapsulate the Xmipp objects representation on Sqlite db. Program are a kind of this objects. Programas db storing will be useful for handle meta-info.

Definition at line 45 of file xmipp_program_sql.h.

Constructor & Destructor Documentation

◆ ProgramDb() [1/2]

ProgramDb::ProgramDb ( )

Empty constructor

Definition at line 44 of file xmipp_program_sql.cpp.

45 {
46  init(formatString("%s/.xmipp_programs.sqlite", getXmippPath()));
47 }
void init(const FileName &dbName)
char * getXmippPath()
String formatString(const char *format,...)

◆ ProgramDb() [2/2]

ProgramDb::ProgramDb ( const FileName dbName)

Constructor, it will create the Sqlite db.

Definition at line 39 of file xmipp_program_sql.cpp.

40 {
41  init(dbName);
42 }
void init(const FileName &dbName)

◆ ~ProgramDb()

virtual ProgramDb::~ProgramDb ( )
inlinevirtual

Destructor

Definition at line 61 of file xmipp_program_sql.h.

61 {}

Member Function Documentation

◆ beginTrans()

bool ProgramDb::beginTrans ( )

Definition at line 59 of file xmipp_program_sql.cpp.

60 {
61  return execStmt("BEGIN TRANSACTION", "Couldn't begin transaction: ");
62 }
bool execStmt(const String &stmt, const String &error="")

◆ commitTrans()

bool ProgramDb::commitTrans ( )

Definition at line 64 of file xmipp_program_sql.cpp.

65 {
66  return execStmt("COMMIT TRANSACTION", "Couldn't commit transaction: ");
67 }
bool execStmt(const String &stmt, const String &error="")

◆ createProgramTables()

bool ProgramDb::createProgramTables ( )

Create tables related with programs

Definition at line 70 of file xmipp_program_sql.cpp.

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 }
bool execStmt(const String &stmt, const String &error="")

◆ execStmt()

bool ProgramDb::execStmt ( const String stmt,
const String error = "" 
)

Begin and end transaction

Definition at line 49 of file xmipp_program_sql.cpp.

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 }

◆ getLabelComment()

String ProgramDb::getLabelComment ( const MDLabel label)

Get from the db the comment for a label

Select programs from db Update program data, id must be valid

Definition at line 162 of file xmipp_program_sql.cpp.

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 }
std::string String
Definition: xmipp_strings.h:34
String formatString(const char *format,...)
static String label2Str(const MDLabel &label)

◆ init()

void ProgramDb::init ( const FileName dbName)

Some initialization

Definition at line 30 of file xmipp_program_sql.cpp.

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 }

◆ insertProgram()

bool ProgramDb::insertProgram ( DictDB program)

Insert a program into db, the id field will be filled

Insert program into db, the id field will be filled

Delete first the program if exist

Definition at line 110 of file xmipp_program_sql.cpp.

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 }
bool execStmt(const String &stmt, const String &error="")
std::string String
Definition: xmipp_strings.h:34
String & escapeSqliteStr(String &str)

◆ printArgument()

void ProgramDb::printArgument ( const ArgumentDef argument,
int  v = 0 
)
virtual

Implements Printer.

Definition at line 221 of file xmipp_program_sql.cpp.

222 {
223 }

◆ printCommentList()

void ProgramDb::printCommentList ( const CommentList comments,
int  v = 0 
)
virtual

Implements Printer.

Definition at line 225 of file xmipp_program_sql.cpp.

226 {
227 }

◆ printParam()

void ProgramDb::printParam ( const ParamDef param,
int  v = 0 
)
virtual

Implements Printer.

Definition at line 217 of file xmipp_program_sql.cpp.

218 {
219 }

◆ printProgram()

void ProgramDb::printProgram ( const ProgramDef program,
int  v = 0 
)
virtual

Implements Printer.

Definition at line 180 of file xmipp_program_sql.cpp.

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 }
String name
Definition: argsparser.h:152
std::vector< SectionDef * > sections
Definition: argsparser.h:241
#define i
virtual void printSection(const SectionDef &section, int v=0)
size_t size() const
String keywords
Definition: argsparser.h:246
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
bool insertProgram(DictDB &program)

◆ printSection()

void ProgramDb::printSection ( const SectionDef section,
int  v = 0 
)
virtual

Implements Printer.

Definition at line 213 of file xmipp_program_sql.cpp.

214 {
215 }

The documentation for this class was generated from the following files: