mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-03-12 12:01:40 -04:00
Switch to SQL Server
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using ChrisKaczor.HomeMonitor.Weather.Models;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Npgsql;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
||||
{
|
||||
@@ -16,22 +16,22 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
||||
|
||||
public void EnsureDatabase()
|
||||
{
|
||||
var connectionStringBuilder = new NpgsqlConnectionStringBuilder
|
||||
var connectionStringBuilder = new SqlConnectionStringBuilder
|
||||
{
|
||||
Host = _configuration["Weather:Database:Host"],
|
||||
Username = _configuration["Weather:Database:User"],
|
||||
DataSource = _configuration["Weather:Database:Host"],
|
||||
UserID = _configuration["Weather:Database:User"],
|
||||
Password = _configuration["Weather:Database:Password"],
|
||||
Database = "postgres"
|
||||
InitialCatalog = "master"
|
||||
};
|
||||
|
||||
using (var connection = new NpgsqlConnection(connectionStringBuilder.ConnectionString))
|
||||
using (var connection = new SqlConnection(connectionStringBuilder.ConnectionString))
|
||||
{
|
||||
var command = new NpgsqlCommand { Connection = connection };
|
||||
var command = new SqlCommand { Connection = connection };
|
||||
|
||||
connection.Open();
|
||||
|
||||
// Check to see if the database exists
|
||||
command.CommandText = $"SELECT TRUE from pg_database WHERE datname='{_configuration["Weather:Database:Name"]}'";
|
||||
command.CommandText = $"SELECT CAST(1 as bit) from sys.databases WHERE name='{_configuration["Weather:Database:Name"]}'";
|
||||
var databaseExists = (bool?)command.ExecuteScalar();
|
||||
|
||||
// Create database if needed
|
||||
@@ -52,17 +52,17 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
||||
}
|
||||
}
|
||||
|
||||
private NpgsqlConnection CreateConnection()
|
||||
private SqlConnection CreateConnection()
|
||||
{
|
||||
var connectionStringBuilder = new NpgsqlConnectionStringBuilder
|
||||
var connectionStringBuilder = new SqlConnectionStringBuilder
|
||||
{
|
||||
Host = _configuration["Weather:Database:Host"],
|
||||
Username = _configuration["Weather:Database:User"],
|
||||
DataSource = _configuration["Weather:Database:Host"],
|
||||
UserID = _configuration["Weather:Database:User"],
|
||||
Password = _configuration["Weather:Database:Password"],
|
||||
Database = _configuration["Weather:Database:Name"]
|
||||
InitialCatalog = _configuration["Weather:Database:Name"]
|
||||
};
|
||||
|
||||
var connection = new NpgsqlConnection(connectionStringBuilder.ConnectionString);
|
||||
var connection = new SqlConnection(connectionStringBuilder.ConnectionString);
|
||||
connection.Open();
|
||||
|
||||
return connection;
|
||||
@@ -74,7 +74,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
||||
{
|
||||
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.CreateReading.sql");
|
||||
|
||||
connection.Execute(query, weatherMessage);
|
||||
connection.Query(query, weatherMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
INSERT INTO weather_reading (timestamp, wind_direction, wind_speed, humidity, humidity_temperature, rain, pressure,
|
||||
pressure_temperature, battery_level, light_level, latitude, longitude, altitude,
|
||||
satellite_count, gps_timestamp)
|
||||
VALUES (:timestamp, :windDirection, :windSpeed, :humidity, :humidityTemperature, :rain, :pressure, :pressureTemperature,
|
||||
:batteryLevel, :lightLevel, :latitude, :longitude, :altitude, :satelliteCount, :gpsTimestamp)
|
||||
ON CONFLICT DO NOTHING
|
||||
INSERT INTO Reading (Timestamp, WindDirection, WindSpeed, Humidity, HumidityTemperature, Rain, Pressure,
|
||||
PressureTemperature, BatteryLevel, LightLevel, Latitude, Longitude, Altitude,
|
||||
SatelliteCount, GpsTimestamp)
|
||||
VALUES (@Timestamp, @WindDirection, @WindSpeed, @Humidity, @HumidityTemperature, @Rain, @Pressure, @PressureTemperature,
|
||||
@BatteryLevel, @LightLevel, @Latitude, @Longitude, @Altitude, @SatelliteCount, @GpsTimestamp)
|
||||
@@ -1,24 +1,21 @@
|
||||
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS weather_reading
|
||||
(
|
||||
timestamp timestamptz NOT NULL
|
||||
CONSTRAINT weather_reading_pk
|
||||
PRIMARY KEY,
|
||||
wind_direction int NOT NULL,
|
||||
wind_speed double precision NOT NULL,
|
||||
humidity double precision NOT NULL,
|
||||
humidity_temperature double precision NOT NULL,
|
||||
rain double precision NOT NULL,
|
||||
pressure double precision NOT NULL,
|
||||
pressure_temperature double precision NOT NULL,
|
||||
battery_level double precision NOT NULL,
|
||||
light_level double precision NOT NULL,
|
||||
latitude double precision NOT NULL,
|
||||
longitude double precision NOT NULL,
|
||||
altitude double precision NOT NULL,
|
||||
satellite_count double precision NOT NULL,
|
||||
gps_timestamp timestamptz NOT NULL
|
||||
);
|
||||
|
||||
SELECT create_hypertable('weather_reading', 'timestamp', if_not_exists => TRUE);
|
||||
IF NOT EXISTS(SELECT 1 FROM sys.tables WHERE name = 'Reading')
|
||||
CREATE TABLE Reading
|
||||
(
|
||||
Timestamp datetimeoffset NOT NULL
|
||||
CONSTRAINT reading_pk
|
||||
PRIMARY KEY,
|
||||
WindDirection int NOT NULL,
|
||||
WindSpeed decimal(4, 1) NOT NULL,
|
||||
Humidity decimal(4, 1) NOT NULL,
|
||||
HumidityTemperature decimal(4, 1) NOT NULL,
|
||||
Rain decimal(2, 2) NOT NULL,
|
||||
Pressure decimal(8, 2) NOT NULL,
|
||||
PressureTemperature decimal(4, 1) NOT NULL,
|
||||
BatteryLevel decimal(3, 2) NOT NULL,
|
||||
LightLevel decimal(3, 2) NOT NULL,
|
||||
Latitude decimal(9, 6) NOT NULL,
|
||||
Longitude decimal(9, 6) NOT NULL,
|
||||
Altitude decimal(5, 1) NOT NULL,
|
||||
SatelliteCount int NOT NULL,
|
||||
GpsTimestamp datetimeoffset NOT NULL
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user