• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

digikam

albumdb_sqlite2.cpp

Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of digiKam project
00004  * http://www.digikam.org
00005  *
00006  * Date        : 2004-06-18
00007  * Description : SQlite version 2 database interface.
00008  *
00009  * Copyright (C) 2004 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
00010  *
00011  * This program is free software; you can redistribute it
00012  * and/or modify it under the terms of the GNU General
00013  * Public License as published by the Free Software Foundation;
00014  * either version 2, or (at your option)
00015  * any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * ============================================================ */
00023 
00024 #include "albumdb_sqlite2.h"
00025 
00026 // C ANSI includes
00027 
00028 extern "C"
00029 {
00030 #include <sys/time.h>
00031 }
00032 
00033 // C++ includes
00034 
00035 #include <ctime>
00036 #include <cstdio>
00037 #include <cstdlib>
00038 
00039 // Qt includes
00040 
00041 #include <QFile>
00042 
00043 // KDE includes
00044 
00045 #include <kdebug.h>
00046 
00047 namespace Digikam
00048 {
00049 
00050 typedef struct sqlite_vm sqlite_vm;
00051 
00052 AlbumDB_Sqlite2::AlbumDB_Sqlite2()
00053 {
00054     m_db    = 0;
00055     m_valid = false;
00056 }
00057 
00058 AlbumDB_Sqlite2::~AlbumDB_Sqlite2()
00059 {
00060     if (m_db)
00061     {
00062         sqlite_close(m_db);
00063     }
00064 }
00065 
00066 void AlbumDB_Sqlite2::setDBPath(const QString& path)
00067 {
00068     if (m_db)
00069     {
00070         sqlite_close(m_db);
00071         m_db    = 0;
00072         m_valid = false;
00073     }
00074 
00075     char *errMsg = 0;
00076     m_db = sqlite_open(QFile::encodeName(path), 0, &errMsg);
00077     if (m_db == 0)
00078     {
00079         kWarning() << "Cannot open database: " << errMsg;
00080         free(errMsg);
00081         return;
00082     }
00083 
00084     QStringList values;
00085     execSql("SELECT * FROM sqlite_master", &values);
00086     m_valid = values.contains("Albums");
00087 }
00088 
00089 bool AlbumDB_Sqlite2::execSql(const QString& sql, QStringList* const values,
00090                       const bool debug)
00091 {
00092     if ( debug )
00093         kDebug() << "SQL-query: " << sql;
00094 
00095     if ( !m_db )
00096     {
00097         kWarning() << "SQLite pointer == NULL";
00098         return false;
00099     }
00100 
00101     const char* tail;
00102     sqlite_vm*  vm;
00103     char*       errorStr;
00104     int         error;
00105 
00106     // Compile SQL program to virtual machine
00107     error = sqlite_compile( m_db, QFile::encodeName(sql), &tail, &vm, &errorStr );
00108 
00109     if ( error != SQLITE_OK )
00110     {
00111         kWarning() << "sqlite_compile error: "
00112                         << errorStr
00113                         << " on query: " << sql;
00114         sqlite_freemem( errorStr );
00115         return false;
00116     }
00117 
00118     int          number;
00119     const char** value;
00120     const char** colName;
00121 
00122     // Execute virtual machine by iterating over rows
00123     while ( true )
00124     {
00125         error = sqlite_step( vm, &number, &value, &colName );
00126         if ( error == SQLITE_DONE || error == SQLITE_ERROR )
00127             break;
00128 
00129         // Iterate over columns
00130         for ( int i = 0; values && i < number; ++i )
00131         {
00132             *values << QString::fromLocal8Bit( value [i] );
00133         }
00134     }
00135 
00136     // Deallocate vm resources
00137     sqlite_finalize( vm, &errorStr );
00138 
00139     if ( error != SQLITE_DONE )
00140     {
00141         kWarning() << "sqlite_step error: "
00142                         << errorStr
00143                         << " on query: " << sql;
00144         return false;
00145     }
00146 
00147     return true;
00148 }
00149 
00150 }  // namespace Digikam

digikam

Skip menu "digikam"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • digikam
Generated for API Reference by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal