niepce r14 - in trunk: . doc src/db



Author: hub
Date: Fri Feb  6 02:23:59 2009
New Revision: 14
URL: http://svn.gnome.org/viewvc/niepce?rev=14&view=rev

Log:
	* doc/database.txt:
	* src/db/Makefile.am:
	* src/db/fsfile.{cpp,h}:
	* src/db/libfile.{cpp,h}:
	* src/db/library.{cpp,h}:
	* src/db/test_library.cpp:
	Implement the FsFiles.
	Also check the DB version.

Added:
   trunk/src/db/fsfile.cpp
   trunk/src/db/fsfile.h
Modified:
   trunk/ChangeLog
   trunk/doc/database.txt
   trunk/src/db/Makefile.am
   trunk/src/db/libfile.cpp
   trunk/src/db/libfile.h
   trunk/src/db/library.cpp
   trunk/src/db/library.h
   trunk/src/db/test_library.cpp

Modified: trunk/doc/database.txt
==============================================================================
--- trunk/doc/database.txt	(original)
+++ trunk/doc/database.txt	Fri Feb  6 02:23:59 2009
@@ -14,6 +14,7 @@
 Files in the library
 
 files           id             Unique ID in the database
+		main_file      ID in fsfiles for the main file.
 		path           The absolute path to the file
 		parent_id      The ID on the containing folder (= folders.id)
 		orientation    The Exif orientation of the file
@@ -28,6 +29,10 @@
 		xmp            The XMP blob
 		xmp_data       The date the XMP is rewritten on disk (time_t)
 
+Filesystem files in the library
+
+fsfiles         id             Unique ID in the database
+                path           The absolute path
 
 Folders for the library "storage"
 

Modified: trunk/src/db/Makefile.am
==============================================================================
--- trunk/src/db/Makefile.am	(original)
+++ trunk/src/db/Makefile.am	Fri Feb  6 02:23:59 2009
@@ -30,4 +30,5 @@
 	libmetadata.h libmetadata.cpp \
 	keyword.h keyword.cpp \
 	storage.h storage.cpp \
+	fsfile.h fsfile.cpp \
 	metadata.h

Added: trunk/src/db/fsfile.cpp
==============================================================================
--- (empty file)
+++ trunk/src/db/fsfile.cpp	Fri Feb  6 02:23:59 2009
@@ -0,0 +1,42 @@
+/*
+ * niepce - db/fsfile.cpp
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+
+#include "fsfile.h"
+
+
+namespace db {
+
+FsFile::FsFile(int _id, const boost::filesystem::path & _path)
+    : m_id(_id), m_path(_path)
+{
+}
+
+}
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/

Added: trunk/src/db/fsfile.h
==============================================================================
--- (empty file)
+++ trunk/src/db/fsfile.h	Fri Feb  6 02:23:59 2009
@@ -0,0 +1,58 @@
+/*
+ * niepce - db/fsfile.h
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __NIEPCE_DB_FSFILE_H__
+#define __NIEPCE_DB_FSFILE_H__
+
+#include <string>
+#include <boost/shared_ptr.hpp>
+#include <boost/filesystem/path.hpp>
+
+
+namespace db {
+
+/** @brief describe a file on file system */
+class FsFile
+{
+public:
+    typedef boost::shared_ptr< FsFile > Ptr;
+
+    FsFile(int id, const boost::filesystem::path & path);
+
+    int id()
+        { return m_id; }
+    const boost::filesystem::path & path() const
+        { return m_path; }
+private:
+    int m_id;
+    boost::filesystem::path m_path; /**< absolute path */
+};
+
+}
+
+#endif
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/

Modified: trunk/src/db/libfile.cpp
==============================================================================
--- trunk/src/db/libfile.cpp	(original)
+++ trunk/src/db/libfile.cpp	Fri Feb  6 02:23:59 2009
@@ -1,7 +1,7 @@
 /*
  * niepce - db/libfile.cpp
  *
- * Copyright (C) 2007 Hubert Figuiere
+ * Copyright (C) 2007-2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -26,10 +26,11 @@
 
 namespace db {
 	
-LibFile::LibFile(int _id, int _folderId, const bfs::path & p,
+LibFile::LibFile(int _id, int _folderId, int _fsfileid, const bfs::path & p,
                  const std::string & _name )
 	: m_id(_id), m_folderId(_folderId),
-	  m_name(_name), m_path(p),
+	  m_name(_name), 
+      m_main_file(_fsfileid, p),
 	  m_orientation(0), m_rating(0), m_label(0),
       m_file_type(FILE_TYPE_UNKNOWN)
 {

Modified: trunk/src/db/libfile.h
==============================================================================
--- trunk/src/db/libfile.h	(original)
+++ trunk/src/db/libfile.h	Fri Feb  6 02:23:59 2009
@@ -1,7 +1,7 @@
 /*
  * niepce - db/libfile.h
  *
- * Copyright (C) 2007, 2008 Hubert Figuiere
+ * Copyright (C) 2007-2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,6 +28,7 @@
 #include "fwk/toolkit/mimetype.h"
 #include "db/keyword.h"
 #include "db/storage.h"
+#include "db/fsfile.h"
 
 namespace db {
 
@@ -49,7 +50,8 @@
 
     static FileType mimetype_to_filetype(framework::MimeType mime);
 
-    LibFile(int id, int folderId, const boost::filesystem::path & p,
+    LibFile(int id, int folderId, int fsfileid, 
+            const boost::filesystem::path & p,
             const std::string & name );
     virtual ~LibFile();
 
@@ -60,7 +62,7 @@
     const std::string & name() const
         { return m_name; }
     const boost::filesystem::path & path() const
-        { return m_path; }
+        { return m_main_file.path(); }
 
 //		Storage::Ptr storage() const;
 
@@ -94,7 +96,7 @@
      * because the Gtk stuff want that.
      */
     const std::string uri() const
-        { return std::string("file://") + m_path.string(); }
+        { return std::string("file://") + m_main_file.path().string(); }
     /** check is the library file is at uri
      * @return true of the uri match
      * @todo
@@ -105,7 +107,8 @@
     int         m_id;           /**< file ID */
     int         m_folderId;     /**< parent folder */
     std::string m_name;         /**< name */
-    boost::filesystem::path  m_path;/**< path name relative to the folder */
+    FsFile      m_main_file;
+//    boost::filesystem::path  m_path;/**< path name relative to the folder */
 //		std::string m_type;
     int32_t     m_orientation;  /**< Exif orientatoin */
     int32_t     m_rating;       /**< rating */

Modified: trunk/src/db/library.cpp
==============================================================================
--- trunk/src/db/library.cpp	(original)
+++ trunk/src/db/library.cpp	Fri Feb  6 02:23:59 2009
@@ -1,7 +1,7 @@
 /*
  * niepce - db/library.cpp
  *
- * Copyright (C) 2007-2008 Hubert Figuiere
+ * Copyright (C) 2007-2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -32,6 +32,7 @@
 #include "metadata.h"
 #include "fwk/utils/exception.h"
 #include "fwk/utils/exempi.h"
+#include "fwk/utils/debug.h"
 #include "fwk/utils/db/sqlite/sqlitecnxmgrdrv.h"
 #include "fwk/utils/db/sqlite/sqlitecnxdrv.h"
 #include "fwk/utils/db/sqlstatement.h"
@@ -47,7 +48,6 @@
 const char * s_databaseName = "niepcelibrary.db";
 
 
-
 Library::Library(const std::string & dir, const NotificationCenter::Ptr & nc)
     : m_maindir(dir),
       m_dbname(m_maindir / s_databaseName),
@@ -109,6 +109,9 @@
         DBG_OUT("version == 0");
         return _initDb();
     }
+    else if(version != DB_SCHEMA_VERSION)
+    {
+    }
     return true;
 }
 
@@ -116,19 +119,22 @@
 {
     SQLStatement adminTable("CREATE TABLE admin (key TEXT NOT NULL,"
                             " value TEXT)");
-    SQLStatement adminVersion("INSERT INTO admin (key, value) "
-                              " VALUES ('version', '1')");
+    SQLStatement adminVersion(boost::format("INSERT INTO admin (key, value) "
+                                            " VALUES ('version', '%1%')") %
+                              DB_SCHEMA_VERSION);
     SQLStatement vaultTable("CREATE TABLE vaults (id INTEGER PRIMARY KEY,"
                             " path TEXT)");
     SQLStatement folderTable("CREATE TABLE folders (id INTEGER PRIMARY KEY,"
                              " path TEXT, name TEXT, vault_id INTEGER, "
                              " parent_id INTEGER)");
     SQLStatement fileTable("CREATE TABLE files (id INTEGER PRIMARY KEY,"
-                           " path TEXT, name TEXT, parent_id INTEGER,"
+                           " main_file INTEGER, name TEXT, parent_id INTEGER,"
                            " orientation INTEGER, file_type INTEGER, "
                            " file_date INTEGER, rating INTEGER, label INTEGER,"
                            " import_date INTEGER, mod_date INTEGER, "
                            " xmp TEXT, xmp_date INTEGER)");
+    SQLStatement fsFileTable("CREATE TABLE fsfiles (id INTEGER PRIMARY KEY,"
+                             " path TEXT)");
     SQLStatement keywordTable("CREATE TABLE keywords (id INTEGER PRIMARY KEY,"
                               " keyword TEXT, parent_id INTEGER)");
     SQLStatement keywordingTable("CREATE TABLE keywording (file_id INTEGER,"
@@ -157,6 +163,7 @@
     m_dbdrv->execute_statement(vaultTable);
     m_dbdrv->execute_statement(folderTable);
     m_dbdrv->execute_statement(fileTable);
+    m_dbdrv->execute_statement(fsFileTable);
     m_dbdrv->execute_statement(keywordTable);
     m_dbdrv->execute_statement(keywordingTable);
     m_dbdrv->execute_statement(xmpUpdateQueueTable);
@@ -203,6 +210,22 @@
 }
 
 
+int Library::addFsFile(const bfs::path & file)
+{
+    int ret = -1;
+
+    SQLStatement sql(boost::format("INSERT INTO fsfiles (path)"
+                                   " VALUES ('%1%')") 
+                     % file.string());
+    if(m_dbdrv->execute_statement(sql)) {
+        int64_t id = m_dbdrv->last_row_id();
+        DBG_OUT("last row inserted %d", (int)id);
+        ret = id;
+    }
+    return ret;
+}
+
+
 int Library::addFile(int folder_id, const bfs::path & file, bool manage)
 {
     int ret = -1;
@@ -223,8 +246,12 @@
             creation_date = 0;
         }
 
+        int fs_file_id = addFsFile(file);
+        if(fs_file_id <= 0) {
+            throw(utils::Exception("add fsfile failed"));
+        }
         SQLStatement sql(boost::format("INSERT INTO files ("
-                                       " path, name, parent_id, "
+                                       " main_file, name, parent_id, "
                                        " import_date, mod_date, "
                                        " orientation, file_date, rating, label, file_type,"
                                        " xmp) "
@@ -233,7 +260,7 @@
                                        " '%4%', '%4%',"
                                        " '%5%', '%6%', '%7%', '%8%', '%9%',"
                                        " ?1);") 
-                         % file.string() % file.leaf() % folder_id
+                         % fs_file_id % file.leaf() % folder_id
                          % time(NULL)
                          % orientation % creation_date % rating
                          % folder_id % file_type);
@@ -258,10 +285,12 @@
     catch(const utils::Exception & e)
     {
         DBG_OUT("db exception %s", e.what());
+        ret = -1;
     }
     catch(const std::exception & e)
     {
         DBG_OUT("unknown exception %s", e.what());
+        ret = -1;
     }
     return ret;
 }
@@ -351,14 +380,17 @@
 {
     int32_t id;
     int32_t fid;
+    int32_t fsfid;
     std::string pathname;
     std::string name;
+    DBG_ASSERT(dbdrv->get_number_of_columns() == 9, "wrong number of columns");
     dbdrv->get_column_content(0, id);
     dbdrv->get_column_content(1, fid);
     dbdrv->get_column_content(2, pathname);
     dbdrv->get_column_content(3, name);
+    dbdrv->get_column_content(8, fsfid);
     DBG_OUT("found %s", pathname.c_str());
-    LibFile::Ptr f(new LibFile(id, fid,
+    LibFile::Ptr f(new LibFile(id, fid, fsfid,
                                bfs::path(pathname), 
                                name));
     int32_t val;
@@ -379,9 +411,13 @@
 
 void Library::getFolderContent(int folder_id, const LibFile::ListPtr & fl)
 {
-    SQLStatement sql(boost::format("SELECT id,parent_id,path,name,"
-                                   "orientation,rating,label,file_type FROM files "
-                                   " WHERE parent_id='%1%'")
+    SQLStatement sql(boost::format("SELECT files.id,parent_id,fsfiles.path,"
+                                   "name,"
+                                   "orientation,rating,label,file_type,"
+                                   "fsfiles.id"
+                                   " FROM files,fsfiles "
+                                   " WHERE parent_id='%1%' "
+                                   " AND files.main_file=fsfiles.id")
                      % folder_id);
     try {
         if(m_dbdrv->execute_statement(sql)) {
@@ -493,11 +529,14 @@
 
 void Library::getKeywordContent(int keyword_id, const LibFile::ListPtr & fl)
 {
-    SQLStatement sql(boost::format("SELECT id,parent_id,path,name,"
-                                   "orientation,rating,label FROM files "
-                                   " WHERE id IN "
+    SQLStatement sql(boost::format("SELECT files.id,parent_id,fsfiles.path,"
+                                   "name,orientation,rating,label,file_type,"
+                                   " fsfiles.id "
+                                   " FROM files,fsfiles "
+                                   " WHERE files.id IN "
                                    " (SELECT file_id FROM keywording "
-                                   " WHERE keyword_id='%1%');")
+                                   " WHERE keyword_id='%1%') "
+                                   " AND fsfiles.id = files.main_file;")
                      % keyword_id);
     try {
         if(m_dbdrv->execute_statement(sql)) {

Modified: trunk/src/db/library.h
==============================================================================
--- trunk/src/db/library.h	(original)
+++ trunk/src/db/library.h	Fri Feb  6 02:23:59 2009
@@ -1,7 +1,7 @@
 /*
  * niepce - db/library.h
  *
- * Copyright (C) 2007-2008 Hubert Figuiere
+ * Copyright (C) 2007-2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -37,6 +37,9 @@
 #include "db/libmetadata.h"
 #include "db/keyword.h"
 
+// The database schema version. Increase at each change.
+// Some will be persistent and have a conversion TBD.
+#define DB_SCHEMA_VERSION 2
 
 namespace db {
 
@@ -83,6 +86,13 @@
 		 */
 		int addFileAndFolder(const boost::filesystem::path & folder, 
 							 const boost::filesystem::path & file, bool manage);
+
+        /** add a fs file to the library  
+		 * @param file the file path
+         * @return the id of the fs_file, -1 in case of error
+         */
+        int addFsFile(const boost::filesystem::path & file);
+
 		/** add a file to the library
 		 * @param folder_id the id of the containing folder
 		 * @param file the file path

Modified: trunk/src/db/test_library.cpp
==============================================================================
--- trunk/src/db/test_library.cpp	(original)
+++ trunk/src/db/test_library.cpp	Fri Feb  6 02:23:59 2009
@@ -1,7 +1,7 @@
 /*
  * niepce - db/test_library.cpp
  *
- * Copyright (C) 2007-2008 Hubert Figuiere
+ * Copyright (C) 2007-2009 Hubert Figuiere
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -34,13 +34,16 @@
 {
 	db::Library lib("./", framework::NotificationCenter::Ptr());
 
-	BOOST_CHECK(lib.checkDatabaseVersion() == 1);
+	BOOST_CHECK(lib.checkDatabaseVersion() == DB_SCHEMA_VERSION);
 
 	db::IConnectionDriver::Ptr db(lib.dbDriver());
 	
-	lib.addFolder("foo");
+    db::LibFolder::Ptr folder_added(lib.addFolder("foo"));
+    BOOST_CHECK(folder_added);
+    BOOST_CHECK(folder_added->id() > 0);
 	db::LibFolder::Ptr f(lib.getFolder("foo"));
 	BOOST_CHECK(f);
+    BOOST_CHECK(f->id() == folder_added->id());
 	lib.addFolder("bar");
 	BOOST_CHECK(lib.getFolder("bar"));
 
@@ -48,7 +51,19 @@
 	lib.getAllFolders( l );
 	BOOST_CHECK( l->size() == 2 );
 
+    int file_id = lib.addFile(folder_added->id(), "foo/myfile", false);
+    BOOST_CHECK(file_id > 0);
 
 	BOOST_CHECK(unlink(lib.dbName().string().c_str()) != -1);
 	return 0;
 }
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]