Exemple #1
0
        /// <summary>
        /// Merge multiple .db files into a single .db file.
        /// </summary>
        /// <param name="fileSpec">The file specification for the .db files to merge.</param>
        /// <param name="recurse">Recursively search for matching .db files in child directories?</param>
        /// <param name="outFileName">The name of the new output file.</param>
        public static void MergeFiles(string fileSpec, bool recurse, string outFileName)
        {
            File.Delete(outFileName);

            string[] filesToMerge = DirectoryUtilities.FindFiles(fileSpec, recurse);
            if (filesToMerge.Length > 1)
            {
                File.Copy(filesToMerge[0], outFileName, overwrite: true);

                var destination = new SQLite();
                destination.OpenDatabase(outFileName, readOnly: false);
                try
                {
                    foreach (var sourceFileName in filesToMerge.Where(file => file != filesToMerge[0]))
                    {
                        var source = new SQLite();
                        source.OpenDatabase(sourceFileName, readOnly: true);
                        try
                        {
                            Merge(source, destination);
                        }
                        finally
                        {
                            source.CloseDatabase();
                        }
                    }
                }
                finally
                {
                    destination.CloseDatabase();
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Merge multiple .db files into a single .db file.
        /// </summary>
        /// <param name="fileSpec">The file specification for the .db files to merge.</param>
        /// <param name="outFileName">The name of the new output file.</param>
        public static void MergeFiles(string fileSpec, string outFileName)
        {
            File.Delete(outFileName);

            string path = Path.GetDirectoryName(fileSpec);

            if (path == "")
            {
                path = Directory.GetCurrentDirectory();
            }
            var filesToMerge = Directory.GetFiles(path, Path.GetFileName(fileSpec)).ToList();

            if (filesToMerge.Count > 1)
            {
                File.Copy(filesToMerge[0], outFileName, overwrite: true);

                var destination = new SQLite();
                destination.OpenDatabase(outFileName, readOnly: false);
                try
                {
                    foreach (var sourceFileName in filesToMerge.Where(file => file != filesToMerge[0]))
                    {
                        var source = new SQLite();
                        source.OpenDatabase(sourceFileName, readOnly: true);
                        try
                        {
                            Merge(source, destination);
                        }
                        finally
                        {
                            source.CloseDatabase();
                        }
                    }
                }
                finally
                {
                    destination.CloseDatabase();
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Merge multiple .db files into a single .db file.
        /// </summary>
        /// <param name="fileSpec">The file specification for the .db files to merge.</param>
        /// <param name="recurse">Recursively search for matching .db files in child directories?</param>
        /// <param name="outFileName">The name of the new output file.</param>
        public static void MergeFiles(string fileSpec, bool recurse, string outFileName)
        {
            File.Delete(outFileName);

            string[] filesToMerge = DirectoryUtilities.FindFiles(fileSpec, recurse);
            if (filesToMerge.Length > 1)
            {
                File.Copy(filesToMerge[0], outFileName, overwrite: true);

                var destination = new SQLite();
                destination.OpenDatabase(outFileName, readOnly: false);
                try
                {
                    foreach (var sourceFileName in filesToMerge.Where(file => file != filesToMerge[0]))
                    {
                        var source = new SQLite();
                        // If readOnly is true then .db-shm files are not removed on close. Bug in SqLite.
                        // http://sqlite.1065341.n5.nabble.com/journal-files-not-always-removed-td83700.html
                        source.OpenDatabase(sourceFileName, readOnly: false);
                        try
                        {
                            Merge(source, destination);
                        }
                        finally
                        {
                            source.CloseDatabase();
                        }
                    }
                }
                finally
                {
                    destination.CloseDatabase();
                }
            }
            else if (filesToMerge.Length == 1)
            {
                File.Copy(filesToMerge[0], outFileName, true);
            }
        }
Exemple #4
0
        /// <summary>Connect to the SQLite database.</summary>
        /// <param name="forWriting">if set to <c>true</c> [for writing].</param>
        /// <exception cref="Models.Core.ApsimXException">Cannot find name of .db file</exception>
        private void Open(bool forWriting)
        {
            lock (Locks)
            {
                if (Filename == null)
                {
                    Simulations simulations = Apsim.Parent(this, typeof(Simulations)) as Simulations;
                    if (simulations != null)
                        Filename = Path.ChangeExtension(simulations.FileName, ".db");
                }

                if (Filename != null &&
                    (Connection == null ||
                    (ForWriting == false && forWriting == true)))
                {
                    if (Filename == null)
                        throw new ApsimXException(this, "Cannot find name of .db file");

                    Disconnect();

                    ForWriting = forWriting;

                    if (!Locks.ContainsKey(Filename))
                        Locks.Add(Filename, new DbMutex());

                    Locks[Filename].Aquire();
                    try
                    {
                        if (!File.Exists(Filename))
                        {
                            Connection = new SQLite();
                            Connection.OpenDatabase(Filename, readOnly: false);
                            Connection.ExecuteNonQuery("CREATE TABLE Simulations (ID INTEGER PRIMARY KEY ASC, Name TEXT COLLATE NOCASE)");
                            Connection.ExecuteNonQuery("CREATE TABLE Messages (SimulationID INTEGER, ComponentName TEXT, Date TEXT, Message TEXT, MessageType INTEGER)");

                            if (!forWriting)
                            {
                                Connection.CloseDatabase();
                                Connection.OpenDatabase(Filename, readOnly: !forWriting);
                            }
                        }
                        else
                        {
                            Connection = new SQLite();
                            Connection.OpenDatabase(Filename, readOnly: !forWriting);
                        }

                    }
                    finally
                    {
                        Locks[Filename].Release();
                    }

                }
            }
        }