Skip to content

LIMIT and OFFSET in Postmodern

25-Apr-10

I’ve been using the Postmodern library in Common Lisp to access PostgresQL databases of late and it’s been a pretty good experience.

That said, I had a little trouble using LIMIT and OFFSET to get DAO objects and wanted to put an example online in case anyone else has trouble with this. Future Postmoderners, May Google protect you.

You’ll want to use query-dao instead of select-dao and wrap the limit around a select statement. It winds up looking something like this:

(query-dao 'card
  (:limit (:select (:distinct 'name) :from 'card) 10 0))

In this example, we’re selecting the first 10 rows (that are distinct by name) from the card table. Obviously, you could use '* instead of (:distinct 'name) to select from all rows.

That’s not much of a post but here it is. Once I’m through finals I’ll try to start posting a bit more regularly. Then again, I’ll be moving once in May and once in June so we’ll see what happens.

A Brief, “Postmodern” Shout-out

11-Apr-10

Things have been crazy lately but I’m not here to give a full update. I will say that there’s been good with the bad, family, friends and supporters all the while and that the bad is mostly the usual bureaucratic and financial troubles that are just a part of life. I’m trying to post more regularly. Today is a brief programming post.

This semester I’ve been taking a database course and we’re building a small, silly webapp as the final project. The course uses SQL+PHP and I asked my professor if he wouldn’t mind if I used SQL+Common Lisp. He accepted and so I’ve been using the Postmodern library for Common Lisp to talk to my Postgresql database. Postmodern has been really nice to use so far but there’s one thing that I had a little trouble with that I’d like to document here.

Generally, if you’re writing classes in Lisp you’re using CLOS and an example might be something like this:

(defclass user ()
  ((username
    :initarg :username :reader :username)
   (password
    :initarg :password :reader :password)
   (salt
    :initarg :salt :reader :salt)
   (email
    :initarg :email :reader :email)
   (first-name
    :initarg :first-name :reader :first-name)
   (last-name
    :initarg :last-name :reader :last-name)
   (zip
    :initarg :zip :reader :zip)))

Postmodern has a nice method for interacting with the database via class definitions that it coins “Database Access Objects”. Note that DAOs neither are nor attempt to be a full ORM solution, a very sane decision in my humble and inexperienced opinion. Anyway, to make a normal class into a DAO class is easy, just do this:

(defclass user ()
  ((username
    :col-type string
    :initarg :username :reader :username)
   (password
    :col-type string
    :initarg :password :reader :password)
   (salt
    :col-type string
    :initarg :salt :reader :salt)
   (email
    :col-type string
    :initarg :email :reader :email)
   (first-name
    :col-type string
    :initarg :first-name :reader :first-name)
   (last-name
    :col-type string
    :initarg :last-name :reader :last-name)
   (zip
    :col-type integer
    :initarg :zip :reader :zip))
  (:metaclass dao-class)
  (:keys username))

All you have to do is add col-types to each slot so the system knows what type is stored in the database rows, list the components of the primary key and declare it a member of the dao-class metaclass. With that done, you can easily work with CLOS objects and fairly seamlessly select, update, delete or instantiate+insert them into the database. Creating the table itself can be done as follows: (execute (dao-table-definition 'user)). However, this is really intended as a shortcut for cases where you have a simple table definition. Say you wanted to allowed users to own collections of things, maybe collectible cards, and track those in the database as well. You ought to have foreign key constraints on the database so that collections couldn’t be owned by users that didn’t exist or consist of cards that didn’t exist or were made up.

In the case where foreign key constraints are desired or other more complex checks should be made, the preferred method is to write a deftable definition in addition to the class and then create the table with (create-table 'class) or (create-all-tables) if you have several tables. This would make for nasty code duplication since you’d still need a dao-object class to interact with the tables as nicely as possible. Thankfully, there’s a macro to clear the situation up and import the simple parts from your dao-class specification. A possible deftable for the collection class might look like this:

(deftable collection
  (!dao-def) ;; Import the existing info from the dao-class definition.
  (!foreign 'user 'username) ;; Ensure that the username that owns the collection exists in our user table.
  ;; Ensure that each card in a collection has a name and edition corresponding to a card in our database.
  (!foreign 'card '(card-name card-edition)
	          '(name edition)))

Of course, if your tables are already created and you just want to access them or you want to create them at the psql prompt, you don’t care about any of this. Hmm…I guess that’s supposed to go at the top. Anyway, a more careful and thorough reading of the documentation would’ve shown me this but examples are nice and here one is in case anyone googles around for it like I did. As far as I can tell, this is the preferred current approach for table creation. Corrections welcome and thanks to Marijn Haverbeke for writing postmodern. It’s been wonderful so far.

After a long absence…

26-Mar-10

It’s been a long time since I’ve posted. I meant to take it easy on myself with blogging this year but not that easy. It has been a damn crazy year so far though. Thankfully, I’ve had a good time writing code and picking up an old hobby or two. I’ve been playing Magic the Gathering again (I know, I’m a nerd). The sun is out and my skateboarding is markedly less crappy than a few months ago. But I miss writing.

Today I’m posting a piece I started a few weeks ago. I’m still not quite pleased with it (it seems a bit over the top) but I probably never will be. I think I’ll just call it “Words”.

I remember having words and
never knowing whence they came.
I often stumbled into them, embarassing myself
before greater edifices to literature.
Though I had no hope of greatness, of poetry,
I miss that corpus into which I could pour:
childish thoughts and old desperation,
longings and abstract hopes for things
I even still cannot name.

Where did my words all run off to?
I used to fit into them so nicely.
A bit of my foot in this one, a leg in that one.
Even a nice place for my fingers to keep me
from grasping at everything, insatiable.
I wore them around proudly, pointing to each
in turn and saying, “Look! Here. This is who I am.”

But these days I am naked and scared to venture outdoors.
I still run into words sometimes.
A few in the sink with the dirty dishes,
others left in a coat pocket with a crumpled bill.
Yet it is only by accident we are in the same place.
My words are on fire and that’s how I’ve been.

PS: You should all pay some attention to the new albums “Gorilla Manor” by the Local Natives and “Tourist History” by Two Door Cinema Club. They’re helpful, also good for the spring season. Cheers.

PPS: Special thanks to Don Gerz and Max Kelley for accidentally providing motivational material to keep blogging.

Ye Olde Personal Update

17-Feb-10

2010 has, to date, been an odd year. I’m not entirely sure what I expected but I had something more idyllic in mind. The last two weeks, I’ve been struggling mightily with some personal issues and come to recognize some character flaws in the process. That’s never fun. In particular, I have some communication problems and there are scenarios in which I simply shut down. Without warning or even conscious recognition, I withdraw and disengage. There are some rough similarities in these scenarios but not enough for me to figure out something conclusive about causality. The flaws caused trouble in both my personal and academic life in late January and early February.

My actual school courses have been going well so far but I’ve been screwing up my internship and am in the process right now of getting caught back up in that department. It’s quite embarrassing as this is something I had looked forward to and involves people that I look up to. Murphy’s Law applies to timing of our character flaws and communications problems too, I suppose. Valentine’s Day was nice at least. I enjoyed preparing some really lovely Steaks with a Cognac-Peppercorn sauce. Clearly, my life is not *too* hard.

To try and keep that positive note, I’ll end with a few things that have made me happy lately:
- Music by Local Natives, Band of Horses, Ametsub, Massive Attack (Heligoland is pretty solid),  Fleet Foxes, Jon Hopkins, The XX and Miles Davis’ Flamenco Sketches.
- Writing wrappers for web services/APIs is (so far) reasonably straightforward and fun. It’s nice to know that there’s a ton of good data out there waiting for neat uses to be made.
- I have ideas and a desire to contribute to way more programming projects than I have time for. Some are others’ projects. Some I’d start myself. I maintain that this is a good thing as long as I stay focused on what’s on my plate and finish one thing at a time.
- I have ideas for future blog posts and github uploads. Still, I’m sticking to my “no pressure blogging” schedule for 2010. There are more important things than…well, this self-aggrandizing whatever it is. That said, I’ve always been surprised that I find my blog so useful for remembering where I was, what I was thinking and what I was struggling with years later. Some days it’s the only way I can convince myself I’m moving forward.
- Factor is a nice language and I’ve spent a few hours playing with it again. There are trivial and non-trivial things I like. The biggest thing is the exceptional interactive nature of the language and how well integrated it all is. From a design standpoint, I just appreciate it. It seems to get a lot of things, compromises…right. More on that another time. Factor 0.92 was released today! It’s been 2 years since the last release and I’ll look forward to helping test a few things before 1.0 and keep hoping for native threading before 2.0. :)
PS: The Factor logo is a raptor! How has Randall Munroe *not* written an XKCD comic about this?
- I’ve finally found a few people (3) at school that are legitimately interested in programming and care about it. It’s taken over a year. That’s far too damn long.

A Small Update

08-Feb-10

It’s been a while since last post. I’m going to try to maintain that this year. If I don’t have something worth putting up, screw putting something up. Maybe there will be some longer, better written essays as a result. For now, I’m putting up poetry which I haven’t done in a *long* time. Wrote this one on the bus to school. Feels okay. PS: I’ve really fallen in love with Band of Horses first album.

Dopplegangers
I can only see another in you and
how she must wind up in 10 years,
though I barely knew her.

So you have a husband, two daughters.
Biscuits in a cafe on a cool Sunday.
A man seemingly earnest, invested.

Is this what becomes of us?
I remember it differently.
Lazy promises I made myself
about who we each would be.

Please, let us remain ever becoming.

Paktahn 0.8.2 and other news…

13-Jan-10

The last week has been thoroughly insane in ways both good and bad. As a gift, I had my thoroughly aged Nokia 6010 replaced by a shiny new Nexus One. Much as I would’ve liked an N900 they aren’t subsidized by any carrier and so will remain out of my price range. I’ve also switched service to T-Mobile and thus far been quite satisfied. Then again, coming from a phone without a data plan I have no way of evaluating the 3G I’m getting.

The holidays were good. I have a skateboard again so when the weather clears up I can get back to enjoying that.  Time with mom was really good as was some peace and quiet and time to reflect. I took the opportunity to discover some new music as I usually do and also to read two novels by Vernor Vinge that I thoroughly enjoyed: A Fire Upon the Deep and A Deepness in the Sky. Careful, those wiki links have spoilers. As for the music, I’ve considered compiling a top 5 favorite albums of 2009 list but haven’t gotten around to it. Besides, last.fm should tell you most of that. I will say I’ve been deeply enjoying Jon Hopkins, Ametsub and Minus the Bear this week. It’s kind of an odd mix.

Moving on, I got back into code last Tuesday after a long holiday absence. I really needed the break to recharge. 2009 was a full year. I spent the bulk of the second half of last week and the weekend writing code, reading code or screwing around with configuration files…which are all things I enjoy a good bit. Over the break I had fooled around with a new window manager (StumpWM in lieu of XMonad) and started using clbuild instead of asdf-install. I also spent a little time adding a lot of projects to clbuild in case I felt like playing with them. In the course of all this fiddling, I made a fresh archlinux install in a new partition with essentially nothing but Lisp and C compilers, a tiling window manager, Chromium, Emacs and a music player. To some degree, I’m fleshing it out still. It’s a dumb diversion but every now and then I just have to rip my system up a little. It’s hard to explain.

On Sunday, after 3 months of work Leslie and I finally made the Paktahn 0.8.2 release. For all intents and purposes, the wait was worth it. A lot of new features and fixed bugs are present but there is still so much on my Paktahn.todo list. And of course there are bugs to fix. It’s hard to explain why I’m so invested in Paktahn. Part of it is the work I’ve put in to date, part of it is how pleasant it’s been working with Leslie and how much I’ve learned. Another large part is that there is great joy in having written some part of my day to day software and having a (relatively) deep understanding of it. It’s kind of silly because AUR Helpers are a dime a dozen (or two dozen) but I’m still having fun.

The Paktahn release was not without some drama though. Almost immediately after the release I started having odd issues building paktahn with sbcl. The resulting executable would exit as soon as you ran it complaining of a fatal error and a lost gc invariant. Not what you want to see. The bad thing was the error was intermittent and I couldn’t isolate the cause. I had issues with it in my old archlinux install as well as the new one, with old and current checkouts of my code and with a checkout of Leslie’s tree. I’m pretty sure I tried it with a fresh, recompiled sbcl and also tried removing all fasls and recompiling. I got very confused in the course of all that trying to figure out what happened. I should’ve been taking notes. At this juncture, it builds fine again and I can’t get it to act up. :( Ah, well. At least I can get back to developing. It certainly gives me some impetus to finish the CCL port I started Dec 28th.

So on to school this semester. I’m interning at a company called Kloudshare and have begun work on open sourcing a portion of their code. It’s good fun and I hope to have more to share on that note very shortly. The administrative person I spoke with before break failed to get the internship registered in SPSU’s system though so I spent a good deal of Tuesday getting that worked out with her. Then I had the unpleasant experience of learning that online courses are *substantially* more expensive than offline ones. Apparently, the state doesn’t subsidize them because they can be taken advantage of by anyone or something like that. In my case, I was just trying to avoid an hour and a half commute both ways and try to find more time to code. I guess you really can’t have it all. Now I have to jump through more financial aid hoops. Joy.

Soon I hope to have some code to show here. Maybe I’ll spend 10 minutes and just throw my dotfiles up on github for the hell of it tomorrow. Other than that…I miss long form writing, poetry, essays…but my focus is elsewhere. Plus I’m tired. The rest will have to wait for another day.

2010 Already?

01-Jan-10

It’s hard to believe it but the new year is already upon us. 2009 was a big year. Dad’s death was a serious blow but I have to recognize that a lot of good came my way too. I started genuinely programming, moved into an apartment with Teresa and finished half of a BS in CS at SPSU. One more year like last year and I’ll at least have a diploma…but the further I go down the programming rabbit hole, the more ignorant I realize I am. It’s not like I’ll even approach competence at the end of 2010. Oh, well. That’s how it is and I can deal with that. Hell, I think that’s part of why I like what I’m doing.

Speaking of programming, I haven’t done near enough of that over the break but I’m trying to get back on top of things this week before classes start. At least my classes this semester require code. I also have a cool internship lined up. Yet another reason to get coding. I made a number of resolutions last year, some successful, some not. I blogged regularly but I didn’t exercise enough. I maintained a 3.5 or better GPA the first two semesters but didn’t excel during the fall. Some of that may have had to do with Dad’s death, I really hadn’t expected him to die in 2010. I think more of it had to do with burnout (on SPSU, not CS) and not liking any of my classes/professors. I also spent virtually no time reading HTDP and little time sampling my music.

My predictions weren’t half bad. I was wildly optimistic about the drop in SSD prices but much closer than I would’ve guessed about the game console war being almost a draw by the end of 2009. Apple has continued to infiltrate the mainstream but I think Linux had more progress in 2008 than 2009. Sadly, neither of my IP Law predictions came to pass and that area needs progress as much as ever.

I’m not going to bother with predictions for 2010 or resolutions either. I think it’s better to work, enjoy what I’m doing and see where it takes me. Happy New Year. And good night.

Vacation Time

17-Dec-09

At long last, this year is drawing to a close. I’ve ended my semester on a mostly positive note and am looking forward to the Christmas holidays. Why you ask? Well, for a few reasons.

There are people to catch up and hang out with. I’ve got books to read: A Deepness in Sky and maybe Diaspora. There may be some light video gaming between FFXII and Borderlands. But mostly, I have projects. I need to dive in to the code I’ll be working with on my internship next semester…but not just to get a head start. The company I’ll be working with is working on some pretty cool stuff and there are plenty of places where even a naive fellow like myself could be of help, provided I don’t get in people’s hair/workflow too much. I want to try to get a Paktahn release out by Christmas with Leslie. That means giving a good read through of the analysis of why SBCL’s PKGBUILD fails on x86_64, fixing it and getting the result to juergen. Also for Paktahn, I want to start work on a branch using Unix-options for CLI argument handling and look over/improve a proxy branch I’ve been working on. Maybe a unit tests branch, as well. Besides that I have some programming books I’d like to read a bit of (and you have to do the exercises!) and various other small projects to toy with.

All that has to wait until at least tomorrow though. I’m taking today off to be with my girlfriend and relax a bit. She leaves Sunday at which point I’ll go full into project-mode, I expect. :)

There’s probably more to say here but I’m forgetting whatever it is…and my rice is ready. Cheers.

Unwind-Protect

09-Dec-09

The semester is finally drawing to a close. I had my last class sessions today. Exams are on Monday and Wednesday followed by a much needed break. It’s been a long 3-semester year with more twists and turns than I could’ve guessed. No matter what happens, the break will be here soon.

I have a variety of projects to work on over the break. I have one code base to get familiar with and another to get features and bug fixes written for. I also need to finally get around to writing the successor post to On Minimalism. Between the writings of Brian Hurt, Paul Snively and Roly Perera I’m getting a better idea what my thoughts and issues are and can hopefully come up with something good to say. I also might push out another “redlinux” release for the hell of it and have various other pet projects.

Now, for the recreational stuff. I’m going through another exploratory phase in my music listening and need to filter through some of the stuff I’ve pulled down from the vastness of the net. I just finished the first fiction reading I’ve done since…oh…Summer 2007? It felt good to read for pleasure again. I read Vernor Vinge’s A Fire Upon the Deep and was thoroughly impressed. I’m looking forward to reading A Deepness in the Sky over the break.  Maybe Rainbow’s End or Greg Egan’s Diaspora if I’m feeling up to it. I can say that recharging my batteries will be my priority for whatever remains of December after finals. That and figuring out where I am in life and sensible goals for the next year. Of course, spending some time with Mom will figure prominently as well.

Another Home Stretch

29-Nov-09

I’m way behind on my blogging schedule. It’s been over two weeks since the last one.

It’s hard to believe 2009 is almost over. It’s been an overwhelming year with a fair amount of negatives (dad’s death, girlfriend’s dad’s death) but also a lot of positives (back in school with good grades, a nice apartment, fun programming projects, an internship for 2010 and a deepening relationship with the lovely Teresa Finn).

This semester has been much harder on me compared to the last two for reasons I won’t elaborate on here. My last two big papers are due by tomorrow and Dec 4th, respectively. Then I’ll be pretty free until exams on the 14th and 16th. I’m almost there but the end seems wildly far away. In part because I’m having trouble focusing on doing what I need to do today. I’m preoccupied with all kinds of concerns about graduating on time, finances, finding a job, where I’ll be moving and other things that just flat out don’t matter right now.

Though there are personal projects I’d love to work on, I may be a vegetable most of this holiday season.

Back to paper writing…