LispCast Episode 4

November 10th, 2007

Episode 4 is ready!

In this short and sweet episode, I add database persistence to the links using clsql and MySQL. The Reddit clone is shaping up.

Only a few functions need to be changed since we refactored it in the last episode.

As an exercise for the viewer, I have left the acceptance tests unchanged. They need to be updated to work with the new storage mechanism.

And for those viewers who are coding along with the video, don’t forget to create your MySQL database and run CLSQL:CREATE-VIEW-FROM-CLASS.

Watch the video and download the main code file with the acceptance tests.

Next episode: Users (link ownership, profiles, etc)

New software used in this episode:

Popularity: 3% [?]


13 Responses to “LispCast Episode 4”

  1. Birlinn on November 12, 2007 6:08 am

    Nice… a useful tutorial on clsql… & it’s nice to see the emacs command logging too!

  2. mathrick on November 12, 2007 10:19 am

    There’s a nice bug visible at 13:00, right when you say “so now the points do work” :). Notice how the score goes 0 -> 1 -> 0 -> *2*.

  3. Birlinn on November 12, 2007 10:33 am

    I think the 0 -> 2 as seen on the screen is the result of a double click rather than a bug…

  4. admin on November 12, 2007 11:09 am

    Yes. I remember that part. It is a double click.

  5. Seth on November 12, 2007 3:10 pm

    Another awesome tutorial. Thanks for everything, especially the command logging.

  6. Joost Diepenmaat on November 12, 2007 6:20 pm

    I’m not sure if you added a transaction in the right place to fix this, but when adding a link, doing an INSERT and then getting the MAX(id) creates a race condition.

    Also, MySQL has it a specific feature where you can get the latest inserted ID for your specific database handle, so you won’t need to use transactions or get MAX(id), but I haven’t checked out clsql to see if that’s supported.

    Cheers,
    Joost.

  7. admin on November 12, 2007 11:21 pm

    Ha! Good eye! You caught me! As I was adding sound to my video, I realized that I had forgotten to add the transaction around saving a new link and fetching the id. There should be a CLSQL:WITH-TRANSACTION around the whole thing.

    About the use of MAX(ID): I did find that feature in the documentation, but it didn’t work as described. Nothing I tried worked better than MAX(ID), so I just stuck with it.

    Thanks for the comments!

  8. brad on November 19, 2007 1:45 am

    Bravo, keep up the good work!

  9. Andreas Wagner on December 1, 2007 7:29 pm

    Great job!

    It would be great if this was on iTunes though.

  10. rohan on December 5, 2007 1:33 am

    Fabulous web casts, thank you.

    Nice and practical, and easy to follow. For me at least you are
    achieving your goal of making lisp very approachable. I do have a
    suggestion, selfishly motivated, and that is packaging things up and
    asdf-ing them.

    Name spacing is important (unless you live in php world, in which case
    it is still important, but there is little you can do about it, been
    there, done that, cried the tears), and cl has a great facility for
    handling this, but it is not very user friendly, and is almost
    definitely a source of frustration for newbies to lisp.

    Add to that, asdf possibly giving additional errors… It is such a
    convenient facility, and allows you to have a neat package at the end
    that handles everything, so you can let others use it.

    Anyway, it is a thought, and thanks again for all the fabulous work.

    Rohan

  11. William Roe on December 5, 2007 6:56 pm

    Great tutorial.
    Now. Anyone whom, like me, has tried to go through it all with Postgres rather than MySQL might come up against the problem I did - the “serial” data type and getting the latest ID when inserting a record won’t work in the same way. The problem is that CLSQL complains that you try to insert a record with NULL in the id field when it’s got a not-null constraint. I can’t find any way to make CLSQL *not* include the id field when creating the record, but the code that works is as follows:

    (defun add-link (link)
    (with-db (db)
    (clsql:with-transaction (:database db)
    (when (null (id link))
    (setf (slot-value link ‘id)
    (first
    (first
    (clsql:query “SELECT NEXTVAL(’link_id_seq’);”)))))
    (clsql:update-records-from-instance link :database db))))

    In other words - grab the next ID first, then insert the record.

  12. LispCast Episode 5 | LispCast on December 10, 2007 2:48 am

    […] Plus, I revised the acceptance tests behind the scenes to use the new database backend. The tests should work for the code from last episode. […]

  13. admin on December 14, 2007 2:32 pm

    @rohan Thanks for the encouragement. I’m planning on making a video showing me package everything up. It will probably be the last LispCast in the Reddit Clone series.

    @William Good work.

Trackback URI | Comments RSS

Leave a Reply

Name (required)

Email (required)

Website

Speak your mind