FetchQuery<MyClass>

Posts   
 
    
Valemus avatar
Valemus
User
Posts: 37
Joined: 09-Jan-2018
# Posted on: 27-Apr-2018 17:14:33   

Hi!

Occasionally I have to get data using a Sql string (ugly, I know, but I have no choice).

So I'm trying to use the following:


string sql = "SELECT ID, Name FROM Customer";
var results = Adapter.FetchQuery<MyClass>(sql);

First question: is there a way to map the results on a generated class (like "CustomerEntity") instead of creating a duplicate ?

However, I've created MyClass like this:

public class MyClass { public Guid ID { get; set; } public string Name { get; set; } }

The program gets stuck on the Adapter.FetchQuery line disappointed Infinite loop without a message.

So...second question: what am I doing wrong?

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 27-Apr-2018 20:57:19   

specific version information might be handy here wink Also if you use a debugger in the infinite loop issue, and break, what's the stacktrace showing? Perhaps there is a lot of data and it is fetching everything? (you didn't specify a predicate after all, so all data is fetched)

Frans Bouma | Lead developer LLBLGen Pro
Valemus avatar
Valemus
User
Posts: 37
Joined: 09-Jan-2018
# Posted on: 02-May-2018 09:16:52   

Yes, sorry: version 5.3.5, Adapter, using C# 4.6.2.

There are just few records in the tables.

A breakpoint does not help, since the program enter this line and stops there:

var results = _db.Adapter.FetchQuery<UserX>(sql);

Entering into this function I see:

/// <summary>Creates a new Dynamic Query engine object and passes in the defined catalog/schema overwrite hashtables.</summary>
        protected override DynamicQueryEngineBase CreateDynamicQueryEngine()
        {
            return this.PostProcessNewDynamicQueryEngine(new DynamicQueryEngine());
        }

And inside this:

/// <summary>Sets the per instance compatibility level on the dqe instance specified.</summary>
        /// <param name="dqe">The dqe.</param>
        protected override void SetPerInstanceCompatibilityLevel(DynamicQueryEngineBase dqe)
        {
            if(_compatibilityLevel.HasValue)
            {
                ((DynamicQueryEngine)dqe).CompatibilityLevel = _compatibilityLevel.Value;
            }
        }

where _compatibilityLevel.HasValue is false. This last method exits and everything stop responding.

However... UPDATE!

I've been unlucky in selecting the table to use for my expertiments. My table is "User" and it does not work. But if I do the same with other tables... it works flushed

I mean:

var sql = "SELECT * FROM Culture";
var results = _db.Adapter.FetchQuery<CultureX>(sql);

with

 public class CultureX
    {
        public string Name { get; set; }
        public short Code { get; set; }
    }

WORKS.

While

var sql = "SELECT * FROM User";
var results = _db.Adapter.FetchQuery<UserX>(sql);

with

 public class UserX
    {
        public Guid Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    }

DOES NOT WORK.

Can "User" be a sort of reserved word which cause problems? disappointed

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 02-May-2018 10:08:03   

If this is sql server, try "SELECT * FROM [User]" simple_smile

Also, it should give an exception, you catch exceptions somewhere?

Frans Bouma | Lead developer LLBLGen Pro
Valemus avatar
Valemus
User
Posts: 37
Joined: 09-Jan-2018
# Posted on: 02-May-2018 10:27:41   

Otis wrote:

If this is sql server, try "SELECT * FROM [User]" simple_smile

Also, it should give an exception, you catch exceptions somewhere?

flushed confused wow... shame on me, I didn't think that simple thing... and it works!

I just keep a warning on the fact that when the sql query is malformed the method just hangs, without exeptions.

Thank you!!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 02-May-2018 11:12:48   

If it hangs it might be a bug in our system. We'll check it out.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 02-May-2018 11:49:36   

Can't reproduce the hang:


public class User
{
    public string UserID { get; set; }
    public string Password { get; set; }
    public string FullName { get; set; }
}


[Test]
public void SelectFromUserWithoutBracketsTest()
{
    using(var adapter = new DataAccessAdapter())
    {
        var q = @"SELECT * FROM User";
        var result = adapter.FetchQuery<User>(q);
        Assert.AreEqual(1, result.Count);
    }
}

gives:

Unittests.TestLibrary.SqlServerTests.Adapter.RawSQLTests_Ordering.SelectFromUserWithoutBracketsTest

SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException : An exception was caught during the execution of a retrieval query: Incorrect syntax near the keyword 'User'.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception. ----> System.Data.SqlClient.SqlException : Incorrect syntax near the keyword 'User'. at SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery.Execute(CommandBehavior behavior) in C:\Myprojects\VS.NET Projects\LLBLGen Pro v5.3\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Query\RetrievalQuery.cs:line 119 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterCore.FetchDataReader(IRetrievalQuery queryToExecute, CommandBehavior readerBehavior) in C:\Myprojects\VS.NET Projects\LLBLGen Pro v5.3\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterCore.cs:line 1450 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterCore.FetchProjection[T](List1 destination, IRetrievalQuery queryToExecute) in C:\Myprojects\VS.NET Projects\LLBLGen Pro v5.3\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterCore.cs:line 5089 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterCore.FetchQuery[T](PlainSQLFetchAspects fetchAspects, String sqlQuery, Object parameterValues) in C:\Myprojects\VS.NET Projects\LLBLGen Pro v5.3\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterCore.cs:line 539 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.<>n__1[T](PlainSQLFetchAspects fetchAspects, String sqlQuery, Object parameterValues) at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.<>c__DisplayClass2_01.<FetchQuery>b__0() in C:\Myprojects\VS.NET Projects\LLBLGen Pro v5.3\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 102 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.ExecuteWithActiveRecoveryStrategy[T](Func`1 toExecute) in C:\Myprojects\VS.NET Projects\LLBLGen Pro v5.3\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 919 ...

So the only thing I can think of where it might hang is that you use a custom recovery strategy (by default none is used) which always retries all exceptions.

It doesn't hang in the runtime.

Frans Bouma | Lead developer LLBLGen Pro
Valemus avatar
Valemus
User
Posts: 37
Joined: 09-Jan-2018
# Posted on: 02-May-2018 14:33:44   

I'll keep an eye on this to see if I'm able to give you more details.

Thanks!