Skip to content

Commit

Permalink
Added support for generating SQLite SQL for nullable columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
NimaAra committed Jan 13, 2019
1 parent ea5c528 commit eff10cf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
10 changes: 10 additions & 0 deletions Easy.Storage.Common/Attributes/NullableAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Easy.Storage.Common.Attributes
{
using System;

/// <summary>
/// Used to mark a given property to be allowed to have its value stored as null.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class NullableAttribute : Attribute { }
}
32 changes: 20 additions & 12 deletions Easy.Storage.SQLite/SqliteSqlGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// ReSharper disable InconsistentNaming
namespace Easy.Storage.SQLite
namespace Easy.Storage.SQLite
{
using System;
using System.Collections.Generic;
Expand All @@ -17,9 +16,6 @@ namespace Easy.Storage.SQLite
/// </summary>
public static class SQLiteSQLGenerator
{
private const string PrimaryKey = " PRIMARY KEY";
private const string NotNull = " NOT NULL";

/// <summary>
/// Returns a <c>CREATE TABLE</c> script for the given <typeparamref name="T"/>.
/// </summary>
Expand Down Expand Up @@ -112,15 +108,27 @@ public static string FTSTable<T>(FTSTableType type, params Expression<Func<T, ob
private static string TableImpl(Table table, string tableName)
{
var builder = StringBuilderCache.Acquire();
builder.AppendLine($"CREATE TABLE IF NOT EXISTS {tableName} (");
builder.AppendLine($"{Formatter.Spacer}[_Entry_TimeStamp_Epoch_ms_] INTEGER DEFAULT (CAST((julianday('now') - 2440587.5)*86400000 AS INTEGER)),");

builder.Append("CREATE TABLE IF NOT EXISTS ")
.Append(tableName)
.AppendLine(" (")
.Append(Formatter.Spacer)
.AppendLine("[_Entry_TimeStamp_Epoch_ms_] INTEGER DEFAULT (CAST((julianday('now') - 2440587.5)*86400000 AS INTEGER)),");

foreach (var pair in table.PropertyToColumns)
{
var sqliteType = GetSQLiteType(pair.Key.PropertyType).ToString();
var isIdColumn = table.IdentityColumn == pair.Key;

builder.AppendLine($"{Formatter.Spacer}{pair.Value} {sqliteType}{(isIdColumn? PrimaryKey : "")}{NotNull},");
var prop = pair.Key;

var sqliteType = GetSQLiteType(prop.PropertyType).ToString();
var isIdColumn = table.IdentityColumn == prop;
var nullable = prop.CustomAttributes.Any(a => a.AttributeType == typeof(NullableAttribute));

builder.Append(Formatter.Spacer)
.Append(pair.Value)
.Append(" ")
.Append(sqliteType)
.Append(isIdColumn ? " PRIMARY KEY" : string.Empty)
.Append(!nullable ? " NOT NULL" : string.Empty)
.AppendLine(",");
}

builder.Remove(builder.Length - 3, 1);
Expand Down
6 changes: 4 additions & 2 deletions Easy.Storage.Tests.Unit/Sqlite/SqliteSqlGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void When_generating_table_sql()
+ " [_Entry_TimeStamp_Epoch_ms_] INTEGER DEFAULT (CAST((julianday('now') - 2440587.5)*86400000 AS INTEGER)),\r\n"
+ " [Id] INTEGER PRIMARY KEY NOT NULL,\r\n"
+ " [Text] TEXT NOT NULL,\r\n"
+ " [Int] INTEGER NOT NULL,\r\n"
+ " [Int] INTEGER,\r\n"
+ " [Decimal] REAL NOT NULL,\r\n"
+ " [Double] REAL NOT NULL,\r\n"
+ " [Float] REAL NOT NULL,\r\n"
Expand All @@ -45,7 +45,7 @@ public void When_generating_table_sql_for_model_with_overridden_name()
+ " [_Entry_TimeStamp_Epoch_ms_] INTEGER DEFAULT (CAST((julianday('now') - 2440587.5)*86400000 AS INTEGER)),\r\n"
+ " [Id] INTEGER PRIMARY KEY NOT NULL,\r\n"
+ " [Text] TEXT NOT NULL,\r\n"
+ " [Int] INTEGER NOT NULL,\r\n"
+ " [Int] INTEGER,\r\n"
+ " [Decimal] REAL NOT NULL,\r\n"
+ " [Double] REAL NOT NULL,\r\n"
+ " [Float] REAL NOT NULL,\r\n"
Expand Down Expand Up @@ -130,6 +130,8 @@ private sealed class SomeModel
{
public long Id { get; set; }
public string Text { get; set; }

[Nullable]
public int Int { get; set; }
public decimal Decimal { get; set; }
public double Double { get; set; }
Expand Down

0 comments on commit eff10cf

Please sign in to comment.