3

I am trying to get the existence of a row using Oracle and Petapoco. So far I have the following code.

var sql =new Sql("select COUNT(*) FROM myTable where field = 'value'");
var exists = myDB.Query<int>(sql) > 0;

This feels kinda dirty as I'm splitting the work between the database and my application. Is there a way I could do something like the following?

var exists = myDB.Query<bool>(someNewSqlThatReturnsBool);
Jim Jeffries
  • 9,841
  • 15
  • 62
  • 103
  • 2
    Unfortunately, I think it is not possible. I would, however, recommend a slightly different (better in my opinion) approach to write a query to check for existency of a record: `SELECT COUNT(1) FROM dual WHERE EXISTS (SELECT 1 FROM yourTable WHERE field = 'value');` – Przemyslaw Kruglej Oct 11 '13 at 11:15

1 Answers1

3

With PetaPoco you should be able to use the Exists method overload as shown below from the API.

/// <summary>
/// Checks for the existance of a row matching the specified condition
/// </summary>
/// <typeparam name="T">The Type representing the table being queried</typeparam>
/// <param name="sqlCondition">The SQL expression to be tested for (ie: the WHERE expression)</param>
/// <param name="args">Arguments to any embedded parameters in the SQL statement</param>
/// <returns>True if a record matching the condition is found.</returns>
public bool Exists<T>(string sqlCondition, params object[] args) 

So you should be able to just call:

var exists = myDB.Exists<myTable>("field = 'value'");
Matt
  • 14,353
  • 5
  • 53
  • 65