Skip to content

Commit

Permalink
PostgreSQL Port (437/437 MS Sql Server Unit Test Coverage)
Browse files Browse the repository at this point in the history
Includes additional files for switching between db backends. A bit manual right now but getting better.
  • Loading branch information
PuttItOut committed May 24, 2017
1 parent 91a713d commit 4e686c5
Show file tree
Hide file tree
Showing 10 changed files with 5,049 additions and 732 deletions.
677 changes: 4 additions & 673 deletions LICENSE

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private static void SetValueIfPresent<T>(string key, string value, bool updateOn
}
else if (typeof(T).IsEnum)
{
T conValue = (T)Enum.Parse(typeof(T), value);
T conValue = (T)Enum.Parse(typeof(T), value, true);
saveValue = conValue;
}
else
Expand Down
2 changes: 1 addition & 1 deletion Voat/Voat.Business/Data/Repository/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,7 @@ public IEnumerable<usp_CommentTree_Result> GetCommentTree(int submissionID, int?
case DataStoreType.SqlServer:
commentTree = _db.usp_CommentTree(submissionID, depth, parentID);
break;
case DataStoreType.PostgreSQL:
case DataStoreType.PostgreSql:

var d = new DapperQuery();
d.Select = "* FROM \"dbo\".\"usp_CommentTree\"(@SubmissionID, @Depth, @ParentID)";
Expand Down
107 changes: 58 additions & 49 deletions Voat/Voat.Business/Data/Repository/SqlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@

namespace Voat.Data
{

public enum DataStoreType
{
SqlServer,
PostgreSQL
PostgreSql
}

public class SqlFormatter
Expand All @@ -46,47 +45,48 @@ public static string DefaultSchema
return "dbo";
}
}
public static string IfExists(bool exists, string existsClause, string trueClause, string falseClause = null)
{
var result = new StringBuilder();
var existNegation = exists ? " " : " NOT ";
switch (Configuration.Settings.DataStore)
{
case DataStoreType.SqlServer:
result.AppendLine($"IF{existNegation}EXISTS ({existsClause})");
result.AppendLine(trueClause);
if (!String.IsNullOrEmpty(falseClause))
{
result.AppendLine($"ELSE");
result.AppendLine(falseClause);
}
break;
case DataStoreType.PostgreSQL:
result.AppendLine("load 'plpgsql';");
result.AppendLine("DO");
result.AppendLine("$$");
result.AppendLine("BEGIN");

result.AppendLine($"IF{existNegation}EXISTS ({existsClause}) THEN");
result.AppendLine("");
result.AppendLine(trueClause);
if (!String.IsNullOrEmpty(falseClause))
{
result.AppendLine($"ELSE");
result.AppendLine(falseClause);
}
result.AppendLine("END IF;");
result.AppendLine("END;");
result.AppendLine("$$");

break;
default:
throw new NotImplementedException();
}


return result.ToString();
}
//public static string IfExists(bool exists, string existsClause, string trueClause, string falseClause = null)
//{
// var result = new StringBuilder();
// var existNegation = exists ? " " : " NOT ";
// switch (Configuration.Settings.DataStore)
// {
// case DataStoreType.SqlServer:
// result.AppendLine($"IF{existNegation}EXISTS ({existsClause})");
// result.AppendLine(trueClause);
// if (!String.IsNullOrEmpty(falseClause))
// {
// result.AppendLine($"ELSE");
// result.AppendLine(falseClause);
// }
// break;
// case DataStoreType.PostgreSQL:
// result.AppendLine("load 'plpgsql';");
// result.AppendLine("DO");
// result.AppendLine("$$");
// result.AppendLine("BEGIN");

// result.AppendLine($"IF{existNegation}EXISTS ({existsClause}) THEN");
// result.AppendLine("");
// result.AppendLine(trueClause);
// if (!String.IsNullOrEmpty(falseClause))
// {
// result.AppendLine($"ELSE");
// result.AppendLine(falseClause);
// }
// result.AppendLine("END IF;");
// result.AppendLine("END;");
// result.AppendLine("$$");

// break;
// default:
// throw new NotImplementedException();
// }


// return result.ToString();
//}
public static string DeleteBlock(string fromTable, string alias = null)
{
var result = "";
Expand All @@ -98,7 +98,7 @@ public static string DeleteBlock(string fromTable, string alias = null)
case DataStoreType.SqlServer:
result = $"DELETE {alias} FROM {fromTable}{aliasClause}";
break;
case DataStoreType.PostgreSQL:
case DataStoreType.PostgreSql:
result = result = $"DELETE FROM {fromTable}{aliasClause}";
break;
default:
Expand All @@ -117,9 +117,18 @@ public static string UpdateSetBlock(string setStatements, string fromTable, stri
switch (Configuration.Settings.DataStore)
{
case DataStoreType.SqlServer:
result = $"UPDATE {alias} SET {setStatements} FROM {fromTable}{aliasClause}";

if (String.IsNullOrEmpty(alias))
{
result = $"UPDATE {fromTable} SET {setStatements}";
}
else
{
result = $"UPDATE {alias} SET {setStatements} FROM {fromTable}{aliasClause}";
}

break;
case DataStoreType.PostgreSQL:
case DataStoreType.PostgreSql:
result = result = $"UPDATE {fromTable}{aliasClause}SET {setStatements}";
break;
default:
Expand All @@ -138,7 +147,7 @@ public static string In(string parameter)
case DataStoreType.SqlServer:
result = $"IN {parameter}";
break;
case DataStoreType.PostgreSQL:
case DataStoreType.PostgreSql:
result = $"= ANY({parameter})";
break;
default:
Expand All @@ -158,7 +167,7 @@ public static string IsNull(string parameter, string defaultValue)
case DataStoreType.SqlServer:
result = $"ISNULL({parameter}, {defaultValue})";
break;
case DataStoreType.PostgreSQL:
case DataStoreType.PostgreSql:
result = $"CASE WHEN {parameter} IS NULL THEN {defaultValue} ELSE {parameter} END";
break;
default:
Expand Down Expand Up @@ -207,7 +216,7 @@ public static string BooleanLiteral(bool value)
var result = "";
switch (Configuration.Settings.DataStore)
{
case DataStoreType.PostgreSQL:
case DataStoreType.PostgreSql:
result = value ? "True" : "False";
break;
default:
Expand All @@ -223,7 +232,7 @@ public static string QuoteIndentifier(string name)
string result = null;
switch (Configuration.Settings.DataStore)
{
case DataStoreType.PostgreSQL:
case DataStoreType.PostgreSql:
result = String.Format("\"{0}\"", name);
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion Voat/Voat.Data/Models/VoatEntityDataModel.Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public virtual ObjectResult<usp_CommentTree_Result> usp_CommentTree(Nullable<int
new ObjectParameter("ParentID", parentID) :
new ObjectParameter("ParentID", typeof(int));

var result = ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<usp_CommentTree_Result>("\"dbo\".\"usp_CommentTree\"", submissionIDParameter, depthParameter, parentIDParameter);
var result = ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<usp_CommentTree_Result>("usp_CommentTree", submissionIDParameter, depthParameter, parentIDParameter);
return result;
}
}
Expand Down
4 changes: 4 additions & 0 deletions Voat/Voat.Data/Voat.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
<Generator>EntityModelCodeGenerator</Generator>
<LastGenOutput>VoatEntityDataModel.Designer.cs</LastGenOutput>
</EntityDeploy>
<None Include="~PostgreSql.App.Config" />
<None Include="App.Config" />
<None Include="~App.Config" />
<None Include="Models\VoatEntityDataModel.edmx.diagram">
Expand All @@ -253,7 +254,10 @@
<DependentUpon>VoatEntityDataModel.edmx</DependentUpon>
<LastGenOutput>VoatEntityDataModel.cs</LastGenOutput>
</Content>
<Content Include="~SqlServer.VoatEntityDataModel.edmx.xml" />
<Content Include="~PostgreSql.VoatEntityDataModel.edmx.xml" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
35 changes: 35 additions & 0 deletions Voat/Voat.Data/~PostgreSql.App.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>

<connectionStrings>
<add name="voatEntities" connectionString="metadata=res://Voat.Data/Models.VoatEntityDataModel.csdl|res://Voat.Data/Models.VoatEntityDataModel.ssdl|res://Voat.Data/Models.VoatEntityDataModel.msl;provider=Npgsql;provider connection string=&quot;Server={server};port=5432;Database={users-database};User ID={userid};Password={password};&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider"
invariant="Npgsql"
support="FF"
description="Data Provider for PostgreSQL"
type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.2.2.0" newVersion="3.2.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

1 comment on commit 4e686c5

@Retia-Adolf
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#928 A year later already. Wondering why ever should this be changed then just left here, though every file by Voat seems still under GPLv3.

Please sign in to comment.