Is there a way of doing a "IF EXISTS" query on SimpleDB

May 25, 2010 at 4:24 PM
So far, i have the following: public bool ItemExists(string ID) { string selectQuery = "select count(*) from MyDomainName where Id = @ID"; var param= new CommandParameter("ID", ID); int count = (int)simpleDB.SelectScalar<MyClass>(selectQuery, param); if (count > 0) return true; return false; } Is this ok? or is there a better way? I'm basically trying to see if an item exists in the db - and return true or false... This will be used in conjunction with SQS to provide a queue system that only delivers once etc...
Coordinator
May 25, 2010 at 5:10 PM

Simply performing a partial get would be simpler (and probably performs better too):

Guid id = new Guid();
PropertyValues values = savant.GetAttributes<Person>(id, "x");
bool exists = values != null;

Requesting the nonexistent "x" property ensures that you won't get back any data except the id if the object does exist.

May 26, 2010 at 1:52 PM

hi,
thanks for your reply.

I tried this, but I get the following exception:

Item type 'Company.Project.Data.AmazonSimpleDB.SentEmailItem' has no property named 'x' mapped to SimpleDB.

I tried using "ID" instead of x - same result :-(

Here's the code i used:


bool Exists(string id) { var values = simpleDB.GetAttributes<SentEmailItem>(id, "x"); return values != null; }



Coordinator
May 26, 2010 at 2:44 PM

Oops, sorry about that. I forgot about the property mapping validation. You could still get this to work with "x" by building your own ItemMapping for use with GetAttributes(ItemMapping, object, string[]) (see Typeless Operations). But for your purposes it will be easier and just about as efficient to use one of the actual mapped property names rather than "x". Adding a single property name limits the amount of data returned to just the item name and that property value (if it exists); invoking GetAttributes with no property name returns all the mapped attributes.

 

May 26, 2010 at 3:25 PM

I tried that using "ID" but got the same error...

My entity has a property of ID - can i not return that?

Coordinator
May 26, 2010 at 5:19 PM

No, it must be a property other than the SimpleDb item name (I'm assuming that ID is your item name in this case). Note that the PropertyValues object has an ItemName property that holds the item name. SimpleDb has special rules around the id/item name so Savant must follow suite.