/[svn]/linuxsampler/trunk/src/db/InstrumentsDb.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/db/InstrumentsDb.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1944 by persson, Tue Jul 14 18:54:08 2009 UTC revision 3091 by schoenebeck, Mon Jan 16 15:01:21 2017 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   Copyright (C) 2007-2009 Grigor Iliev, Benno Senoner                   *   *   Copyright (C) 2007-2013 Grigor Iliev, Benno Senoner                   *
4   *                                                                         *   *                                                                         *
5   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
6   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 41  namespace LinuxSampler { Line 41  namespace LinuxSampler {
41      InstrumentsDb InstrumentsDb::instance;      InstrumentsDb InstrumentsDb::instance;
42    
43      void InstrumentsDb::CreateInstrumentsDb(String FilePath) {      void InstrumentsDb::CreateInstrumentsDb(String FilePath) {
44            if (FilePath.empty()) {
45                FilePath = GetDefaultDBLocation();
46                dmsg(0,("InstrumentsDb: Creating database at default location '%s'\n", FilePath.c_str()));
47            }
48    
49          File f = File(FilePath);          File f = File(FilePath);
50          if (f.Exist()) {          if (f.Exist()) {
51              throw Exception("File exists: " + FilePath);              throw Exception("File exists: " + FilePath);
52          }          }
53            
54          GetInstrumentsDb()->SetDbFile(FilePath);          SetDbFile(FilePath);
55    
56          String sql =          String sql =
57              "  CREATE TABLE instr_dirs (                                      "              "  CREATE TABLE instr_dirs (                                      "
# Line 60  namespace LinuxSampler { Line 65  namespace LinuxSampler {
65              "      UNIQUE (parent_dir_id,dir_name)                            "              "      UNIQUE (parent_dir_id,dir_name)                            "
66              "  );                                                             ";              "  );                                                             ";
67                    
68          GetInstrumentsDb()->ExecSql(sql);          ExecSql(sql);
69    
70          sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, -2, '/');";          sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, -2, '/');";
71          GetInstrumentsDb()->ExecSql(sql);          ExecSql(sql);
72    
73          sql =          sql =
74              "  CREATE TABLE instruments (                                "              "  CREATE TABLE instruments (                                "
# Line 86  namespace LinuxSampler { Line 91  namespace LinuxSampler {
91              "      UNIQUE (dir_id,instr_name)                            "              "      UNIQUE (dir_id,instr_name)                            "
92              "  );                                                        ";              "  );                                                        ";
93                    
94          GetInstrumentsDb()->ExecSql(sql);          ExecSql(sql);
95      }      }
96    
97      InstrumentsDb::InstrumentsDb() {      InstrumentsDb::InstrumentsDb() {
# Line 111  namespace LinuxSampler { Line 116  namespace LinuxSampler {
116      }      }
117            
118      void InstrumentsDb::SetDbFile(String File) {      void InstrumentsDb::SetDbFile(String File) {
119          DbInstrumentsMutex.Lock();          LockGuard lock(DbInstrumentsMutex);
120          if (File.empty() || DbFile.length() > 0) {          if (File.empty() || DbFile.length() > 0) {
             DbInstrumentsMutex.Unlock();  
121              throw Exception("Failed to set the database file");              throw Exception("Failed to set the database file");
122          }          }
123          DbFile = File;          DbFile = File;
         DbInstrumentsMutex.Unlock();  
124      }      }
125    
126      sqlite3* InstrumentsDb::GetDb() {      String InstrumentsDb::GetDefaultDBLocation() {
127          if ( db != NULL) return db;          #ifdef WIN32
128            char* userprofile = getenv("USERPROFILE");
129            if (userprofile) {
130                String s = userprofile;
131                s += "\\.linuxsampler\\instruments.db";
132                return s;
133            } else {
134                // in case USERPROFILE is not set (which should not occur)
135                return "instruments.db";
136            }
137            #else // POSIX ...
138            String s = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION;
139            # if defined(__APPLE__)
140            if (s.find("~") == 0)
141                s.replace(0, 1, getenv("HOME"));
142            # endif
143            return s;
144            #endif
145        }
146    
147          if (DbFile.empty()) {      void InstrumentsDb::EnsureDBFileExists() {
148                      #ifndef WIN32          if (DbFile.empty())
149                      DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION;              DbFile = GetDefaultDBLocation();
                         #else  
                         char *userprofile = getenv("USERPROFILE");  
                         if(userprofile) {  
                             String DbPath = userprofile;  
                                 DbPath += "\\.linuxsampler";  
                             DbFile = DbPath + "\\instruments.db";  
                                 File InstrumentsDbFile(DbFile);  
                                 // if no DB exists create the subdir and then the DB  
                                 if( !InstrumentsDbFile.Exist() ) {  
                                     _mkdir( DbPath.c_str() );  
                                         // formats the DB, which creates a new instruments.db file  
                                         Format();  
                                 }  
                     }  
                         else {  
                             // in case USERPROFILE is not set (which should not occur)  
                             DbFile = "instruments.db";  
                         }  
                         #endif  
             }  
150                  #if defined(__APPLE__)  /* 20071224 Toshi Nagata  */                  #if defined(__APPLE__)  /* 20071224 Toshi Nagata  */
151                  if (DbFile.find("~") == 0)                  if (DbFile.find("~") == 0)
152                          DbFile.replace(0, 1, getenv("HOME"));                          DbFile.replace(0, 1, getenv("HOME"));
153                  #endif                  #endif
154            Path DbPath(DbFile);
155            String DbDir = DbPath.stripLastName();
156            // create directory if it does not exist yet
157            if (!DbPath.nodes().empty()) {
158                File d(DbDir);
159                if (!d.Exist()) {
160                    #ifdef WIN32
161                    if (_mkdir(DbDir.c_str()))
162                        throw Exception("Could not create instruments DB directory '" + DbDir + "'");
163                    #else
164                    if (mkdir(DbDir.c_str(), S_IRWXU))
165                        throw Exception("Could not create instruments DB directory '" + DbDir + "'");
166                    #endif
167                }
168            }
169            // create database file if it does not exist yet
170            File f(DbFile);
171            if (!f.Exist()) {
172                // formats the DB, which creates a new instruments.db file
173                Format();
174            }
175        }
176    
177        sqlite3* InstrumentsDb::GetDb() {
178            if ( db != NULL) return db;
179    
180            if (DbFile.empty())
181                DbFile = GetDefaultDBLocation();
182    
183            {
184                // first check if the instruments DB's directory exists, if not give up
185                Path path(DbFile);
186                String sDir = path.stripLastName();
187                File d(sDir);
188                if (!d.Exist())
189                    throw Exception("Instruments DB directory '" + sDir + "' does not exist!");
190    
191                // just to give the user a notice about the DB file being created in case it does not exist yet
192                File f(DbFile);
193                if (!f.Exist())
194                    dmsg(0,("Instruments DB file '%s' does not exist yet. Trying to create it now.\n", DbFile.c_str()));
195            }
196    
197            dmsg(0,("Opening instruments DB at '%s'\n", DbFile.c_str()));
198          int rc = sqlite3_open(DbFile.c_str(), &db);          int rc = sqlite3_open(DbFile.c_str(), &db);
199          if (rc) {          if (rc) {
200              sqlite3_close(db);              sqlite3_close(db);
# Line 464  namespace LinuxSampler { Line 509  namespace LinuxSampler {
509    
510      bool InstrumentsDb::DirectoryExist(String Dir) {      bool InstrumentsDb::DirectoryExist(String Dir) {
511          dmsg(2,("InstrumentsDb: DirectoryExist(Dir=%s)\n", Dir.c_str()));          dmsg(2,("InstrumentsDb: DirectoryExist(Dir=%s)\n", Dir.c_str()));
512          bool b;          {
513                LockGuard lock(DbInstrumentsMutex);
514          DbInstrumentsMutex.Lock();              return GetDirectoryId(Dir) != -1;
         try { b = GetDirectoryId(Dir) != -1; }  
         catch (Exception e) {  
             DbInstrumentsMutex.Unlock();  
             throw e;  
515          }          }
         DbInstrumentsMutex.Unlock();  
   
         return b;  
516      }      }
517    
518      DbDirectory InstrumentsDb::GetDirectoryInfo(String Dir) {      DbDirectory InstrumentsDb::GetDirectoryInfo(String Dir) {
# Line 714  namespace LinuxSampler { Line 752  namespace LinuxSampler {
752          dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,insDir=%d,FilePath=%s,Index=%d)\n", DbDir.c_str(), insDir, FilePath.c_str(), Index));          dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,insDir=%d,FilePath=%s,Index=%d)\n", DbDir.c_str(), insDir, FilePath.c_str(), Index));
753          if (DbDir.empty() || FilePath.empty()) return;          if (DbDir.empty() || FilePath.empty()) return;
754                    
755          DbInstrumentsMutex.Lock();          {
756          try {              LockGuard lock(DbInstrumentsMutex);
757    
758              int dirId = GetDirectoryId(DbDir);              int dirId = GetDirectoryId(DbDir);
759              if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedText(DbDir));              if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedText(DbDir));
760    
# Line 734  namespace LinuxSampler { Line 773  namespace LinuxSampler {
773    
774              String dir = insDir ? PrepareSubdirectory(DbDir, FilePath) : DbDir;              String dir = insDir ? PrepareSubdirectory(DbDir, FilePath) : DbDir;
775              AddInstrumentsFromFile(dir, FilePath, Index, pProgress);              AddInstrumentsFromFile(dir, FilePath, Index, pProgress);
         } catch (Exception e) {  
             DbInstrumentsMutex.Unlock();  
             throw e;  
776          }          }
   
         DbInstrumentsMutex.Unlock();  
777      }      }
778    
779      void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, bool insDir, ScanProgress* pProgress) {      void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, bool insDir, ScanProgress* pProgress) {
780          dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), insDir));          dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), insDir));
781          if (DbDir.empty() || FsDir.empty()) return;          if (DbDir.empty() || FsDir.empty()) return;
782                    
783          DbInstrumentsMutex.Lock();          {
784          try {              LockGuard lock(DbInstrumentsMutex);
785    
786              int dirId = GetDirectoryId(DbDir);              int dirId = GetDirectoryId(DbDir);
787              if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir));              if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir));
788    
# Line 774  namespace LinuxSampler { Line 809  namespace LinuxSampler {
809                  }                  }
810              } catch(Exception e) {              } catch(Exception e) {
811                  e.PrintMessage();                  e.PrintMessage();
                 DbInstrumentsMutex.Unlock();  
                 return;  
812              }              }
         } catch (Exception e) {  
             DbInstrumentsMutex.Unlock();  
             throw e;  
813          }          }
   
         DbInstrumentsMutex.Unlock();  
814      }      }
815    
816      void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, bool insDir, ScanProgress* pProgress) {      void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, bool insDir, ScanProgress* pProgress) {
# Line 1466  namespace LinuxSampler { Line 1494  namespace LinuxSampler {
1494          }          }
1495          InTransaction = false;          InTransaction = false;
1496                    
1497          if(db == NULL) {          if (db == NULL) {
1498              DbInstrumentsMutex.Unlock();              DbInstrumentsMutex.Unlock();
1499              return;              return;
1500          }          }
# Line 1670  namespace LinuxSampler { Line 1698  namespace LinuxSampler {
1698    
1699      void InstrumentsDb::BindTextParam(sqlite3_stmt* pStmt, int Index, String Text) {      void InstrumentsDb::BindTextParam(sqlite3_stmt* pStmt, int Index, String Text) {
1700          if (pStmt == NULL) return;          if (pStmt == NULL) return;
1701          int res = sqlite3_bind_text(pStmt, Index, Text.c_str(), -1, SQLITE_STATIC);          int res = sqlite3_bind_text(pStmt, Index, Text.c_str(), -1, SQLITE_TRANSIENT);
1702          if (res != SQLITE_OK) {          if (res != SQLITE_OK) {
1703              sqlite3_finalize(pStmt);              sqlite3_finalize(pStmt);
1704              throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));              throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));
# Line 1741  namespace LinuxSampler { Line 1769  namespace LinuxSampler {
1769      }      }
1770    
1771      void InstrumentsDb::Format() {      void InstrumentsDb::Format() {
1772          DbInstrumentsMutex.Lock();          {
1773          if (db != NULL) {              LockGuard lock(DbInstrumentsMutex);
             sqlite3_close(db);  
             db = NULL;  
         }  
1774    
1775          if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION;              if (db != NULL) {
1776          String bkp = DbFile + ".bkp";                  sqlite3_close(db);
1777          remove(bkp.c_str());                  db = NULL;
1778          if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) {              }
1779              DbInstrumentsMutex.Unlock();  
1780              throw Exception(String("Failed to backup database: ") + strerror(errno));              if (DbFile.empty()) DbFile = GetDefaultDBLocation();
1781          }              String bkp = DbFile + ".bkp";
1782                remove(bkp.c_str());
1783                if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) {
1784                    throw Exception(String("Failed to backup database: ") + strerror(errno));
1785                }
1786                    
1787          String f = DbFile;              String f = DbFile;
1788          DbFile = "";              DbFile = "";
1789          try { CreateInstrumentsDb(f); }              CreateInstrumentsDb(f);
         catch(Exception e) {  
             DbInstrumentsMutex.Unlock();  
             throw e;  
1790          }          }
         DbInstrumentsMutex.Unlock();  
           
1791          FireDirectoryCountChanged("/");          FireDirectoryCountChanged("/");
1792          FireInstrumentCountChanged("/");          FireInstrumentCountChanged("/");
1793      }      }

Legend:
Removed from v.1944  
changed lines
  Added in v.3091

  ViewVC Help
Powered by ViewVC