update queries

This commit is contained in:
Luke Pulverenti
2016-11-20 00:59:36 -05:00
parent b06d1851da
commit 64d15be839
9 changed files with 253 additions and 125 deletions

View File

@@ -100,14 +100,17 @@ namespace Emby.Server.Implementations.Data
private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection)
{
var commandText = "replace into userdisplaypreferences (id, userid, client, data) values (?, ?, ?, ?)";
var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider);
using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userid, @client, @data)"))
{
var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider);
connection.Execute(commandText,
displayPreferences.Id.ToGuidParamValue(),
userId.ToGuidParamValue(),
client,
serialized);
statement.BindParameters.TryBind("@id", displayPreferences.Id.ToGuidParamValue());
statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue());
statement.BindParameters.TryBind("@client", client);
statement.BindParameters.TryBind("@data", serialized);
statement.MoveNext();
}
}
/// <summary>
@@ -163,16 +166,16 @@ namespace Emby.Server.Implementations.Data
{
using (var connection = CreateConnection(true))
{
var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?";
var paramList = new List<object>();
paramList.Add(guidId.ToGuidParamValue());
paramList.Add(userId.ToGuidParamValue());
paramList.Add(client);
foreach (var row in connection.Query(commandText, paramList.ToArray()))
using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client"))
{
return Get(row);
statement.BindParameters.TryBind("@id", guidId.ToGuidParamValue());
statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue());
statement.BindParameters.TryBind("@client", client);
foreach (var row in statement.ExecuteQuery())
{
return Get(row);
}
}
return new DisplayPreferences
@@ -197,14 +200,14 @@ namespace Emby.Server.Implementations.Data
{
using (var connection = CreateConnection(true))
{
var commandText = "select data from userdisplaypreferences where userId=?";
var paramList = new List<object>();
paramList.Add(userId.ToGuidParamValue());
foreach (var row in connection.Query(commandText, paramList.ToArray()))
using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId"))
{
list.Add(Get(row));
statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue());
foreach (var row in statement.ExecuteQuery())
{
list.Add(Get(row));
}
}
}
}

View File

@@ -168,14 +168,54 @@ namespace Emby.Server.Implementations.Data
return result[index].ToFloat();
}
public static DateTime GetDateTime(this IReadOnlyList<IResultSetValue> result, int index)
{
return result[index].ReadDateTime();
}
public static Guid GetGuid(this IReadOnlyList<IResultSetValue> result, int index)
{
return result[index].ReadGuid();
}
public static void TryBind(this IReadOnlyDictionary<string, IBindParameter> bindParameters, string name, string value)
{
IBindParameter bindParam;
if (bindParameters.TryGetValue(name, out bindParam))
{
bindParam.Bind(value);
}
}
public static void TryBind(this IReadOnlyDictionary<string, IBindParameter> bindParameters, string name, bool value)
{
IBindParameter bindParam;
if (bindParameters.TryGetValue(name, out bindParam))
{
bindParam.Bind(value);
}
}
public static void TryBind(this IReadOnlyDictionary<string, IBindParameter> bindParameters, string name, byte[] value)
{
IBindParameter bindParam;
if (bindParameters.TryGetValue(name, out bindParam))
{
bindParam.Bind(value);
}
}
public static void TryBindNull(this IReadOnlyDictionary<string, IBindParameter> bindParameters, string name)
{
IBindParameter bindParam;
if (bindParameters.TryGetValue(name, out bindParam))
{
bindParam.BindNull();
}
}
public static IEnumerable<IReadOnlyList<IResultSetValue>> ExecuteQuery(
this IStatement This)
{
while (This.MoveNext())
{
yield return This.Current;
}
}
}
}

View File

@@ -89,11 +89,12 @@ namespace Emby.Server.Implementations.Data
{
connection.RunInTransaction(db =>
{
var commandText = "replace into users (guid, data) values (?, ?)";
db.Execute(commandText,
user.Id.ToGuidParamValue(),
serialized);
using (var statement = db.PrepareStatement("replace into users (guid, data) values (@guid, @data)"))
{
statement.BindParameters.TryBind("@guid", user.Id.ToGuidParamValue());
statement.BindParameters.TryBind("@data", serialized);
statement.MoveNext();
}
});
}
}
@@ -151,10 +152,11 @@ namespace Emby.Server.Implementations.Data
{
connection.RunInTransaction(db =>
{
var commandText = "delete from users where guid=?";
db.Execute(commandText,
user.Id.ToGuidParamValue());
using (var statement = db.PrepareStatement("delete from users where guid=@id"))
{
statement.BindParameters.TryBind("@id", user.Id.ToGuidParamValue());
statement.MoveNext();
}
});
}
}