Skip to content

Commit

Permalink
iOS: Fixed bug with generics in DatabaseFacade.
Browse files Browse the repository at this point in the history
  • Loading branch information
ycastonguay committed Feb 27, 2013
1 parent 3e330e5 commit cc0142a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
13 changes: 0 additions & 13 deletions MPfm/MPfm.Core/Conversion.cs
Expand Up @@ -264,19 +264,6 @@ public static T TryParse<T>(string input)
return default(T);
}

/// <summary>
/// Returns the value of an enum, as specified in the generics parameter.
/// </summary>
/// <typeparam name="T">Enum type</typeparam>
/// <param name="value">Value</param>
/// <returns>Enum with value</returns>
public static T GetEnumValue<T>(string value) where T : struct
{
T obj = new T();
Enum.TryParse<T>(value, out obj);
return obj;
}

public static short HighWord(int dWord)
{
return (short)(dWord >> 16 & 65535);
Expand Down
25 changes: 15 additions & 10 deletions MPfm/MPfm.Library/Database/DatabaseFacade.cs
Expand Up @@ -95,7 +95,7 @@ public List<AudioFile> SelectAudioFiles(AudioFileFormat format, string artistNam
if(format != AudioFileFormat.All)
{
count++;
sql.AppendLine(" [FileType] = '" + format.ToString() + "'");
sql.AppendLine(" [FileType] = '" + FormatSQLValue(format.ToString()) + "'");
}
if(!String.IsNullOrEmpty(artistName))
{
Expand All @@ -104,7 +104,7 @@ public List<AudioFile> SelectAudioFiles(AudioFileFormat format, string artistNam
{
sql.AppendLine(" AND ");
}
sql.AppendLine(" [ArtistName] = '" + artistName + "' ");
sql.AppendLine(" [ArtistName] = '" + FormatSQLValue(artistName) + "' ");
}
if(!String.IsNullOrEmpty(albumTitle))
{
Expand All @@ -113,7 +113,7 @@ public List<AudioFile> SelectAudioFiles(AudioFileFormat format, string artistNam
{
sql.AppendLine(" AND ");
}
sql.AppendLine(" [AlbumTitle] = '" + albumTitle + "' ");
sql.AppendLine(" [AlbumTitle] = '" + FormatSQLValue(albumTitle) + "' ");
}
if(!String.IsNullOrEmpty(search))
{
Expand Down Expand Up @@ -197,7 +197,7 @@ public void DeleteAudioFile(Guid audioFileId)
/// <param name="basePath">Base audio file path</param>
public void DeleteAudioFiles(string basePath)
{
_gateway.Delete("AudioFiles", "FilePath LIKE '" + basePath + "%'");
_gateway.Delete("AudioFiles", "FilePath LIKE '" + FormatSQLValue(basePath) + "%'");
}

/// <summary>
Expand Down Expand Up @@ -274,15 +274,15 @@ public List<string> SelectDistinctArtistNames(AudioFileFormat audioFileFormat)
sql.AppendLine("SELECT DISTINCT ArtistName, AlbumTitle FROM AudioFiles ");
if (audioFileFormat != AudioFileFormat.All && !String.IsNullOrEmpty(artistName))
{
sql.AppendLine(" WHERE FileType = '" + audioFileFormat.ToString() + "' AND ArtistName = '" + artistName.Replace("'", "''") + "'");
sql.AppendLine(" WHERE FileType = '" + audioFileFormat.ToString() + "' AND ArtistName = '" + FormatSQLValue(artistName) + "'");
}
else if (audioFileFormat != AudioFileFormat.All)
{
sql.AppendLine(" WHERE FileType = '" + audioFileFormat.ToString() + "' ");
}
else if(!String.IsNullOrEmpty(artistName))
{
sql.AppendLine(" WHERE ArtistName = '" + artistName.Replace("'", "''").ToString() + "' ");
sql.AppendLine(" WHERE ArtistName = '" + FormatSQLValue(artistName) + "' ");
}
sql.AppendLine(" ORDER BY ArtistName");

Expand Down Expand Up @@ -387,7 +387,7 @@ public void UpdatePlayCount(Guid audioFileId)
/// <returns>Folder</returns>
public Folder SelectFolderByPath(string path)
{
Folder folder = _gateway.SelectOne<Folder>("SELECT * FROM Folders WHERE FolderPath = '" + path + "'");
Folder folder = _gateway.SelectOne<Folder>("SELECT * FROM Folders WHERE FolderPath = '" + FormatSQLValue(path) + "'");
return folder;
}

Expand Down Expand Up @@ -455,7 +455,7 @@ public List<EQPreset> SelectEQPresets()
/// <returns>EQPreset</returns>
public EQPreset SelectEQPreset(string name)
{
EQPreset preset = _gateway.SelectOne<EQPreset>("SELECT * FROM EQPresets WHERE Name = '" + name + "'");
EQPreset preset = _gateway.SelectOne<EQPreset>("SELECT * FROM EQPresets WHERE Name = '" + FormatSQLValue(name) + "'");
return preset;
}

Expand Down Expand Up @@ -804,7 +804,7 @@ public List<Setting> SelectSettings()
/// <returns>Setting object</returns>
public Setting SelectSetting(string name)
{
Setting setting = _gateway.SelectOne<Setting>("SELECT * FROM Settings WHERE SettingName = '" + name + "'");
Setting setting = _gateway.SelectOne<Setting>("SELECT * FROM Settings WHERE SettingName = '" + FormatSQLValue(name) + "'");
return setting;
}

Expand Down Expand Up @@ -882,7 +882,7 @@ public void InsertPlaylistFile(PlaylistFile dto)
public void DeletePlaylistFile(string filePath)
{
// Delete loop
_gateway.Delete("PlaylistFiles", "FilePath = '" + filePath + "'");
_gateway.Delete("PlaylistFiles", "FilePath = '" + FormatSQLValue(filePath) + "'");
}

#endregion
Expand Down Expand Up @@ -1006,5 +1006,10 @@ public void CompactDatabase()
{
_gateway.CompactDatabase();
}

private string FormatSQLValue(string value)
{
return value.Replace("'", "''");
}
}
}
4 changes: 1 addition & 3 deletions MPfm/MPfm.Library/Database/MonoSQLiteGateway.cs
Expand Up @@ -453,9 +453,7 @@ public List<T> Select<T>(string sql) where T : new()
// Check if the type is an enum
if (info.PropertyType.IsEnum)
{
// Try to cast dynamically
MethodInfo castMethod = typeof(Conversion).GetMethod("GetEnumValue").MakeGenericMethod(info.PropertyType);
fieldValue = castMethod.Invoke(null, new object[] { fieldValue.ToString() });
fieldValue = Enum.Parse(info.PropertyType, fieldValue.ToString());
}
else if (info.PropertyType.FullName.ToUpper() == "SYSTEM.GUID")
{
Expand Down
3 changes: 2 additions & 1 deletion MPfm/MPfm.iOS/MPfm.iOS.csproj
Expand Up @@ -50,12 +50,13 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>False</ConsolePause>
<CodesignKey>iPhone Developer</CodesignKey>
<CodesignKey>iPhone Developer: Yanick Castonguay (N8WC5U9B3N)</CodesignKey>
<MtouchDebug>True</MtouchDebug>
<IpaPackageName />
<MtouchI18n />
<MtouchArch>ARMv7</MtouchArch>
<MtouchExtraArgs>-v -v -v -gcc_flags "-L${ProjectDir}/Lib/ -framework Accelerate -ObjC -lstdc++ -lbass -lbassmix -lbass_fx -lbass_ape -lbass_mpc -lbassflac -lbasswv -all_load"</MtouchExtraArgs>
<CodesignProvision>1DD1E6B7-C93C-4B7E-BC11-47742877E8E0</CodesignProvision>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' ">
<DebugType>none</DebugType>
Expand Down

0 comments on commit cc0142a

Please sign in to comment.