We'll start out by defining the internal form for an FCUR value. As I mentioned before, you want to store three pieces of information for each value: the name of the currency (dollars, euros, yen, and so on), the number of units, and the exchange rate at the time the value was created. Why do you need to store the exchange rate with each value? Because exchange rates vary over time, and you need to know the rate at the time the value is created.
Because you are going to use the C programming language to implement the required conversion functions, you need to define a structure[4] containing the three components. Listing 6.9 shows the first few lines of the implementation file:
[4] This is not necessarily the most efficient (or even realistic) way to store a foreign currency value, but it works well for purposes of illustration. In a real-world implementation, you would not want to store monetary values using floating-point data types because of their inherent lack of precision. You would also want more control over the format of the currency name.
Listing 6.9. fcur.c (Part 1)
1 /* 2 ** File name: fcur.c 3 */ 4 5 #include "postgres.h" 6 #include "fmgr.h" 7 8 typedef struct 9 { 10 char fcur_name[4]; /* Currency name */ 11 float4 fcur_units; /* Units of currency */ 12 float4 fcur_xrate; /* Exchange rate */ 13 } fcur; 14 15 static char * baseCurrencyName = "US$"; 16 static char * unknownCurrencyName = "???"; 17
Start by #including the postgres.h and fmgr.h header files, just like you did for the earlier examples. The fcur structure defines the internal form for your fcur data type. Store the currency name (fcur_name) as a three-character, null-terminated string. The fcur_units member stores the number of currency units as a floating-point number. The exchange rate is stored as a floating-point number in fcur_xrate.
At lines 15 and 16, you define two currency names. The baseCurrencyName is the name of the local currency. When the fcur_name of a value is equal to baseCurrencyName, the value is said to be normalized. A normalized value will always have an exchange rate (fcur_xrate) of 1.0: One U.S. dollar always equals one U.S. dollar. The unknownCurrencyName is used when the user enters a value containing a number of units and an exchange rate, but fails to provide the currency name. We'll use each of these variables in a moment.
Part I: General PostgreSQL Use
Introduction to PostgreSQL and SQL
Working with Data in PostgreSQL
PostgreSQL SQL Syntax and Use
Performance
Part II: Programming with PostgreSQL
Introduction to PostgreSQL Programming
Extending PostgreSQL
PL/pgSQL
The PostgreSQL C APIlibpq
A Simpler C APIlibpgeasy
The New PostgreSQL C++ APIlibpqxx
Embedding SQL Commands in C Programsecpg
Using PostgreSQL from an ODBC Client Application
Using PostgreSQL from a Java Client Application
Using PostgreSQL with Perl
Using PostgreSQL with PHP
Using PostgreSQL with Tcl and Tcl/Tk
Using PostgreSQL with Python
Npgsql: The .NET Data Provider
Other Useful Programming Tools
Part III: PostgreSQL Administration
Introduction to PostgreSQL Administration
PostgreSQL Administration
Internationalization and Localization
Security
Replicating PostgreSQL Data with Slony
Contributed Modules
Index