Posts

Showing posts with the label programming

Easiest way to get SQLite to put in timestamps

After doing some more digging (must have been blind before), I found that SQLite supports a default timestamp at insert. Here is an example of a create statement using the default with SQLite CURRENT_TIMESTAMP keyword. create table user ( id integer primary key, username text not null, password text not null, first_name text, last_name text, created text default CURRENT_TIMESTAMP UNIQUE(username) ); That is much easier that my previous attempt. Not sure how I missed it. But it doesn't solve the missing now() function for updates. ( did find a language solution for that in Perl but that is for another post). In that case, it will still require the use of datetime('now') . Reference: SQLite CREATE TABLE reference .

2 ways to get SQLite to put dates into columns

2 ways to get SQLite to put dates into a column. insert into mytable values( null, datetime('now') ); insert into mytable values( null, strftime('%s', 'now')); The first one inserts a row somewhat like this: 1|2009-03-10 18:47:46 The second inserts an unix timestamp: 2|1236711411 It might be best to use that unix timestamp with an integer column type for dates since SQLite doesn't support a datetime one. It makes comparisons and ordering much easier: select * from dt where lu > strftime('%s', '2009-03-10'); Output of: id|lu 6|1236643200 2|1236711411 3|1236711516 4|1236711518 5|1236711519 But the formatting is pretty ugly. :-/ Hey what about formatting it within the select with the SQLite datetime function: select id, datetime(lu, 'unixepoch') lu from dt order by lu; id|lu 6|2009-03-10 00:00:00 2|2009-03-10 18:56:51 3|2009-03-10 18:58:36 4|2009-03-10 18:58:38 5|2009-03-10 18:58:39 Better but having to add that to each select is kin...

Adding http request/response logging to SOAP::Lite

Sometimes I just want to save the xml versions of the SOAP requests and responses (generally to share with someone else). The SOAP::Lite perldoc page points to using SOAP::Trace . It has lots of good stuff but its a bit heavy. And in my case, I'm trying to figure out how to log response and requests over http so the following will hopefully help simplify my next search. :) here is my approach, first the code (all good things start in code :) : package SOAP::HTTP::Logging; use warnings; use strict; use Exporter; our @EXPORT = qw( enable_logging disable_logging soap_log_file log_message ); our $logfile; our $logboth = 0; our $log_request = 0; our $log_response = 0; # setup global options sub enable_logging { $log_request = $log_response = 1; } sub disable_logging { $log_request = $log_response = 0; } sub soap_log_file { $logfile = shift if @_; return $logfile; } # logging sub sub log_message { my $in = shift; # SOAP::Lite pushes in the object its called on # only log ...

vim - insert current filename

When writing comments in source code or for some other reason than I can think of right now, I like to include the current filename into current vim session. How? Enter insert mode use following key sequence: ctrl-r % POOF! You have the filename (it does include the path). In vim % is the current filename. It is very useful when writing mappings or running commands from the : prompt.

vim makes me pasty when pasting

For a while its bugged me that when I paste into a vim session with all nice options turned on, it formats the crap out of it. At some point setting set noautoindent stopped fixing this problem. Thank goodness that I found a solution, it probably exists for years but with vim sometimes knowing the right verbage is the key to a finding how to do it. (its probably been there since version 5.0) Here is how it works for me: pre paste - :set paste paste in code/snippet/sql/guinea pig :set nopaste Man that makes my life so much easier...

A new must use module - Test::Exception

I write quite a few unit tests that have methods or subs that throw exceptions (like most good code should... this rule has numerous good exceptions! hahaha, sorry couldn't resist.). A few months ago I ran across Test::Exception . In the past I would often write unit tests for these cases like: { my $err; my $obj; eval { $obj = MyObject->new; }; ## constructor expected to fail $err = $@; ok $err, "constructor - missing parameters"; ## or for methods that aren't suppose to throw eval { $obj = MyObject->new( server => 'localhost' ) }; ## check constructor $err = $@; ok !$err, "constructor"; isa_ok $obj, 'MyObject'; } This construct became very tiring, verbose and distracts from what is really going on. (Yuck!) With Test::Exception, I can convert these annoying eval blocks into nicely contain lines. Example from above: use Test::Exception; { my $obj; throws_ok { $obj = MyObject->new; } qr/Error Message to match/, 'constructor...

Vim, Tidy and XML - how to make them work together

Background I use vim as my main IDE. I found that I often have to muck and view XML. Often this XML is from debugging logs or output that don't add whitespace (and why would they). The following is my setup for a small vim filetype mapping to automatically clean up xml using HTML tidy . Another great tool. How To First, You'll need $HOME/.vim and $HOME/.vim/ftplugin directories. Create them, if they don't exist: mkdir -p $HOME/.vim/ftplugin Second, create a new ftplugin for xml. vim $HOME/.vim/ftplugin/xml.vim Third, add mapping for tidy (I use F3) " tidy for xml -q makes it quiet map <F3> :%! tidy -q -i -xml % <CR> Now when you edit a file with a type of xml, you can press F3 your xml will be tidy'd. This does require that you save the xml file somewhere first (you'll need a valid filename). Caveats I've only used this on UN*Xs OSs I use vim 7.x, it will probably work on 6 I enable filetype support in my .vimrc with command filetyp...