Re: Storing Configuration settings for a database?

From: Eric Ridge <ebr(at)tcdi(dot)com>
To: dante(at)lorenso(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Storing Configuration settings for a database?
Date: 2004-01-26 16:05:35
Message-ID: 79E8C016-5019-11D8-88CF-000A95BB5944@tcdi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Jan 26, 2004, at 6:47 AM, D. Dante Lorenso wrote:
<snip>

> If I use two columns instead like KEY / VALUE, then all values must be
> of
> the same datatype (probably VARCHAR) and setting or reading values will
> require casts and parsing of data constantly by applications using the
> configs.
>
> Who has built something similar to what I'm attempting, and what do you
> recommend?

heh, I'd say use XML (and Castor to autogenerate objects for your
configuration schema), but since you're talking about postgres...

Using the JDBC drivers (dunno about the other interfaces), you can
actually call ResultSet.getInt(), .getLong(), .getFloat(). getXXX() on
a column that is defined as a character field (varchar, text).

CREATE TABLE settings (
section varchar(8),
key varchar(255),
value text
);
INSERT INTO settings values ('sec1', 'mykey', '42');

Then just do this:

Statement stmt = _conn.createStatement();
ResultSet rs = stmt.executeQuery("select value from settings where
section='sec1' and key='mykey'");
rs.next();
int value = rs.getInt("value");
stmt.close();

In reality, I don't know if this is a good idea or not as I suppose
you're relying on an implementation detail of the JDBC drivers, but it
works.

One of the nice side benefits of this is that if you call .getInt() for
a value of, say, "beer", you'll get a nice
java.lang.NumberFormatException, so it'll be rather easy to trap and
workaround misconfigurations.

eric

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Greg Stark 2004-01-26 16:13:32 Re: FAQ (disk space)
Previous Message Richard Huxton 2004-01-26 15:51:15 Re: Casting varchar to interval.?