[chronojump] processMultiDatabases separated sqlite methods



commit 59c83cdb89bd148155cb2f892e235c1113ded292
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Dec 20 12:53:08 2019 +0100

    processMultiDatabases separated sqlite methods

 processMultiDatabases/howto_compile.txt        |   2 +-
 processMultiDatabases/processMultiDatabases.cs | 134 ++---------------------
 processMultiDatabases/sqlite.cs                | 142 +++++++++++++++++++++++++
 3 files changed, 152 insertions(+), 126 deletions(-)
---
diff --git a/processMultiDatabases/howto_compile.txt b/processMultiDatabases/howto_compile.txt
index 93fcbbb7..83669479 100644
--- a/processMultiDatabases/howto_compile.txt
+++ b/processMultiDatabases/howto_compile.txt
@@ -1 +1 @@
-mcs processMultiDatabases.cs callR.cs constants.cs encoderGraphOptions.cs encoderParams.cs encoderSQL.cs 
encoderStruct.cs person.cs util.cs utilDate.cs utilEncoder.cs -r:Mono.Data.Sqlite -r:System.Data
+mcs processMultiDatabases.cs callR.cs constants.cs encoderGraphOptions.cs encoderParams.cs encoderSQL.cs 
encoderStruct.cs person.cs sqlite.cs util.cs utilDate.cs utilEncoder.cs -r:Mono.Data.Sqlite -r:System.Data
diff --git a/processMultiDatabases/processMultiDatabases.cs b/processMultiDatabases/processMultiDatabases.cs
index 44246fb3..8d271a3e 100644
--- a/processMultiDatabases/processMultiDatabases.cs
+++ b/processMultiDatabases/processMultiDatabases.cs
@@ -34,7 +34,6 @@
 using System;
 using System.IO; //"File" things. TextWriter. Path
 using System.Collections.Generic; //List<T>
-using Mono.Data.Sqlite;
 
 class ProcessMultiDatabases
 {
@@ -55,6 +54,7 @@ class ProcessMultiDatabases
        private string currentExerciseString;
        private int currentExercise;
        private int currentPercentWeight;
+       private Sqlite sqlite;
 
        //hardcoded stuff (search 'hardcoded' on):
        //callR.cs
@@ -62,9 +62,6 @@ class ProcessMultiDatabases
 
        // <---- end of configuration variables
 
-       private string database = "chronojump.db";
-        private SqliteConnection dbcon;
-       private SqliteCommand dbcmd;
 
        public static void Main(string[] args)
        {
@@ -93,18 +90,20 @@ class ProcessMultiDatabases
        public ProcessMultiDatabases()
        {
                configure();
-               sqliteCreateConnection();
-               sqliteOpen();
+
+               sqlite = new Sqlite();
+               sqlite.CreateConnection(currentDBPath);
+               sqlite.Open();
 
                processDatabase();
 
-               sqliteClose();
+               sqlite.Close();
                Console.WriteLine("processMultiDatabases done!");
        }
 
        private void processDatabase()
        {
-               List<EncoderSQL> list = SelectEncoder (currentExercise);
+               List<EncoderSQL> list = sqlite.SelectEncoder (currentExercise);
 
                TextWriter writer = File.CreateText("/tmp/" + currentFilenamePre + "-" + 
currentExerciseString + ".csv");
                
writer.WriteLine("city,exercise,person,sex,moment,rep,series,exercise,massBody,massExtra,start,width,height,meanSpeed,maxSpeed,maxSpeedT,meanPower,peakPower,peakPowerT,RPD,meanForce,maxForce,maxForceT,RFD,workJ,impulse,laterality,inertiaM");
@@ -113,8 +112,8 @@ class ProcessMultiDatabases
                foreach(EncoderSQL eSQL in list)
                {
                        Console.WriteLine(string.Format("progress: {0}/{1} - ", count, list.Count) + 
eSQL.ToString());
-                       Person person = SelectPerson (eSQL.personID);
-                       double personWeight = SelectPersonWeight(eSQL.personID);
+                       Person person = sqlite.SelectPerson (eSQL.personID);
+                       double personWeight = sqlite.SelectPersonWeight(eSQL.personID);
 
                        EncoderParams ep = new EncoderParams(
                                        20, 
//preferences.EncoderCaptureMinHeight(encoderConfigurationCurrent.has_inertia), 
@@ -186,121 +185,6 @@ class ProcessMultiDatabases
                Console.WriteLine("processDatabase done!");
        }
 
-       // ---- sqlite main methods ----
-
-       private void CreateAndOpen()
-       {
-               sqliteCreateConnection();
-               sqliteOpen();
-       }
-
-       private void sqliteCreateConnection()
-       {
-               dbcon = new SqliteConnection ();
-               string sqlFile = currentDBPath + Path.DirectorySeparatorChar + database;
-               Console.WriteLine(sqlFile);
-               dbcon.ConnectionString = "version = 3; Data source = " + sqlFile;
-               dbcmd = dbcon.CreateCommand();
-       }
-       private void sqliteOpen()
-       {
-               dbcon.Open();
-       }
-       private void sqliteClose()
-       {
-               dbcon.Close();
-       }
-
-       public Person SelectPerson (int uniqueID)
-       {
-               dbcmd.CommandText = "SELECT * FROM person77 WHERE uniqueID = " + uniqueID;
-               Console.WriteLine(dbcmd.CommandText.ToString());
-
-               SqliteDataReader reader;
-               reader = dbcmd.ExecuteReader();
-
-               Person p = new Person(-1);
-               if(reader.Read()) {
-                       p = new Person(
-                                       Convert.ToInt32(reader[0].ToString()), //uniqueID
-                                       reader[1].ToString(),                   //name
-                                       reader[2].ToString(),                   //sex
-                                       UtilDate.FromSql(reader[3].ToString()),//dateBorn
-                                       Convert.ToInt32(reader[4].ToString()), //race
-                                       Convert.ToInt32(reader[5].ToString()), //countryID
-                                       reader[6].ToString(),                   //description
-                                       reader[7].ToString(),                   //future1: rfid
-                                       reader[8].ToString(),                   //future2: clubID
-                                       Convert.ToInt32(reader[9].ToString()) //serverUniqueID
-                                     );
-               }
-               reader.Close();
-
-               return p;
-       }
-
-       private double SelectPersonWeight (int personID)
-       {
-               dbcmd.CommandText = "SELECT weight FROM personSession77 WHERE personID = " + personID;
-               SqliteDataReader reader;
-               reader = dbcmd.ExecuteReader();
-
-               double myReturn = 0;
-                if(reader.Read()) {
-                        myReturn = Convert.ToDouble(Util.ChangeDecimalSeparator(reader[0].ToString()));
-                }
-                reader.Close();
-
-                return myReturn;
-       }
-
-       //TODO: will need session to process this by sessions or compare with filenames
-       private List<EncoderSQL> SelectEncoder (int exerciseID)
-        {
-               dbcmd.CommandText = "SELECT * FROM encoder WHERE signalOrCurve = 'signal' AND exerciseID = " 
+ exerciseID;
-               SqliteDataReader reader;
-               reader = dbcmd.ExecuteReader();
-
-               List<EncoderSQL> list = new List<EncoderSQL>();
-               while(reader.Read())
-               {
-                       EncoderSQL eSQL = new EncoderSQL (
-                                       reader[0].ToString(),                   //uniqueID
-                                       Convert.ToInt32(reader[1].ToString()),  //personID      
-                                       Convert.ToInt32(reader[2].ToString()),  //sessionID
-                                       Convert.ToInt32(reader[3].ToString()),  //exerciseID
-                                       reader[4].ToString(),                   //eccon
-                                       reader[5].ToString(),//laterality
-                                       Util.ChangeDecimalSeparator(reader[6].ToString()),      //extraWeight
-                                       reader[7].ToString(),                   //signalOrCurve
-                                       reader[8].ToString(),                   //filename
-                                       fixOSpath(reader[9].ToString()), 
//Util.MakeURLabsolute(fixOSpath(reader[9].ToString())),  //url
-                                       Convert.ToInt32(reader[10].ToString()), //time
-                                       Convert.ToInt32(reader[11].ToString()), //minHeight
-                                       reader[12].ToString(),                  //description
-                                       reader[13].ToString(),                  //status
-                                       reader[14].ToString(), //videoURL,                               
//videoURL
-                                       reader[15].ToString(), //econf,                                  
//encoderConfiguration
-                                       Util.ChangeDecimalSeparator(reader[16].ToString()),     //future1 
(meanPower on curves)
-                                       reader[17].ToString(),                  //future2
-                                       reader[18].ToString()//,                  //future3
-                                       //reader[19].ToString()                   //EncoderExercise.name
-                                               );
-                       list.Add (eSQL);
-               }
-               reader.Close();
-               return list;
-       }
-
-       private static string fixOSpath(string url) {
-               //if(UtilAll.IsWindows())
-               //      return url.Replace("/","\\");
-               //else
-                       return url.Replace("\\","/");
-       }
-
-       
-       // ---- end of sqlite main methods ----
 
 }
 
diff --git a/processMultiDatabases/sqlite.cs b/processMultiDatabases/sqlite.cs
new file mode 100644
index 00000000..8a2dff50
--- /dev/null
+++ b/processMultiDatabases/sqlite.cs
@@ -0,0 +1,142 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or   
+ *    (at your option) any later version.
+ *              
+ * ChronoJump is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ *    GNU General Public License for more details.
+ *                      
+ * You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software 
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *              
+ * Copyright (C) 2019   Xavier de Blas <xaviblas gmail com> 
+ */
+
+using System;
+using System.IO; //"File" things. TextWriter. Path
+using System.Collections.Generic; //List<T>
+using Mono.Data.Sqlite;
+
+public class Sqlite
+{
+       private SqliteConnection dbcon;
+       private SqliteCommand dbcmd;
+
+       public Sqlite()
+       {
+       }
+
+       public void CreateConnection(string currentDBPath)
+       {
+               dbcon = new SqliteConnection ();
+               string sqlFile = currentDBPath + Path.DirectorySeparatorChar + "chronojump.db";
+               Console.WriteLine(sqlFile);
+               dbcon.ConnectionString = "version = 3; Data source = " + sqlFile;
+               dbcmd = dbcon.CreateCommand();
+       }
+
+       public void Open()
+       {
+               dbcon.Open();
+       }
+
+       public void Close()
+       {
+               dbcon.Close();
+       }
+
+       public Person SelectPerson (int uniqueID)
+       {
+               dbcmd.CommandText = "SELECT * FROM person77 WHERE uniqueID = " + uniqueID;
+               Console.WriteLine(dbcmd.CommandText.ToString());
+
+               SqliteDataReader reader;
+               reader = dbcmd.ExecuteReader();
+
+               Person p = new Person(-1);
+               if(reader.Read()) {
+                       p = new Person(
+                                       Convert.ToInt32(reader[0].ToString()), //uniqueID
+                                       reader[1].ToString(),                   //name
+                                       reader[2].ToString(),                   //sex
+                                       UtilDate.FromSql(reader[3].ToString()),//dateBorn
+                                       Convert.ToInt32(reader[4].ToString()), //race
+                                       Convert.ToInt32(reader[5].ToString()), //countryID
+                                       reader[6].ToString(),                   //description
+                                       reader[7].ToString(),                   //future1: rfid
+                                       reader[8].ToString(),                   //future2: clubID
+                                       Convert.ToInt32(reader[9].ToString()) //serverUniqueID
+                                     );
+               }
+               reader.Close();
+
+               return p;
+       }
+
+       //is not in above method because it checks personSession77
+       public double SelectPersonWeight (int personID)
+       {
+               dbcmd.CommandText = "SELECT weight FROM personSession77 WHERE personID = " + personID;
+               SqliteDataReader reader;
+               reader = dbcmd.ExecuteReader();
+
+               double myReturn = 0;
+                if(reader.Read()) {
+                        myReturn = Convert.ToDouble(Util.ChangeDecimalSeparator(reader[0].ToString()));
+                }
+                reader.Close();
+
+                return myReturn;
+       }
+
+       //TODO: will need session to process this by sessions or compare with filenames
+       public List<EncoderSQL> SelectEncoder (int exerciseID)
+        {
+               dbcmd.CommandText = "SELECT * FROM encoder WHERE signalOrCurve = 'signal' AND exerciseID = " 
+ exerciseID;
+               SqliteDataReader reader;
+               reader = dbcmd.ExecuteReader();
+
+               List<EncoderSQL> list = new List<EncoderSQL>();
+               while(reader.Read())
+               {
+                       EncoderSQL eSQL = new EncoderSQL (
+                                       reader[0].ToString(),                   //uniqueID
+                                       Convert.ToInt32(reader[1].ToString()),  //personID      
+                                       Convert.ToInt32(reader[2].ToString()),  //sessionID
+                                       Convert.ToInt32(reader[3].ToString()),  //exerciseID
+                                       reader[4].ToString(),                   //eccon
+                                       reader[5].ToString(),//laterality
+                                       Util.ChangeDecimalSeparator(reader[6].ToString()),      //extraWeight
+                                       reader[7].ToString(),                   //signalOrCurve
+                                       reader[8].ToString(),                   //filename
+                                       fixOSpath(reader[9].ToString()), 
//Util.MakeURLabsolute(fixOSpath(reader[9].ToString())),  //url
+                                       Convert.ToInt32(reader[10].ToString()), //time
+                                       Convert.ToInt32(reader[11].ToString()), //minHeight
+                                       reader[12].ToString(),                  //description
+                                       reader[13].ToString(),                  //status
+                                       reader[14].ToString(), //videoURL,                               
//videoURL
+                                       reader[15].ToString(), //econf,                                  
//encoderConfiguration
+                                       Util.ChangeDecimalSeparator(reader[16].ToString()),     //future1 
(meanPower on curves)
+                                       reader[17].ToString(),                  //future2
+                                       reader[18].ToString()//,                  //future3
+                                       //reader[19].ToString()                   //EncoderExercise.name
+                                               );
+                       list.Add (eSQL);
+               }
+               reader.Close();
+               return list;
+       }
+
+       private static string fixOSpath(string url) {
+               //if(UtilAll.IsWindows())
+               //      return url.Replace("/","\\");
+               //else
+                       return url.Replace("\\","/");
+       }
+}


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