MS SQL Reserved Keywords and Hibernate

Surprise of the day, it turns out that User is a reserved keyword in T-SQL. You are still allowed to use keywords as identifiers, but you will need to escape them with square brackets:

[code lang=”latex”]
CREATE TABLE [User] (Id int);
[/code]

So far, so inconvenient. But things start getting really pretty when you clicked your schema together with SQL Server Management Studio, which will hide the keyword restrictions from you. Unfortunately Hibernate is not equally generous, so if you go from there and use your generated Hibernate mapping, you will be presented with a beautiful exception:

[code lang=”java”]
org.hibernate.exception.SQLGrammarException: Incorrect syntax near the keyword ‘user’.
[/code]

The solution for my case was to escape the table name in the related User.hbm.xml to table=”[user]”. Mildly annoying caveat: now the file has to be edited manually each time you generate your mappings, since there is no option to wire this type of adjustment into the reveng.xml.

Ignoring Input to a Text Field with jQuery

The other day I was trying to use an input field for display only: the user would select couple of elements from an overlay box and, once their selection was completed, be presented with a generated output in the input field. This should be for overview purposes only and the user should not be able to type manually into the field; a scenario much like the one covered by the jQuery Datepicker. My first reflex of setting the fields disabled attribute unfortunately did not work well since inputs with disabled=”disabled” will not only drop the onClick event but, even worse, be ignored for the submission of the surrounding form. However, it turns out that there is a jQuery one-liner for the problem:

[code lang=”javascript”]
$(‘#month_range’).keypress(function(event){event.preventDefault();});
[/code]

This will essentially ignore any key events on the month_range input field. Be aware though that it will as always just protect the user a bit from themselves and not in any way relieve you from validating the input (just try to c&p something into the field…).

I Will Do It By Hand One More Time

If you catch yourself saying this about a task for the third time, it is time to write a script for it. My contribution for today: when I want to process a large amount of photos my workflow usually starts with sorting through the contents of my SD cards and deleting the pictures with which I am not happy. If you are working on a rather dated computer as mine, having the corresponding RAW files in the same folder will cause you a headache with most picture viewers, especially when you try to flip through the files quickly. Moving them to a separate folder, however, will make you do the work twice. The following PurgeRaw script fixes this problem:

[code lang=”perl”]
#!/usr/bin/perl -w
use strict;

print “Searching for RAW files without matching JPG preview…\n”;

my @jpgs = glob(“*.jpg *.JPG”);
@jpgs or die “There are no preview JPGs in this directory.”;

my @raws = glob(“NEF/*.NEF RAW/*.NEF”);
@raws or die “There are no RAWs we could purge.”;

foreach (@jpgs) {
$_ =~ s/\..*//;
}

my @toDelete;
my $raw;
foreach (@raws) {
$raw = $_;
$_ =~ s/\..*//;
$_ =~ s/^.*\///;
if(!($_ ~~ @jpgs)){
push(@toDelete, $raw);
}
}

if(@toDelete){
print “Looks like we should delte the following RAW files:\n”;
foreach(@toDelete){print $_ . “\n”;}

print “Proceed with deleting? (Y/N): “;
my $answer = ;
chomp($answer);

if($answer eq “Y”){
print “Deleting…”;
foreach my $file ( @toDelete ) {
unlink $file or warn “Could not delete $file: $!”;
}
}
elsif($answer eq “N”){
print “Aborting without deleting anything.”;
}
else{
print “Wrong input – aborting without deleting anything.”;
}
}
else{
print “Did not find any RAW files eligible to be deleted.”
}
[/code]

This is obviously far from being anything sophisticated, but it does the job. If needed, you can grab the code from my github repository, that is also where I will add the next scripts when I catch myself counting to three again.