|
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Pfz.Databasing.Filtering;
using Pfz.Databasing.Internal;
using Pfz.Databasing.Managers;
namespace Pfz.Databasing
{
/// <summary>
/// Provides methods for creating and reading Records easily.
/// </summary>
public static class Record
{
#region CreateThreadConnection
/// <summary>
/// Creates a new default thread connection.
/// </summary>
public static ThreadConnection CreateThreadConnection()
{
return CreateThreadConnection("Default");
}
/// <summary>
/// Creates a new thread connection using the given connection name.
/// </summary>
public static ThreadConnection CreateThreadConnection(string name)
{
var manager = DatabaseManager.Get();
var connection = manager.CreateConnection(name);
return new ThreadConnection(connection, true);
}
#endregion
#region Create
/// <summary>
/// Creates a database record for insert of the given type.
/// </summary>
/// <typeparam name="T">The type of the record.</typeparam>
/// <returns>A new created record, for insert.</returns>
public static T Create<T>()
where
T: class, IRecord
{
return DatabaseManager.Get().Create<T>(ThreadConnection.Value);
}
#endregion
#region LoadByPrimaryKey
/// <summary>
/// Loads a record by it's primary key values.
/// </summary>
public static T LoadByPrimaryKey<T>(params object[] primaryKeyValues)
where
T: class, IRecord
{
T result = TryLoadByPrimaryKey<T>(primaryKeyValues);
if (result == null)
throw new DatabaseException("Record of type " + typeof(T).FullName + "not found by it's primary key values.");
return result;
}
#endregion
#region TryLoadByPrimaryKey
/// <summary>
/// Tries to load a record by it's primary key values.
/// </summary>
public static T TryLoadByPrimaryKey<T>(params object[] primaryKeyValues)
where
T: class, IRecord
{
return DatabaseManager.Get().TryLoadByPrimaryKey<T>(ThreadConnection.Get(), primaryKeyValues);
}
#endregion
#region FastLoadByPartialSql
/// <summary>
/// Returns a fast-enumerator to read all records from the given partial sql.
/// </summary>
public static IFastEnumerator<T> FastLoadByPartialSql<T>(string partialSql, params object[] parameterValues)
where
T: class, IRecord
{
return DatabaseManager.Get().FastLoadByPartialSql<T>(ThreadConnection.Get(), partialSql, parameterValues);
}
#endregion
#region LoadByPartialSql
/// <summary>
/// Returns a custom enumerable collection to read all records from the given
/// partial sql. Consider using FastLoadByPartialSql if possible.
/// </summary>
public static IEnumerable<T> LoadByPartialSql<T>(string partialSql, params object[] parameterValues)
where
T: class, IRecord
{
using(var fastEnumerator = FastLoadByPartialSql<T>(partialSql, parameterValues))
{
while(true)
{
T result = fastEnumerator.GetNext();
if (result == null)
yield break;
yield return result;
}
}
}
#endregion
#region FastLoadByLinqFilter
/// <summary>
/// Loads records using a linq filter.
/// Returns a FastEnumerator.
/// </summary>
public static IFastEnumerator<T> FastLoadByLinqFilter<T>(Expression<Func<T, bool>> filterExpression)
where
T: class, IRecord
{
var manager = DatabaseManager.Get();
FilterGroup filterGroup = null;
if (filterExpression != null)
{
var filterLinqWhere = new FilterLinqWhere(filterExpression.Body);
filterGroup = filterLinqWhere.fGroup;
}
return FastLoadByFilterGroup<T>(filterGroup);
}
#endregion
#region LoadByLinqFilter
/// <summary>
/// Loads records by a linq filter.
/// </summary>
public static IEnumerable<T> LoadByLinqFilter<T>(Expression<Func<T, bool>> filterExpression)
where
T: class, IRecord
{
using(var enumerator = FastLoadByLinqFilter<T>(filterExpression))
{
while(true)
{
T result = enumerator.GetNext();
if (result == null)
yield break;
yield return result;
}
}
}
#endregion
#region FastLoadByFilterGroup
/// <summary>
/// Gets a fast enumerator to read all records from the given filterGroup.
/// </summary>
public static IFastEnumerator<T> FastLoadByFilterGroup<T>(FilterGroup filterGroup)
where
T: class, IRecord
{
var manager = DatabaseManager.Get();
return manager.FastLoadByFilterGroup<T>(ThreadConnection.Get(), filterGroup);
}
#endregion
#region LoadByFilterGroup
/// <summary>
/// Gets an enumerator to read all the records from the given filter group.
/// </summary>
public static IEnumerable<T> LoadByFilterGroup<T>(FilterGroup filterGroup)
where
T: class, IRecord
{
using(var fastEnumerator = FastLoadByFilterGroup<T>(filterGroup))
{
T result = fastEnumerator.GetNext();
if (result == null)
yield break;
yield return result;
}
}
#endregion
#region GetQuery
/// <summary>
/// Gets a new query for the specific record type.
/// </summary>
/// <typeparam name="T">The type of the record to create a query for.</typeparam>
public static RecordQuery<T> GetQuery<T>()
where
T: class, IRecord
{
return new RecordQuery<T>();
}
#endregion
#region CreateFilterGroupFromLinqFilter
/// <summary>
/// Creates a filtergroup from a linq expression.
/// </summary>
public static FilterGroup CreateFilterGroupFromLinqFilter<T>(Expression<Func<T, bool>> filterExpression)
where
T: class, IRecord
{
if (filterExpression == null)
return null;
var filterLinqWhere = new FilterLinqWhere(filterExpression.Body);
return filterLinqWhere.fGroup;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.
At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.
Want more info or simply want to contact me?
Take a look at:
http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com
Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).