Exemplo n.º 1
0
        public void BuildSqLiteConnectionString_InvalidPath_ThrowsArgumentNullException(string invalidPath)
        {
            // Call
            void Call() => SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(invalidPath, true);

            // Assert
            Assert.Throws <ArgumentNullException>(Call);
        }
Exemplo n.º 2
0
        private void SqliteButton_Click(object sender, RoutedEventArgs e)
        {
            SqLiteConnectionStringBuilder stringBuilder
                = new SqLiteConnectionStringBuilder(new FolderProvider());

            BuildDatabase(new DbContextOptionsBuilder()
                          .UseSqlite(stringBuilder.Create())
                          .Options);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Constructs a connection string to connect the Entity Framework to <paramref name="filePath"/>.
        /// </summary>
        /// <param name="filePath">Location of the storage file.</param>
        /// <returns>A new connection string.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="filePath"/> is <c>null</c> or empty (only whitespaces).</exception>
        public static string BuildSqLiteEntityConnectionString(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentNullException(nameof(filePath), @"Cannot create a connection string without the path to the file to connect to.");
            }

            return(new EntityConnectionStringBuilder
            {
                Metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "DbContext.RiskeerEntities"),
                Provider = @"System.Data.SQLite.EF6",
                ProviderConnectionString = SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(GetDataSourceLocation(filePath), false)
            }.ConnectionString);
        }
Exemplo n.º 4
0
        public void BuildSqLiteConnectionString_ValidPathToSqLiteFile_ValidConnectionString()
        {
            // Call
            bool   readOnly         = new Random(643).NextBoolean();
            string connectionString = SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(pathToSqLiteFile, readOnly);

            // Assert
            Assert.That(!string.IsNullOrEmpty(connectionString));
            StringAssert.Contains(pathToSqLiteFile, connectionString);
            StringAssert.DoesNotContain("metadata=", connectionString);
            StringAssert.DoesNotContain("System.Data.SQLite.EF6", connectionString);
            StringAssert.Contains("failifmissing=True", connectionString);
            StringAssert.Contains($"data source={pathToSqLiteFile}", connectionString);
            StringAssert.Contains($"read only={readOnly}", connectionString);
            StringAssert.Contains("foreign keys=True", connectionString);
            StringAssert.Contains("version=3", connectionString);
            StringAssert.Contains("pooling=False", connectionString);
            StringAssert.Contains("journal mode=Off", connectionString);
        }
Exemplo n.º 5
0
        public void BuildSqLiteConnectionString_UncPathToSqLiteFile_ValidConnectionString()
        {
            // Setup
            bool         readOnly         = new Random(645).NextBoolean();
            const string uncPathToSqlFile = @"\\server\share\file.sqlite";

            // Call
            string connectionString = SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(uncPathToSqlFile, readOnly);

            // Assert
            Assert.That(!string.IsNullOrEmpty(connectionString));
            StringAssert.DoesNotContain("metadata=", connectionString);
            StringAssert.DoesNotContain("System.Data.SQLite.EF6", connectionString);
            StringAssert.Contains("failifmissing=True", connectionString);
            StringAssert.Contains($@"data source={uncPathToSqlFile}", connectionString);
            StringAssert.Contains($"read only={readOnly}", connectionString);
            StringAssert.Contains("foreign keys=True", connectionString);
            StringAssert.Contains("version=3", connectionString);
            StringAssert.Contains("pooling=False", connectionString);
            StringAssert.Contains("journal mode=Off", connectionString);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a new file with the basic database structure for a Riskeer database at
        /// <paramref name="databaseFilePath"/>.
        /// </summary>
        /// <param name="databaseFilePath">Path of the new database file.</param>
        /// <exception cref="ArgumentException">Thrown when either:
        /// <list type="bullet">
        /// <item><paramref name="databaseFilePath"/> is invalid</item>
        /// <item><paramref name="databaseFilePath"/> points to an existing file</item>
        /// </list></exception>
        /// <exception cref="StorageException">Thrown when executing <c>DatabaseStructure</c> script fails.</exception>
        public static void CreateDatabaseStructure(string databaseFilePath)
        {
            IOUtils.ValidateFilePath(databaseFilePath);

            if (File.Exists(databaseFilePath))
            {
                string message = $"File '{databaseFilePath}' already exists.";
                throw new ArgumentException(message);
            }

            SQLiteConnection.CreateFile(databaseFilePath);
            string connectionString = SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(databaseFilePath, false);

            try
            {
                using (var dbContext = new SQLiteConnection(connectionString, true))
                {
                    dbContext.Open();
                    using (SQLiteTransaction transaction = dbContext.BeginTransaction())
                        using (SQLiteCommand command = dbContext.CreateCommand())
                        {
                            command.CommandText = Resources.DatabaseStructure;
                            command.ExecuteNonQuery();

                            transaction.Commit();
                        }
                }
            }
            catch (SQLiteException exception)
            {
                string message = new FileWriterErrorMessageBuilder(databaseFilePath).Build(Resources.Error_writing_structure_to_database);
                throw new StorageException(message, new UpdateStorageException("", exception));
            }
            finally
            {
                SQLiteConnectionHelper.ForcefullyDisposeSQLiteConnection();
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Performs the command on a database.
        /// </summary>
        /// <param name="databaseFilePath">The file path to the database.</param>
        /// <param name="commandText">The command text/query.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="databaseFilePath"/> is <c>null</c> or whitespace.</exception>
        private static void PerformCommandOnDatabase(string databaseFilePath, string commandText)
        {
            string connectionString = SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(databaseFilePath, false);

            using (var dbContext = new SQLiteConnection(connectionString, true))
            {
                dbContext.Open();
                using (SQLiteTransaction transaction = dbContext.BeginTransaction())
                    using (SQLiteCommand command = dbContext.CreateCommand())
                    {
                        try
                        {
                            command.CommandText = commandText;
                            command.ExecuteNonQuery();

                            transaction.Commit();
                        }
                        finally
                        {
                            SQLiteConnection.ClearAllPools();
                        }
                    }
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Opens the connection to the project database file.
 /// </summary>
 public void OpenDatabaseConnection()
 {
     connection = new SQLiteConnection(SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(filePath, false));
     connection.Open();
 }