mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-23 14:55:20 -07:00
parent
4d5a5ed2c1
commit
685c5daf36
6 changed files with 96 additions and 30 deletions
|
@ -14,8 +14,10 @@ You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
|
using System.Runtime.InteropServices.ComTypes;
|
||||||
using Marr.Data.Converters;
|
using Marr.Data.Converters;
|
||||||
|
|
||||||
namespace Marr.Data.Parameters
|
namespace Marr.Data.Parameters
|
||||||
|
@ -42,6 +44,14 @@ namespace Marr.Data.Parameters
|
||||||
Type valueType = value.GetType();
|
Type valueType = value.GetType();
|
||||||
|
|
||||||
// Check for a registered IConverter
|
// Check for a registered IConverter
|
||||||
|
//If we have a list of ints, we ignore the converter since we want to do an in statement!
|
||||||
|
var list = value as List<int>;
|
||||||
|
if (list != null)
|
||||||
|
{
|
||||||
|
Parameter.Value = $"{string.Join(",", list)}";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
IConverter converter = MapRepository.Instance.GetConverter(valueType);
|
IConverter converter = MapRepository.Instance.GetConverter(valueType);
|
||||||
if (converter != null)
|
if (converter != null)
|
||||||
{
|
{
|
||||||
|
@ -51,6 +61,8 @@ namespace Marr.Data.Parameters
|
||||||
{
|
{
|
||||||
Parameter.Value = value;
|
Parameter.Value = value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//// Determine the correct DbType based on the passed in value type
|
//// Determine the correct DbType based on the passed in value type
|
||||||
//IDbTypeBuilder typeBuilder = MapRepository.Instance.DbTypeBuilder;
|
//IDbTypeBuilder typeBuilder = MapRepository.Instance.DbTypeBuilder;
|
||||||
|
|
|
@ -68,5 +68,13 @@ namespace Marr.Data.QGen.Dialects
|
||||||
{
|
{
|
||||||
get { return "({0} LIKE '%' + {1} + '%')"; }
|
get { return "({0} LIKE '%' + {1} + '%')"; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual string InFormat
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "({0} in ({1}))";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
|
@ -93,6 +94,10 @@ namespace Marr.Data.QGen
|
||||||
Write_EndsWith(expression);
|
Write_EndsWith(expression);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "In":
|
||||||
|
Write_In(expression);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
string msg = string.Format("'{0}' expressions are not yet implemented in the where clause expression tree parser.", method);
|
string msg = string.Format("'{0}' expressions are not yet implemented in the where clause expression tree parser.", method);
|
||||||
throw new NotImplementedException(msg);
|
throw new NotImplementedException(msg);
|
||||||
|
@ -140,31 +145,47 @@ namespace Marr.Data.QGen
|
||||||
return expression;
|
return expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
private object GetRightValue(Expression rightExpression)
|
private object GetRightValue(Expression expression)
|
||||||
{
|
{
|
||||||
object rightValue = null;
|
object rightValue = null;
|
||||||
|
|
||||||
var right = rightExpression as ConstantExpression;
|
var simpleConstExp = expression as ConstantExpression;
|
||||||
if (right == null) // Value is not directly passed in as a constant
|
if (simpleConstExp == null) // Value is not directly passed in as a constant
|
||||||
{
|
{
|
||||||
var rightMemberExp = (rightExpression as MemberExpression);
|
MemberExpression memberExp = expression as MemberExpression;
|
||||||
var parentMemberExpression = rightMemberExp.Expression as MemberExpression;
|
ConstantExpression constExp = null;
|
||||||
if (parentMemberExpression != null) // Value is passed in as a property on a parent entity
|
|
||||||
|
// Value may be nested in multiple levels of objects/properties, so traverse the MemberExpressions
|
||||||
|
// until a ConstantExpression property value is found, and then unwind the stack to get the value.
|
||||||
|
var memberNames = new Stack<string>();
|
||||||
|
|
||||||
|
while (memberExp != null)
|
||||||
{
|
{
|
||||||
string entityName = (rightMemberExp.Expression as MemberExpression).Member.Name;
|
memberNames.Push(memberExp.Member.Name);
|
||||||
var container = ((rightMemberExp.Expression as MemberExpression).Expression as ConstantExpression).Value;
|
|
||||||
var entity = _repos.ReflectionStrategy.GetFieldValue(container, entityName);
|
// Function calls are not supported - user needs to simplify their Where expression.
|
||||||
rightValue = _repos.ReflectionStrategy.GetFieldValue(entity, rightMemberExp.Member.Name);
|
var methodExp = memberExp.Expression as MethodCallExpression;
|
||||||
|
if (methodExp != null)
|
||||||
|
{
|
||||||
|
var errMsg = string.Format("Function calls are not supported by the Where clause expression parser. Please evaluate your function call, '{0}', manually and then use the resulting paremeter value in your Where expression.", methodExp.Method.Name);
|
||||||
|
throw new NotSupportedException(errMsg);
|
||||||
}
|
}
|
||||||
else // Value is passed in as a variable
|
|
||||||
{
|
constExp = memberExp.Expression as ConstantExpression;
|
||||||
var parent = (rightMemberExp.Expression as ConstantExpression).Value;
|
memberExp = memberExp.Expression as MemberExpression;
|
||||||
rightValue = _repos.ReflectionStrategy.GetFieldValue(parent, rightMemberExp.Member.Name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object entity = constExp.Value;
|
||||||
|
while (memberNames.Count > 0)
|
||||||
|
{
|
||||||
|
string entityName = memberNames.Pop();
|
||||||
|
entity = _repos.ReflectionStrategy.GetFieldValue(entity, entityName);
|
||||||
|
}
|
||||||
|
rightValue = entity;
|
||||||
}
|
}
|
||||||
else // Value is passed in directly as a constant
|
else // Value is passed in directly as a constant
|
||||||
{
|
{
|
||||||
rightValue = right.Value;
|
rightValue = simpleConstExp.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return rightValue;
|
return rightValue;
|
||||||
|
@ -238,6 +259,17 @@ namespace Marr.Data.QGen
|
||||||
_sb.AppendFormat(_dialect.ContainsFormat, fqColumn, paramName);
|
_sb.AppendFormat(_dialect.ContainsFormat, fqColumn, paramName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Write_In(MethodCallExpression body)
|
||||||
|
{
|
||||||
|
object value = GetRightValue(body.Arguments[1]);
|
||||||
|
//string paramName = string.Concat(_paramPrefix, "P", _command.Parameters.Count.ToString());
|
||||||
|
//var parameter = new ParameterChainMethods(_command, paramName, value).Parameter;
|
||||||
|
|
||||||
|
MemberExpression memberExp = (body.Arguments[0] as MemberExpression);
|
||||||
|
string fqColumn = GetFullyQualifiedColumnName(memberExp.Member, memberExp.Expression.Type);
|
||||||
|
_sb.AppendFormat(_dialect.InFormat, fqColumn, string.Join(",", value as List<int>));
|
||||||
|
}
|
||||||
|
|
||||||
private void Write_StartsWith(MethodCallExpression body)
|
private void Write_StartsWith(MethodCallExpression body)
|
||||||
{
|
{
|
||||||
// Add parameter to Command.Parameters
|
// Add parameter to Command.Parameters
|
||||||
|
|
|
@ -105,5 +105,9 @@ namespace NzbDrone.Common.Extensions
|
||||||
yield return buffer.Dequeue();
|
yield return buffer.Dequeue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public static bool In<T>(this T source, List<T> list)
|
||||||
|
{
|
||||||
|
return list.Contains(source);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -83,11 +83,12 @@ namespace NzbDrone.Core.Datastore
|
||||||
{
|
{
|
||||||
var idList = ids.ToList();
|
var idList = ids.ToList();
|
||||||
var query = string.Format("Id IN ({0})", string.Join(",", idList));
|
var query = string.Format("Id IN ({0})", string.Join(",", idList));
|
||||||
var result = Query.Where(query).ToList();
|
var result = Query.Where(m => m.Id.In(idList)).ToList();
|
||||||
|
//var result = Query.Where(query).ToList();
|
||||||
|
|
||||||
if (result.Count != idList.Count())
|
if (result.Count != idList.Count())
|
||||||
{
|
{
|
||||||
throw new ApplicationException("Expected query to return {0} rows but returned {1}".Inject(idList.Count(), result.Count));
|
throw new ApplicationException("Expected query to return {0} rows but returned {1}.".Inject(idList.Count(), result.Count));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data.SQLite;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
@ -197,6 +198,8 @@ namespace NzbDrone.Core.MediaFiles
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Execute(RenameMovieFolderCommand message)
|
public void Execute(RenameMovieFolderCommand message)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
_logger.Debug("Renaming movie folder for selected movie if necessary");
|
_logger.Debug("Renaming movie folder for selected movie if necessary");
|
||||||
var moviesToRename = _movieService.GetMovies(message.MovieIds);
|
var moviesToRename = _movieService.GetMovies(message.MovieIds);
|
||||||
|
@ -207,5 +210,11 @@ namespace NzbDrone.Core.MediaFiles
|
||||||
RenameMoviePath(movie);
|
RenameMoviePath(movie);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (SQLiteException ex)
|
||||||
|
{
|
||||||
|
_logger.Warn(ex, "wtf: {0}, {1}", ex.ResultCode, ex.Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue