[chronojump] Transactions on creation of jumps and runs. First boot reduced from 15s to 8



commit 25f6fc0a6de1b745e0193e440972b0ad7048e135
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Feb 16 00:24:36 2015 +0100

    Transactions on creation of jumps and runs. First boot reduced from 15s to 8

 src/sqlite/event.cs    |   19 +++--
 src/sqlite/jumpType.cs |  105 ++++++++++++++++++------
 src/sqlite/runType.cs  |  209 +++++++++++++++++++++++++++++++-----------------
 3 files changed, 225 insertions(+), 108 deletions(-)
---
diff --git a/src/sqlite/event.cs b/src/sqlite/event.cs
index d1a2a8a..be9121b 100644
--- a/src/sqlite/event.cs
+++ b/src/sqlite/event.cs
@@ -47,21 +47,28 @@ class SqliteEvent : Sqlite
                dbcmd.ExecuteNonQuery();
        }
        
-       public static int GraphLinkInsert(string tableName, string eventName, string graphFileName, bool 
dbconOpened)
+       //called from some Chronojump methods
+       //adds dbcmd to be used on next Insert method
+       public static int GraphLinkInsert(string tableName, string eventName, string graphFileName, bool 
dbconOpened) 
+       {
+               return GraphLinkInsert(tableName, eventName, graphFileName, dbconOpened, dbcmd);
+       }
+       //Called from initialize jump, jumpRj
+       public static int GraphLinkInsert(string tableName, string eventName, string graphFileName, bool 
dbconOpened, SqliteCommand mycmd)
        {
                if(! dbconOpened) {
                        Sqlite.Open();
                }
-               dbcmd.CommandText = "INSERT INTO graphLinkTable" + 
+               mycmd.CommandText = "INSERT INTO graphLinkTable" + 
                                "(uniqueID, tableName, eventName, graphFileName, other1, other2)" +
                                " VALUES (NULL, '" + tableName + "', '" + eventName + "', '" + graphFileName 
+ "', '', '')" ;
-               LogB.SQL(dbcmd.CommandText.ToString());
-               dbcmd.ExecuteNonQuery();
+               LogB.SQL(mycmd.CommandText.ToString());
+               mycmd.ExecuteNonQuery();
                //int myLast = dbcon.LastInsertRowId;
                //http://stackoverflow.com/questions/4341178/getting-the-last-insert-id-with-sqlite-net-in-c
                string myString = @"select last_insert_rowid()";
-               dbcmd.CommandText = myString;
-               int myLast = Convert.ToInt32(dbcmd.ExecuteScalar()); // Need to type-cast since 
`ExecuteScalar` returns an object.
+               mycmd.CommandText = myString;
+               int myLast = Convert.ToInt32(mycmd.ExecuteScalar()); // Need to type-cast since 
`ExecuteScalar` returns an object.
                if(! dbconOpened) {
                        Sqlite.Close();
                }
diff --git a/src/sqlite/jumpType.cs b/src/sqlite/jumpType.cs
index 1f58820..5d836b1 100644
--- a/src/sqlite/jumpType.cs
+++ b/src/sqlite/jumpType.cs
@@ -69,9 +69,19 @@ class SqliteJumpType : Sqlite
                };
                conversionSubRateTotal = iniJumpTypes.Length;
                conversionSubRate = 0;
-               foreach(string myJumpType in iniJumpTypes) {
-                       JumpTypeInsert(myJumpType, true);
-                       conversionSubRate ++;
+
+               using(SqliteTransaction tr = dbcon.BeginTransaction())
+               {
+                       using (SqliteCommand dbcmdTr = dbcon.CreateCommand())
+                       {
+                               dbcmdTr.Transaction = tr;
+       
+                               foreach(string myJumpType in iniJumpTypes) {
+                                       JumpTypeInsert(myJumpType, true, dbcmdTr);
+                                       conversionSubRate ++;
+                               }
+                       }
+                       tr.Commit();
                }
 
                AddGraphLinks();        
@@ -81,18 +91,26 @@ class SqliteJumpType : Sqlite
        //don't put the full description because if the user changes language, description will be in old lang
        //description will be on src/jumpType
        public static void AddGraphLinks() {
-               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "Free", "jump_free.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "SJ", "jump_sj.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "SJl", "jump_sj_l.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "CMJ", "jump_cmj.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "CMJl", "jump_cmj_l.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "ABK", "jump_abk.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "ABKl", "jump_abk_l.png", true);
-               //SqliteEvent.GraphLinkInsert (Constants.JumpTable, "Max", "jump_max.png", true); //we 
already have "Free"
-               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "Rocket", "jump_rocket.png", true);
-               //SqliteEvent.GraphLinkInsert (Constants.JumpTable, "DJ", "jump_dj.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "DJa", "jump_dj_a.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "DJna", "jump_dj.png", true);
+               using(SqliteTransaction tr = dbcon.BeginTransaction())
+               {
+                       using (SqliteCommand dbcmdTr = dbcon.CreateCommand())
+                       {
+                               dbcmdTr.Transaction = tr;
+
+                               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "Free", "jump_free.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "SJ", "jump_sj.png", true, 
dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "SJl", "jump_sj_l.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "CMJ", "jump_cmj.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "CMJl", "jump_cmj_l.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "ABK", "jump_abk.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "ABKl", "jump_abk_l.png", 
true, dbcmdTr);
+                               //SqliteEvent.GraphLinkInsert (Constants.JumpTable, "Max", "jump_max.png", 
true, dbcmdTr); //we already have "Free"
+                               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "Rocket", 
"jump_rocket.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "DJa", "jump_dj_a.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpTable, "DJna", "jump_dj.png", 
true, dbcmdTr);
+                       }
+                       tr.Commit();
+               }
        }
 
        //creates table containing the types of repetitive Jumps
@@ -132,56 +150,89 @@ class SqliteJumpType : Sqlite
                        "triple jump:0:0:1:3:Triple jump",
                        //"RunAnalysis:0:0:1:-1:Run between two photocells recording contact and flight times 
in contact platform/s. Until finish button is clicked."
                };
-               foreach(string myJumpType in iniJumpTypes) {
-                       JumpRjTypeInsert(myJumpType, true);
+
+               using(SqliteTransaction tr = dbcon.BeginTransaction())
+               {
+                       using (SqliteCommand dbcmdTr = dbcon.CreateCommand())
+                       {
+                               dbcmdTr.Transaction = tr;
+
+                               foreach(string myJumpType in iniJumpTypes) {
+                                       JumpRjTypeInsert(myJumpType, true, dbcmdTr);
+                               }
+                       }
+                       tr.Commit();
                }
                
                AddGraphLinksRj();      
        }
 
        public static void AddGraphLinksRj() {
-               SqliteEvent.GraphLinkInsert (Constants.JumpRjTable, "RJ(j)", "jump_rj.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpRjTable, "RJ(t)", "jump_rj.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpRjTable, "RJ(unlimited)", "jump_rj_in.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.JumpRjTable, "triple jump", "jump_rj.png", true);
+               using(SqliteTransaction tr = dbcon.BeginTransaction())
+               {
+                       using (SqliteCommand dbcmdTr = dbcon.CreateCommand())
+                       {
+                               dbcmdTr.Transaction = tr;
+                               
+                               SqliteEvent.GraphLinkInsert (Constants.JumpRjTable, "RJ(j)", "jump_rj.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpRjTable, "RJ(t)", "jump_rj.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpRjTable, "RJ(unlimited)", 
"jump_rj_in.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.JumpRjTable, "triple jump", 
"jump_rj.png", true, dbcmdTr);
+                       }
+                       tr.Commit();
+               }
        }
 
        /*
         * JumpType class methods
         */
 
+       //called from some Chronojump methods
+       //adds dbcmd to be used on next Insert method
        public static void JumpTypeInsert(string myJump, bool dbconOpened)
        {
+               JumpTypeInsert(myJump, dbconOpened, dbcmd);
+       }
+       //Called from initialize
+       public static void JumpTypeInsert(string myJump, bool dbconOpened, SqliteCommand mycmd)
+       {
                string [] myStr = myJump.Split(new char[] {':'});
                if(! dbconOpened) {
                        Sqlite.Open();
                }
-               dbcmd.CommandText = "INSERT INTO " + Constants.JumpTypeTable +  
+               mycmd.CommandText = "INSERT INTO " + Constants.JumpTypeTable +  
                                " (uniqueID, name, startIn, weight, description)" +
                                " VALUES (NULL, '"
                                + myStr[0] + "', " + myStr[1] + ", " +  //name, startIn
                                myStr[2] + ", '" + myStr[3] + "')" ;    //weight, description
-               LogB.SQL(dbcmd.CommandText.ToString());
-               dbcmd.ExecuteNonQuery();
+               LogB.SQL(mycmd.CommandText.ToString());
+               mycmd.ExecuteNonQuery();
                if(! dbconOpened) {
                        Sqlite.Close();
                }
        }
 
+       //called from some Chronojump methods
+       //adds dbcmd to be used on next Insert method
        public static void JumpRjTypeInsert(string myJump, bool dbconOpened)
        {
+               JumpRjTypeInsert(myJump, dbconOpened, dbcmd);
+       }
+       //Called from initialize
+       public static void JumpRjTypeInsert(string myJump, bool dbconOpened, SqliteCommand mycmd)
+       {
                string [] myStr = myJump.Split(new char[] {':'});
                if(! dbconOpened) {
                        Sqlite.Open();
                }
-               dbcmd.CommandText = "INSERT INTO " + Constants.JumpRjTypeTable + 
+               mycmd.CommandText = "INSERT INTO " + Constants.JumpRjTypeTable + 
                                " (uniqueID, name, startIn, weight, jumpsLimited, fixedValue, description)" +
                                " VALUES (NULL, '"
                                + myStr[0] + "', " + myStr[1] + ", " +  //name, startIn
                                myStr[2] + ", " + myStr[3] + ", " +     //weight, jumpsLimited
                                myStr[4] + ", '" + myStr[5] + "')" ;    //fixedValue, description
-               LogB.SQL(dbcmd.CommandText.ToString());
-               dbcmd.ExecuteNonQuery();
+               LogB.SQL(mycmd.CommandText.ToString());
+               mycmd.ExecuteNonQuery();
                if(! dbconOpened) {
                        Sqlite.Close();
                }
diff --git a/src/sqlite/runType.cs b/src/sqlite/runType.cs
index 9e70bfb..447844d 100644
--- a/src/sqlite/runType.cs
+++ b/src/sqlite/runType.cs
@@ -78,15 +78,25 @@ class SqliteRunType : Sqlite
                };
                conversionSubRateTotal = iniRunTypes.Length;
                conversionSubRate = 0;
-               foreach(string myString in iniRunTypes) {
-                       //RunTypeInsert(myString, true);
-                       conversionSubRate ++;
-                       string [] s = myString.Split(new char[] {':'});
-                       RunType type = new RunType();
-                       type.Name = s[0];
-                       type.Distance = Convert.ToDouble(Util.ChangeDecimalSeparator(s[1]));
-                       type.Description = s[2];
-                       Insert(type, Constants.RunTypeTable, true);
+
+               using(SqliteTransaction tr = dbcon.BeginTransaction())
+               {
+                       using (SqliteCommand dbcmdTr = dbcon.CreateCommand())
+                       {
+                               dbcmdTr.Transaction = tr;
+
+                               foreach(string myString in iniRunTypes) {
+                                       //RunTypeInsert(myString, true);
+                                       conversionSubRate ++;
+                                       string [] s = myString.Split(new char[] {':'});
+                                       RunType type = new RunType();
+                                       type.Name = s[0];
+                                       type.Distance = Convert.ToDouble(Util.ChangeDecimalSeparator(s[1]));
+                                       type.Description = s[2];
+                                       Insert(type, Constants.RunTypeTable, true, dbcmdTr);
+                               }
+                       }
+                       tr.Commit();
                }
        
                AddGraphLinksRunSimple();       
@@ -97,14 +107,20 @@ class SqliteRunType : Sqlite
         * RunType class methods
         */
 
-       //public static void RunTypeInsert(string myRun, bool dbconOpened)
+       //called from some Chronojump methods
+       //adds dbcmd to be used on next Insert method
        public static int Insert(RunType t, string tableName, bool dbconOpened)
        {
+               return Insert(t, tableName, dbconOpened, dbcmd);
+       }
+       //Called from initialize
+       public static int Insert(RunType t, string tableName, bool dbconOpened, SqliteCommand mycmd)
+       {
                //string [] myStr = myRun.Split(new char[] {':'});
                if(! dbconOpened) {
                        Sqlite.Open();
                }
-               dbcmd.CommandText = "INSERT INTO " + tableName + 
+               mycmd.CommandText = "INSERT INTO " + tableName + 
                                " (uniqueID, name, distance, description)" +
                                " VALUES (NULL, '" +
                                /*
@@ -112,14 +128,14 @@ class SqliteRunType : Sqlite
                                myStr[2] + "')" ;       //description
                                */
                                t.Name + "', " + Util.ConvertToPoint(t.Distance) + ", '" + t.Description +    
  "')" ;  
-               LogB.SQL(dbcmd.CommandText.ToString());
-               dbcmd.ExecuteNonQuery();
+               LogB.SQL(mycmd.CommandText.ToString());
+               mycmd.ExecuteNonQuery();
 
                //int myLast = dbcon.LastInsertRowId;
                //http://stackoverflow.com/questions/4341178/getting-the-last-insert-id-with-sqlite-net-in-c
                string myString = @"select last_insert_rowid()";
-               dbcmd.CommandText = myString;
-               int myLast = Convert.ToInt32(dbcmd.ExecuteScalar()); // Need to type-cast since 
`ExecuteScalar` returns an object.
+               mycmd.CommandText = myString;
+               int myLast = Convert.ToInt32(mycmd.ExecuteScalar()); // Need to type-cast since 
`ExecuteScalar` returns an object.
 
                if(! dbconOpened) {
                        Sqlite.Close();
@@ -241,22 +257,40 @@ class SqliteRunType : Sqlite
        }
        
        public static void AddGraphLinksRunSimple() {
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "20m", "run_simple.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "100m", "run_simple.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "200m", "run_simple.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "400m", "run_simple.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "1000m", "run_simple.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "2000m", "run_simple.png", true);
+               using(SqliteTransaction tr = dbcon.BeginTransaction())
+               {
+                       using (SqliteCommand dbcmdTr = dbcon.CreateCommand())
+                       {
+                               dbcmdTr.Transaction = tr;
+
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "20m", "run_simple.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "100m", "run_simple.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "200m", "run_simple.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "400m", "run_simple.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "1000m", "run_simple.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "2000m", "run_simple.png", 
true, dbcmdTr);
+                       }
+                       tr.Commit();
+               }
        }
 
        public static void AddGraphLinksRunSimpleAgility() {
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Agility-20Yard", "agility_20yard.png", 
true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Agility-505", "agility_505.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Agility-Illinois", "agility_illinois.png", 
true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Agility-Shuttle-Run", 
"agility_shuttle.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Agility-ZigZag", "agility_zigzag.png", 
true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Margaria", "margaria.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Gesell-DBT", "gesell_dbt.png", true);
+               using(SqliteTransaction tr = dbcon.BeginTransaction())
+               {
+                       using (SqliteCommand dbcmdTr = dbcon.CreateCommand())
+                       {
+                               dbcmdTr.Transaction = tr;
+
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Agility-20Yard", 
"agility_20yard.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Agility-505", 
"agility_505.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Agility-Illinois", 
"agility_illinois.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Agility-Shuttle-Run", 
"agility_shuttle.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Agility-ZigZag", 
"agility_zigzag.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Margaria", "margaria.png", 
true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunTable, "Gesell-DBT", 
"gesell_dbt.png", true, dbcmdTr);
+                       }
+                       tr.Commit();
+               }
        }
 
 
@@ -321,18 +355,28 @@ class SqliteRunIntervalType : SqliteRunType
                        "MTGUG:-1:1:3:0:Modified time Getup and Go test:1-7-19",
                        "Agility-3L3R:-1:1:3:0:Turn left three times and turn right three times:24.14-24.14"
                };
-               foreach(string myString in iniRunTypes) {
-                       //RunIntervalTypeInsert(myString, true);
-                       string [] s = myString.Split(new char[] {':'});
-                       RunType type = new RunType();
-                       type.Name = s[0];
-                       type.Distance = Convert.ToDouble(Util.ChangeDecimalSeparator(s[1]));
-                       type.TracksLimited = Util.IntToBool(Convert.ToInt32(s[2]));
-                       type.FixedValue = Convert.ToInt32(s[3]);
-                       type.Unlimited = Util.IntToBool(Convert.ToInt32(s[4]));
-                       type.Description = s[5];
-                       type.DistancesString = s[6];
-                       Insert(type, Constants.RunIntervalTypeTable, true);
+               
+               using(SqliteTransaction tr = dbcon.BeginTransaction())
+               {
+                       using (SqliteCommand dbcmdTr = dbcon.CreateCommand())
+                       {
+                               dbcmdTr.Transaction = tr;
+
+                               foreach(string myString in iniRunTypes) {
+                                       //RunIntervalTypeInsert(myString, true);
+                                       string [] s = myString.Split(new char[] {':'});
+                                       RunType type = new RunType();
+                                       type.Name = s[0];
+                                       type.Distance = Convert.ToDouble(Util.ChangeDecimalSeparator(s[1]));
+                                       type.TracksLimited = Util.IntToBool(Convert.ToInt32(s[2]));
+                                       type.FixedValue = Convert.ToInt32(s[3]);
+                                       type.Unlimited = Util.IntToBool(Convert.ToInt32(s[4]));
+                                       type.Description = s[5];
+                                       type.DistancesString = s[6];
+                                       Insert(type, Constants.RunIntervalTypeTable, true, dbcmdTr);
+                               }
+                       }
+                       tr.Commit();
                }
                
                AddGraphLinksRunInterval();
@@ -357,53 +401,59 @@ class SqliteRunIntervalType : SqliteRunType
                        "RSA Wadley 20, R17 x 12:-1:1:24:0:RSA Wadley and Le Rossignol 1998:20-R17",
                        "RSA Wragg 34.2, R25 x 7:-1:1:14:0:RSA Wragg et al. 2000:34.2-R25"
                };
-               foreach(string myString in iniRunTypes) {
-                       //RunIntervalTypeInsert(myString, true);
-                       string [] s = myString.Split(new char[] {':'});
-                       RunType type = new RunType();
-                       type.Name = s[0];
-                       type.Distance = Convert.ToDouble(Util.ChangeDecimalSeparator(s[1]));
-                       type.TracksLimited = Util.IntToBool(Convert.ToInt32(s[2]));
-                       type.FixedValue = Convert.ToInt32(s[3]);
-                       type.Unlimited = Util.IntToBool(Convert.ToInt32(s[4]));
-                       type.Description = s[5];
-                       type.DistancesString = s[6];
-                       Insert(type, Constants.RunIntervalTypeTable, true);
+               using(SqliteTransaction tr = dbcon.BeginTransaction())
+               {
+                       using (SqliteCommand dbcmdTr = dbcon.CreateCommand())
+                       {
+                               dbcmdTr.Transaction = tr;
+
+                               foreach(string myString in iniRunTypes) {
+                                       //RunIntervalTypeInsert(myString, true);
+                                       string [] s = myString.Split(new char[] {':'});
+                                       RunType type = new RunType();
+                                       type.Name = s[0];
+                                       type.Distance = Convert.ToDouble(Util.ChangeDecimalSeparator(s[1]));
+                                       type.TracksLimited = Util.IntToBool(Convert.ToInt32(s[2]));
+                                       type.FixedValue = Convert.ToInt32(s[3]);
+                                       type.Unlimited = Util.IntToBool(Convert.ToInt32(s[4]));
+                                       type.Description = s[5];
+                                       type.DistancesString = s[6];
+                                       Insert(type, Constants.RunIntervalTypeTable, true, dbcmdTr);
+                               }
+                       }
+                       tr.Commit();
                }
        }
 
 
-
-       //public static void RunIntervalTypeInsert(string myRun, bool dbconOpened)
+       //called from some Chronojump methods
+       //adds dbcmd to be used on next Insert method
        public static new int Insert(RunType t, string tableName, bool dbconOpened)
        {
+               return Insert(t, tableName, dbconOpened, dbcmd);
+       }
+       //Called from initialize
+       public static new int Insert(RunType t, string tableName, bool dbconOpened, SqliteCommand mycmd)
+       {
                //done here for not having twho Sqlite.Opened
                //double distance = t.Distance;
 
-               //string [] myStr = myRun.Split(new char[] {':'});
                if(! dbconOpened) {
                        Sqlite.Open();
                }
-               dbcmd.CommandText = "INSERT INTO " + tableName + 
+               mycmd.CommandText = "INSERT INTO " + tableName + 
                                " (uniqueID, name, distance, tracksLimited, fixedValue, unlimited, 
description, distancesString)" +
                                " VALUES (NULL, '" +
-                               /*
-                               myStr[0] + "', " + myStr[1] + ", " +    //name, distance
-                               myStr[2] + ", " + myStr[3] + ", " +     //tracksLimited, fixedValue
-                               myStr[4] + ", '" + myStr[5] + ", " +    //unlimited, description
-                               myStr[6] + "')" ;                       //distancesString
-                               */
-                               //t.Name +      "', " + distance + ", " + t.TracksLimited +     ", " + 
t.FixedValue + ", " +
                                t.Name +        "', " + t.Distance + ", " + Util.BoolToInt(t.TracksLimited) + 
  ", " + t.FixedValue + ", " +
                                Util.BoolToInt(t.Unlimited) +   ", '" + t.Description + "', '" + 
t.DistancesString +    "')" ;  
-               LogB.SQL(dbcmd.CommandText.ToString());
-               dbcmd.ExecuteNonQuery();
+               LogB.SQL(mycmd.CommandText.ToString());
+               mycmd.ExecuteNonQuery();
                
                //int myLast = dbcon.LastInsertRowId;
                //http://stackoverflow.com/questions/4341178/getting-the-last-insert-id-with-sqlite-net-in-c
                string myString = @"select last_insert_rowid()";
-               dbcmd.CommandText = myString;
-               int myLast = Convert.ToInt32(dbcmd.ExecuteScalar()); // Need to type-cast since 
`ExecuteScalar` returns an object.
+               mycmd.CommandText = myString;
+               int myLast = Convert.ToInt32(mycmd.ExecuteScalar()); // Need to type-cast since 
`ExecuteScalar` returns an object.
 
                if(! dbconOpened) {
                        Sqlite.Close();
@@ -508,14 +558,23 @@ class SqliteRunIntervalType : SqliteRunType
        }
 
        public static void AddGraphLinksRunInterval() {
-               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "byLaps", "run_interval.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "byTime", "run_interval.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "unlimited", "run_interval.png", 
true);
-               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "20m10times", "run_interval.png", 
true);
-               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "7m30seconds", "run_interval.png", 
true);
-               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "20m endurance", "run_interval.png", 
true);
-               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "MTGUG", "mtgug.png", true);
-               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "Agility-3L3R", "agility_3l3r.png", 
true);
+               using(SqliteTransaction tr = dbcon.BeginTransaction())
+               {
+                       using (SqliteCommand dbcmdTr = dbcon.CreateCommand())
+                       {
+                               dbcmdTr.Transaction = tr;
+
+                               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "byLaps", 
"run_interval.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "byTime", 
"run_interval.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "unlimited", 
"run_interval.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "20m10times", 
"run_interval.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "7m30seconds", 
"run_interval.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "20m endurance", 
"run_interval.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "MTGUG", 
"mtgug.png", true, dbcmdTr);
+                               SqliteEvent.GraphLinkInsert (Constants.RunIntervalTable, "Agility-3L3R", 
"agility_3l3r.png", true, dbcmdTr);
+                       }
+                       tr.Commit();
+               }
        }
        
        public static void Delete(string name)


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