Esempio n. 1
0
        public void When_accessing_embedded_resource_QuartzMySql_embedded_resource_is_returned()
        {
            //Arrange
            var resourceText = "DROP TABLE";
            var fileName     = "QuartzMySql.sql";

            //Act
            var resourceResult = EmbeddedResourceHelper.GetTextResource(fileName);

            //Assert
            Assert.NotNull(resourceResult);
            Assert.Contains(resourceText, resourceText);
        }
Esempio n. 2
0
        private void CreateDatabase(string connectionString)
        {
            try
            {
                //Create database
                var connectionOnly = connectionString.GetConnectionOnlySqlServer();
                var database       = connectionString.GetDatabaseNameSqlServer();
                using (var connection = new SqlConnection(connectionOnly))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
                    command.CommandText = $"CREATE DATABASE {database}";
                    command.ExecuteNonQuery();
                }

                _logger.LogDebug($"Database {database} Created.");

                //Create tables for database
                using (var connection = new SqlConnection(connectionOnly))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
                    command.CommandType = CommandType.Text;

                    _logger.LogDebug("Using QuartzSqlServer.sql to create database tables.");

                    command.CommandText = EmbeddedResourceHelper.GetTextResource("QuartzSqlServer.sql").Replace("my_database_name", database);

                    if (string.IsNullOrEmpty(command.CommandText))
                    {
                        _logger.LogError($"Embedded resource QuartzSqlServer.sql is missing.");
                        throw new Exception("Reading embedded resource QuartzSqlServer.sql resulted in null value.");
                    }

                    command.ExecuteNonQuery();
                }
                _logger.LogDebug("Quartz database tables created.");
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to create database.");
            }
        }
        /// <summary>
        /// Create database file for sqlite.
        /// </summary>
        /// <param name="connectionString"></param>
        private void CreateDatabase(string connectionString)
        {
            try
            {
                if (!Directory.Exists(connectionString))
                {
                    Directory.CreateDirectory(connectionString.Substring(0, GetPathEnd(connectionString)));
                }

                //Create DB file
                File.WriteAllBytes(connectionString.GetOsDependentString(), new byte[0]);

                _logger.LogDebug($"Created database file: {connectionString}");

                using (var connection = new SqliteConnection(connectionString.GetSqliteConnectionString()))
                {
                    connection.Open();
                    _logger.LogDebug($"Building database using: {connectionString.GetSqliteConnectionString()}");
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandType = System.Data.CommandType.Text;
                        command.CommandText = EmbeddedResourceHelper.GetTextResource("QurtzSqlite.sql");

                        if (string.IsNullOrEmpty(command.CommandText))
                        {
                            _logger.LogError($"Embedded resource QurtzSqlite.sql is missing.");
                            throw new Exception("Reading embedded resource QurtzSqlite.sql resulted in null value.");
                        }

                        command.ExecuteNonQuery();
                    }

                    connection.Close();
                    _logger.LogDebug("Quartz database tables created.");
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to create database.");
            }
        }