DEV Community

Andy Hopwood
Andy Hopwood

Posted on

All Wrapped Up

Wrap it up and ship it out!

No. I know Christmas is around the corner but I'm not talking about gift wrapping.

Any programming language you work on, will have great built in classes for your common tasks. e.g. Database access, File System, web services...etc.

You can use these directly in your other classes perfectly fine and safely. I personally Wrap these up in my own class with it's own properties and Methods...

...And Heres Why!

As I highlighted recently, I've just started using Xojo for work. On my first project I happily wrapped up my PostgreSQLDatabase calls into my own class called DBConnection as I learned how to use them. I exported this class so I could re-use it. My class saved the PostgreSQLDatabase object to it's own private variable and called methods from there.

I made a wrapper method in this, calledSelectData which mapped directly to Xojo's SQLSelect, which returns a Recordset.

The Method

public Function SelectData(Sql As String) As RecordSet
    Return Self.CurrentConnection.SQLSelect(Sql)
End Function

It's that simple!! So I hear ya...

Why Is that Useful??

Can't you just call that directly in your code??

How many coffee breaks did you take whilst writing that??

Well I'll answer them now!

Xojo API 2.0

In the update there was a new Data Type called RowSet and with it a slightly renamed method for selecting data from a database.

The advantage in this new data type is with error catching. The Recordset had "Silent Errors" where the software you are building would just carry on as if the select data had produced no data.

The Change

public Function SelectData(Sql As String) As RecordSet
    Return Self.CurrentConnection.SelectSQL(Sql)
End Function

Can ya see it??

...it's subtle.

We've basically gone from..

Self.CurrentConnection.SQLSelect(Sql)

to

Self.CurrentConnection.SelectSQL(Sql)

so to answer the question, Why is that useful?

If everywhere in my code, I had called SQLSelect(Sql), to get data from my database, I would've had to have found them all to change it.

In my case the only change I made was the one you've just seen.

In Conclusion

Wrapping up the code meant I could make that change in function name in one spot rather than various spots throughout my program.

Top comments (0)