Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL Test Error when get Hours on TimeSpan #2950

Open
sasukevita opened this issue Apr 13, 2021 · 8 comments · May be fixed by #3994
Open

PostgreSQL Test Error when get Hours on TimeSpan #2950

sasukevita opened this issue Apr 13, 2021 · 8 comments · May be fixed by #3994

Comments

@sasukevita
Copy link

sasukevita commented Apr 13, 2021

model

	[Table(Schema="Member", Name="Table")]
	public partial class Table
	{
		[PrimaryKey, NotNull    ] public int       Id   { get; set; } // integer
		[Column,        Nullable] public DateTime? Date { get; set; } // date
		[Column,        Nullable] public TimeSpan? Time { get; set; } // time without time zone
	}

error test

var query = (from x in db.Table
                         where x.Time.Value.Hours >= 0//Npgsql.PostgresException: '42883: operator does not exist: time without time zone > integer'
                         select new
                         {
                             Output = x.Time.Value.Hours,//LinqToDB.Common.LinqToDBConvertException: 'Cannot convert value '12:34:56' to type 'System.Int32''
                         });
            var r = query.ToArray();

Environment details

linq2db version: 3.3.0
Database: PostgreSQL 12.1
.NET Framework: 4.7.2

@jogibear9988
Copy link
Member

jogibear9988 commented Apr 17, 2021

We need a TimeSpan Part Builder like the DatePart Builder:

public static int? DatePart([SqlQueryDependent] Sql.DateParts part, [ExprParameter] DateTime? date)

and then the Expressions for all parts like here:

{ M(() => DateTime.Now.Year ), N(() => L<DateTime,int> ((DateTime obj) => Sql.DatePart(Sql.DateParts.Year, obj)!.Value )) },

@MaceWindu MaceWindu added this to the Future milestone May 29, 2021
@ptvbnk
Copy link

ptvbnk commented Jun 6, 2021

@jogibear9988 @MaceWindu
Interested to contribute to this issue.
I am trying to find a way to test this on my local.

I tried searching for creating a nuget locally and then attaching the nuget to the project and test it out.
So far my search didn't yield any results.
I searched through the documentation and couldn't find anything on this.
Can you please guide me on this?

@MaceWindu
Copy link
Contributor

To build nuget locally, use NuGet\PackLocal.cmd, but I'm not sure why you need to use nuget for testing.

@ptvbnk
Copy link

ptvbnk commented Jun 16, 2021

Is there any easy way to test without building nuget?
How can I use the linq2db assembly in another project?

@sdanyliv
Copy link
Member

Just add linq2db.csproj to solution and add project dependecy. Also we have linq2db.playground.slnf and you can add new test by copying TestTemplate.cs

@ptvbnk
Copy link

ptvbnk commented Jun 18, 2021

@sdanyliv @MaceWindu

I have created a console app in .NET CORE and added this project as reference.

Main method where I am creating connection as per code given in gitpage.

static void Main(string[] args)
       {
           // create options builder
           var builder = new LinqToDbConnectionOptionsBuilder();

           // configure connection string
           builder.UsePostgreSQL("Server=PostgreSQL 12;Port=5432;Database=postgres;User Id=postgres;Password=abcd;Pooling=true;MinPoolSize=10;MaxPoolSize=100;");

           // pass configured options to data connection constructor
           using (var dc = new DataConnection(builder.Build()))
           {

           }            
       }

Error I am getting

System.InvalidOperationException
  HResult=0x80131509
  Message=Cannot load assembly Npgsql
  Source=linq2db
  StackTrace:
   at LinqToDB.DataProvider.PostgreSQL.NpgsqlProviderAdapter.GetInstance() in C:\CodeContribute\linq2db\Source\LinqToDB\DataProvider\PostgreSQL\NpgsqlProviderAdapter.cs:line 165
   at LinqToDB.DataProvider.PostgreSQL.PostgreSQLDataProvider..ctor(String name, PostgreSQLVersion version) in C:\CodeContribute\linq2db\Source\LinqToDB\DataProvider\PostgreSQL\PostgreSQLDataProvider.cs:line 22
   at LinqToDB.DataProvider.PostgreSQL.PostgreSQLTools.<>c.<.cctor>b__19_0() in C:\CodeContribute\linq2db\Source\LinqToDB\DataProvider\PostgreSQL\PostgreSQLTools.cs:line 18
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()

  This exception was originally thrown at this call stack:
    LinqToDB.DataProvider.PostgreSQL.NpgsqlProviderAdapter.GetInstance() in NpgsqlProviderAdapter.cs
    LinqToDB.DataProvider.PostgreSQL.PostgreSQLDataProvider.PostgreSQLDataProvider(string, LinqToDB.DataProvider.PostgreSQL.PostgreSQLVersion) in PostgreSQLDataProvider.cs
    LinqToDB.DataProvider.PostgreSQL.PostgreSQLTools..cctor.AnonymousMethod__19_0() in PostgreSQLTools.cs
    System.Lazy<T>.ViaFactory(System.Threading.LazyThreadSafetyMode)
    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    System.Lazy<T>.CreateValue()
    System.Lazy<T>.Value.get()

I debugged it and found that assembly is getting as null in NpgsqlProviderAdapter.cs file

public static NpgsqlProviderAdapter GetInstance()
		{
			if (_instance == null)
				lock (_syncRoot)
					if (_instance == null)
					{
						var assembly = Tools.TryLoadAssembly(AssemblyName, null);
						if (assembly == null)
							throw new InvalidOperationException($"Cannot load assembly {AssemblyName}");

Appreciate any help in fixing this.

@MaceWindu
Copy link
Contributor

Looks like you didn't added rerefence to Npgsql nuget

@ptvbnk
Copy link

ptvbnk commented Jun 20, 2021

@MaceWindu
I am able to run the code after adding Npgsql nuget.

I am somewhat confused with the solution proposed by @jogibear9988
I understood that we need to add similar code for TimeParts as well.
But I am not sure if I should add another class or can I do it in sql.DateTime class or any other existing class?

Also, any reference to the internal working of the code would be helpful.
I checked Contributing.md page and couldn't find any.
I am having hard time debugging through full code and trying to make sense.
Thanks.

@MaceWindu MaceWindu removed this from the Future milestone Dec 24, 2022
@MaceWindu MaceWindu linked a pull request Feb 3, 2024 that will close this issue
@MaceWindu MaceWindu added status: has-pr There is active PR for issue and removed good first issue labels Feb 3, 2024
@MaceWindu MaceWindu added this to the In-progress milestone Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

5 participants