X Tutup
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@

.DS_Store
_ReSharper.Npgsql2010
src/bin/
src/obj/
src/build/
*.suo
*.csproj.user
*.DotSettings.user
testsuite/noninteractive/NUnit20/bin/
testsuite/noninteractive/NUnit20/obj/
NUnit20/obj/
npgsql_tests.log
Binary file added lib/EntityFramework.dll
Binary file not shown.
4 changes: 2 additions & 2 deletions src/Npgsql/NpgsqlProviderManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ public override TypeUsage GetStoreType(TypeUsage edmType)
if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
!facet.IsUnbounded && facet.Value != null)
{
return TypeUsage.CreateDateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["interval"], (byte)facet.Value);
return TypeUsage.CreateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["interval"], (byte)facet.Value);
}
else
{
return TypeUsage.CreateDateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["interval"], null);
return TypeUsage.CreateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["interval"], null);
}
case PrimitiveTypeKind.Binary:
{
Expand Down
67 changes: 66 additions & 1 deletion src/Npgsql/SqlGenerators/SqlBaseGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,26 @@ private VisitedExpression VisitFunction(EdmFunction function, IList<DbExpression
return StringModifier("btrim", args);

// date functions
//case "AddNanoseconds":
// date functions
case "AddDays":
case "AddHours":
case "AddMicroseconds":
case "AddMilliseconds":
case "AddMinutes":
case "AddMonths":
case "AddNanoseconds":
case "AddSeconds":
case "AddYears":
case "DiffDays":
case "DiffHours":
case "DiffMicroseconds":
case "DiffMilliseconds":
case "DiffMinutes":
case "DiffMonths":
case "DiffNanoseconds":
case "DiffSeconds":
case "DiffYears":
return DateAdd(function.Name, args);
// return
case "Day":
case "Hour":
Expand Down Expand Up @@ -1178,6 +1197,52 @@ private VisitedExpression DatePart(string partName, IList<DbExpression> args)
return extract_date;
}

/// <summary>
/// PostgreSQL has no direct functions to implements DateTime canonical functions
/// http://msdn.microsoft.com/en-us/library/bb738626.aspx
/// http://msdn.microsoft.com/en-us/library/bb738626.aspx
/// but we can use workaround:
/// expression + number * INTERVAL '1 number_type'
/// where number_type is the number type (days, years and etc)
/// </summary>
/// <param name="functionName"></param>
/// <param name="args"></param>
/// <returns></returns>
private VisitedExpression DateAdd(string functionName, IList<DbExpression> args)
{
string operation = "";
string part = "";
bool nano = false;
if (functionName.Contains("Add"))
{
operation = "+";
part = functionName.Substring(3);
}
else if (functionName.Contains("Diff"))
{
operation = "-";
part = functionName.Substring(4);
}
else throw new NotSupportedException();

if (part == "Nanoseconds")
{
nano = true;
part = "Microseconds";
}

System.Diagnostics.Debug.Assert(args.Count == 2);
VisitedExpression dateAddDiff = new LiteralExpression("");
dateAddDiff.Append(args[0].Accept(this));
dateAddDiff.Append(operation);
dateAddDiff.Append(args[1].Accept(this));
dateAddDiff.Append(nano
? String.Format("/ 1000 * INTERVAL '1 {0}'", part)
: String.Format(" * INTERVAL '1 {0}'", part));

return dateAddDiff;
}

private VisitedExpression BitwiseOperator(IList<DbExpression> args, string oper)
{
System.Diagnostics.Debug.Assert(args.Count == 2);
Expand Down
5 changes: 3 additions & 2 deletions src/Npgsql/SqlGenerators/VisitedExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ internal override void WriteSql(StringBuilder sqlText)
case PrimitiveTypeKind.Boolean:
case PrimitiveTypeKind.Guid:
case PrimitiveTypeKind.String:
NpgsqlTypesHelper.TryGetNativeTypeInfo(GetDbType(_primitiveType), out typeInfo);
NpgsqlTypesHelper.TryGetNativeTypeInfo(GetDbType(_primitiveType), out typeInfo);
sqlText.Append('E');
sqlText.Append(typeInfo.ConvertToBackend(_value, false));
break;
case PrimitiveTypeKind.Time:
sqlText.AppendFormat(ni, "TIME '{0:T}'", _value);
sqlText.AppendFormat(ni, "INTERVAL '{0:T}'", _value);
break;
case PrimitiveTypeKind.Byte:
case PrimitiveTypeKind.SByte:
Expand Down
15 changes: 8 additions & 7 deletions src/NpgsqlTypes/NpgsqlTypesHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,13 +1112,14 @@ private String ConvertToBackendPlainQuery(Object NativeData)
Convert.ChangeType(Enum.Format(NativeData.GetType(), NativeData, "d"), typeof (String),
CultureInfo.InvariantCulture));
}
else if (NativeData is IFormattable)
{
return
(this.Quote
? QuoteString(((IFormattable) NativeData).ToString(null, ni).Replace("'", "''").Replace("\\", "\\\\"))
: ((IFormattable) NativeData).ToString(null, ni).Replace("'", "''").Replace("\\", "\\\\"));
}
IFormattable nativeData = NativeData as IFormattable;
if (nativeData != null)
{
return
(this.Quote
? QuoteString((nativeData).ToString(null, ni).Replace("'", "''").Replace("\\", "\\\\"))
: (nativeData).ToString(null, ni).Replace("'", "''").Replace("\\", "\\\\"));
}

// Do special handling of strings when in simple query. Escape quotes and backslashes.
return
Expand Down
14 changes: 12 additions & 2 deletions testsuite/noninteractive/NUnit20/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
<add key="ConnectionStringV2" value="Server=127.0.0.1;Port=5432;User Id=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests;Encoding=UNICODE;syncnotification=false;protocol=2"/>
</appSettings>
<connectionStrings>
<add name="XmlTestContext" providerName="System.Data.EntityClient" connectionString="metadata=XmlModel\.;provider=Npgsql;provider connection string=&quot;server=127.0.0.1;database=modelXmlTest;user id=npgsql_tests;password=npgsql_tests;enlist=true;&quot;"/>
<!--<add name="XmlTestContext" providerName="System.Data.EntityClient" connectionString="metadata=XmlModel\.;provider=Npgsql;provider connection string=&quot;server=127.0.0.1;database=modelXmlTest;user id=npgsql_tests;password=npgsql_tests;enlist=true;&quot;"/>-->
<add name="TestContext" connectionString="Server=localhost;port=5432;UserId=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests;Preload Reader = true;" providerName="Npgsql"/>
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql"></remove>
<add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Framework Data Provider for Postgresql Server" type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.00, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
</DbProviderFactories>
</system.data>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
139 changes: 139 additions & 0 deletions testsuite/noninteractive/NUnit20/BackslashTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using System;
using System.Data;
using System.Configuration;
using Npgsql;

using NpgsqlTypes;

using NUnit.Framework;



namespace NpgsqlTests
{
[TestFixture]
public class BackslashTests: BaseClassTests
{

protected override NpgsqlConnection TheConnection
{
get { return _conn; }
}
protected override NpgsqlTransaction TheTransaction
{
get { return _t; }
set { _t = value; }
}
protected virtual string TheConnectionString
{
get { return _connString; }
}

[Test]
public void TestStoreUsingSQL()
{
CommitTransaction = true;
const string testString = "av\\fs\\dgdg\t\n\b\f\n\t";

NpgsqlCommand command = new NpgsqlCommand();
command.Connection = TheConnection;
command.CommandText = "INSERT INTO tablea(\"field_text\") VALUES(@field_text)";
command.Parameters.AddWithValue("field_text", testString);
command.ExecuteNonQuery();

command.CommandText = "SELECT field_text FROM tablea WHERE oid =" + command.LastInsertedOID;
var result = command.ExecuteScalar();

if (result != null)
Assert.AreEqual(testString, result.ToString());
else
{
Assert.Fail();
}
}

[Test]
public void TestSingleUsingSQL()
{
CommitTransaction = true;
const string testString = @"\";

NpgsqlCommand command = new NpgsqlCommand();
command.Connection = TheConnection;
command.CommandText = "INSERT INTO tablea(\"field_text\") VALUES(@field_text)";
command.Parameters.AddWithValue("field_text", testString);
command.ExecuteNonQuery();

command.CommandText = "SELECT field_text FROM tablea WHERE oid =" + command.LastInsertedOID;
var result = command.ExecuteScalar();

if (result != null)
Assert.AreEqual(testString, result.ToString());
else
{
Assert.Fail();
}
}

[Test]
public void TestStoreUsignEF()
{
string testString = "av\\fs\\dgdg\t\n\b\f\n\t";

var context = new TestContext();
var obj = new Tablea()
{
Field_text = testString,
};
context.Tablea.Add(obj);
context.SaveChanges();
context.Dispose();

context = new TestContext();
var result = context.Tablea.FirstOrDefault(x => x.Field_serial == obj.Field_serial);
if (result != null)
{
Assert.AreEqual(result.Field_text, testString);
}
else
{
Assert.Fail();
}
}

[Test]
public void TestSingleUsignEF()
{
const string testString = @"\";

var context = new TestContext();
var obj = new Tablea()
{
Field_text = testString,
};
context.Tablea.Add(obj);
context.SaveChanges();
context.Dispose();

context = new TestContext();
var result = context.Tablea.FirstOrDefault(x => x.Field_serial == obj.Field_serial);
if (result != null)
{
Assert.AreEqual(result.Field_text, testString);
}
else
{
Assert.Fail();
}
}

}
}
7 changes: 1 addition & 6 deletions testsuite/noninteractive/NUnit20/BaseClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,7 @@ protected virtual void TearDown()
if (_connV2.State != ConnectionState.Closed)
_connV2.Close();

}





}
}

}
Expand Down
21 changes: 9 additions & 12 deletions testsuite/noninteractive/NUnit20/NpgsqlTests2010.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\lib\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="nunit.framework" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.configuration" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
Expand All @@ -77,33 +82,25 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="BackslashTests.cs" />
<Compile Include="BaseClassTests.cs" />
<Compile Include="CommandTests.cs" />
<Compile Include="ConnectionTests.cs" />
<Compile Include="POCO\Context.cs" />
<Compile Include="DataAdapterTests.cs" />
<Compile Include="DataReaderTests.cs" />
<Compile Include="NpgsqlParameterTests.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="PrepareTests.cs" />
<Compile Include="SystemTransactionsTest.cs" />
<Compile Include="POCO\Tablea.cs" />
<Compile Include="TypesTests.cs" />
<Compile Include="xmlModel\XmlTest.ObjectLayer.cs" />
<Compile Include="xmlModel\XmlTest.Views.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="xmlModel\XmlTest.csdl">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="xmlModel\XmlTest.msl">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="xmlModel\XmlTest.ssdl">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
Expand Down Expand Up @@ -139,4 +136,4 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
</Project>
</Project>
13 changes: 13 additions & 0 deletions testsuite/noninteractive/NUnit20/POCO/Context.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace NpgsqlTests
{
public class TestContext : DbContext
{
public DbSet<Tablea> Tablea { get; set; }
}
}
Loading
X Tutup