58 |
|
|
59 |
GetInstrumentsDb()->ExecSql(sql); |
GetInstrumentsDb()->ExecSql(sql); |
60 |
|
|
61 |
sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, 0, '/');"; |
sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, -2, '/');"; |
62 |
GetInstrumentsDb()->ExecSql(sql); |
GetInstrumentsDb()->ExecSql(sql); |
63 |
|
|
64 |
sql = |
sql = |
136 |
} |
} |
137 |
rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL); |
rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL); |
138 |
if (rc) { throw Exception("Failed to add user function for handling regular expressions."); } |
if (rc) { throw Exception("Failed to add user function for handling regular expressions."); } |
139 |
|
|
140 |
|
// TODO: remove this in the next version |
141 |
|
try { |
142 |
|
int i = ExecSqlInt("SELECT parent_dir_id FROM instr_dirs WHERE dir_id=0"); |
143 |
|
// The parent ID of the root directory should be -2 now. |
144 |
|
if(i != -2) ExecSql("UPDATE instr_dirs SET parent_dir_id=-2 WHERE dir_id=0"); |
145 |
|
} catch(Exception e) { } |
146 |
|
//////////////////////////////////////// |
147 |
|
|
148 |
return db; |
return db; |
149 |
} |
} |
157 |
|
|
158 |
int count = ExecSqlInt(sql.str()); |
int count = ExecSqlInt(sql.str()); |
159 |
|
|
|
// While the root dir has ID 0 and parent ID 0, the directory |
|
|
// count for the root dir will be incorrect, so we should fix it. |
|
|
if (count != -1 && DirId == 0) count--; |
|
160 |
return count; |
return count; |
161 |
} |
} |
162 |
|
|
178 |
throw e; |
throw e; |
179 |
} |
} |
180 |
EndTransaction(); |
EndTransaction(); |
181 |
if (i == -1) throw Exception("Unkown DB directory: " + Dir); |
if (i == -1) throw Exception("Unkown DB directory: " + toEscapedPath(Dir)); |
182 |
|
|
183 |
return i; |
return i; |
184 |
} |
} |
197 |
BeginTransaction(); |
BeginTransaction(); |
198 |
try { |
try { |
199 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
200 |
if(dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
201 |
|
|
202 |
StringListPtr pDirs; |
StringListPtr pDirs; |
203 |
if (Recursive) { |
if (Recursive) { |
220 |
std::stringstream sql; |
std::stringstream sql; |
221 |
sql << "SELECT dir_name FROM instr_dirs "; |
sql << "SELECT dir_name FROM instr_dirs "; |
222 |
sql << "WHERE parent_dir_id=" << DirId << " AND dir_id!=0"; |
sql << "WHERE parent_dir_id=" << DirId << " AND dir_id!=0"; |
223 |
return ExecSqlStringList(sql.str()); |
StringListPtr dirs = ExecSqlStringList(sql.str()); |
224 |
|
|
225 |
|
for (int i = 0; i < dirs->size(); i++) { |
226 |
|
for (int j = 0; j < dirs->at(i).length(); j++) { |
227 |
|
if (dirs->at(i).at(j) == '/') dirs->at(i).at(j) = '\0'; |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
|
return dirs; |
232 |
} |
} |
233 |
|
|
234 |
int InstrumentsDb::GetDirectoryId(String Dir) { |
int InstrumentsDb::GetDirectoryId(String Dir) { |
257 |
|
|
258 |
int InstrumentsDb::GetDirectoryId(int ParentDirId, String DirName) { |
int InstrumentsDb::GetDirectoryId(int ParentDirId, String DirName) { |
259 |
dmsg(2,("InstrumentsDb: GetDirectoryId(ParentDirId=%d, DirName=%s)\n", ParentDirId, DirName.c_str())); |
dmsg(2,("InstrumentsDb: GetDirectoryId(ParentDirId=%d, DirName=%s)\n", ParentDirId, DirName.c_str())); |
260 |
|
DirName = toDbName(DirName); |
261 |
std::stringstream sql; |
std::stringstream sql; |
262 |
sql << "SELECT dir_id FROM instr_dirs WHERE parent_dir_id="; |
sql << "SELECT dir_id FROM instr_dirs WHERE parent_dir_id="; |
263 |
sql << ParentDirId << " AND dir_name=?"; |
sql << ParentDirId << " AND dir_name=?"; |
310 |
|
|
311 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
312 |
if(ParentDir.empty() || dirName.empty()) { |
if(ParentDir.empty() || dirName.empty()) { |
313 |
throw Exception("Failed to add DB directory: " + Dir); |
throw Exception("Failed to add DB directory: " + toEscapedPath(Dir)); |
314 |
} |
} |
315 |
|
|
316 |
int id = GetDirectoryId(ParentDir); |
int id = GetDirectoryId(ParentDir); |
317 |
if (id == -1) throw Exception("DB directory doesn't exist: " + ParentDir); |
if (id == -1) throw Exception("DB directory doesn't exist: " + toEscapedPath(ParentDir)); |
318 |
int id2 = GetDirectoryId(id, dirName); |
int id2 = GetDirectoryId(id, dirName); |
319 |
if (id2 != -1) throw Exception("DB directory already exist: " + Dir); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(Dir)); |
320 |
id2 = GetInstrumentId(id, dirName); |
id2 = GetInstrumentId(id, dirName); |
321 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + Dir); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(Dir)); |
322 |
|
|
323 |
std::stringstream sql; |
std::stringstream sql; |
324 |
sql << "INSERT INTO instr_dirs (parent_dir_id, dir_name) VALUES ("; |
sql << "INSERT INTO instr_dirs (parent_dir_id, dir_name) VALUES ("; |
325 |
sql << id << ", ?)"; |
sql << id << ", ?)"; |
326 |
|
|
327 |
ExecSql(sql.str(), dirName); |
ExecSql(sql.str(), toDbName(dirName)); |
328 |
} catch (Exception e) { |
} catch (Exception e) { |
329 |
EndTransaction(); |
EndTransaction(); |
330 |
throw e; |
throw e; |
343 |
BeginTransaction(); |
BeginTransaction(); |
344 |
try { |
try { |
345 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
346 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
347 |
if (dirId == 0) throw Exception("Cannot delete the root directory: " + Dir); |
if (dirId == 0) throw Exception("Cannot delete the root directory: " + Dir); |
348 |
if(ParentDir.empty()) throw Exception("Unknown parent directory"); |
if(ParentDir.empty()) throw Exception("Unknown parent directory"); |
349 |
if (Force) RemoveDirectoryContent(dirId); |
if (Force) RemoveDirectoryContent(dirId); |
430 |
|
|
431 |
try { |
try { |
432 |
int id = GetDirectoryId(Dir); |
int id = GetDirectoryId(Dir); |
433 |
if(id == -1) throw Exception("Unknown DB directory: " + Dir); |
if(id == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
434 |
|
|
435 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
436 |
std::stringstream sql; |
std::stringstream sql; |
453 |
if (res != SQLITE_DONE) { |
if (res != SQLITE_DONE) { |
454 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
455 |
} else { |
} else { |
456 |
throw Exception("Unknown DB directory: " + Dir); |
throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
457 |
} |
} |
458 |
} |
} |
459 |
|
|
470 |
void InstrumentsDb::RenameDirectory(String Dir, String Name) { |
void InstrumentsDb::RenameDirectory(String Dir, String Name) { |
471 |
dmsg(2,("InstrumentsDb: RenameDirectory(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str())); |
dmsg(2,("InstrumentsDb: RenameDirectory(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str())); |
472 |
CheckFileName(Name); |
CheckFileName(Name); |
473 |
|
String dbName = toDbName(Name); |
474 |
|
|
475 |
BeginTransaction(); |
BeginTransaction(); |
476 |
try { |
try { |
477 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
478 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedText(Dir)); |
479 |
|
|
480 |
std::stringstream sql; |
std::stringstream sql; |
481 |
sql << "SELECT parent_dir_id FROM instr_dirs WHERE dir_id=" << dirId; |
sql << "SELECT parent_dir_id FROM instr_dirs WHERE dir_id=" << dirId; |
482 |
|
|
483 |
int parent = ExecSqlInt(sql.str()); |
int parent = ExecSqlInt(sql.str()); |
484 |
if (parent == -1) throw Exception("Unknown parent directory: " + Dir); |
if (parent == -1) throw Exception("Unknown parent directory: " + toEscapedPath(Dir)); |
485 |
if (GetDirectoryId(parent, Name) != -1) { |
|
486 |
throw Exception("Cannot rename. Directory with that name already exists: " + Name); |
if (GetDirectoryId(parent, dbName) != -1) { |
487 |
|
String s = toEscapedPath(Name); |
488 |
|
throw Exception("Cannot rename. Directory with that name already exists: " + s); |
489 |
} |
} |
490 |
|
|
491 |
if (GetInstrumentId(parent, Name) != -1) { |
if (GetInstrumentId(parent, dbName) != -1) { |
492 |
throw Exception("Cannot rename. Instrument with that name exist: " + Dir); |
throw Exception("Cannot rename. Instrument with that name exist: " + toEscapedPath(Dir)); |
493 |
} |
} |
494 |
|
|
495 |
sql.str(""); |
sql.str(""); |
496 |
sql << "UPDATE instr_dirs SET dir_name=? WHERE dir_id=" << dirId; |
sql << "UPDATE instr_dirs SET dir_name=? WHERE dir_id=" << dirId; |
497 |
ExecSql(sql.str(), Name); |
ExecSql(sql.str(), dbName); |
498 |
} catch (Exception e) { |
} catch (Exception e) { |
499 |
EndTransaction(); |
EndTransaction(); |
500 |
throw e; |
throw e; |
501 |
} |
} |
502 |
|
|
503 |
EndTransaction(); |
EndTransaction(); |
504 |
FireDirectoryNameChanged(Dir, Name); |
FireDirectoryNameChanged(Dir, toAbstractName(Name)); |
505 |
} |
} |
506 |
|
|
507 |
void InstrumentsDb::MoveDirectory(String Dir, String Dst) { |
void InstrumentsDb::MoveDirectory(String Dir, String Dst) { |
514 |
BeginTransaction(); |
BeginTransaction(); |
515 |
try { |
try { |
516 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
517 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
518 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
519 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
520 |
if (dirId == dstId) { |
if (dirId == dstId) { |
521 |
throw Exception("Cannot move directory to itself"); |
throw Exception("Cannot move directory to itself"); |
522 |
} |
} |
532 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
533 |
|
|
534 |
int id2 = GetDirectoryId(dstId, dirName); |
int id2 = GetDirectoryId(dstId, dirName); |
535 |
if (id2 != -1) throw Exception("DB directory already exist: " + dirName); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(dirName)); |
536 |
id2 = GetInstrumentId(dstId, dirName); |
id2 = GetInstrumentId(dstId, dirName); |
537 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + dirName); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(dirName)); |
538 |
|
|
539 |
std::stringstream sql; |
std::stringstream sql; |
540 |
sql << "UPDATE instr_dirs SET parent_dir_id=" << dstId; |
sql << "UPDATE instr_dirs SET parent_dir_id=" << dstId; |
560 |
BeginTransaction(); |
BeginTransaction(); |
561 |
try { |
try { |
562 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
563 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
564 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
565 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
566 |
if (dirId == dstId) { |
if (dirId == dstId) { |
567 |
throw Exception("Cannot copy directory to itself"); |
throw Exception("Cannot copy directory to itself"); |
568 |
} |
} |
578 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
579 |
|
|
580 |
int id2 = GetDirectoryId(dstId, dirName); |
int id2 = GetDirectoryId(dstId, dirName); |
581 |
if (id2 != -1) throw Exception("DB directory already exist: " + dirName); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(dirName)); |
582 |
id2 = GetInstrumentId(dstId, dirName); |
id2 = GetInstrumentId(dstId, dirName); |
583 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + dirName); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(dirName)); |
584 |
|
|
585 |
DirectoryCopier directoryCopier(ParentDir, Dst); |
DirectoryCopier directoryCopier(ParentDir, Dst); |
586 |
DirectoryTreeWalk(Dir, &directoryCopier); |
DirectoryTreeWalk(Dir, &directoryCopier); |
598 |
BeginTransaction(); |
BeginTransaction(); |
599 |
try { |
try { |
600 |
int id = GetDirectoryId(Dir); |
int id = GetDirectoryId(Dir); |
601 |
if(id == -1) throw Exception("Unknown DB directory: " + Dir); |
if(id == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
602 |
|
|
603 |
std::stringstream sql; |
std::stringstream sql; |
604 |
sql << "UPDATE instr_dirs SET description=?,modified=CURRENT_TIMESTAMP "; |
sql << "UPDATE instr_dirs SET description=?,modified=CURRENT_TIMESTAMP "; |
662 |
DbInstrumentsMutex.Lock(); |
DbInstrumentsMutex.Lock(); |
663 |
try { |
try { |
664 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
665 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedText(DbDir)); |
666 |
|
|
667 |
struct stat statBuf; |
struct stat statBuf; |
668 |
int res = stat(FilePath.c_str(), &statBuf); |
int res = stat(FilePath.c_str(), &statBuf); |
694 |
DbInstrumentsMutex.Lock(); |
DbInstrumentsMutex.Lock(); |
695 |
try { |
try { |
696 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
697 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir)); |
698 |
|
|
699 |
struct stat statBuf; |
struct stat statBuf; |
700 |
int res = stat(FsDir.c_str(), &statBuf); |
int res = stat(FsDir.c_str(), &statBuf); |
783 |
} |
} |
784 |
EndTransaction(); |
EndTransaction(); |
785 |
|
|
786 |
if (i == -1) throw Exception("Unknown Db directory: " + Dir); |
if (i == -1) throw Exception("Unknown Db directory: " + toEscapedPath(Dir)); |
787 |
return i; |
return i; |
788 |
} |
} |
789 |
|
|
799 |
BeginTransaction(); |
BeginTransaction(); |
800 |
try { |
try { |
801 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
802 |
if(dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
803 |
|
|
804 |
StringListPtr pInstrs; |
StringListPtr pInstrs; |
805 |
|
|
813 |
sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId; |
sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId; |
814 |
|
|
815 |
pInstrs = ExecSqlStringList(sql.str()); |
pInstrs = ExecSqlStringList(sql.str()); |
816 |
|
// Converting to abstract names |
817 |
|
for (int i = 0; i < pInstrs->size(); i++) { |
818 |
|
for (int j = 0; j < pInstrs->at(i).length(); j++) { |
819 |
|
if (pInstrs->at(i).at(j) == '/') pInstrs->at(i).at(j) = '\0'; |
820 |
|
} |
821 |
|
} |
822 |
} |
} |
823 |
EndTransaction(); |
EndTransaction(); |
824 |
return pInstrs; |
return pInstrs; |
843 |
std::stringstream sql; |
std::stringstream sql; |
844 |
sql << "SELECT instr_id FROM instruments WHERE dir_id="; |
sql << "SELECT instr_id FROM instruments WHERE dir_id="; |
845 |
sql << DirId << " AND instr_name=?"; |
sql << DirId << " AND instr_name=?"; |
846 |
return ExecSqlInt(sql.str(), InstrName); |
return ExecSqlInt(sql.str(), toDbName(InstrName)); |
847 |
} |
} |
848 |
|
|
849 |
String InstrumentsDb::GetInstrumentName(int InstrId) { |
String InstrumentsDb::GetInstrumentName(int InstrId) { |
850 |
dmsg(2,("InstrumentsDb: GetInstrumentName(InstrId=%d)\n", InstrId)); |
dmsg(2,("InstrumentsDb: GetInstrumentName(InstrId=%d)\n", InstrId)); |
851 |
std::stringstream sql; |
std::stringstream sql; |
852 |
sql << "SELECT instr_name FROM instruments WHERE instr_id=" << InstrId; |
sql << "SELECT instr_name FROM instruments WHERE instr_id=" << InstrId; |
853 |
return ExecSqlString(sql.str()); |
return toAbstractName(ExecSqlString(sql.str())); |
854 |
} |
} |
855 |
|
|
856 |
void InstrumentsDb::RemoveInstrument(String Instr) { |
void InstrumentsDb::RemoveInstrument(String Instr) { |
862 |
try { |
try { |
863 |
int instrId = GetInstrumentId(Instr); |
int instrId = GetInstrumentId(Instr); |
864 |
if(instrId == -1) { |
if(instrId == -1) { |
865 |
throw Exception("The specified instrument does not exist: " + Instr); |
throw Exception("The specified instrument does not exist: " + toEscapedPath(Instr)); |
866 |
} |
} |
867 |
RemoveInstrument(instrId); |
RemoveInstrument(instrId); |
868 |
} catch (Exception e) { |
} catch (Exception e) { |
897 |
BeginTransaction(); |
BeginTransaction(); |
898 |
try { |
try { |
899 |
int id = GetInstrumentId(Instr); |
int id = GetInstrumentId(Instr); |
900 |
if(id == -1) throw Exception("Unknown DB instrument: " + Instr); |
if(id == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
901 |
i = GetInstrumentInfo(id); |
i = GetInstrumentInfo(id); |
902 |
} catch (Exception e) { |
} catch (Exception e) { |
903 |
EndTransaction(); |
EndTransaction(); |
956 |
BeginTransaction(); |
BeginTransaction(); |
957 |
try { |
try { |
958 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
959 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
960 |
|
|
961 |
int instrId = GetInstrumentId(dirId, GetFileName(Instr)); |
int instrId = GetInstrumentId(dirId, GetFileName(Instr)); |
962 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
963 |
|
|
964 |
if (GetInstrumentId(dirId, Name) != -1) { |
if (GetInstrumentId(dirId, Name) != -1) { |
965 |
throw Exception("Cannot rename. Instrument with that name already exists: " + Name); |
String s = toEscapedPath(Name); |
966 |
|
throw Exception("Cannot rename. Instrument with that name already exists: " + s); |
967 |
} |
} |
968 |
|
|
969 |
if (GetDirectoryId(dirId, Name) != -1) { |
if (GetDirectoryId(dirId, Name) != -1) { |
970 |
throw Exception("Cannot rename. Directory with that name already exists: " + Name); |
String s = toEscapedPath(Name); |
971 |
|
throw Exception("Cannot rename. Directory with that name already exists: " + s); |
972 |
} |
} |
973 |
|
|
974 |
std::stringstream sql; |
std::stringstream sql; |
975 |
sql << "UPDATE instruments SET instr_name=? WHERE instr_id=" << instrId; |
sql << "UPDATE instruments SET instr_name=? WHERE instr_id=" << instrId; |
976 |
ExecSql(sql.str(), Name); |
ExecSql(sql.str(), toDbName(Name)); |
977 |
} catch (Exception e) { |
} catch (Exception e) { |
978 |
EndTransaction(); |
EndTransaction(); |
979 |
throw e; |
throw e; |
980 |
} |
} |
981 |
EndTransaction(); |
EndTransaction(); |
982 |
FireInstrumentNameChanged(Instr, Name); |
FireInstrumentNameChanged(Instr, toAbstractName(Name)); |
983 |
} |
} |
984 |
|
|
985 |
void InstrumentsDb::MoveInstrument(String Instr, String Dst) { |
void InstrumentsDb::MoveInstrument(String Instr, String Dst) { |
989 |
|
|
990 |
BeginTransaction(); |
BeginTransaction(); |
991 |
try { |
try { |
992 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(ParentDir); |
993 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
994 |
|
|
995 |
String instrName = GetFileName(Instr); |
String instrName = GetFileName(Instr); |
996 |
int instrId = GetInstrumentId(dirId, instrName); |
int instrId = GetInstrumentId(dirId, instrName); |
997 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
998 |
|
|
999 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
1000 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
1001 |
if (dirId == dstId) { |
if (dirId == dstId) { |
1002 |
EndTransaction(); |
EndTransaction(); |
1003 |
return; |
return; |
1004 |
} |
} |
1005 |
|
|
1006 |
if (GetInstrumentId(dstId, instrName) != -1) { |
if (GetInstrumentId(dstId, instrName) != -1) { |
1007 |
throw Exception("Cannot move. Instrument with that name already exists: " + instrName); |
String s = toEscapedPath(instrName); |
1008 |
|
throw Exception("Cannot move. Instrument with that name already exists: " + s); |
1009 |
} |
} |
1010 |
|
|
1011 |
if (GetDirectoryId(dstId, instrName) != -1) { |
if (GetDirectoryId(dstId, instrName) != -1) { |
1012 |
throw Exception("Cannot move. Directory with that name already exists: " + instrName); |
String s = toEscapedPath(instrName); |
1013 |
|
throw Exception("Cannot move. Directory with that name already exists: " + s); |
1014 |
} |
} |
1015 |
|
|
1016 |
std::stringstream sql; |
std::stringstream sql; |
1034 |
BeginTransaction(); |
BeginTransaction(); |
1035 |
try { |
try { |
1036 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
1037 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1038 |
|
|
1039 |
String instrName = GetFileName(Instr); |
String instrName = GetFileName(Instr); |
1040 |
int instrId = GetInstrumentId(dirId, instrName); |
int instrId = GetInstrumentId(dirId, instrName); |
1041 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1042 |
|
|
1043 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
1044 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
1045 |
if (dirId == dstId) { |
if (dirId == dstId) { |
1046 |
EndTransaction(); |
EndTransaction(); |
1047 |
return; |
return; |
1048 |
} |
} |
1049 |
|
|
|
if (GetInstrumentId(dstId, instrName) != -1) { |
|
|
throw Exception("Cannot copy. Instrument with that name already exists: " + instrName); |
|
|
} |
|
|
|
|
|
if (GetDirectoryId(dstId, instrName) != -1) { |
|
|
throw Exception("Cannot copy. Directory with that name already exists: " + instrName); |
|
|
} |
|
|
|
|
1050 |
CopyInstrument(instrId, instrName, dstId, Dst); |
CopyInstrument(instrId, instrName, dstId, Dst); |
1051 |
} catch (Exception e) { |
} catch (Exception e) { |
1052 |
EndTransaction(); |
EndTransaction(); |
1057 |
} |
} |
1058 |
|
|
1059 |
void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) { |
void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) { |
1060 |
|
if (GetInstrumentId(DstDirId, InstrName) != -1) { |
1061 |
|
String s = toEscapedPath(InstrName); |
1062 |
|
throw Exception("Cannot copy. Instrument with that name already exists: " + s); |
1063 |
|
} |
1064 |
|
|
1065 |
|
if (GetDirectoryId(DstDirId, InstrName) != -1) { |
1066 |
|
String s = toEscapedPath(InstrName); |
1067 |
|
throw Exception("Cannot copy. Directory with that name already exists: " + s); |
1068 |
|
} |
1069 |
|
|
1070 |
DbInstrument i = GetInstrumentInfo(InstrId); |
DbInstrument i = GetInstrumentInfo(InstrId); |
1071 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
1072 |
std::stringstream sql; |
std::stringstream sql; |
1080 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1081 |
} |
} |
1082 |
|
|
1083 |
BindTextParam(pStmt, 1, InstrName); |
String s = toDbName(InstrName); |
1084 |
|
BindTextParam(pStmt, 1, s); |
1085 |
BindTextParam(pStmt, 2, i.InstrFile); |
BindTextParam(pStmt, 2, i.InstrFile); |
1086 |
BindTextParam(pStmt, 3, i.FormatFamily); |
BindTextParam(pStmt, 3, i.FormatFamily); |
1087 |
BindTextParam(pStmt, 4, i.FormatVersion); |
BindTextParam(pStmt, 4, i.FormatVersion); |
1106 |
BeginTransaction(); |
BeginTransaction(); |
1107 |
try { |
try { |
1108 |
int id = GetInstrumentId(Instr); |
int id = GetInstrumentId(Instr); |
1109 |
if(id == -1) throw Exception("Unknown DB instrument: " + Instr); |
if(id == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1110 |
|
|
1111 |
std::stringstream sql; |
std::stringstream sql; |
1112 |
sql << "UPDATE instruments SET description=?,modified=CURRENT_TIMESTAMP "; |
sql << "UPDATE instruments SET description=?,modified=CURRENT_TIMESTAMP "; |
1147 |
void InstrumentsDb::AddGigInstruments(String DbDir, String File, int Index, ScanProgress* pProgress) { |
void InstrumentsDb::AddGigInstruments(String DbDir, String File, int Index, ScanProgress* pProgress) { |
1148 |
dmsg(2,("InstrumentsDb: AddGigInstruments(DbDir=%s,File=%s,Index=%d)\n", DbDir.c_str(), File.c_str(), Index)); |
dmsg(2,("InstrumentsDb: AddGigInstruments(DbDir=%s,File=%s,Index=%d)\n", DbDir.c_str(), File.c_str(), Index)); |
1149 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
1150 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir)); |
1151 |
|
|
1152 |
struct stat statBuf; |
struct stat statBuf; |
1153 |
int res = stat(File.c_str(), &statBuf); |
int res = stat(File.c_str(), &statBuf); |
1182 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1183 |
} |
} |
1184 |
|
|
1185 |
BindTextParam(pStmt, 2, File); |
String s = toEscapedFsPath(File); |
1186 |
|
BindTextParam(pStmt, 2, s); |
1187 |
String ver = ""; |
String ver = ""; |
1188 |
if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); |
if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); |
1189 |
BindTextParam(pStmt, 4, ver); |
BindTextParam(pStmt, 4, ver); |
1242 |
std::stringstream sql2; |
std::stringstream sql2; |
1243 |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
1244 |
sql2 << "instr_nr=" << Index; |
sql2 << "instr_nr=" << Index; |
1245 |
if (ExecSqlInt(sql2.str(), File) > 0) return; |
String s = toEscapedFsPath(File); |
1246 |
|
if (ExecSqlInt(sql2.str(), s) > 0) return; |
1247 |
|
|
1248 |
BindTextParam(pStmt, 1, name); |
BindTextParam(pStmt, 1, name); |
1249 |
BindIntParam(pStmt, 3, Index); |
BindIntParam(pStmt, 3, Index); |
1272 |
FireInstrumentCountChanged(DbDir); |
FireInstrumentCountChanged(DbDir); |
1273 |
} |
} |
1274 |
|
|
1275 |
void InstrumentsDb::DirectoryTreeWalk(String Path, DirectoryHandler* pHandler) { |
void InstrumentsDb::DirectoryTreeWalk(String AbstractPath, DirectoryHandler* pHandler) { |
1276 |
int DirId = GetDirectoryId(Path); |
int DirId = GetDirectoryId(AbstractPath); |
1277 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Path); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(AbstractPath)); |
1278 |
DirectoryTreeWalk(pHandler, Path, DirId, 0); |
DirectoryTreeWalk(pHandler, AbstractPath, DirId, 0); |
1279 |
} |
} |
1280 |
|
|
1281 |
void InstrumentsDb::DirectoryTreeWalk(DirectoryHandler* pHandler, String Path, int DirId, int Level) { |
void InstrumentsDb::DirectoryTreeWalk(DirectoryHandler* pHandler, String AbstractPath, int DirId, int Level) { |
1282 |
if(Level == 1000) throw Exception("Possible infinite loop detected"); |
if(Level == 1000) throw Exception("Possible infinite loop detected"); |
1283 |
pHandler->ProcessDirectory(Path, DirId); |
pHandler->ProcessDirectory(AbstractPath, DirId); |
1284 |
|
|
1285 |
String s; |
String s; |
1286 |
StringListPtr pDirs = GetDirectories(DirId); |
StringListPtr pDirs = GetDirectories(DirId); |
1287 |
for(int i = 0; i < pDirs->size(); i++) { |
for(int i = 0; i < pDirs->size(); i++) { |
1288 |
if (Path.length() == 1 && Path.at(0) == '/') s = "/" + pDirs->at(i); |
if (AbstractPath.length() == 1 && AbstractPath.at(0) == '/') { |
1289 |
else s = Path + "/" + pDirs->at(i); |
s = "/" + pDirs->at(i); |
1290 |
|
} else { |
1291 |
|
s = AbstractPath + "/" + pDirs->at(i); |
1292 |
|
} |
1293 |
DirectoryTreeWalk(pHandler, s, GetDirectoryId(DirId, pDirs->at(i)), Level + 1); |
DirectoryTreeWalk(pHandler, s, GetDirectoryId(DirId, pDirs->at(i)), Level + 1); |
1294 |
} |
} |
1295 |
} |
} |
1301 |
BeginTransaction(); |
BeginTransaction(); |
1302 |
try { |
try { |
1303 |
int DirId = GetDirectoryId(Dir); |
int DirId = GetDirectoryId(Dir); |
1304 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
1305 |
|
|
1306 |
if (Recursive) DirectoryTreeWalk(Dir, &directoryFinder); |
if (Recursive) DirectoryTreeWalk(Dir, &directoryFinder); |
1307 |
else directoryFinder.ProcessDirectory(Dir, DirId); |
else directoryFinder.ProcessDirectory(Dir, DirId); |
1321 |
BeginTransaction(); |
BeginTransaction(); |
1322 |
try { |
try { |
1323 |
int DirId = GetDirectoryId(Dir); |
int DirId = GetDirectoryId(Dir); |
1324 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
1325 |
|
|
1326 |
if (Recursive) DirectoryTreeWalk(Dir, &instrumentFinder); |
if (Recursive) DirectoryTreeWalk(Dir, &instrumentFinder); |
1327 |
else instrumentFinder.ProcessDirectory(Dir, DirId); |
else instrumentFinder.ProcessDirectory(Dir, DirId); |
1625 |
|
|
1626 |
void InstrumentsDb::CheckFileName(String File) { |
void InstrumentsDb::CheckFileName(String File) { |
1627 |
if (File.empty()) throw Exception("Invalid file name: " + File); |
if (File.empty()) throw Exception("Invalid file name: " + File); |
|
if (File.find('/') != std::string::npos) { |
|
|
throw Exception("Invalid file name: " + File); |
|
|
} |
|
1628 |
} |
} |
1629 |
|
|
1630 |
String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { |
String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { |
1643 |
throw Exception("Unable to find an unique name: " + Name); |
throw Exception("Unable to find an unique name: " + Name); |
1644 |
} |
} |
1645 |
|
|
1646 |
|
String InstrumentsDb::toDbName(String AbstractName) { |
1647 |
|
for (int i = 0; i < AbstractName.length(); i++) { |
1648 |
|
if (AbstractName.at(i) == '\0') AbstractName.at(i) = '/'; |
1649 |
|
} |
1650 |
|
return AbstractName; |
1651 |
|
} |
1652 |
|
|
1653 |
|
String InstrumentsDb::toEscapedPath(String AbstractName) { |
1654 |
|
for (int i = 0; i < AbstractName.length(); i++) { |
1655 |
|
if (AbstractName.at(i) == '\0') AbstractName.replace(i++, 1, "\\x2f"); |
1656 |
|
else if (AbstractName.at(i) == '\\') AbstractName.replace(i++, 1, "\\\\"); |
1657 |
|
else if (AbstractName.at(i) == '\'') AbstractName.replace(i++, 1, "\\'"); |
1658 |
|
else if (AbstractName.at(i) == '"') AbstractName.replace(i++, 1, "\\\""); |
1659 |
|
else if (AbstractName.at(i) == '\r') AbstractName.replace(i++, 1, "\\r"); |
1660 |
|
else if (AbstractName.at(i) == '\n') AbstractName.replace(i++, 1, "\\n"); |
1661 |
|
} |
1662 |
|
return AbstractName; |
1663 |
|
} |
1664 |
|
|
1665 |
|
String InstrumentsDb::toEscapedText(String text) { |
1666 |
|
for (int i = 0; i < text.length(); i++) { |
1667 |
|
if (text.at(i) == '\\') text.replace(i++, 1, "\\\\"); |
1668 |
|
else if (text.at(i) == '\'') text.replace(i++, 1, "\\'"); |
1669 |
|
else if (text.at(i) == '"') text.replace(i++, 1, "\\\""); |
1670 |
|
else if (text.at(i) == '\r') text.replace(i++, 1, "\\r"); |
1671 |
|
else if (text.at(i) == '\n') text.replace(i++, 1, "\\n"); |
1672 |
|
} |
1673 |
|
return text; |
1674 |
|
} |
1675 |
|
|
1676 |
|
String InstrumentsDb::toEscapedFsPath(String FsPath) { |
1677 |
|
return toEscapedText(FsPath); |
1678 |
|
} |
1679 |
|
|
1680 |
|
String InstrumentsDb::toAbstractName(String DbName) { |
1681 |
|
for (int i = 0; i < DbName.length(); i++) { |
1682 |
|
if (DbName.at(i) == '/') DbName.at(i) = '\0'; |
1683 |
|
} |
1684 |
|
return DbName; |
1685 |
|
} |
1686 |
|
|
1687 |
void InstrumentsDb::FireDirectoryCountChanged(String Dir) { |
void InstrumentsDb::FireDirectoryCountChanged(String Dir) { |
1688 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1689 |
llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir); |