OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimSqliteUtil.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 // File: ossimGpkgUtil.cpp
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Description: OSSIM SQLite utility class.
9 //----------------------------------------------------------------------------
10 // $Id$
11 
12 #include "ossimSqliteUtil.h"
13 #include <ossim/base/ossimNotify.h>
14 #include <ossim/base/ossimTrace.h>
15 #include <sqlite3.h>
16 #include <ctime>
17 #include <mutex>
18 
19 static std::mutex timeMutex;
20 static ossimTrace traceDebug("ossimSqliteUtil:debug");
21 
22 int ossim_sqlite::exec( sqlite3* db, const std::string& sql )
23 {
24  int rc = SQLITE_ERROR;
25  if ( db && sql.size() )
26  {
27  if (traceDebug())
28  {
30  << "sql:\n" << sql << "\n";
31  }
32 
33  sqlite3_stmt* pStmt = 0; // The current SQL statement
34 
35  rc = sqlite3_prepare_v2(db, // Database handle
36  sql.c_str(), // SQL statement, UTF-8 encoded
37  -1, // Maximum length of zSql in bytes.
38  &pStmt, // OUT: Statement handle
39  NULL);
40  if ( rc == SQLITE_OK )
41  {
42  rc = sqlite3_step(pStmt);
43  }
44  else
45  {
47  << "ossim_sqlite::exec error: " << sqlite3_errmsg(db) << std::endl;
48  }
49 
50  sqlite3_finalize(pStmt);
51  }
52  return rc;
53 }
54 
55 bool ossim_sqlite::tableExists( sqlite3* db, const std::string& tableName )
56 {
57  bool status = false;
58 
59  if ( db && tableName.size() )
60  {
61  const char *zLeftover; /* Tail of unprocessed SQL */
62  sqlite3_stmt *pStmt = 0; /* The current SQL statement */
63  std::string sql = "SELECT * from ";
64  sql += tableName;
65 
66  int rc = sqlite3_prepare_v2(db, // Database handle
67  sql.c_str(), // SQL statement, UTF-8 encoded
68  -1, // Maximum length of zSql in bytes.
69  &pStmt, // OUT: Statement handle
70  &zLeftover); // OUT: Pointer to unused portion of zSql
71  if ( rc == SQLITE_OK )
72  {
73  int nCol = sqlite3_column_count( pStmt );
74  if ( nCol )
75  {
76  status = true;
77  }
78  }
79  sqlite3_finalize(pStmt);
80  }
81 
82  return status;
83 
84 } // End: ossim_sqlite::tableExists(...)
85 
86 void ossim_sqlite::warn( const std::string& module,
87  const std::string& columnName,
88  ossim_int32 columnIndex,
89  ossim_int32 type )
90 {
92  << module << " Unexpected column name or type[" << columnIndex << "]: "
93  << "name: " << columnName << " type: " << type << std::endl;
94 }
95 
96 void ossim_sqlite::getTime( std::string& result )
97 {
98  timeMutex.lock();
99 
100  time_t rawTime;
101  time(&rawTime);
102 
103  struct tm* timeInfo = gmtime(&rawTime);
104 
105  size_t size = 0;
106  if ( timeInfo )
107  {
108  const size_t STRING_SIZE = 25;
109  char outStr[STRING_SIZE];
110 
111  size = strftime(outStr, STRING_SIZE, "%Y-%m-%dT%H:%M:%S.000Z", timeInfo );
112  if ( size )
113  {
114  // Per strftime spec not needed but null terminating anyway.
115  outStr[STRING_SIZE-1] = '\0';
116  result = outStr;
117  }
118  }
119  if ( !size )
120  {
121  result.clear();
122  }
123 
124  timeMutex.unlock();
125 }
void warn(const std::string &module, const std::string &columnName, ossim_int32 columnIndex, ossim_int32 type)
Outputs formated warning message.
int exec(sqlite3 *db, const std::string &sql)
Preforms sqlite3_prepare_v2, sqlite3_step and sqlite3_finalize.
bool tableExists(sqlite3 *db, const std::string &tableName)
Checks for existance of table.
yy_size_t size
return status
void getTime(std::string &result)
Gets time in the form of "%Y-%m-%dT%H:%M:%S.000Z".
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
int ossim_int32