-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CRUD ORM implementation for directly quering database, without interacting with caching mechanics.
- Loading branch information
Showing
12 changed files
with
538 additions
and
17 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Root Type="DevExpress.CodeRush.Foundation.CodePlaces.Options.FavoritesListContainer"> | ||
<Options Language="Neutral"> | ||
<Groups /> | ||
</Options> | ||
</Root> |
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,4 +1,5 @@ | ||
/.vs/ | ||
/AdoCacheEngine/bin | ||
/AdoCacheEngine/obj | ||
/packages/ | ||
/packages/ | ||
/TestConsoleApp/ |
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
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,44 @@ | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
namespace AdoCache.ORM | ||
{ | ||
public class Database | ||
{ | ||
private SqlConnection _sqlConn; | ||
|
||
public Database(string connectionString) => ConnectionString = connectionString; | ||
|
||
#region API | ||
|
||
public Delete<TEntity> Delete<TEntity>() where TEntity : AdoCacheEntity, new() => new Delete<TEntity>(_sqlConn); | ||
|
||
public Insert<TEntity> Insert<TEntity>() where TEntity : AdoCacheEntity, new() => new Insert<TEntity>(_sqlConn); | ||
|
||
public Select<TEntity> Select<TEntity>() where TEntity : AdoCacheEntity, new() => new Select<TEntity>(_sqlConn); | ||
|
||
public Update<TEntity> Update<TEntity>() where TEntity : AdoCacheEntity, new() => new Update<TEntity>(_sqlConn); | ||
|
||
#endregion API | ||
|
||
#region Open/Close connection | ||
|
||
public string ConnectionString { get; } | ||
|
||
public void Close() | ||
{ | ||
if (_sqlConn != null && (_sqlConn.State != ConnectionState.Closed || _sqlConn.State != ConnectionState.Broken)) | ||
{ | ||
_sqlConn.Close(); | ||
} | ||
} | ||
|
||
public void Open() | ||
{ | ||
_sqlConn = new SqlConnection(ConnectionString); | ||
_sqlConn.Open(); | ||
} | ||
|
||
#endregion Open/Close connection | ||
} | ||
} |
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,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.SqlClient; | ||
using System.Linq.Expressions; | ||
using AdoCache.Attributes; | ||
using AdoCache.ryanohs; | ||
|
||
namespace AdoCache.ORM | ||
{ | ||
public class Delete<TEntity> where TEntity : AdoCacheEntity, new() | ||
{ | ||
private readonly SqlConnection _sqlConn; | ||
|
||
private Expression<Func<TEntity, bool>> _clause; | ||
|
||
internal Delete(SqlConnection sqlConn) | ||
{ | ||
_sqlConn = sqlConn; | ||
|
||
string tableName = (typeof(TEntity).GetCustomAttributes(typeof(TableNameAttribute), false)[0] as TableNameAttribute)?.TableName; | ||
if (string.IsNullOrWhiteSpace(tableName)) | ||
{ | ||
throw new ArgumentException("Could not deduce table name."); | ||
} | ||
|
||
TableName = tableName; | ||
} | ||
|
||
/// <summary> | ||
/// Name of Table in data base. | ||
/// </summary> | ||
public string TableName { get; } | ||
|
||
#region API | ||
|
||
public int All() => Execute(); | ||
|
||
public int Execute() | ||
{ | ||
SqlCommand command = new SqlCommand("", _sqlConn); | ||
|
||
if (_clause == null) | ||
{ | ||
command.CommandText = $"DELETE FROM {TableName}"; | ||
} | ||
else | ||
{ | ||
WherePart sql = new WhereBuilder().ToSql(_clause); | ||
string whereClause = sql.Sql; | ||
|
||
foreach (KeyValuePair<string, object> pair in sql.Parameters) | ||
{ | ||
if (pair.Value == null) | ||
{ | ||
whereClause = whereClause.Replace($"@{pair.Key}", "NULL"); | ||
} | ||
else | ||
{ | ||
command.Parameters.AddWithValue($"@{pair.Key}", pair.Value); | ||
} | ||
} | ||
|
||
command.CommandText = $"DELETE FROM {TableName} WHERE {whereClause}"; | ||
} | ||
|
||
return command.ExecuteNonQuery(); | ||
} | ||
|
||
public Delete<TEntity> Where(Expression<Func<TEntity, bool>> clause) | ||
{ | ||
_clause = clause; | ||
return this; | ||
} | ||
|
||
#endregion API | ||
} | ||
} |
Oops, something went wrong.