Skip to content

Commit

Permalink
Added ddl code
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjcowan committed Aug 26, 2024
1 parent bce6db6 commit ccc92eb
Show file tree
Hide file tree
Showing 54 changed files with 6,219 additions and 19 deletions.
6 changes: 0 additions & 6 deletions src/DapperMatic/Class1.cs

This file was deleted.

10 changes: 10 additions & 0 deletions src/DapperMatic/DataTypeMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace DapperMatic;

public class DataTypeMap
{
public Type DotnetType { get; set; } = null!;
public string SqlType { get; set; } = null!;
public string? SqlTypeWithLength { get; set; }
public string? SqlTypeWithMaxLength { get; set; }
public string? SqlTypeWithPrecisionAndScale { get; set; }
}
204 changes: 204 additions & 0 deletions src/DapperMatic/DataTypeMapFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
using System.Collections.Concurrent;

namespace DapperMatic;

public static class DataTypeMapFactory
{
private static ConcurrentDictionary<
DatabaseTypes,
List<DataTypeMap>
> _databaseTypeDataTypeMappings = new();

public static List<DataTypeMap> GetDefaultDatabaseTypeDataTypeMap(DatabaseTypes databaseType)
{
return _databaseTypeDataTypeMappings.GetOrAdd(
databaseType,
dbt =>
{
return dbt switch
{
DatabaseTypes.SqlServer => GetSqlServerDataTypeMap(),
DatabaseTypes.PostgreSql => GetPostgresqlDataTypeMap(),
DatabaseTypes.MySql => GetMySqlDataTypeMap(),
DatabaseTypes.Sqlite => GetSqliteDataTypeMap(),
_ => throw new NotSupportedException($"Database type {dbt} is not supported.")
};
}
);
}

private static List<DataTypeMap> GetSqliteDataTypeMap()
{
var types = new List<DataTypeMap>
{
new DataTypeMap { DotnetType = typeof(string), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(Guid), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(int), SqlType = "INTEGER" },
new DataTypeMap { DotnetType = typeof(long), SqlType = "INTEGER" },
new DataTypeMap { DotnetType = typeof(float), SqlType = "REAL" },
new DataTypeMap { DotnetType = typeof(double), SqlType = "REAL" },
new DataTypeMap { DotnetType = typeof(decimal), SqlType = "NUMERIC" },
new DataTypeMap { DotnetType = typeof(bool), SqlType = "INTEGER" },
new DataTypeMap { DotnetType = typeof(DateTime), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(DateTimeOffset), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(byte[]), SqlType = "BLOB" },
new DataTypeMap { DotnetType = typeof(Guid[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(int[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(long[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(double[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(decimal[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(string[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(Dictionary<string, string>), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(Dictionary<string, object>), SqlType = "TEXT" }
};

return UnionNullableValueTypes(types);
}

private static List<DataTypeMap> GetMySqlDataTypeMap()
{
// get the type map for MySql
var types = new List<DataTypeMap>
{
new DataTypeMap
{
DotnetType = typeof(string),
SqlType = "VARCHAR(255)",
SqlTypeWithLength = "VARCHAR({0})",
SqlTypeWithMaxLength = "TEXT"
},
new DataTypeMap { DotnetType = typeof(Guid), SqlType = "CHAR(36)" },
new DataTypeMap { DotnetType = typeof(int), SqlType = "INT" },
new DataTypeMap { DotnetType = typeof(long), SqlType = "BIGINT" },
new DataTypeMap { DotnetType = typeof(float), SqlType = "FLOAT" },
new DataTypeMap { DotnetType = typeof(double), SqlType = "DOUBLE" },
new DataTypeMap { DotnetType = typeof(decimal), SqlType = "DECIMAL" },
new DataTypeMap { DotnetType = typeof(bool), SqlType = "TINYINT" },
new DataTypeMap { DotnetType = typeof(DateTime), SqlType = "DATETIME" },
new DataTypeMap { DotnetType = typeof(DateTimeOffset), SqlType = "DATETIME" },
new DataTypeMap { DotnetType = typeof(byte[]), SqlType = "BLOB" },
new DataTypeMap { DotnetType = typeof(Guid[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(int[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(long[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(double[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(decimal[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(string[]), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(Dictionary<string, string>), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(Dictionary<string, object>), SqlType = "TEXT" }
};

return UnionNullableValueTypes(types);
}

private static List<DataTypeMap> GetPostgresqlDataTypeMap()
{
// get the type map for Postgresql
var types = new List<DataTypeMap>
{
new DataTypeMap
{
DotnetType = typeof(string),
SqlType = "CHARACTER VARYING",
SqlTypeWithLength = "CHARACTER VARYING({0})",
SqlTypeWithMaxLength = "TEXT"
},
new DataTypeMap { DotnetType = typeof(Guid), SqlType = "UUID" },
new DataTypeMap { DotnetType = typeof(int), SqlType = "INTEGER" },
new DataTypeMap { DotnetType = typeof(long), SqlType = "BIGINT" },
new DataTypeMap { DotnetType = typeof(float), SqlType = "REAL" },
new DataTypeMap { DotnetType = typeof(double), SqlType = "DOUBLE PRECISION" },
new DataTypeMap { DotnetType = typeof(decimal), SqlType = "DECIMAL" },
new DataTypeMap { DotnetType = typeof(bool), SqlType = "BOOLEAN" },
new DataTypeMap { DotnetType = typeof(DateTime), SqlType = "TIMESTAMP" },
new DataTypeMap
{
DotnetType = typeof(DateTimeOffset),
SqlType = "TIMESTAMP WITH TIME ZONE"
},
new DataTypeMap { DotnetType = typeof(byte[]), SqlType = "BYTEA" },
// new DataTypeMap { DotnetType = typeof(int[]), SqlType = "TEXT" },
// new DataTypeMap { DotnetType = typeof(long[]), SqlType = "TEXT" },
// new DataTypeMap { DotnetType = typeof(double[]), SqlType = "TEXT" },
// new DataTypeMap { DotnetType = typeof(decimal[]), SqlType = "TEXT" },
// new DataTypeMap { DotnetType = typeof(string[]), SqlType = "TEXT" },
// new DataTypeMap { DotnetType = typeof(Dictionary<string, string>), SqlType = "TEXT" },
// new DataTypeMap { DotnetType = typeof(Dictionary<string, object>), SqlType = "TEXT" },
new DataTypeMap { DotnetType = typeof(Guid[]), SqlType = "UUID[]" },
new DataTypeMap { DotnetType = typeof(int[]), SqlType = "INTEGER[]" },
new DataTypeMap { DotnetType = typeof(long[]), SqlType = "BIGINT[]" },
new DataTypeMap { DotnetType = typeof(double[]), SqlType = "DOUBLE PRECISION[]" },
new DataTypeMap { DotnetType = typeof(decimal[]), SqlType = "DECIMAL[]" },
new DataTypeMap { DotnetType = typeof(string[]), SqlType = "CHARACTER VARYING[]" },
new DataTypeMap { DotnetType = typeof(Dictionary<string, string>), SqlType = "JSONB" },
new DataTypeMap { DotnetType = typeof(Dictionary<string, object>), SqlType = "JSONB" }
};

return UnionNullableValueTypes(types);
}

private static List<DataTypeMap> GetSqlServerDataTypeMap()
{
// get the type map for SqlServer
var types = new List<DataTypeMap>
{
new DataTypeMap
{
DotnetType = typeof(string),
SqlType = "NVARCHAR",
SqlTypeWithLength = "NVARCHAR({0})",
SqlTypeWithMaxLength = "NVARCHAR(MAX)"
},
new DataTypeMap { DotnetType = typeof(Guid), SqlType = "UNIQUEIDENTIFIER" },
new DataTypeMap { DotnetType = typeof(int), SqlType = "INT" },
new DataTypeMap { DotnetType = typeof(long), SqlType = "BIGINT" },
new DataTypeMap { DotnetType = typeof(float), SqlType = "REAL" },
new DataTypeMap { DotnetType = typeof(double), SqlType = "FLOAT" },
new DataTypeMap { DotnetType = typeof(decimal), SqlType = "DECIMAL" },
new DataTypeMap { DotnetType = typeof(bool), SqlType = "BIT" },
new DataTypeMap { DotnetType = typeof(DateTime), SqlType = "DATETIME2" },
new DataTypeMap { DotnetType = typeof(DateTimeOffset), SqlType = "DATETIMEOFFSET" },
new DataTypeMap { DotnetType = typeof(byte[]), SqlType = "VARBINARY" },
new DataTypeMap { DotnetType = typeof(Guid[]), SqlType = "NVARCHAR(MAX)" },
new DataTypeMap { DotnetType = typeof(int[]), SqlType = "NVARCHAR(MAX)" },
new DataTypeMap { DotnetType = typeof(long[]), SqlType = "NVARCHAR(MAX)" },
new DataTypeMap { DotnetType = typeof(double[]), SqlType = "NVARCHAR(MAX)" },
new DataTypeMap { DotnetType = typeof(decimal[]), SqlType = "NVARCHAR(MAX)" },
new DataTypeMap { DotnetType = typeof(string[]), SqlType = "NVARCHAR(MAX)" },
new DataTypeMap
{
DotnetType = typeof(Dictionary<string, string>),
SqlType = "NVARCHAR(MAX)"
},
new DataTypeMap
{
DotnetType = typeof(Dictionary<string, object>),
SqlType = "NVARCHAR(MAX)"
}
};

return UnionNullableValueTypes(types);
}

private static List<DataTypeMap> UnionNullableValueTypes(List<DataTypeMap> types)
{
// add nullable version of all the value types
foreach (var type in types.ToArray())
{
if (type.DotnetType.IsValueType)
{
types.Add(
new DataTypeMap
{
DotnetType = typeof(Nullable<>).MakeGenericType(type.DotnetType),
SqlType = type.SqlType,
SqlTypeWithLength = type.SqlTypeWithLength,
SqlTypeWithMaxLength = type.SqlTypeWithMaxLength,
SqlTypeWithPrecisionAndScale = type.SqlTypeWithPrecisionAndScale
}
);
}
}

return types;
}
}
Loading

0 comments on commit ccc92eb

Please sign in to comment.