Header Image - Obake (お化け) is a Japanese term refering to a state of transforming or shapeshifting. Technology is constantly doing just that everyday.

Category Archives

2 Articles

Having DataMapper Build You A SQL Query

ORMs are great and they can handle a lot of operations that I don’t particularly want to write from scratch.  But sometimes you need to get in and use the SQL or the power of whatever back-end you want to play.  DataMapper allows you to do this and you can find the documentation here under “talking directly to your datastore“.  But lets say I want to take advantage of the way DataMapper builds my query and sanitizes it because I’m too lazy or their are way more injection attacks then I can keep up with but a community ORM adapter can.  Also,  sometimes I don’t want DataMapper to construct a ruby object for me that consumes more resources and I can get by with just the structs when I’m getting back 100,000s of records.  So in summary I want DataMapper to build my sanitized query but I want to   talk directly to my backend.

Here is the solution that we came up with to extend DataMapper in our application and in this case we are assuming that we are using some sql version of an adapter.

module DataMapper
  def self.generate_sql(dm_query)
    statement, bind_vars = repository.adapter.send(:select_statement, dm_query.query)
    repository.adapter.send(:open_connection).create_command(statement).send(:escape_sql, bind_vars)
  end
end

With this in our application we can now make a call like:

  sql = DataMapper.generate_sql(User.all(:name.like=>’%Bob%’) 

This will return an SQL statement for selecting all the users in the User Table that match Bob in them.  If we want to execute this SQL against our backend then all we need is:

  results = repository(:default).adapter.select(sql)

And now results holds the records as structs.  This is useful when dealing with a large number of records and you just want the results. Other fun things you may want to do include building transactions out of your DataMapper generated statements since I find some things to be lacking with the DataMapper transactions.

12 views

Agile Task Mobile App

So a friend of mine, @jaderobbins, and some of his buddies wrote a cool productivity service called AgileTask and I have been using it for the last two months and find it well suited in helping me get things done each day.  I like it so much that I wanted to get a mobile version for my Droid Incredible.  Unfortunately, there is no app at this time.  However, as I have wanted to write an android application for months now and AgileTask has an easy to use API I have started.

I looked at Titanium’s Appcelerator at first and didn’t get to far and then I ran into rhomobile as I was looking for something that used Ruby since that is what I use day in and day out at work currently.  Having started playing with the framework yesterday afternoon I have already been able to churn out an app that can pull my tasks for “Today” and my “Icebox”.  So needless to say I find this framework Super Awesome  right now.

So I the next few days I’ll put together a git repo (https://github.com/scleveland/AgileTaskMobile) and crank out some more feature and hopefully will have an alpha/beta app in the next week for other AgileTask Pro users to use (Sorry but you will need to have a Pro account to use the app as API keys are only available for Pro Users – but the price is totally worth it for such an awesome software as a service).

Oh and I almost forgot… The rhomobile framework can also output an iPhone app using the same code so I’ll also play with testing that (Blackberry is also supported but I’m not motivated to churn anything out for that – someone else can fork the project if they wish to do a blackberry port)

2 views