-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major internal cleanup and refactoring; Better support for tables with identity/primary keys other than long and int; More tests.
- Loading branch information
Nima Ara
committed
Mar 15, 2017
1 parent
68563b1
commit d439406
Showing
33 changed files
with
963 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,80 @@ | ||
namespace Easy.Storage.Common | ||
{ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
/// <summary> | ||
/// Represents the kind of the <c>SQL</c> database. | ||
/// An abstraction for specifying the set of commands and languages | ||
/// used by the back-end storage provider which is then utilized by this library. | ||
/// </summary> | ||
[SuppressMessage("ReSharper", "InconsistentNaming")] | ||
public enum Dialect | ||
public abstract class Dialect | ||
{ | ||
/// <summary> | ||
/// Generic <c>SQL</c> dialect. | ||
/// </summary> | ||
Generic = 0, | ||
protected Dialect(DialectType type) | ||
{ | ||
Type = type; | ||
} | ||
|
||
/// <summary> | ||
/// <c>SQLite</c> specific dialect. | ||
/// Gets the type of the dialect. | ||
/// </summary> | ||
SQLite, | ||
public DialectType Type { get; } | ||
|
||
/// <summary> | ||
/// <c>SQLServer</c> specific dialect. | ||
/// </summary> | ||
SQLServer | ||
internal virtual string GetSelectQuery(Table table) | ||
{ | ||
var propNames = table.PropertyToColumns.Keys.Select(p => p.Name).ToArray(); | ||
var colNames = table.PropertyToColumns.Values.ToArray(); | ||
var colsAsPropNameAlias = string.Join(Formatter.ColumnSeparator, colNames.Zip(propNames, (col, prop) => $"{table.Name}.{col} AS '{prop}'")); | ||
return $"SELECT{Formatter.NewLine}{Formatter.Spacer}{colsAsPropNameAlias}{Formatter.NewLine}FROM {table.Name}{Formatter.NewLine}WHERE{Formatter.NewLine}{Formatter.Spacer}1 = 1;"; | ||
} | ||
|
||
internal virtual string GetDeleteQuery(Table table) => $"DELETE FROM {table.Name}{Formatter.NewLine}WHERE{Formatter.NewLine}{Formatter.Spacer}1 = 1;"; | ||
|
||
internal virtual string GetInsertQuery(Table table, bool includeIdentity) | ||
{ | ||
var columnsAndProps = GetColumnsAndProperties(table, includeIdentity); | ||
var insertSeg = $"INSERT INTO {table.Name}{Formatter.NewLine}({Formatter.NewLine}{Formatter.Spacer}{columnsAndProps.Key}{Formatter.NewLine})"; | ||
var valuesSeg = $"{Formatter.NewLine}VALUES{Formatter.NewLine}({Formatter.NewLine}{Formatter.Spacer}{columnsAndProps.Value}{Formatter.NewLine});"; | ||
return insertSeg + valuesSeg; | ||
} | ||
|
||
internal virtual string GetUpdateQuery(Table table, bool includeIdentity) | ||
{ | ||
if (includeIdentity) | ||
{ | ||
var propNames = table.PropertyToColumns.Keys.Select(p => p.Name).ToArray(); | ||
var colNames = table.PropertyToColumns.Values.ToArray(); | ||
var allColsEqualProp = string.Join(Formatter.ColumnSeparator, colNames.Zip(propNames, (col, propName) => $"{col} = @{propName}")); | ||
return $"UPDATE {table.Name} SET{Formatter.NewLine}{Formatter.Spacer}{allColsEqualProp}{Formatter.NewLine}WHERE{Formatter.NewLine}{Formatter.Spacer}1 = 1;"; | ||
} | ||
|
||
var propToColsMinusIdentity = table.PropertyToColumns.Where(p => p.Key != table.IdentityColumn).ToArray(); | ||
var colNamesMinusIdentity = propToColsMinusIdentity.Select(kv => kv.Value).ToArray(); | ||
var propNamesMinusIdentity = propToColsMinusIdentity.Select(kv => kv.Key.Name).ToArray(); | ||
var colEqualPropMinusIdentity = string.Join(Formatter.ColumnSeparator, colNamesMinusIdentity.Zip(propNamesMinusIdentity, (col, propName) => $"{col} = @{propName}")); | ||
return $"UPDATE {table.Name} SET{Formatter.NewLine}{Formatter.Spacer}{colEqualPropMinusIdentity}{Formatter.NewLine}WHERE{Formatter.NewLine}{Formatter.Spacer}{table.PropertyToColumns[table.IdentityColumn]} = @{table.IdentityColumn.Name};"; | ||
} | ||
|
||
protected KeyValuePair<string, string> GetColumnsAndProperties(Table table, bool includeIdentity) | ||
{ | ||
var allColNames = table.PropertyToColumns.Select(kv => kv.Value).ToArray(); | ||
var allPropNames = table.PropertyToColumns.Select(kv => kv.Key.Name).ToArray(); | ||
|
||
string columns, properties; | ||
if (includeIdentity) | ||
{ | ||
columns = string.Join(Formatter.ColumnSeparator, allColNames); | ||
properties = string.Join(Formatter.ColumnSeparator, allPropNames.Select(x => "@" + x)); | ||
} else | ||
{ | ||
var propToColsMinusIdentity = table.PropertyToColumns.Where(p => p.Key != table.IdentityColumn).ToArray(); | ||
var colNamesMinusIdentity = propToColsMinusIdentity.Select(kv => kv.Value).ToArray(); | ||
var propNamesMinusIdentity = propToColsMinusIdentity.Select(kv => kv.Key.Name).ToArray(); | ||
|
||
columns = string.Join(Formatter.ColumnSeparator, colNamesMinusIdentity); | ||
properties = string.Join(Formatter.ColumnSeparator, propNamesMinusIdentity.Select(x => "@" + x)); | ||
} | ||
|
||
return new KeyValuePair<string, string>(columns, properties); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Easy.Storage.Common | ||
{ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
/// <summary> | ||
/// Represents the type of the dialect used by the storage. | ||
/// </summary> | ||
[SuppressMessage("ReSharper", "InconsistentNaming")] | ||
public enum DialectType | ||
{ | ||
/// <summary> | ||
/// Generic <c>SQL</c>. | ||
/// </summary> | ||
Generic = 0, | ||
|
||
/// <summary> | ||
/// <c>SQLite</c>. | ||
/// </summary> | ||
SQLite, | ||
|
||
/// <summary> | ||
/// <c>SQLServer</c>. | ||
/// </summary> | ||
SQLServer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Easy.Storage.Common | ||
{ | ||
/// <summary> | ||
/// Represents a generic <c>SQL</c> dialect. | ||
/// </summary> | ||
public sealed class GenericSQLDialect : Dialect | ||
{ | ||
static GenericSQLDialect() { } | ||
private GenericSQLDialect() : base(DialectType.Generic) { } | ||
|
||
/// <summary> | ||
/// Gets a single instance of the <see cref="GenericSQLDialect"/>. | ||
/// </summary> | ||
public static GenericSQLDialect Instance { get; } = new GenericSQLDialect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.