/[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 1717 by iliev, Sun Mar 16 17:43:20 2008 UTC revision 1727 by iliev, Tue Apr 29 15:44:09 2008 UTC
# Line 260  namespace LinuxSampler { Line 260  namespace LinuxSampler {
260          return ExecSqlInt(sql.str(), DirName);          return ExecSqlInt(sql.str(), DirName);
261      }      }
262    
263        int InstrumentsDb::GetDirectoryId(int InstrId) {
264            dmsg(2,("InstrumentsDb: GetDirectoryId(InstrId=%d)\n", InstrId));
265            std::stringstream sql;
266            sql << "SELECT dir_id FROM instruments WHERE instr_id=" << InstrId;
267            return ExecSqlInt(sql.str());
268        }
269    
270      String InstrumentsDb::GetDirectoryName(int DirId) {      String InstrumentsDb::GetDirectoryName(int DirId) {
271          String sql = "SELECT dir_name FROM instr_dirs WHERE dir_id=" + ToString(DirId);          String sql = "SELECT dir_name FROM instr_dirs WHERE dir_id=" + ToString(DirId);
272          String name = ExecSqlString(sql);          String name = ExecSqlString(sql);
# Line 284  namespace LinuxSampler { Line 291  namespace LinuxSampler {
291                  path = "/" + path;                  path = "/" + path;
292                  break;                  break;
293              }              }
294              path = GetDirectoryName(DirId) + path;              path = GetDirectoryName(DirId) + "/" + path;
295              DirId = GetParentDirectoryId(DirId);              DirId = GetParentDirectoryId(DirId);
296          }          }
297    
# Line 292  namespace LinuxSampler { Line 299  namespace LinuxSampler {
299    
300          return path;          return path;
301      }      }
302        
303        StringListPtr InstrumentsDb::GetInstrumentsByFile(String File) {
304            dmsg(2,("InstrumentsDb: GetInstrumentsByFile(File=%s)\n", File.c_str()));
305    
306            StringListPtr instrs(new std::vector<String>);
307            
308            BeginTransaction();
309            try {
310                File = toEscapedFsPath(File);
311                IntListPtr ids = ExecSqlIntList("SELECT instr_id FROM instruments WHERE instr_file=?", File);
312                
313                for (int i = 0; i < ids->size(); i++) {
314                    String name = GetInstrumentName(ids->at(i));
315                    String dir = GetDirectoryPath(GetDirectoryId(ids->at(i)));
316                    instrs->push_back(dir + name);
317                }
318            } catch (Exception e) {
319                EndTransaction();
320                throw e;
321            }
322            EndTransaction();
323            
324            return instrs;
325        }
326    
327      void InstrumentsDb::AddDirectory(String Dir) {      void InstrumentsDb::AddDirectory(String Dir) {
328          dmsg(2,("InstrumentsDb: AddDirectory(Dir=%s)\n", Dir.c_str()));          dmsg(2,("InstrumentsDb: AddDirectory(Dir=%s)\n", Dir.c_str()));
# Line 1315  namespace LinuxSampler { Line 1346  namespace LinuxSampler {
1346    
1347          return instrumentFinder.GetInstruments();          return instrumentFinder.GetInstruments();
1348      }      }
1349        
1350        StringListPtr InstrumentsDb::FindLostInstrumentFiles() {
1351            dmsg(2,("InstrumentsDb: FindLostInstrumentFiles()\n"));
1352    
1353            BeginTransaction();
1354            try {
1355                StringListPtr files = ExecSqlStringList("SELECT DISTINCT instr_file FROM instruments");
1356                StringListPtr result(new std::vector<String>);
1357                for (int i = 0; i < files->size(); i++) {
1358                    File f(toNonEscapedFsPath(files->at(i)));
1359                    if (!f.Exist()) result->push_back(files->at(i));
1360                }
1361                return result;
1362            } catch (Exception e) {
1363                EndTransaction();
1364                throw e;
1365            }
1366            EndTransaction();
1367        }
1368        
1369        void InstrumentsDb::SetInstrumentFilePath(String OldPath, String NewPath) {
1370            if (OldPath == NewPath) return;
1371            StringListPtr instrs;
1372            BeginTransaction();
1373            try {
1374                std::vector<String> params(2);
1375                params[0] = toEscapedFsPath(NewPath);
1376                params[1] = toEscapedFsPath(OldPath);
1377                instrs = GetInstrumentsByFile(OldPath);
1378                ExecSql("UPDATE instruments SET instr_file=? WHERE instr_file=?", params);
1379            } catch (Exception e) {
1380                EndTransaction();
1381                throw e;
1382            }
1383            EndTransaction();
1384            
1385            for (int i = 0; i < instrs->size(); i++) {
1386                FireInstrumentInfoChanged(instrs->at(i));
1387            }
1388        }
1389    
1390      void InstrumentsDb::BeginTransaction() {      void InstrumentsDb::BeginTransaction() {
1391          dmsg(2,("InstrumentsDb: BeginTransaction(InTransaction=%d)\n", InTransaction));          dmsg(2,("InstrumentsDb: BeginTransaction(InTransaction=%d)\n", InTransaction));
# Line 1376  namespace LinuxSampler { Line 1447  namespace LinuxSampler {
1447    
1448      void InstrumentsDb::ExecSql(String Sql) {      void InstrumentsDb::ExecSql(String Sql) {
1449          dmsg(2,("InstrumentsDb: ExecSql(Sql=%s)\n", Sql.c_str()));          dmsg(2,("InstrumentsDb: ExecSql(Sql=%s)\n", Sql.c_str()));
1450          sqlite3_stmt *pStmt = NULL;          std::vector<String> Params;
1451                    ExecSql(Sql, Params);
         int res = sqlite3_prepare(GetDb(), Sql.c_str(), -1, &pStmt, NULL);  
         if (res != SQLITE_OK) {  
             throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));  
         }  
           
         res = sqlite3_step(pStmt);  
         if(res != SQLITE_DONE) {  
             sqlite3_finalize(pStmt);  
             throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));  
         }  
   
         sqlite3_finalize(pStmt);  
1452      }      }
1453    
1454      void InstrumentsDb::ExecSql(String Sql, String Param) {      void InstrumentsDb::ExecSql(String Sql, String Param) {
1455          dmsg(2,("InstrumentsDb: ExecSql(Sql=%s,Param=%s)\n", Sql.c_str(), Param.c_str()));          dmsg(2,("InstrumentsDb: ExecSql(Sql=%s,Param=%s)\n", Sql.c_str(), Param.c_str()));
1456            std::vector<String> Params;
1457            Params.push_back(Param);
1458            ExecSql(Sql, Params);
1459        }
1460    
1461        void InstrumentsDb::ExecSql(String Sql, std::vector<String>& Params) {
1462            dmsg(2,("InstrumentsDb: ExecSql(Sql=%s,Params)\n", Sql.c_str()));
1463          sqlite3_stmt *pStmt = NULL;          sqlite3_stmt *pStmt = NULL;
1464                    
1465          int res = sqlite3_prepare(GetDb(), Sql.c_str(), -1, &pStmt, NULL);          int res = sqlite3_prepare(GetDb(), Sql.c_str(), -1, &pStmt, NULL);
# Line 1402  namespace LinuxSampler { Line 1468  namespace LinuxSampler {
1468              throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));              throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));
1469          }          }
1470    
1471          BindTextParam(pStmt, 1, Param);          for(int i = 0; i < Params.size(); i++) {
1472                BindTextParam(pStmt, i + 1, Params[i]);
1473            }
1474    
1475          res = sqlite3_step(pStmt);          res = sqlite3_step(pStmt);
1476          if (res != SQLITE_DONE) {          if (res != SQLITE_DONE) {
# Line 1484  namespace LinuxSampler { Line 1552  namespace LinuxSampler {
1552      }      }
1553    
1554      IntListPtr InstrumentsDb::ExecSqlIntList(String Sql) {      IntListPtr InstrumentsDb::ExecSqlIntList(String Sql) {
1555            dmsg(2,("InstrumentsDb: ExecSqlIntList(Sql=%s)\n", Sql.c_str()));
1556            std::vector<String> Params;
1557            return ExecSqlIntList(Sql, Params);
1558        }
1559    
1560        IntListPtr InstrumentsDb::ExecSqlIntList(String Sql, String Param) {
1561            dmsg(2,("InstrumentsDb: ExecSqlIntList(Sql=%s,Param=%s)\n", Sql.c_str(), Param.c_str()));
1562            std::vector<String> Params;
1563            Params.push_back(Param);
1564            return ExecSqlIntList(Sql, Params);
1565        }
1566    
1567        IntListPtr InstrumentsDb::ExecSqlIntList(String Sql, std::vector<String>& Params) {
1568            dmsg(2,("InstrumentsDb: ExecSqlIntList(Sql=%s)\n", Sql.c_str()));
1569          IntListPtr intList(new std::vector<int>);          IntListPtr intList(new std::vector<int>);
1570                    
1571          sqlite3_stmt *pStmt = NULL;          sqlite3_stmt *pStmt = NULL;
# Line 1493  namespace LinuxSampler { Line 1575  namespace LinuxSampler {
1575              throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));              throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));
1576          }          }
1577                    
1578            for(int i = 0; i < Params.size(); i++) {
1579                BindTextParam(pStmt, i + 1, Params[i]);
1580            }
1581            
1582          res = sqlite3_step(pStmt);          res = sqlite3_step(pStmt);
1583          while(res == SQLITE_ROW) {          while(res == SQLITE_ROW) {
1584              intList->push_back(sqlite3_column_int(pStmt, 0));              intList->push_back(sqlite3_column_int(pStmt, 0));
# Line 1510  namespace LinuxSampler { Line 1596  namespace LinuxSampler {
1596      }      }
1597            
1598      StringListPtr InstrumentsDb::ExecSqlStringList(String Sql) {      StringListPtr InstrumentsDb::ExecSqlStringList(String Sql) {
1599            dmsg(2,("InstrumentsDb: ExecSqlStringList(Sql=%s)\n", Sql.c_str()));
1600          StringListPtr stringList(new std::vector<String>);          StringListPtr stringList(new std::vector<String>);
1601                    
1602          sqlite3_stmt *pStmt = NULL;          sqlite3_stmt *pStmt = NULL;
# Line 1683  namespace LinuxSampler { Line 1770  namespace LinuxSampler {
1770          return text;          return text;
1771      }      }
1772            
1773        String InstrumentsDb::toNonEscapedText(String text) {
1774            String sb;
1775            for (int i = 0; i < text.length(); i++) {
1776                char c = text.at(i);
1777                            if(c == '\\') {
1778                                    if(i >= text.length()) {
1779                                            std::cerr << "Broken escape sequence!" << std::endl;
1780                                            break;
1781                                    }
1782                                    char c2 = text.at(++i);
1783                                    if(c2 == '\'')      sb.push_back('\'');
1784                                    else if(c2 == '"')  sb.push_back('"');
1785                                    else if(c2 == '\\') sb.push_back('\\');
1786                                    else if(c2 == 'r')  sb.push_back('\r');
1787                                    else if(c2 == 'n')  sb.push_back('\n');
1788                                    else std::cerr << "Unknown escape sequence \\" << c2 << std::endl;
1789                            } else {
1790                                    sb.push_back(c);
1791                            }
1792            }
1793            return sb;
1794        }
1795        
1796      String InstrumentsDb::toEscapedFsPath(String FsPath) {      String InstrumentsDb::toEscapedFsPath(String FsPath) {
1797          return toEscapedText(FsPath);          return toEscapedText(FsPath);
1798      }      }
1799            
1800        String InstrumentsDb::toNonEscapedFsPath(String FsPath) {
1801            return toNonEscapedText(FsPath);
1802        }
1803        
1804      String InstrumentsDb::toAbstractName(String DbName) {      String InstrumentsDb::toAbstractName(String DbName) {
1805          for (int i = 0; i < DbName.length(); i++) {          for (int i = 0; i < DbName.length(); i++) {
1806              if (DbName.at(i) == '/') DbName.at(i) = '\0';              if (DbName.at(i) == '/') DbName.at(i) = '\0';

Legend:
Removed from v.1717  
changed lines
  Added in v.1727

  ViewVC Help
Powered by ViewVC