mirror of
https://github.com/ckaczor/WeatherService.git
synced 2026-01-26 02:05:37 -05:00
Initial commit
This commit is contained in:
99
Data/Database.cs
Normal file
99
Data/Database.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using Common.Debug;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Linq;
|
||||
using System.Data.SqlServerCe;
|
||||
using System.Linq;
|
||||
using WeatherService.Values;
|
||||
|
||||
namespace WeatherService.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// This class manages database connectivity and logging
|
||||
/// </summary>
|
||||
internal static class Database
|
||||
{
|
||||
private static DatabaseContext _databaseContext;
|
||||
private static SqlCeConnection _connection;
|
||||
|
||||
public static void Connect(string databasePath)
|
||||
{
|
||||
_connection = new SqlCeConnection(string.Format("Data Source={0}", databasePath));
|
||||
_connection.Open();
|
||||
}
|
||||
|
||||
public static SqlCeConnection Connection
|
||||
{
|
||||
get
|
||||
{
|
||||
return _connection;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Initialize(string databasePath)
|
||||
{
|
||||
Connect(databasePath);
|
||||
|
||||
// Create a database context
|
||||
_databaseContext = new DatabaseContext(databasePath);
|
||||
|
||||
// Turn on logging if requested
|
||||
if (Settings.Default.LogDatabase)
|
||||
_databaseContext.Log = Tracer.Writer;
|
||||
|
||||
// Create the database engine
|
||||
SqlCeEngine engine = new SqlCeEngine(string.Format("Data Source={0}", databasePath));
|
||||
|
||||
// Check to see if the database exists
|
||||
if (!_databaseContext.DatabaseExists())
|
||||
{
|
||||
// Create the database itself
|
||||
engine.CreateDatabase();
|
||||
|
||||
// Run the creation script
|
||||
// executeScript(Resources.CreateDatabase);
|
||||
}
|
||||
|
||||
// Compact the database
|
||||
engine.Shrink();
|
||||
}
|
||||
|
||||
public static Table<DeviceData> DeviceTable
|
||||
{
|
||||
get { return _databaseContext.DeviceTable; }
|
||||
}
|
||||
|
||||
public static Table<ReadingData> ReadingTable
|
||||
{
|
||||
get { return _databaseContext.ReadingTable; }
|
||||
}
|
||||
|
||||
public static IEnumerable<DeviceData> DeviceList
|
||||
{
|
||||
get { return from device in DeviceTable select device; }
|
||||
}
|
||||
|
||||
public static IEnumerable<ReadingData> ReadingList(int deviceId)
|
||||
{
|
||||
return from reading in ReadingTable where reading.DeviceId == deviceId select reading;
|
||||
}
|
||||
|
||||
public static IEnumerable<ReadingData> ReadingList(int deviceId, WeatherValueType valueType)
|
||||
{
|
||||
return from reading in ReadingTable where reading.DeviceId == deviceId && reading.Type == valueType select reading;
|
||||
}
|
||||
|
||||
public static void SaveChanges()
|
||||
{
|
||||
_databaseContext.SubmitChanges(ConflictMode.ContinueOnConflict);
|
||||
}
|
||||
|
||||
public static void DiscardChanges()
|
||||
{
|
||||
ChangeSet changeSet = _databaseContext.GetChangeSet();
|
||||
|
||||
_databaseContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Inserts);
|
||||
_databaseContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Deletes);
|
||||
_databaseContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Updates);
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Data/DatabaseContext.cs
Normal file
12
Data/DatabaseContext.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Data.Linq;
|
||||
|
||||
namespace WeatherService.Data
|
||||
{
|
||||
public class DatabaseContext : DataContext
|
||||
{
|
||||
public Table<DeviceData> DeviceTable;
|
||||
public Table<ReadingData> ReadingTable;
|
||||
|
||||
public DatabaseContext(string databaseFile) : base(databaseFile) { }
|
||||
}
|
||||
}
|
||||
28
Data/DeviceData.cs
Normal file
28
Data/DeviceData.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System.Data.Linq.Mapping;
|
||||
|
||||
namespace WeatherService.Data
|
||||
{
|
||||
[Table(Name = "Device")]
|
||||
public class DeviceData
|
||||
{
|
||||
[Column(UpdateCheck = UpdateCheck.Never, IsDbGenerated = true, IsPrimaryKey = true)]
|
||||
public int Id;
|
||||
|
||||
[Column(UpdateCheck = UpdateCheck.Never)]
|
||||
public string Address;
|
||||
|
||||
[Column(UpdateCheck = UpdateCheck.Never)]
|
||||
public string Name;
|
||||
|
||||
[Column(UpdateCheck = UpdateCheck.Never)]
|
||||
public int ReadInterval;
|
||||
|
||||
//private readonly EntitySet<Reading> _readingSet = new EntitySet<Reading>();
|
||||
//[Association(Storage = "_readingSet", OtherKey = "DeviceId", ThisKey = "Id")]
|
||||
//public EntitySet<Reading> Readings
|
||||
//{
|
||||
// get { return _readingSet; }
|
||||
// set { _readingSet.Assign(value); }
|
||||
//}
|
||||
}
|
||||
}
|
||||
60
Data/ReadingData.cs
Normal file
60
Data/ReadingData.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Data.Linq.Mapping;
|
||||
using System.Data.SqlTypes;
|
||||
using WeatherService.Values;
|
||||
|
||||
namespace WeatherService.Data
|
||||
{
|
||||
[Table(Name = "Reading")]
|
||||
public class ReadingData
|
||||
{
|
||||
public ReadingData()
|
||||
{
|
||||
ReadTime = SqlDateTime.MinValue.Value;
|
||||
}
|
||||
|
||||
[Column(UpdateCheck = UpdateCheck.Never, IsDbGenerated = true, IsPrimaryKey = true)]
|
||||
public int Id
|
||||
{
|
||||
get;
|
||||
internal set;
|
||||
}
|
||||
|
||||
[Column(UpdateCheck = UpdateCheck.Never)]
|
||||
public int DeviceId
|
||||
{
|
||||
get;
|
||||
internal set;
|
||||
}
|
||||
|
||||
[Column(UpdateCheck = UpdateCheck.Never)]
|
||||
public WeatherValueType Type
|
||||
{
|
||||
get;
|
||||
internal set;
|
||||
}
|
||||
|
||||
[Column(UpdateCheck = UpdateCheck.Never)]
|
||||
public double Value
|
||||
{
|
||||
get;
|
||||
internal set;
|
||||
}
|
||||
|
||||
[Column(UpdateCheck = UpdateCheck.Never)]
|
||||
public DateTime ReadTime
|
||||
{
|
||||
get;
|
||||
internal set;
|
||||
}
|
||||
|
||||
//private EntityRef<DeviceData> _device;
|
||||
//[Association(Storage = "_device", ThisKey = "DeviceId", OtherKey = "Id")]
|
||||
//public DeviceData Device
|
||||
//{
|
||||
// get { return _device.Entity; }
|
||||
// set { _device.Entity = value; }
|
||||
//}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user