Exemple #1
0
        /// <summary>
        ///     Call RefreshPossibleTableId() first!
        /// </summary>
        /// <returns></returns>
        internal string GetNextPossibleTableName()
        {
            var i     = PossibleTableId;
            var sName = string.Format("Table{0}", i);

            // possible infinite loop, but how many tables do you have anyway?
            while (TableNames.Contains(sName))
            {
                ++i;
                sName = string.Format("Table{0}", i);
            }
            return(sName);
        }
Exemple #2
0
 public bool IsExistTable(string tableName)
 {
     tableName = (tableName ?? string.Empty);
     return(TableNames.Contains(tableName));
 }
Exemple #3
0
 public bool IsExistTable(string tableName)
 {
     return(TableNames.Contains(tableName, StringComparer.OrdinalIgnoreCase));
 }
Exemple #4
0
 public bool IsExistTable(string tableName)
 {
     return(TableNames.Contains(tableName));
 }
 private bool IsTableValid(string tableName)
 {
     return(TableNames.Contains(tableName));
 }
Exemple #6
0
 internal bool HasTableName(string TableName)
 {
     return(TableNames.Contains(TableName));
 }
Exemple #7
0
    public void OnInitialised()
    {
        if (Title == null)
        {
            Component Simulation = Paddock.LinkByType("Simulation") as Component;
            if (Simulation != null)
            {
                Simulation.Get("Title", out Title);
            }
        }

        FileName = Title + "-" + My.Name + ".db";
#if __MonoCS__
        Connection = new SqliteConnection("Data Source=" + FileName + ";Version=3;New=False;Compress=True;");
        Connection.Open();
        SqliteCommand sql_cmd = new SqliteCommand(Connection);
#else
        Connection = new SQLiteConnection("Data Source=" + FileName + ";Version=3;New=False;Compress=True;");
        Connection.Open();
        SQLiteCommand sql_cmd = new SQLiteCommand(Connection);
#endif

#if __MonoCS__
        using (SqliteTransaction dbTrans = Connection.BeginTransaction())
#else
        using (SQLiteTransaction dbTrans = Connection.BeginTransaction())
#endif
        {
            // Make sure we have a Simulations table.
            if (!TableNames.Contains("Simulations"))
            {
                sql_cmd             = Connection.CreateCommand();
                sql_cmd.CommandText = "CREATE TABLE Simulations (ID INTEGER PRIMARY KEY ASC, Title TEXT)";;
                sql_cmd.ExecuteNonQuery();
            }
            dbTrans.Commit();
        }

#if __MonoCS__
        using (SqliteTransaction dbTrans = Connection.BeginTransaction())
#else
        using (SQLiteTransaction dbTrans = Connection.BeginTransaction())
#endif
        {
            // Make sure we have a Data table.
            if (!TableNames.Contains("Data"))
            {
                string Cmd = "CREATE TABLE Data (SimulationID INTEGER)";
                sql_cmd             = Connection.CreateCommand();
                sql_cmd.CommandText = Cmd;
                sql_cmd.ExecuteNonQuery();
            }
            dbTrans.Commit();
        }

        // Try and get our SimulationID. If it's not in the Simulations table
        // then add it. The title is used as the simulation name.
        SimulationID = GetSimulationID();
        if (SimulationID == -1)
        {
            sql_cmd.CommandText = "INSERT INTO [Simulations] (Title) VALUES ('" + Title + "')";
            sql_cmd.ExecuteNonQuery();
            SimulationID = GetSimulationID();
        }

        // Remove existing data for this simulation.
        sql_cmd.CommandText = "DELETE FROM [Data] WHERE SimulationID = " + SimulationID.ToString();
        sql_cmd.ExecuteNonQuery();
        SimulationID = GetSimulationID();

        // Give Sqlite some commands to speed up the adding of data.
        sql_cmd             = Connection.CreateCommand();
        sql_cmd.CommandText = "PRAGMA synchronous=OFF";
        sql_cmd.ExecuteNonQuery();

        // subscribe to all events.
        Paddock.Subscribe(Variables.OutputFrequency, OnDoReport);
    }
Exemple #8
0
        /// <summary>Remove all simulations from the database that don't exist in 'simulationsToKeep'</summary>
        /// <param name="simulationsToKeep">The simulations to keep.</param>
        /// <param name="simulationNamesToBeRun">The simulation names about to be run.</param>
        public void RemoveUnwantedSimulations(Simulations simulationsToKeep, List <string> simulationNamesToBeRun)
        {
            Open(forWriting: true);

            string[] simulationNamesToKeep = simulationsToKeep.FindAllSimulationNames();


            // Tell SQLite that we're beginning a transaction.
            Connection.ExecuteNonQuery("BEGIN");

            try
            {
                // Make sure that the list of simulations in 'simulationsToKeep' are in the
                // Simulations table.
                string[] simulationNames = this.SimulationNames;
                string   sql             = string.Empty;
                int      j = 0;
                foreach (string simulationNameToKeep in simulationNamesToKeep)
                {
                    if (!StringUtilities.Contains(simulationNames, simulationNameToKeep))
                    {
                        if (sql != string.Empty)
                        {
                            sql += "),(";
                        }
                        sql += "'" + simulationNameToKeep + "'";
                    }
                    if (j == 100)
                    {
                        if (sql != string.Empty)
                        {
                            RunQueryWithNoReturnData("INSERT INTO [Simulations] (Name) VALUES (" + sql + ")");
                        }
                        sql = string.Empty;
                        j   = 0;
                    }
                    j++;
                }

                if (sql != string.Empty)
                {
                    RunQueryWithNoReturnData("INSERT INTO [Simulations] (Name) VALUES (" + sql + ")");
                }

                // Get a list of simulation IDs that we are to delete.
                List <int> idsToDelete = new List <int>();
                foreach (string simulationNameInDB in SimulationNames)
                {
                    if (!simulationNamesToKeep.Contains(simulationNameInDB))
                    {
                        idsToDelete.Add(GetSimulationID(simulationNameInDB));
                    }
                }

                // create an SQL WHERE clause with all IDs
                string idString = "";
                for (int i = 0; i < idsToDelete.Count; i++)
                {
                    if (i > 0)
                    {
                        idString += " OR ";
                    }
                    idString += "ID = " + idsToDelete[i].ToString();
                }

                if (idString != string.Empty)
                {
                    RunQueryWithNoReturnData("DELETE FROM Simulations WHERE " + idString);
                }

                // Now add to IDs to delete the simulations IDs of the simulations we are
                // about to run i.e. remove the rows that we are about to regenerate.
                idsToDelete.Clear();
                foreach (string simulationNameToBeRun in simulationNamesToBeRun)
                {
                    idsToDelete.Add(GetSimulationID(simulationNameToBeRun));
                }

                idString = string.Empty;
                j        = 0;
                for (int i = 0; i < idsToDelete.Count; i++)
                {
                    if (j > 0)
                    {
                        idString += " OR ";
                    }
                    idString += "SimulationID = " + idsToDelete[i].ToString();

                    if (j == 100 || j == idsToDelete.Count - 1)
                    {
                        foreach (string tableName in TableNames)
                        {
                            // delete this simulation
                            RunQueryWithNoReturnData("DELETE FROM " + tableName + " WHERE " + idString);
                        }

                        if (TableNames.Contains(UnitsTableName))
                        {
                            RunQueryWithNoReturnData("DELETE FROM " + UnitsTableName + " WHERE " + idString);
                        }

                        idString = string.Empty;
                        j        = 0;
                    }
                    else
                    {
                        j++;
                    }
                }
            }
            finally
            {
                // Tell SQLite that we're ending a transaction.
                Connection.ExecuteNonQuery("END");
            }
        }