[chronojump/optional_foreign_keys] Creates the table with foreign keys (only for newly created databases).



commit 9e29930e26f5beec465fda30a59dbc0c1cd9f318
Author: Carles Pina i Estany <carles pina cat>
Date:   Sun Aug 21 10:35:34 2016 +0200

    Creates the table with foreign keys (only for newly created databases).
    
    The foreign keys are enabled only if variable environment:
    CHRONOJUMP_SQLITE_FOREIGN_KEYS is defined (e.g.
    CHRONOJUMP_SQLITE_FOREIGN_KEYS=1 chronojump)

 src/sqlite/jump.cs         |    3 ++-
 src/sqlite/jumpRj.cs       |    3 ++-
 src/sqlite/main.cs         |   12 ++++++++++++
 src/sqlite/pulse.cs        |    3 ++-
 src/sqlite/reactionTime.cs |    3 ++-
 src/sqlite/run.cs          |    3 ++-
 src/sqlite/runInterval.cs  |    3 ++-
 src/util.cs                |   14 +++++++++++++-
 8 files changed, 37 insertions(+), 7 deletions(-)
---
diff --git a/src/sqlite/jump.cs b/src/sqlite/jump.cs
index dfc1be7..8f8e2af 100644
--- a/src/sqlite/jump.cs
+++ b/src/sqlite/jump.cs
@@ -52,10 +52,11 @@ class SqliteJump : Sqlite
                        "weight TEXT, " + //string because can contain "33%" or "50Kg"
                        "description TEXT, " +
                        "angle FLOAT, " + //-1.0 if undef
-                       "simulated INT )";      //since db: 0.60 (cj 0.8.1.2) simulated = -1, real test (not 
uploaded to server) = 0, 
+                       "simulated INT, " +     //since db: 0.60 (cj 0.8.1.2) simulated = -1, real test (not 
uploaded to server) = 0, 
                                                //positive numbers represent the serverUniqueID
                                                //the simulated has two purposes, but it's logical because 
                                                //only real tests can be uploaded
+                       "FOREIGN KEY (sessionID) REFERENCES Session(uniqueID))";
                dbcmd.ExecuteNonQuery();
        }
        
diff --git a/src/sqlite/jumpRj.cs b/src/sqlite/jumpRj.cs
index 670fa0a..62d43ef 100644
--- a/src/sqlite/jumpRj.cs
+++ b/src/sqlite/jumpRj.cs
@@ -54,7 +54,8 @@ class SqliteJumpRj : SqliteJump
                        "time FLOAT, " + //if limit it's 'n' jumps, we probably waste 7.371 seconds
                        "limited TEXT, " + //for RJ, "11J" or "11S" (11 Jumps, 11 seconds)
                        "angleString TEXT, " + //"-1" if undef
-                       "simulated INT )";
+                       "simulated INT, " +
+                       "FOREIGN KEY (sessionID) REFERENCES Session(uniqueID))";
                dbcmd.ExecuteNonQuery();
        }
 
diff --git a/src/sqlite/main.cs b/src/sqlite/main.cs
index 762b549..a7d7edc 100644
--- a/src/sqlite/main.cs
+++ b/src/sqlite/main.cs
@@ -153,6 +153,18 @@ class Sqlite
                 */
                string sqlFileTest = home + Path.DirectorySeparatorChar + "test.db";
                string sqlFileTestTemp = temp + Path.DirectorySeparatorChar + "test.db";
+
+               string foreignKeys;
+
+               if (Util.IsEnvironmentVariableEnabled("CHRONOJUMP_SQLITE_FOREIGN_KEYS")) {
+                       foreignKeys = "; foreign keys=true";
+                       LogB.SQL("sqlite foreign keys enabled");
+               }
+               else {
+                       foreignKeys = "; foreign keys=false";
+                       LogB.SQL("sqlite foreign keys disabled");
+               }
+
                string connectionStringTest = "version = 3; Data source = " + sqlFileTest;
                string connectionStringTestTemp = "version = 3; Data source = " + sqlFileTestTemp;
 
diff --git a/src/sqlite/pulse.cs b/src/sqlite/pulse.cs
index 39b0c36..ab866c3 100644
--- a/src/sqlite/pulse.cs
+++ b/src/sqlite/pulse.cs
@@ -48,7 +48,8 @@ class SqlitePulse : Sqlite
                        "totalPulsesNum INT, " +
                        "timeString TEXT, " +
                        "description TEXT, " +
-                       "simulated INT )";              
+                       "simulated INT, " +
+                       "FOREIGN KEY (sessionID) REFERENCES Session(uniqueID))";
                dbcmd.ExecuteNonQuery();
        }
        
diff --git a/src/sqlite/reactionTime.cs b/src/sqlite/reactionTime.cs
index 0484634..337fa2b 100644
--- a/src/sqlite/reactionTime.cs
+++ b/src/sqlite/reactionTime.cs
@@ -46,7 +46,8 @@ class SqliteReactionTime : Sqlite
                        "type TEXT, " +
                        "time FLOAT, " +
                        "description TEXT, " +
-                       "simulated INT )";              
+                       "simulated INT, " +
+                       "FOREIGN KEY (sessionID) REFERENCES Session(uniqueID))";
                dbcmd.ExecuteNonQuery();
        }
        
diff --git a/src/sqlite/run.cs b/src/sqlite/run.cs
index fa704cc..b5060f9 100644
--- a/src/sqlite/run.cs
+++ b/src/sqlite/run.cs
@@ -48,7 +48,8 @@ class SqliteRun : Sqlite
                        "time FLOAT, " +
                        "description TEXT, " +
                        "simulated INT, " +
-                       "initialSpeed INT )";
+                       "initialSpeed INT, " +
+                       "FOREIGN KEY (sessionID) REFERENCES Session(uniqueID))";
                dbcmd.ExecuteNonQuery();
        }
        
diff --git a/src/sqlite/runInterval.cs b/src/sqlite/runInterval.cs
index a2dda81..79b3775 100644
--- a/src/sqlite/runInterval.cs
+++ b/src/sqlite/runInterval.cs
@@ -50,7 +50,8 @@ class SqliteRunInterval : SqliteRun
                        "description TEXT, " +
                        "limited TEXT, " +
                        "simulated INT, " +
-                       "initialSpeed INT)";
+                       "initialSpeed INT, " +
+                       "FOREIGN KEY (sessionID) REFERENCES Session(uniqueID))";
                dbcmd.ExecuteNonQuery();
        }
 
diff --git a/src/util.cs b/src/util.cs
index 3b51ff8..fd24f17 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -60,9 +60,21 @@ public class Util
                else
                        return ConvertToPoint(s);
        }
-               
 
 
+       // If the @p variable is defined and different to 0 and different to
+       // OFF (case insensitive): returns true.
+       public static bool IsEnvironmentVariableEnabled(string variable) {
+               string value = Environment.GetEnvironmentVariable("CHRONOJUMP_SQLITE_FOREIGN_KEYS");
+
+               if (String.IsNullOrEmpty(value) || value == "0" || value.ToUpper() == "OFF") {
+                       return false;
+               }
+               else {
+                       return true;
+               }
+       }
+
        //when we do a query to the server, it returns avg as "0,54" because it's latin localized
        //if client is on english machine, need to convert this to "0.54"
        public static string ConvertToPointIfNeeded (string myString)


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