diff --git a/src/engine/sql/Makefile.am b/src/engine/sql/Makefile.am index a9d06fa4eb..dda94db3ef 100644 --- a/src/engine/sql/Makefile.am +++ b/src/engine/sql/Makefile.am @@ -26,6 +26,7 @@ EXTRA_DIST = \ base-objects.m4 \ check-objects.m4 \ design.txt \ + functions.sql \ kvp-objects.m4 \ table.m4 \ table-create.sql \ @@ -43,8 +44,8 @@ LDADD = ${GLIB_LIBS} # Some of the required C files are built with the m4 pre-processor # As a result, we need to manually specify dependencies, clean targets. -PostgresBackend.o: base-autogen.c table-create.c table-drop.c -PostgresBackend.lo: base-autogen.c table-create.c table-drop.c +PostgresBackend.o: base-autogen.c table-create.c table-drop.c functions.c +PostgresBackend.lo: base-autogen.c table-create.c table-drop.c functions.c checkpoint.o: check-autogen.c checkpoint.lo: check-autogen.c @@ -61,18 +62,26 @@ check-autogen.c: table.m4 check-objects.m4 kvp-autogen.c: table.m4 kvp-objects.m4 m4 kvp-objects.m4 > kvp-autogen.c +functions.c: functions.sql + echo \" > functions.c + echo "-- DO NOT EDIT THIS FILE. IT IS AUTOGENERATED." >> functions.c + cat functions.sql >> functions.c + echo \" >> functions.c + table-drop.c: table-drop.sql echo \" > table-drop.c + echo "-- DO NOT EDIT THIS FILE. IT IS AUTOGENERATED." >> table-drop.c cat table-drop.sql >> table-drop.c echo \" >> table-drop.c table-create.c: table-create.sql echo \" > table-create.c + echo "-- DO NOT EDIT THIS FILE. IT IS AUTOGENERATED." >> table-create.c cat table-create.sql >> table-create.c echo \" >> table-create.c clean: -rm -f base-autogen.c check-autogen.c kvp-autogen.c - -rm -f table-drop.c table-create.c + -rm -f table-drop.c table-create.c functions.c diff --git a/src/engine/sql/PostgresBackend.c b/src/engine/sql/PostgresBackend.c index 1b6c68a438..903eac4867 100644 --- a/src/engine/sql/PostgresBackend.c +++ b/src/engine/sql/PostgresBackend.c @@ -211,6 +211,10 @@ static const char *table_create_str = #include "table-create.c" ; +static const char *sql_functions_str = +#include "functions.c" +; + static const char *table_drop_str = #include "table-drop.c" ; @@ -2675,9 +2679,14 @@ pgend_session_begin (GNCBook *sess, const char * sessionid, return; } - /* finally, create all the tables and indexes */ + /* Finally, create all the tables and indexes. + * We do this in pieces, so as not to exceed the max length + * for postgres queries (which is 8192). + */ SEND_QUERY (be,table_create_str, ); FINISH_QUERY(be->connection); + SEND_QUERY (be,sql_functions_str, ); + FINISH_QUERY(be->connection); } #endif diff --git a/src/engine/sql/functions.sql b/src/engine/sql/functions.sql new file mode 100644 index 0000000000..328c4f96eb --- /dev/null +++ b/src/engine/sql/functions.sql @@ -0,0 +1,68 @@ +-- +-- FILE: +-- functions.sql +-- +-- FUNCTION: +-- Define assorted utility functions. +-- +-- HISTORY: +-- Copyright (C) 2001 Linas Vepstas +-- + + +-- utility functions to compute checkpoint balance subtotals + +CREATE FUNCTION gncSubtotalBalance (CHAR(32), DATETIME, DATETIME) + RETURNS NUMERIC + AS 'SELECT sum(gncEntry.value) + FROM gncEntry, gncTransaction + WHERE + gncEntry.accountGuid = $1 AND + gncEntry.transGuid = gncTransaction.transGuid AND + gncTransaction.date_posted BETWEEN $2 AND $3' + LANGUAGE 'sql'; + +CREATE FUNCTION gncSubtotalClearedBalance (char(32), DATETIME, DATETIME) + RETURNS NUMERIC + AS 'SELECT sum(gncEntry.value) + FROM gncEntry, gncTransaction + WHERE + gncEntry.accountGuid = $1 AND + gncEntry.transGuid = gncTransaction.transGuid AND + gncTransaction.date_posted BETWEEN $2 AND $3 AND + gncEntry.reconciled <> \\'n\\'' + LANGUAGE 'sql'; + +CREATE FUNCTION gncSubtotalReconedBalance (CHAR(32), DATETIME, DATETIME) + RETURNS NUMERIC + AS 'SELECT sum(gncEntry.value) + FROM gncEntry, gncTransaction + WHERE + gncEntry.accountGuid = $1 AND + gncEntry.transGuid = gncTransaction.transGuid AND + gncTransaction.date_posted BETWEEN $2 AND $3 AND + gncEntry.reconciled = \\'y\\'' + LANGUAGE 'sql'; + +-- helper functions. These intentionally use the 'wrong' fraction. +-- This is because value_frac * amount * price = value * amount_frac + +CREATE FUNCTION gncHelperPrVal (gncEntry) + RETURNS INT8 + AS 'SELECT abs($1 . value * gncCommodity.fraction) + FROM gncEntry, gncAccount, gncCommodity + WHERE + $1 . accountGuid = gncAccount.accountGuid AND + gncAccount.commodity = gncCommodity.commodity' + LANGUAGE 'sql'; + +CREATE FUNCTION gncHelperPrAmt (gncEntry) + RETURNS INT8 + AS 'SELECT abs($1 . amount * gncCommodity.fraction) + FROM gncEntry, gncTransaction, gncCommodity + WHERE + $1 . transGuid = gncTransaction.transGuid AND + gncTransaction.currency = gncCommodity.commodity' + LANGUAGE 'sql'; + +-- end of file diff --git a/src/engine/sql/table-create.sql b/src/engine/sql/table-create.sql index 936f1c96a9..12c0ae6a3b 100644 --- a/src/engine/sql/table-create.sql +++ b/src/engine/sql/table-create.sql @@ -115,7 +115,8 @@ CREATE TABLE gncPrice ( source TEXT, type TEXT, valueNum INT8 DEFAULT '0', - valueDenom INT4 DEFAULT '100' + valueDenom INT4 DEFAULT '100', + version INT4 NOT NULL ); @@ -187,59 +188,4 @@ CREATE TABLE gncKVPvalue_list ( data TEXT[] ) INHERITS (gncKVPvalue); --- utility functions to compute chackpoint balance subtotals - -CREATE FUNCTION gncSubtotalBalance (CHAR(32), DATETIME, DATETIME) - RETURNS NUMERIC - AS 'SELECT sum(gncEntry.value) - FROM gncEntry, gncTransaction - WHERE - gncEntry.accountGuid = $1 AND - gncEntry.transGuid = gncTransaction.transGuid AND - gncTransaction.date_posted BETWEEN $2 AND $3' - LANGUAGE 'sql'; - -CREATE FUNCTION gncSubtotalClearedBalance (char(32), DATETIME, DATETIME) - RETURNS NUMERIC - AS 'SELECT sum(gncEntry.value) - FROM gncEntry, gncTransaction - WHERE - gncEntry.accountGuid = $1 AND - gncEntry.transGuid = gncTransaction.transGuid AND - gncTransaction.date_posted BETWEEN $2 AND $3 AND - gncEntry.reconciled <> \\'n\\'' - LANGUAGE 'sql'; - -CREATE FUNCTION gncSubtotalReconedBalance (CHAR(32), DATETIME, DATETIME) - RETURNS NUMERIC - AS 'SELECT sum(gncEntry.value) - FROM gncEntry, gncTransaction - WHERE - gncEntry.accountGuid = $1 AND - gncEntry.transGuid = gncTransaction.transGuid AND - gncTransaction.date_posted BETWEEN $2 AND $3 AND - gncEntry.reconciled = \\'y\\'' - LANGUAGE 'sql'; - --- helper functions. These intentionally use the 'wrong' fraction. --- This is because value_frac * amount * price = value * amount_frac - -CREATE FUNCTION gncHelperPrVal (gncEntry) - RETURNS INT8 - AS 'SELECT abs($1 . value * gncCommodity.fraction) - FROM gncEntry, gncAccount, gncCommodity - WHERE - $1 . accountGuid = gncAccount.accountGuid AND - gncAccount.commodity = gncCommodity.commodity' - LANGUAGE 'sql'; - -CREATE FUNCTION gncHelperPrAmt (gncEntry) - RETURNS INT8 - AS 'SELECT abs($1 . amount * gncCommodity.fraction) - FROM gncEntry, gncTransaction, gncCommodity - WHERE - $1 . transGuid = gncTransaction.transGuid AND - gncTransaction.currency = gncCommodity.commodity' - LANGUAGE 'sql'; - - +-- end of file