Month: March 2005

Oracle Ingress and Postgres

In response to this:

http://www.theregister.co.uk/2005/03/24/ingres_and_open_source/

Hi Philip,

Speaking as someone who has jumped from Oracle to Postgres, mostly for cost reasons, I’m curious about what the relationship is between Ingres and Postgres. I thought Postres was Ingres. I obviously haven’t been following it too well.

The other thing about this database is that it is a much better implementation of the relational model. I can create proper domains that describe what a thing is instead of some techie crap like varchar(27) maybe or maybe not being a soundex code (or whatever). I like this because you can see what a table is supposed to do when you look at its description. Oracle still haven’t understood this.

Apropos Oracle, a lot of my friends say that they’d be very happy with an open-source (or just virtually free) version of the old Oracle 7.3.4, perhaps with the 8.1.5 PL/SQL engine. I would say that most of us just want a reliable data store and don’t give a stuff about all of the high-end stuff. 9i RAC and 10g – who gives a toss, really, apart from the half-dozen big corps (mostly telcos) that need big databases. I don’t care about Java in the database either, quite happy running it in the middle tier where it belongs, or just using PHP because it takes a tenth of the time to do something useful without fity squnitillion XML files that are sometimes ignored in some middle-tier framework that’s too hard to use.

Don’t get me started on their Applicaton Server – was a nice usable thing and is now a pregnant elephant in a temper with more layers than an alpine climber. In the light of which I’d like to see an article looking at the relative merits of JBOSS, OAS and WebSphere in the real world. Are there any plans to do anything like that?

Somewhat off the point but never mind.

Regards

Converting Java XML to PHP XML

Thought I might make some notes about this to help the next person along.

Firstly, PHP5 on windows just has the XML SAX parser installed by default so, even though there is a PEAR XML library, you don’t need it.

Attributes

The Java classes have a org.xml.sax.Attributes class. When you get passed this you can call a method to get values you are expecting, e.g.:

1
2
3
4
5
6
7
8
  void handleTable( final Attributes att )
  {
    firstColumn = true ;
    currentTable = att.getValue("name");
    os.println("create table " + currentTable );
    // Set up markers to create the tags table later
    if ( "true".equals(att.getValue("hasTag") )) tagTables.put(currentTable,currentTable) ;
  }

In Java there is an idiom where if there isn’t a value it will return null (which is why I have the string to test against the constant string first in the equals test, so that you don’t get a null pointer error if the attribute is null). PHP, on the other hand, uses the very useful associative arrays and the function became:

1
2
3
4
5
6
    function handleDomain( $att )
    {
      os::doprint("create domain " . $att["name"] . " " . $att["type"]);
      if ( array_key_exists( "nullable", $att ) && $att["nullable"] == "" ) os::doprintln(" not null");
      os::doprintln(";");
    }

Notice that I now have to use array_key_exists to test for something’s existence before I can do anything with it.
In this class, instead of having a local variable that is set to a print stream I have created a static class that I can use to do the printing instead. I may change this again, not sure.

A lot of the O-O junk isn’t there in PHP, a class is static if it doesn’t have any methods that need to use the this variable. Purists won’t like this but I don’t care, to be honest.

Method calls

One thing to be aware of is that PHP insists on the $this→; operator in front of any class methods. If you don’t put it there it will think you are calling an internal function but won’t complain until you try and call it.

Simplification using associative arrays

This:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  void handleTags()
  {
    String table = null
    ,      column = null ;
    Object [] theKeys = tagTables.keySet().toArray();
    for (int i = 0; i < theKeys.length; i++) 
    {
      table = (String)theKeys[i] ;
      column = (String)tagTables.get(theKeys[i]) ;
      os.println("create table " + table + "_tag" );
      os.println("( " + column + " id references " + table + "(" + column + ") not null" ) ;
      os.println(", tag_id id references tag(tag_id) not null" );
      os.println(");");
    }
  }

Becomes:

1
2
3
4
5
6
7
8
9
10
    function handleTags()
    {
      foreach ($this->;tagTables as $table =>; $column) 
      {
        os::doprintln("create table " . $table . "_tag" );
        os::doprintln("( " . $column . " id references " . $table . "(" . $column . ") not null" ) ;
        os::doprintln(", tag_id id references tag(tag_id) not null" );
        os::doprintln(");");
      }
    }

Which I think is quite good. A ton less code to get to what you want to do. I think there is a similar thing to foreach in java 1.5 but nothing as easy to use as this.

All in all, for getting the job done, I think PHP wins hands down. Java is very low level and has to do everything through a class and chuck arrays of Object around. The people who developed PHP weren’t afraid of putting things like associative arrays in the core language and this saves a whole ton of code. If you want to override methods and go the whole O-O hog you can, but you have to explicitly say you are going to do so.

PHP5 has most of the Java-like constructs apart from this see http://www.developer.com/lang/php/article.php/10941_3302171_1

Got my vote

If you want to see a very coherent and logical explanation of why software patents are such ridiculous nonsense see http://www.kuro5hin.org/story/2005/3/9/93357/91614.

Can you imagine a world where the particular genius of your favorite author’s rendering of an archetypal story would violate a patent? Quite often I read a novel for the characterisation and the inner life of the characters, but if the plot was patented then it wouldn’t exist. All those fantasy novels

  1. Problem – evil in the good land
  2. Gathering of the good guys
  3. Gathering of the forces of evil
  4. The impossible quest
  5. The struggle to complete the quest (sub plots, party gets split, is betrayed etc.)
  6. Quest is complete and good triumphs (always at the last second)

I read them if the characters are interesting and well-written. I know what the plot is, the interest is in the execution! Maybe I should copyright this, only problem is I don’t want to get any money from the really bad ones because it would be too embarassing.

Imagine if physics was patented. You would be paying money to the patent holders when you read a book explaining it, instead of which the author has to acknowledge who did the work, had the idea.

I mean, what if they patented breathing?

See how silly it is?

Has Knuth patented his fundamental algorithms (well he gathered them together and validated them, but you know what I mean)? There’s a scary thought.

PHP

Got talked into looking at PHP rather than Python.

I really like it: it’s got a lot of the lovely features of the unix shell (heredocument and `), you can have nice structures like

if ( cond ) :
expr
endif

So you can block your code properly. I have always hated the C and Java thing about the closing brace not telling you what’s being closed; this can be a source of bugs that only becomes apparent when you run the code through a formatter. Of course, it also supports the annoying {} as well, which makes it nice and flexible and easy for the Java/C heads to get into. I know you can do stuff like

if ( cond )
{
// 20 pages of “stuff” (Should only be about 20 lines if you structure it properly)
} // If

I hate this though. Just looks ugly and you have to remember to do it or enforce some really annoying coding standards that people will ignore because they think you’re being too pedantic. By the way, I always place the opening brace under the if so that if you have a mountain of conditions in the if statement you can see where the executable expressions begin. In PL/SQL I do this for the same reason

if cond – no brackets, irritiating things; why do a lot of modern languages pretend to be C?
then
Stuff
end if ;

I’m skim reading the tutorials at the moment to get a feel of the language’s capabilities.

I am going to rearchitect my system away from Struts and the like: I think I can just get a move on and get the thing finished quckly in PHP. Like Paul Graham says: the power of the abstractions of a language help you to get the job done and think PHP is ideal for what I need. And there are tons of exellent tools and large well-written projects to look at. There are also lots of nice free editors (including my all-time favourite VIM).

My plan currently is to redo the XML parsing I’ve been doing in PHP (a good learning exercise). Then I will create some self-describing objects that will define how the site is put together. Then PHP will be used to deliver these objects up to the end user. Make a change, regenerate (or maybe just change the XML descriptors), site stays up. That has always been the goal. I plan to use Open LDAP and suchlike for user and resorce management.

It looks loads less hassle than Struts for a small project like mine. I’ve been trying off and on for weeks just to get an HTML form that allows me to update some data, haven’t even got to the XML rewrite capabilities yet. Time to change to more powerful tools that can help me build the tools I need before I die of old age.

Postgres vs MySQL

I prefer Postgres as a platform, even to Oracle, because it is a purer implementation of the relational model. Particularly the ability to create domains, which is central to relational theory, but Oracle doesn’t do it. Types in Oracle don’t cut it, sorry, too fiddly and hard to insert and retrieve out again. Besides, they answer a different problem about composite data types. Problem is I need to run PG inside Cygwin on my XP machine and it hasn’t worked for a while, I discoverd that I need to run something called cygserver but it won’t play. There is now a native Windows version (have a look on the site here) but I need some time to investigate it. I’ve downladed MySQL as a temporary stopgap but don’t like it. Transactions and other stuff you need are relatively new: Postgres and Oracle have had them for as long as I can remember. MySQL is (I believe) mostly in-memory which makes it fast, but what happens when things fall over? Maybe it’s just my old-fashoined, data-centric training making me want stronger medicine than MySQL, I know it works very well for lots of people. I just want something stronger that suits my purist leanings, describe a table and you can see that a given column has a type of ID or Adress_line, you know what it’s for; what use is integer or varchar2(256), where are the semantics? Also change the domain and you know all of the related tables are OK. That is the way it should be done, not by relying on a tool like Designer or some external XML document that is used to generate scripts to align domain-related columns – rubbish, a waste of time and energy.

Amazon

Whacked 25 old computing books up on Amazon, I’ll give them a couple of months to sell and then bin them. I was holding on to a load of old Unix, Prolog, C programming, Smalltalk, Forth (from 1985!). I also inherited my mother’s poetry collection, which I will sell on too, but suspect that a ruck of paperback poetry won’t sell.

Ah well, off to bed. G’night all.

PS – I suspect I’ll be lookng at Python too, maybe to replace Awk as my text-processing “quickie” tool. Not sure yet.

Mail to Paul Graham

In response to this

OK Paul

You’ve persuaded me to look at Python. I must admit I use Gawk a lot and I’ve made my windows XP machine into a Linux one by installing Cyngwin. I use Vim all the time, I find the way the vi navigation lets me get to the end of a word, or delete up to an underscore Just Works. This is by far more productive for me, even though I spend a lot of time writing PL/SQL and use the TOAD tool. I find I use TOAD for browsing stuff but need a decent editor with RE’s and callouts to Awk to be productive. I will probably migrate my Awking to Python. PL/SQL is Ada without the interesting bits.

Anyway, I half agree with what you say about Java being “for the rest of us”, but the interesting thing is that there are a lot of cool(ish) projects, particularly Apache Struts and Jakarta, that give you a lot of leverage if you are looking at solving some kind of web-based problem quickly, and it’s all open source. If I have a problem in Java I can usually find someone else has solved it in about 10 minutes using Google.

One of the guiding principles of J2EE was that people wouldn’t need to know how to program with threads, for example, and I don’t think this is necessarily a Bad Thing in and of itself. Most of us have to solve boring business problems for a living and we want well-defined architectures that will take the weight and let us get rid of them as fast as possible. I confess I’m still trying to see the point of EJB’s, other than being able to talk intelligently about them at job interviews.

So, I think that when you are being a craftsman it is far better to use more expressive languages (I’ve even worked my way through your book on ANSI Lisp and loved it) but when you are assembling stuff for boring work applications what you want is components you can assemble quickly and others will be able to follow when you’ve gone. That’s the problem, I think.