Skip to content

Commit

Permalink
Refactored some naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjcowan committed Nov 8, 2024
1 parent 11f6efd commit 8a6143a
Show file tree
Hide file tree
Showing 44 changed files with 2,583 additions and 2,015 deletions.
4 changes: 4 additions & 0 deletions src/DapperMatic/DataAnnotations/DxColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public DxColumnAttribute(
}

public string? ColumnName { get; }

/// <summary>
/// /// Format of provider data types: {mysql:varchar(255),sqlserver:nvarchar(255)}
/// </summary>
public string? ProviderDataType { get; }
public int? Length { get; }
public int? Precision { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace DapperMatic.DataAnnotations;

[AttributeUsage(
AttributeTargets.Property | AttributeTargets.Class,
AllowMultiple = true
)]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = true)]
public class DxPrimaryKeyConstraintAttribute : Attribute
{
public DxPrimaryKeyConstraintAttribute() { }
Expand All @@ -21,6 +18,11 @@ public DxPrimaryKeyConstraintAttribute(string constraintName, params string[] co
Columns = columnNames.Select(columnName => new DxOrderedColumn(columnName)).ToArray();
}

public DxPrimaryKeyConstraintAttribute(string[] columnNames)
{
Columns = columnNames.Select(columnName => new DxOrderedColumn(columnName)).ToArray();
}

public string? ConstraintName { get; }
public DxOrderedColumn[]? Columns { get; }
}
7 changes: 5 additions & 2 deletions src/DapperMatic/DbConnectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ public static async Task<Version> GetDatabaseVersionAsync(
return await Database(db).GetDatabaseVersionAsync(db, tx, cancellationToken);
}

public static IProviderTypeMap GetProviderTypeMap(this IDbConnection db)
public static IDbProviderTypeMap GetProviderTypeMap(this IDbConnection db)
{
return Database(db).ProviderTypeMap;
}

public static (Type dotnetType, int? length, int? precision, int? scale, bool? autoIncrementing, Type[] allSupportedTypes) GetDotnetTypeFromSqlType(this IDbConnection db, string sqlType)
public static DbProviderDotnetTypeDescriptor GetDotnetTypeFromSqlType(
this IDbConnection db,
string sqlType
)
{
return Database(db).GetDotnetTypeFromSqlType(sqlType);
}
Expand Down
74 changes: 74 additions & 0 deletions src/DapperMatic/DbProviderSqlType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace DapperMatic.Providers;

/// <summary>
/// The provider SQL type.
/// </summary>
/// <param name="SqlType"></param>
/// <param name="AliasForSqlType"></param>
/// <param name="SqlTypeWithLength"></param>
/// <param name="SqlTypeWithPrecision"></param>
/// <param name="SqlTypeWithPrecisionAndScale"></param>
/// <param name="SqlTypeWithMaxLength"></param>
/// <param name="CanAutoIncrement"></param>
/// <param name="NotNullable"></param>
/// <param name="DefaultLength"></param>
/// <param name="DefaultPrecision"></param>
/// <param name="DefaultScale"></param>
public class DbProviderSqlType(
DbProviderSqlTypeAffinity affinity,
string name,
Type? recommendedDotnetType = null,
string? aliasOf = null,
string? formatWithLength = null,
string? formatWithPrecision = null,
string? formatWithPrecisionAndScale = null,
int? defaultLength = null,
int? defaultPrecision = null,
int? defaultScale = null,
bool canUseToAutoIncrement = false,
bool autoIncrementsAutomatically = false,
double? minValue = null,
double? maxValue = null,
bool includesTimeZone = false,
bool isDateOnly = false,
bool isTimeOnly = false,
bool isYearOnly = false,
bool isFixedLength = false,
bool isGuidOnly = false,
bool isUnicode = false
)
{
public DbProviderSqlTypeAffinity Affinity { get; init; } = affinity;
public string Name { get; init; } = name;
public Type? RecommendedDotnetType { get; init; } = recommendedDotnetType;
public string? AliasOf { get; set; } = aliasOf;
public string? FormatWithLength { get; init; } = formatWithLength;
public string? FormatWithPrecision { get; init; } = formatWithPrecision;
public string? FormatWithPrecisionAndScale { get; init; } = formatWithPrecisionAndScale;
public int? DefaultLength { get; set; } = defaultLength;
public int? DefaultPrecision { get; set; } = defaultPrecision;
public int? DefaultScale { get; set; } = defaultScale;
public bool CanUseToAutoIncrement { get; init; } = canUseToAutoIncrement;
public bool AutoIncrementsAutomatically { get; init; } = autoIncrementsAutomatically;
public double? MinValue { get; init; } = minValue;
public double? MaxValue { get; init; } = maxValue;
public bool IncludesTimeZone { get; init; } = includesTimeZone;
public bool IsDateOnly { get; init; } = isDateOnly;
public bool IsTimeOnly { get; init; } = isTimeOnly;
public bool IsYearOnly { get; init; } = isYearOnly;
public bool IsFixedLength { get; init; } = isFixedLength;
public bool IsGuidOnly { get; init; } = isGuidOnly;
public bool IsUnicode { get; set; } = isUnicode;
}

public static class DbProviderSqlTypeExtensions
{
public static bool SupportsLength(this DbProviderSqlType providerSqlType) =>
!string.IsNullOrWhiteSpace(providerSqlType.FormatWithLength);

public static bool SupportsPrecision(this DbProviderSqlType providerSqlType) =>
!string.IsNullOrWhiteSpace(providerSqlType.FormatWithPrecision);

public static bool SupportsPrecisionAndScale(this DbProviderSqlType providerSqlType) =>
!string.IsNullOrWhiteSpace(providerSqlType.FormatWithPrecisionAndScale);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DapperMatic.Providers;
namespace DapperMatic;

public enum ProviderSqlTypeAffinity
public enum DbProviderSqlTypeAffinity
{
Integer,
Real,
Expand All @@ -11,4 +11,4 @@ public enum ProviderSqlTypeAffinity
Geometry,
RangeType,
Other
}
}
51 changes: 51 additions & 0 deletions src/DapperMatic/DbProviderType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Collections.Concurrent;
using System.Data;

namespace DapperMatic;

public enum DbProviderType
Expand All @@ -7,3 +10,51 @@ public enum DbProviderType
MySql,
PostgreSql
}

public static class DbProviderTypeExtensions
{
private static readonly ConcurrentDictionary<Type, DbProviderType> ProviderTypes = new();

public static DbProviderType GetDbProviderType(this IDbConnection db)
{
var type = db.GetType();
if (ProviderTypes.TryGetValue(type, out var dbType))
{
return dbType;
}

dbType = ToDbProviderType(type.FullName!);
ProviderTypes.TryAdd(type, dbType);

return dbType;
}

private static DbProviderType ToDbProviderType(string provider)
{
if (provider.Contains("sqlite", StringComparison.OrdinalIgnoreCase))
return DbProviderType.Sqlite;

if (
provider.Contains("mysql", StringComparison.OrdinalIgnoreCase)
|| provider.Contains("maria", StringComparison.OrdinalIgnoreCase)
)
return DbProviderType.MySql;

if (
provider.Contains("postgres", StringComparison.OrdinalIgnoreCase)
|| provider.Contains("npgsql", StringComparison.OrdinalIgnoreCase)
|| provider.Contains("pg", StringComparison.OrdinalIgnoreCase)
)
return DbProviderType.PostgreSql;

if (
provider.Contains("sqlserver", StringComparison.OrdinalIgnoreCase)
|| provider.Contains("mssql", StringComparison.OrdinalIgnoreCase)
|| provider.Contains("localdb", StringComparison.OrdinalIgnoreCase)
|| provider.Contains("sqlclient", StringComparison.OrdinalIgnoreCase)
)
return DbProviderType.SqlServer;

throw new NotSupportedException($"Db type {provider} is not supported.");
}
}
55 changes: 0 additions & 55 deletions src/DapperMatic/DbProviderTypeExtensions.cs

This file was deleted.

34 changes: 34 additions & 0 deletions src/DapperMatic/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ namespace DapperMatic;
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public static partial class ExtensionMethods
{
public static string GetFriendlyName(this Type type)
{
if (type == null)
return "(Unknown Type)";

if (!type.IsGenericType)
return type.Name;

var genericTypeName = type.GetGenericTypeDefinition().Name;
var friendlyGenericTypeName = genericTypeName[..genericTypeName.LastIndexOf("`")];

var genericArguments = type.GetGenericArguments();
var genericArgumentNames = genericArguments.Select(GetFriendlyName).ToArray();
var genericTypeArgumentsString = string.Join(", ", genericArgumentNames);

return $"{friendlyGenericTypeName}<{genericTypeArgumentsString}>";
}

public static TValue? GetFieldValue<TValue>(this object instance, string name)
{
var type = instance.GetType();
Expand Down Expand Up @@ -72,6 +90,22 @@ public static int[] ExtractNumbers(this string input)
return [.. numbers];
}

public static string DiscardLengthPrecisionAndScaleFromSqlTypeName(this string sqlTypeName)
{
// extract the type name from the sql type name where a sqlTypeName might be "time(5, 2) without time zone" and the return value would be "time without time zone",
// it could also be "time ( 122, 2 ) without time zone" and the return value would be "time without time zone
var openIndex = sqlTypeName.IndexOf('(');
var closeIndex = sqlTypeName.IndexOf(')');
var txt = (
openIndex > 0 && closeIndex > 0
? sqlTypeName.Remove(openIndex, closeIndex - openIndex + 1)
: sqlTypeName
).Trim();
while (txt.Contains(" "))
txt = txt.Replace(" ", " ");
return txt;
}

public static string ToQuotedIdentifier(
this string prefix,
char[] quoteChar,
Expand Down
7 changes: 3 additions & 4 deletions src/DapperMatic/Interfaces/IDatabaseMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IDatabaseMethods
IDatabaseViewMethods
{
DbProviderType ProviderType { get; }
IProviderTypeMap ProviderTypeMap { get; }
IDbProviderTypeMap ProviderTypeMap { get; }

bool SupportsSchemas { get; }

Expand All @@ -39,9 +39,8 @@ Task<Version> GetDatabaseVersionAsync(
CancellationToken cancellationToken = default
);

(Type dotnetType, int? length, int? precision, int? scale, bool? isAutoIncrementing, Type[] allSupportedTypes)
GetDotnetTypeFromSqlType(string sqlType);
string GetSqlTypeFromDotnetType(Type type, int? length, int? precision, int? scale, bool? autoIncrementing);
DbProviderDotnetTypeDescriptor GetDotnetTypeFromSqlType(string sqlType);
string GetSqlTypeFromDotnetType(DbProviderDotnetTypeDescriptor descriptor);

string NormalizeName(string name);
}
Loading

0 comments on commit 8a6143a

Please sign in to comment.