X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 80C4ED7E32 for ; Sun, 28 Aug 2005 14:07:36 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 34011-05 for ; Sun, 28 Aug 2005 17:07:30 +0000 (GMT) Received: from svana.org (svana.org [203.20.62.76]) by svr1.postgresql.org (Postfix) with ESMTP id D51D3D7D1D for ; Sun, 28 Aug 2005 14:07:29 -0300 (ADT) Received: from kleptog by svana.org with local (Exim 3.35 #1 (Debian)) id 1E9Qcy-0004xZ-00; Mon, 29 Aug 2005 03:07:28 +1000 Date: Sun, 28 Aug 2005 19:07:27 +0200 From: Martijn van Oosterhout To: Postgresql General Subject: A real currency type Message-ID: <20050828170727.GC12498@svana.org> Reply-To: Martijn van Oosterhout Mail-Followup-To: Postgresql General Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="GRPZ8SYKNexpdSJ7" Content-Disposition: inline User-Agent: Mutt/1.3.28i X-PGP-Key-ID: Length=1024; ID=0x0DC67BE6 X-PGP-Key-Fingerprint: 295F A899 A81A 156D B522 48A7 6394 F08A 0DC6 7BE6 X-PGP-Key-URL: X-Warning: Normally my messages are signed, but not on the ozTiVo list due to legacy application support X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.003 required=5 tests=[AWL=0.003] X-Spam-Level: X-Archive-Number: 200508/1427 X-Sequence-Number: 82835 --GRPZ8SYKNexpdSJ7 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable For a while I've been wondering about making a type that was really a shell around a base type that tagged the type in some way. For example, associating a currency with a numeric and complaining about additions between mismatches. Well, I did it and it's available here: http://svana.org/kleptog/pgsql/taggedtypes.html Below some examples of it in action. Yes, that's a timestamp that remembers the timezone. Neat huh? Tested on 7.4 and a recent 8.1devel so it should work for most people. Installation reports welcome. Note, this is beta software, don't run it on your production server. Thanks. Have a nice day, test=3D# select '5.6 USD'::currency + '4.5 USD'::currency;; ?column? =20 ----------- 10.10 USD (1 row) test=3D# select '5.6 USD'::currency + '4.5 AUD'::currency;; ERROR: Using operator +(currency,currency) with incompatable tags (USD,AUD) test=3D# select c1, print_currency(c1) from c; c1 | print_currency=20 ------------+---------------- 232.44 USD | US$ 232.44 21.20 EUR | =A4 21.20 -13.44 AUD | AU$ -13.44 0.01 USD | US$ 0.01 14.00 AUD | AU$ 14.00 (5 rows) test=3D# select 5.4*c1 from c where tag(c1) =3D 'AUD'; ?column? =20 ------------ -72.58 AUD 75.60 AUD (2 rows) test=3D# select t, "timestamp"(t), date_part('hour',t) from c; t | timestamp | date_part= =20 -----------------------------------------+---------------------+----------- 2005-08-14 02:00:00+02 Europe/Amsterdam | 2005-08-14 02:00:00 | 2 2005-08-14 02:00:00+02 Australia/Sydney | 2005-08-14 10:00:00 | 10 2005-08-14 02:00:00+02 Asia/Hong_Kong | 2005-08-14 08:00:00 | 8 2005-08-14 02:00:00+02 America/New_York | 2005-08-13 20:00:00 | 20 2005-08-14 02:00:00+02 Asia/Kuwait | 2005-08-14 03:00:00 | 3 (5 rows) --=20 Martijn van Oosterhout http://svana.org/kleptog/ > Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a > tool for doing 5% of the work and then sitting around waiting for someone > else to do the other 95% so you can sue them. --GRPZ8SYKNexpdSJ7 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQFDEe9PIB7bNG8LQkwRAuFIAJ9QNpPL+tqGrHmQunnnghDX+68gTgCdEG+M Mj2eMwm5ju0c4QFjklSo4ik= =ozHP -----END PGP SIGNATURE----- --GRPZ8SYKNexpdSJ7--