public void BuildSqLiteConnectionString_InvalidPath_ThrowsArgumentNullException(string invalidPath) { // Call void Call() => SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(invalidPath, true); // Assert Assert.Throws <ArgumentNullException>(Call); }
private void SqliteButton_Click(object sender, RoutedEventArgs e) { SqLiteConnectionStringBuilder stringBuilder = new SqLiteConnectionStringBuilder(new FolderProvider()); BuildDatabase(new DbContextOptionsBuilder() .UseSqlite(stringBuilder.Create()) .Options); }
/// <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); }
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); }
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); }
/// <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(); } }
/// <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(); } } } }
/// <summary> /// Opens the connection to the project database file. /// </summary> public void OpenDatabaseConnection() { connection = new SQLiteConnection(SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(filePath, false)); connection.Open(); }