12/16/2004

Object oriented KSH

I've decided to start off my programming tools page with a tip, something I've never seen anyone else implement or use, but which I find extremely useful.

I'm a pretty heavy shell user in my day to day work, and use databases a lot (Sybase in my case). I never liked the GUI sql shells, never quite got used to starting emacs to use sqsh. Dammit, I like the shell. I figured there has to be an easier way.

Sorry I can't post the code, but the trick is to write the function S:
function S {
...
eval 'function '"$1"' { (
my_isql_like_thingy '"$2"' '"$3"' '"$4"' ...
)}'
}

The idea is that I can run:
S mytag SERVER DB USER PASSWORD

and then whenever I want to run sql on that database, I need only run
mytag '... sql ...'
and I can use all the ordinary shell mechanics for editing the line, redirecting, loops, etc...

Step 2: simple enough... In your startup .rc file, autodefine a bunch of these for the databases you use most often.

Step 3: write a kick ass my_isql_like_thingy... Mine can present the data three different ways, bcp in/out raw data on stdin (streaming so you can bcp out with one sql and into something else at the same time), or list tables or procs or views, print the text of tables or procs or views. So, e.g., I can whip up simple scripties like:

for p in $(p2 print views | fgrep Equity) ; do
print "*** $p"
p2 cols "$p" | fgrep -i tick
done

in a flash...
Anyway, I find this useful. Where I work is a Sybase shop though, so even if I made a generic version who would care... Anything like this for mysql?

1 Comments:

Curt Sampson said...

I think the issue here is just bad front ends. I know that the MySQL one is poor, and the Oracle one is absolutely horrible.

PostgreSQL has a very nice one which makes it simple to do the kinds of things you want. You can specify the host, database, login name, etc. on the command line or as environment variables. Passwords can be stored in $HOME/.pgpass so that they're not visible to other users on the system. You can use a command line option to send a single query (or series of queries), or you can feed them in on standard input, or you can ask it to read a file. There are lots of different options for output.

I use the psql client extensively in an automated test system, and it works very well for me. We just need clients of this quality for all of hte other DBMSes.

12:55 PM  

Post a Comment

Links to this post:

Create a Link

<< Home