-
May 20th, 2021, 09:18 AM
#1
[RESOLVED] PetaPoco: Operation could destabilize the runtime
At work, we use PetaPoco as our ORM and it works great when using strongly typed objects.
However, I have a requirement to allow users (specifically just one user) enter their own queries to return an array of objects that will then be used on the front-end using the PivotTable.js library.
The issue I am running into is that when I go to query the user entered SQL and I pass a dynamic or an object as the type, I get the following exception:
Operation could destabilize the runtime
For reference, this is the code that I'm using:
Code:
public List<object> ExecuteSql(int adHocQueryId)
{
_database.EnableAutoSelect = false;
var adHocQuery = Get(adHocQueryId);
var results = _database.Query<object>(adHocQuery.Sql).ToList();
_database.EnableAutoSelect = true;
return results;
}
The Get method is defined as the following:
Code:
public AdHocQueryViewModel Get(int AdHocQueryId)
{
var filter = new Dictionary<string, dynamic>
{
{ "AdHocQueryId", AdHocQueryId }
};
var records = Query(filter);
if (!records.Any())
{
throw new SystemError($"No record found for AdHocQueryId {AdHocQueryId}");
}
if (records.Count > 1)
{
throw new SystemError($"More than one record found for AdHocQueryId {AdHocQueryId}");
}
return records.Single();
}
The Query method ultimately hits the IDatabase's Query method using a List<AdHocQueryViewModel> as the strongly typed object.
I was thinking about writing a wrapper to just return the collection of datarows, but before I started down that path, I wanted to see if anyone else ran into this same issue.
-
May 20th, 2021, 10:06 AM
#2
Re: PetaPoco: Operation could destabilize the runtime
This is what I wound up adding to the PetaPoco.cs file, though I'm not very happy with it:
Code:
public IEnumerable<KeyValuePair<string, object>[]> Query(string sql)
{
OpenSharedConnection();
try
{
using (var cmd = CreateCommand(_sharedConnection, sql))
{
IDataReader r;
try
{
r = cmd.ExecuteReader();
OnExecutedCommand(cmd);
}
catch (Exception x)
{
if (OnException(x))
throw;
yield break;
}
using (r)
{
while (true)
{
var poco = new List<KeyValuePair<string, object>>();
try
{
if (!r.Read())
yield break;
for (int counter = 0; counter < r.FieldCount; counter++)
{
poco.Add(new KeyValuePair<string, object>(r.GetName(counter), r.GetValue(counter)));
}
}
catch (Exception x)
{
if (OnException(x))
throw;
yield break;
}
yield return poco.ToArray();
}
}
}
}
finally
{
CloseSharedConnection();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|