[chronojump] DB:1.12 encoder paths are relative in the DB



commit 733cae703ae32cca5e3bb17fd25fa7935dfbd89e
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Aug 27 19:32:36 2014 +0200

    DB:1.12 encoder paths are relative in the DB

 src/encoder.cs           |    4 ++--
 src/gui/preferences.cs   |    2 +-
 src/sqlite/encoder.cs    |   32 +++++++++++++++++++++++++-------
 src/sqlite/main.cs       |   16 +++++++++++++---
 src/sqlite/oldConvert.cs |   27 +++++++++++++++++++++++++++
 src/util.cs              |   11 +++++++++--
 6 files changed, 77 insertions(+), 15 deletions(-)
---
diff --git a/src/encoder.cs b/src/encoder.cs
index 43631e0..d2f563d 100644
--- a/src/encoder.cs
+++ b/src/encoder.cs
@@ -433,12 +433,12 @@ public class EncoderSQL
        public string extraWeight;
        public string signalOrCurve;
        public string filename;
-       public string url;
+       public string url;      //URL of data of signals and curves. Stored in DB as relative. Used in 
software as absolute. See SqliteEncoder
        public int time;
        public int minHeight;
        public string description;
        public string status;   //active or inactive curves
-       public string videoURL; //URL of video of signals
+       public string videoURL; //URL of video of signals. Stored in DB as relative. Used in software as 
absolute. See SqliteEncoder
        
        //encoderConfiguration conversions
        //in signals and curves, need to do conversions (invert, inertiaMomentum, diameter)
diff --git a/src/gui/preferences.cs b/src/gui/preferences.cs
index 77c7e3c..193db13 100644
--- a/src/gui/preferences.cs
+++ b/src/gui/preferences.cs
@@ -521,7 +521,7 @@ public class PreferencesWindow {
        }
        
        private void copyRecursive() {
-               Util.CopyFilesRecursively(new DirectoryInfo(Util.GetParentDir()), new 
DirectoryInfo(fileCopy));
+               Util.CopyFilesRecursively(new DirectoryInfo(Util.GetParentDir(false)), new 
DirectoryInfo(fileCopy));
        }
        
        private bool PulseGTK ()
diff --git a/src/sqlite/encoder.cs b/src/sqlite/encoder.cs
index b039d9e..255b350 100644
--- a/src/sqlite/encoder.cs
+++ b/src/sqlite/encoder.cs
@@ -49,12 +49,12 @@ class SqliteEncoder : Sqlite
                        "extraWeight TEXT, " +  //string because can contain "33%" or "50Kg"
                        "signalOrCurve TEXT, " + //"signal" or "curve"
                        "filename TEXT, " +
-                       "url TEXT, " +
+                       "url TEXT, " +          //URL of data of signals and curves. stored as relative
                        "time INT, " +
                        "minHeight INT, " +
                        "description TEXT, " +
                        "status TEXT, " +       //"active", "inactive"
-                       "videoURL TEXT, " +     //URL of video of signals
+                       "videoURL TEXT, " +     //URL of video of signals. stored as relative
                        "encoderConfiguration TEXT, " + //text separated by ':'
                        "future1 TEXT, " +      //Since 1.4.4 (DB 1.06) this stores last meanPower detected 
on a curve 
                                                //(as string with '.' because future1 was created as TEXT)
@@ -84,8 +84,10 @@ class SqliteEncoder : Sqlite
                        es.exerciseID + ", '" + es.eccon + "', '" +
                        es.laterality + "', '" + es.extraWeight + "', '" +
                        es.signalOrCurve + "', '" + es.filename + "', '" +
-                       es.url + "', " + es.time + ", " + es.minHeight + ", '" + es.description + 
-                       "', '" + es.status + "', '" + es.videoURL + "', '" + 
+                       removeURLpath(es.url) + "', " + 
+                       es.time + ", " + es.minHeight + ", '" + es.description + 
+                       "', '" + es.status + "', '" + 
+                       removeURLpath(es.videoURL) + "', '" + 
                        es.encoderConfiguration.ToString(":",true) + "', '" + 
                        Util.ConvertToPoint(es.future1) + "', '" + es.future2 + "', '" + es.future3 + "')";
                Log.WriteLine(dbcmd.CommandText.ToString());
@@ -120,7 +122,7 @@ class SqliteEncoder : Sqlite
                                "', extraWeight = '" + es.extraWeight +
                                "', signalOrCurve = '" + es.signalOrCurve +
                                "', filename = '" + es.filename +
-                               "', url = '" + es.url +
+                               "', url = '" + removeURLpath(es.url) +
                                "', time = " + es.time +
                                ", minHeight = " + es.minHeight +
                                ", description = '" + es.description + 
@@ -229,7 +231,7 @@ class SqliteEncoder : Sqlite
                                        reader[6].ToString(),                   //extraWeight
                                        reader[7].ToString(),                   //signalOrCurve
                                        reader[8].ToString(),                   //filename
-                                       reader[9].ToString(),                   //url
+                                       addURLpath(reader[9].ToString()),       //url
                                        Convert.ToInt32(reader[10].ToString()), //time
                                        Convert.ToInt32(reader[11].ToString()), //minHeight
                                        reader[12].ToString(),                  //description
@@ -413,8 +415,24 @@ class SqliteEncoder : Sqlite
                if( ! dbconOpened)
                        dbcon.Close();
        }
+       //url and videoURL stored path is relative to be able to move data between computers
+       //then SELECT: makes it abolute (addURLpath)
+       //INSERT and UPDATE: makes it relative (removeURLpath)
+       private static string addURLpath(string url) {
+               string parentDir = Util.GetParentDir(true); //add final '/' or '\'
+               if( ! url.StartsWith(parentDir) )
+                       url = parentDir + url; 
+
+               return url;
+       }
+       private static string removeURLpath(string url) {
+               string parentDir = Util.GetParentDir(true); //add final '/' or '\'
+               if( url.StartsWith(parentDir) )
+                       url = url.Replace(parentDir, ""); 
+
+               return url;
+       }
 
-       
 
        /*
         * EncoderExercise stuff
diff --git a/src/sqlite/main.cs b/src/sqlite/main.cs
index 24f53a1..68a0832 100644
--- a/src/sqlite/main.cs
+++ b/src/sqlite/main.cs
@@ -74,7 +74,7 @@ class Sqlite
         * Important, change this if there's any update to database
         * Important2: if database version get numbers higher than 1, check if the comparisons with 
currentVersion works ok
         */
-       static string lastChronojumpDatabaseVersion = "1.11";
+       static string lastChronojumpDatabaseVersion = "1.12";
 
        public Sqlite() {
        }
@@ -1638,9 +1638,17 @@ class Sqlite
 
                                currentVersion = "1.11";
                        }
-               
-               
+                       if(currentVersion == "1.11") {
+                               dbcon.Open();
+                       
+                               Log.WriteLine("URLs from absolute to relative)");
+                               
+                               SqliteOldConvert.ConvertAbsolutePathsToRelative(); 
+                               SqlitePreferences.Update ("databaseVersion", "1.12", true); 
+                               dbcon.Close();
 
+                               currentVersion = "1.12";
+                       }
                                
                }
 
@@ -1782,6 +1790,8 @@ class Sqlite
                SqliteCountry.initialize();
                
                //changes [from - to - desc]
+               //1.11 - 1.12 Converted DB to 1.12 URLs from absolute to relative
+               //1.10 - 1.11 Converted DB to 1.11 Added option on autosave curves on capture 
(all/bestmeanpower/none)
                //1.09 - 1.10 Converted DB to 1.10 Added RSA RAST on runType
                //1.08 - 1.09 Converted DB to 1.09 Added option on preferences to useHeightsOnJumpIndexes 
(default) or not
                //1.07 - 1.08 Converted DB to 1.08 Added translate statistics graph option to preferences
diff --git a/src/sqlite/oldConvert.cs b/src/sqlite/oldConvert.cs
index fbdf16f..273f8b7 100644
--- a/src/sqlite/oldConvert.cs
+++ b/src/sqlite/oldConvert.cs
@@ -29,6 +29,33 @@ using Mono.Data.Sqlite;
 class SqliteOldConvert : Sqlite
 {
 
+       //to be easier to move data between computers, absolute paths have to be converted to relative
+       //DB 1.11 -> 1.12
+       //dbcon is already opened
+       public static void ConvertAbsolutePathsToRelative () 
+       {
+               //get parentDir with the final '/' or '\'
+               string parentDir = Util.GetParentDir(true);
+
+               ConvertAbsolutePathsToRelativeDo(parentDir, "encoder", "videoURL");
+               ConvertAbsolutePathsToRelativeDo(parentDir, "encoder", "url");
+
+               //URLs of videos of contact tests: jump, run,... are not in the database
+               //URLs of images of person77                     are not in the database
+       }
+       public static void ConvertAbsolutePathsToRelativeDo (string parentDir, string table, string column) {
+               //eg. dbcmd.CommandText = "UPDATE encoder SET videoURL = replace( videoURL, 
'/home/user/.local/share/Chronojump/', '' ) " + 
+               //      "WHERE videoURL LIKE '/home/user/.local/share/Chronojump/%'";
+
+               dbcmd.CommandText = "UPDATE " + table + " SET " + column + " = replace( " + column + ", '" + 
parentDir + "', '' ) " + 
+                       "WHERE " + column + " LIKE '" + parentDir + "%'";
+
+               Log.WriteLine(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+
+       }       
+
+
        //pass uniqueID value and then will return one record. do like this:
        //EncoderSQL eSQL = (EncoderSQL) SqliteEncoder.Select(false, myUniqueID, 0, 0, "")[0];
        //or
diff --git a/src/util.cs b/src/util.cs
index 78e44bb..31eadcd 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -708,10 +708,16 @@ public class Util
        /********** start of LocalApplicationData path ************/
        
        //parent of database, multimedia and encoder
-       public static string GetParentDir() {
-               return Path.Combine(
+       //if withFinalSeparator, then return a '\' or '/' at the end
+       public static string GetParentDir(bool withFinalSeparator) {
+               string path = Path.Combine(
                                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                "Chronojump");
+
+               if(withFinalSeparator)
+                       path += Path.DirectorySeparatorChar;
+
+               return path;
        }
        
        /********** end of LocalApplicationData path ************/
@@ -801,6 +807,7 @@ public class Util
                }
        }
 
+       //returns absolute path, but in encoder this URL is stored in database as relative to be able to move 
data between computers
        public static string GetVideoFileName (int sessionID, Constants.TestTypes testType, int uniqueID) {
                return GetVideoSessionDir(sessionID) + Path.DirectorySeparatorChar + 
                        testType.ToString() + "-" + uniqueID.ToString() +


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