Setting up SqlKorma with Postgresql on Windows 7

SqlKorma is a really nice Clojure DSL for handling things like selects, inserts, but not deletes. Ok, so that’s a lie, it can do deletes too. What would be really useful is if I had a project already setup for all to view. Maybe even something up on github. This is totally not a link to it.

For this walk through I used the lobos_example project as a start since it already had the table creation stuff in it. (This is the walkthrough for Lobos, which is also a walk through for setting up Postgresql.)

First off, if you are using the lobos_example project (if not, this post will walk through the important parts in the komra_example project), there is test that needs to be shanked.  It’s in the test/lobo_example/core_test.clj file:

Also get rid of the old database setting “open-global” in the lobos/config.clj file:

The next step is to add the dependency for SqlKorma to the project.clj file in the project root folder.  Depending on how old this post is, the version may not be correct.

Now it’s time to create the entities that will match the already existing tables.  If you’ve never used an ORM before, this is simply spelling out to SqlKorma what the tables are, their columns, and their relationships.  However, this is not done with any SQL statements.  In fact, the point of this file, and SqlKorma in general, is to create a high level representation of the persistence layer.  Whoa, that got serious fast.  Basically, you are using class like entities so that you can easily query, or persist stuff, without caring about anything database wise.

So anywho, here is the entity file:

A quick note to those new to Clojure, like uuuuhhh me, :use statements can be consolidated to the root namespace.  So instead of:

[korma.core]

[korma.db]

You can do this:

One other note is the example of a relationship.  The symbol “belongs-to” is needed if one entity is owned by another. This being a many to one relationship between many posts, and one user:

One file to make.  Basically it’s a test file that is run to make sure that an insert, select, and delete work:

Nothing all that complicated.  As you can see, there is an insert, assertion (is (empty?….), and a delete.

One other note for those like me who are just picking up Clojure, there are two :use/:require keywords of note in this other note…note.  If you look at the :require block you’ll see the :as keyword.  This is basically an alias to help with keeping typing down.  There might be other reasons, but I’m not the one to ask.  So don’t.  Not kidding.

The second keyword is the : only keyword (So annoying that I can’t put the : near the o since it makes a :o).  This allows you to only use the parts of the namespace you need.  It wasn’t necessary in this project example, but I threw it in anyhow.  So live with it.

Ok so now everything is ready (Pretty easy huh?).  All that’s left is to prove that I am awesome, and now you have a small bit of said awesome.  Just open a command prompt (windows key; type in cmd; hit enter), and navigate to the project root directory.  Then type lein test.

OH EM GEE FAILURE!!!  That’s right, you just failed to win.  Or won by failing.  Or something actually clever.

As you can see, the reason it failed is that I had the test asserting that the select won’t bring back anything.  However, since I had the insert command, it brought back one user.  And that’s a working SqlKorma example.

Clojure on Windows 7 with Leinigen

If nothing else, this is just a checklist for myself that I’m allowing you, due to my giving nature, to use if you need. This is a very simple bullet point thing. Got help from this post.

Get Java EE:
This is the current link as of this post (11/23/2012)

Create an environment variable:
– Hit the windows key
– Type in envi
– Choose “Edit the system environment variables”
– alt + n (Environment Variables)
– alt + w (New)
– Variable Name: JDK_HOME
– Variable Value: C:\Program Files\Java\jdk1.7.0_09\bin (May be different if you are not running 64 bit windows)
– Tab to “OK” or click it
– alt + s (System variables)
– scroll to “Path”
– append ;%JDK_HOME%;
– Enter (Click OK)
– Enter (Click OK)

Get jar for lein:
This is the current link as of this post (11/23/2012)

Get Wget:
This is the current link as of this post (11/23/2012)

Place both in:
– c:/lib/lein

Create environment variable: (Same procedure as before)
– Variable Name: LEIN_HOME
– Variable Value: c:/lib/lein

Open a command prompt and go to the Lein directory:
– Windows key
– type cmd
– hit enter
– cd c:/lib/lein

Run two lein commands from the command line:
– lein self-install
– lein repl

Those two last commands should get you everything you need to run the Clojure Repl.

Quick Link – VS 2010 Dark Styling

So after a long day of working outside, I came in to code… Only to be greeted by a large white screen…

OH EM GEE… My eyes were killing me… And I thought… I AM GOING BLIND…

Then I thought, oh crap, maybe Sean was onto something…

So I went Googling around and found the Son of Obsidian and all I can say so far is, damn, my eyes don’t hurt and Sean was right… but we don’t need to tell him that.

Check out this link (http://studiostyl.es/schemes/son-of-obsidian-with-resharper) for a dark theme that works with Resharper AND Razor (and XAML can come too).

THE END

Clojure: Default Unit Test Setup

Code for this project is on github.

So you want to test in Clojure, huh? Well good thing you found this oasis of awesomeness. Getting a test up and running is pretty easy, but involves a few simple steps that I will stretch out to the point of pain.

First off, start a new project. Again simple. Just let Linegen to do if for you:


Doing this should create a new project space age tubular frame:


Still with me? Good. Now for the test creation.


As you can see, I added a file with a test. What you might notice, or cause panic, is that I don’t have a file to test with this test file test. And that’s ok, because we’re @#$%ing this $@#% up Test First style. Basically, create a test, and then create what it’s supposed to test. I won’t get into Test First here, but just wanted to calm your concerns.

Now to run the test. This is done by opening up a command window, navigating to the root folder for your project (It will have a project.clj file in it), and typing

	lein test

Ruh roh. Looks like that file that isn’t in existence can’t be tested. Weird, right? Oh well, time to create it.


There are a couple points at this eh point. You will see that I added a file named “simple_math.clj” to the “test_example” folder.

Something I found out while I was creating this little gem of a tutorial; Apparently the convention for a folder is to use a _ where a class in a file uses a -. So as you can see, the folder “test_example” translates the namespace part “test-example”, and “simple_math.clj” becomes “simple-math”. From what I can tell, since I am pretty new to Clojure, lienigen will try to resolve the namespace of “test-directory.simple-math” to “test_directory/simple_math”. I assume this is part of the “convention over configuration” school of thought. Since I come from a .net background where conventions just don’t exist, it caught me off guard.

Anyways, since that is done, it’s time to run the test again.


One failure? Oh yeah, the junk test created by lienegin. Well, just get rid of that:


And run it again:


And things are looking a lot better.

Use of Ruby lambda expressions as method parameters like C# ones

So as I transition from the world of C# to Ruby due to a change in employment (And that I have grown to hate C#), I’ve been spending time getting used to certain concepts I’ve used heavily in C# done in Ruby. One big one is the ability to pass anonymous methods into other methods. So something like this in C#:

 public void LambdaHolder()
  {
    MethodThatUsesTheLambda(x => { Console.WriteLine(x +1));
  }

  public void MethodThatUsesTheLambda(Action<int> passedInMethod)
  {
    foreach(var item in Enumerable.Range(0, 10)
    {
      passedInMethod(item);
    }
  }

When done in Ruby it looks like this:

 def lambda_holder
    method_that_uses_the_lambda (lambda {|x| puts x +1}) 
  end

  def method_that_uses_the_lambda (passed_in_method)
    (1...10).each &passed_in_method
  end

The main thing is the & character. This allows the each method to recognize passed_in_method as an expression it can call and pass an argument to. Essential the same as:

  def method_that_uses_the_lambda (passed_in_method)
    (1...10).each {|x| passed_in_method.call(x)}
  end

Why would you want to do that? Readability if nothing else.

 def method_that_uses_the_lambda (will_output_a_number)
    (1...10).each &will_output_a_number
  end 

Compared to:

 def method_that_uses_the_lambda (will_output_a_number)
    (1...10).each {|x| will_output_a_number.call(x)}
  end 

Yeah… so there it is… I got nothin’ else… this is really awkward.