-spelling, typos, indentation

This commit is contained in:
Christian Grothoff 2023-04-04 17:26:51 +02:00
parent 979ec38ec4
commit d4f9417d8c
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
2 changed files with 420 additions and 351 deletions

View File

@ -19,18 +19,22 @@
PG_MODULE_MAGIC;
#endif
typedef struct {
typedef struct
{
Datum col1;
Datum col2;
} valuest;
void _PG_init (void);
void _PG_fini (void);
void _PG_init(void)
void
_PG_init (void)
{
}
PG_FUNCTION_INFO_V1 (pg_spi_insert_int);
PG_FUNCTION_INFO_V1 (pg_spi_select_from_x);
PG_FUNCTION_INFO_V1 (pg_spi_select_pair_from_y);
@ -55,8 +59,9 @@ pg_spi_prepare_example(PG_FUNCTION_ARGS)
SPIPlanPtr new_plan;
ret = SPI_connect ();
if (ret != SPI_OK_CONNECT) {
elog(ERROR, "DB connexion failed ! \n");
if (ret != SPI_OK_CONNECT)
{
elog (ERROR, "DB connection failed ! \n");
}
{
if (prepared_plan == NULL)
@ -71,11 +76,13 @@ pg_spi_prepare_example(PG_FUNCTION_ARGS)
}
ret = SPI_execute_plan (prepared_plan, NULL, 0,false, 0);
if (ret != SPI_OK_SELECT) {
if (ret != SPI_OK_SELECT)
{
elog (ERROR, "SELECT FAILED %d !\n", ret);
}
if (SPI_tuptable != NULL && SPI_tuptable->vals != NULL && SPI_tuptable->tupdesc != NULL)
if (SPI_tuptable != NULL && SPI_tuptable->vals != NULL &&
SPI_tuptable->tupdesc != NULL)
{
value = SPI_getvalue (SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
result = atoi (value);
@ -90,7 +97,6 @@ pg_spi_prepare_example(PG_FUNCTION_ARGS)
}
Datum
pg_spi_prepare_example_without_saveplan (PG_FUNCTION_ARGS)
{
@ -100,14 +106,16 @@ pg_spi_prepare_example_without_saveplan(PG_FUNCTION_ARGS)
SPIPlanPtr new_plan;
ret = SPI_connect ();
if (ret != SPI_OK_CONNECT) {
elog(ERROR, "DB connexion failed ! \n");
if (ret != SPI_OK_CONNECT)
{
elog (ERROR, "DB connection failed ! \n");
}
{
new_plan = SPI_prepare ("SELECT 1 FROM joseph_test.X", 0, NULL);
ret = SPI_execute_plan (new_plan, NULL, 0,false, 0);
if (ret != SPI_OK_SELECT) {
if (ret != SPI_OK_SELECT)
{
elog (ERROR, "SELECT FAILED %d !\n", ret);
}
@ -155,6 +163,7 @@ pg_spi_select_from_x(PG_FUNCTION_ARGS)
PG_RETURN_INT64 (proc);
}
// INSERT INTO X VALUES (1)
Datum
pg_spi_insert_int (PG_FUNCTION_ARGS)
@ -202,7 +211,8 @@ pg_spi_prepare_insert(PG_FUNCTION_ARGS)
{
elog (ERROR, "SPI_connect failed ! \n");
}
if (prepared_plan == NULL) {
if (prepared_plan == NULL)
{
argtypes[0] = INT4OID;
nargs = 1;
@ -229,6 +239,8 @@ pg_spi_prepare_insert(PG_FUNCTION_ARGS)
PG_RETURN_VOID ();
}
/*
Datum
pg_spi_prepare_insert_bytea(PG_FUNCTION_ARGS)
@ -314,10 +326,6 @@ pg_spi_prepare_insert_without_saveplan(PG_FUNCTION_ARGS)
}
/*
Datum
pg_spi_select_pair_from_y(PG_FUNCTION_ARGS)
@ -443,7 +451,9 @@ Datum pg_spi_prepare_select_with_cond(PG_FUNCTION_ARGS) {
}
*/
Datum pg_spi_prepare_select_with_cond_without_saveplan(PG_FUNCTION_ARGS) {
Datum
pg_spi_prepare_select_with_cond_without_saveplan (PG_FUNCTION_ARGS)
{
SPIPlanPtr new_plan;
int ret;
@ -473,21 +483,25 @@ Datum pg_spi_prepare_select_with_cond_without_saveplan(PG_FUNCTION_ARGS) {
ret = SPI_execute_plan (new_plan, values, NULL, false, 0);
if (ret != SPI_OK_SELECT) {
if (ret != SPI_OK_SELECT)
{
elog (ERROR, "SPI_execute_plan failed: %d \n", ret);
}
proc = SPI_processed;
if (proc > 0) {
if (proc > 0)
{
SPITupleTable *tuptable = SPI_tuptable;
TupleDesc tupdesc = tuptable->tupdesc;
HeapTuple tuple;
int i;
for (i = 0; i < proc; i++) {
for (i = 0; i < proc; i++)
{
tuple = tuptable->vals[i];
for (int j = 1; j <= tupdesc->natts; j++) {
for (int j = 1; j <= tupdesc->natts; j++)
{
char *value = SPI_getvalue (tuple, tupdesc, j);
result += atoi (value);
}
@ -498,8 +512,6 @@ Datum pg_spi_prepare_select_with_cond_without_saveplan(PG_FUNCTION_ARGS) {
}
Datum
pg_spi_update_y (PG_FUNCTION_ARGS)
{
@ -531,10 +543,6 @@ pg_spi_update_y(PG_FUNCTION_ARGS)
}
Datum
pg_spi_prepare_update (PG_FUNCTION_ARGS)
{
@ -573,23 +581,26 @@ pg_spi_prepare_update(PG_FUNCTION_ARGS)
SPI_finish ();
PG_RETURN_VOID ();
}
/*
Datum
pg_spi_prepare_update_without_saveplan(PG_FUNCTION_ARGS)
{}*/
void _PG_fini(void)
void
_PG_fini (void)
{
}
/*
*/
Datum
pg_spi_get_dep_ref_fees (PG_FUNCTION_ARGS) {
pg_spi_get_dep_ref_fees (PG_FUNCTION_ARGS)
{
/* Define plan to save */
static SPIPlanPtr deposit_plan;
static SPIPlanPtr ref_plan;
@ -635,7 +646,8 @@ pg_spi_get_dep_ref_fees (PG_FUNCTION_ARGS) {
int res = SPI_connect ();
/* Connect to SPI */
if (res < 0) {
if (res < 0)
{
elog (ERROR, "Could not connect to SPI manager");
}
if (deposit_plan == NULL)
@ -684,14 +696,24 @@ pg_spi_get_dep_ref_fees (PG_FUNCTION_ARGS) {
// STORE TUPTABLE deposit
dep_res = SPI_tuptable;
for (unsigned int i = 0; i < SPI_processed; i++) {
int64 dep_deposit_serial_ids = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &is_null));
bytea *dep_coin_pub = DatumGetByteaP(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 2, &is_null));
int64 dep_amount_val = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 3, &is_null));
int32 dep_amount_frac = DatumGetInt32(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 4, &is_null));
for (unsigned int i = 0; i < SPI_processed; i++)
{
int64 dep_deposit_serial_ids = DatumGetInt64 (SPI_getbinval (
SPI_tuptable->vals[i],
SPI_tuptable->tupdesc, 1,
&is_null));
bytea *dep_coin_pub = DatumGetByteaP (SPI_getbinval (SPI_tuptable->vals[i],
SPI_tuptable->tupdesc,
2, &is_null));
int64 dep_amount_val = DatumGetInt64 (SPI_getbinval (SPI_tuptable->vals[i],
SPI_tuptable->tupdesc,
3, &is_null));
int32 dep_amount_frac = DatumGetInt32 (SPI_getbinval (SPI_tuptable->vals[i],
SPI_tuptable->tupdesc,
4, &is_null));
if (is_null)
elog(ERROR, "Failed to retrive data from deposit \n");
elog (ERROR, "Failed to retrieve data from deposit \n");
if (ref_plan == NULL)
{
// Execute second query with parameters from first query and store results in variables
@ -700,7 +722,8 @@ pg_spi_get_dep_ref_fees (PG_FUNCTION_ARGS) {
"FROM refunds "
"WHERE coin_pub=$1 "
"AND deposit_serial_id=$2;";
SPIPlanPtr new_plan = SPI_prepare(ref_sql, 3, (Oid[]){BYTEAOID, INT8OID});
SPIPlanPtr new_plan = SPI_prepare (ref_sql, 3, (Oid[]){BYTEAOID,
INT8OID});
if (new_plan == NULL)
elog (ERROR, "SPI_prepare failed for refund\n");
ref_plan = SPI_saveplan (new_plan);
@ -718,11 +741,24 @@ pg_spi_get_dep_ref_fees (PG_FUNCTION_ARGS) {
elog (ERROR, "Failed to execute subquery 2\n");
// STORE TUPTABLE refund
ref_res = SPI_tuptable;
for (unsigned int j = 0; j < SPI_processed; j++) {
int64 ref_refund_val = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[j], SPI_tuptable->tupdesc, 1, &is_null));
int32 ref_refund_frac = DatumGetInt32(SPI_getbinval(SPI_tuptable->vals[j], SPI_tuptable->tupdesc, 2, &is_null));
bytea *ref_coin_pub = DatumGetByteaP(SPI_getbinval(SPI_tuptable->vals[j], SPI_tuptable->tupdesc, 3, &is_null));
int64 ref_deposit_serial_id = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[j], SPI_tuptable->tupdesc, 4, &is_null));
for (unsigned int j = 0; j < SPI_processed; j++)
{
int64 ref_refund_val = DatumGetInt64 (SPI_getbinval (
SPI_tuptable->vals[j],
SPI_tuptable->tupdesc, 1,
&is_null));
int32 ref_refund_frac = DatumGetInt32 (SPI_getbinval (
SPI_tuptable->vals[j],
SPI_tuptable->tupdesc, 2,
&is_null));
bytea *ref_coin_pub = DatumGetByteaP (SPI_getbinval (
SPI_tuptable->vals[j],
SPI_tuptable->tupdesc, 3,
&is_null));
int64 ref_deposit_serial_id = DatumGetInt64 (SPI_getbinval (
SPI_tuptable->vals[j],
SPI_tuptable->tupdesc, 4,
&is_null));
// Execute third query with parameters from second query and store results in variables
ref_by_coin_coin_pub = ref_coin_pub;
ref_by_coin_deposit_serial_id = ref_deposit_serial_id;
@ -730,10 +766,14 @@ pg_spi_get_dep_ref_fees (PG_FUNCTION_ARGS) {
for (unsigned int i = 0; i<SPI_processed; i++)
{
if ((ref_by_coin_coin_pub ==
DatumGetByteaP(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &is_null)))
DatumGetByteaP (SPI_getbinval (SPI_tuptable->vals[i],
SPI_tuptable->tupdesc, 1,
&is_null)))
&&
(ref_by_coin_deposit_serial_id ==
DatumGetUInt64(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 2, &is_null)))
DatumGetUInt64 (SPI_getbinval (SPI_tuptable->vals[i],
SPI_tuptable->tupdesc, 2,
&is_null)))
)
{
sum_refund_val += ref_refund_val;
@ -768,7 +808,8 @@ pg_spi_get_dep_ref_fees (PG_FUNCTION_ARGS) {
"FROM known_coins kc"
"JOIN denominations denom USING (denominations_serial) "
"WHERE kc.coin_pub = $1 AND kc.coin_pub != $2;";
SPIPlanPtr new_plan = SPI_prepare(fees_sql, 3, (Oid[]){BYTEAOID, BYTEAOID});
SPIPlanPtr new_plan = SPI_prepare (fees_sql, 3, (Oid[]){BYTEAOID,
BYTEAOID});
if (new_plan == NULL)
{
elog (ERROR, "SPI_prepare for fees failed ! \n");
@ -790,9 +831,14 @@ pg_spi_get_dep_ref_fees (PG_FUNCTION_ARGS) {
{
HeapTuple tuple = fees_res->vals[i];
bool is_null;
uint64_t fee_val = DatumGetUInt64(SPI_getbinval(tuple, tupdesc, 1, &is_null));
uint32_t fee_frac = DatumGetUInt32(SPI_getbinval(tuple, tupdesc, 2, &is_null));
uint64_t fees_deposit_serial_id = DatumGetUInt64(SPI_getbinval(tuple, tupdesc, 3, &is_null));
uint64_t fee_val = DatumGetUInt64 (SPI_getbinval (tuple, tupdesc, 1,
&is_null));
uint32_t fee_frac = DatumGetUInt32 (SPI_getbinval (tuple, tupdesc, 2,
&is_null));
uint64_t fees_deposit_serial_id = DatumGetUInt64 (SPI_getbinval (tuple,
tupdesc,
3,
&is_null));
if (dummy_plan == NULL)
{
const char *insert_dummy_sql =
@ -800,7 +846,8 @@ pg_spi_get_dep_ref_fees (PG_FUNCTION_ARGS) {
"aggregation_tracking(deposit_serial_id, wtid_raw)"
" VALUES ($1, $2)";
SPIPlanPtr new_plan = SPI_prepare(insert_dummy_sql, 2, (Oid[]){INT8OID, BYTEAOID});
SPIPlanPtr new_plan = SPI_prepare (insert_dummy_sql, 2, (Oid[]){INT8OID,
BYTEAOID});
if (new_plan == NULL)
elog (ERROR, "FAILED to prepare aggregation tracking \n");
dummy_plan = SPI_saveplan (new_plan);

View File

@ -8,7 +8,8 @@ PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1 (get_deposit_summary);
Datum get_deposit_summary(PG_FUNCTION_ARGS)
Datum
get_deposit_summary (PG_FUNCTION_ARGS)
{
static SPIPlanPtr deposit_plan;
@ -27,7 +28,7 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
int conn = SPI_connect ();
if (conn != SPI_OK_CONNECT)
{
elog(ERROR, "DB connexion failed ! \n");
elog (ERROR, "DB connection failed ! \n");
}
if (deposit_plan == NULL
@ -35,7 +36,8 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
|| refund_by_coin_plan == NULL
|| norm_refund_by_coin_plan = NULL
|| fully_refunded_coins_plan = NULL
|| fees_plan == NULL )
|| fees_plan
== NULL)
{
if (deposit_plan == NULL)
{
@ -57,7 +59,7 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
" ,amount_with_fee_val AS amount_val"
" ,amount_with_fee_frac AS amount_frac";
SPIPlanPtr new_plan =
SPI_prepare(dep_sql, 4, argtypes});
SPI_prepare (dep_sql, 4, argtypes);
if (new_plan == NULL)
{
elog (ERROR, "SPI_prepare for deposit failed ! \n");
@ -82,11 +84,13 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
{
elog (ERROR, "Failed to execute subquery 1\n");
}
uint64_t *dep_deposit_serial_ids = palloc(sizeof(uint64_t) * SPI_processed);
uint64_t *dep_deposit_serial_ids = palloc (sizeof(uint64_t)
* SPI_processed);
BYTEA **dep_coin_pubs = palloc (sizeof(BYTEA *) * SPI_processed);
uint64_t *dep_amount_vals = palloc (sizeof(uint64_t) * SPI_processed);
uint32_t *dep_amount_fracs = palloc (sizeof(uint32_t) * SPI_processed);
for (unsigned int i = 0; i < SPI_processed; i++) {
for (unsigned int i = 0; i < SPI_processed; i++)
{
HeapTuple tuple = SPI_tuptable->vals[i];
dep_deposit_serial_ids[i] =
DatumGetInt64 (SPI_getbinval (tuple, SPI_tuptable->tupdesc, 1, &ret));
@ -134,9 +138,11 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
{
HeapTuple tuple = tuptable->vals[i];
Datum refund_val = SPI_getbinval (tuple, tupdesc, 1, &refund_val_isnull);
Datum refund_frac = SPI_getbinval(tuple, tupdesc, 2, &refund_frac_isnull);
Datum refund_frac = SPI_getbinval (tuple, tupdesc, 2,
&refund_frac_isnull);
Datum coin_pub = SPI_getbinval (tuple, tupdesc, 3, &coin_pub_isnull);
Datum deposit_serial_id = SPI_getbinval(tuple, tupdesc, 4, &deposit_serial_id_isnull);
Datum deposit_serial_id = SPI_getbinval (tuple, tupdesc, 4,
&deposit_serial_id_isnull);
if (refund_val_isnull
|| refund_frac_isnull
|| coin_pub_isnull
@ -188,10 +194,13 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
for (unsigned int i = 0; i < SPI_processed; i++)
{
HeapTuple tuple = tuptable->vals[i];
Datum sum_refund_val = SPI_getbinval(tuple, tupdesc, 1, &refund_val_isnull);
Datum sum_refund_frac = SPI_getbinval(tuple, tupdesc, 2, &refund_frac_isnull);
Datum sum_refund_val = SPI_getbinval (tuple, tupdesc, 1,
&refund_val_isnull);
Datum sum_refund_frac = SPI_getbinval (tuple, tupdesc, 2,
&refund_frac_isnull);
Datum coin_pub = SPI_getbinval (tuple, tupdesc, 3, &coin_pub_isnull);
Datum deposit_serial_id_int = SPI_getbinval(tuple, tupdesc, 4, &deposit_serial_id_isnull);
Datum deposit_serial_id_int = SPI_getbinval (tuple, tupdesc, 4,
&deposit_serial_id_isnull);
if (refund_val_isnull
|| refund_frac_isnull
|| coin_pub_isnull
@ -292,7 +301,8 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
ereport (
ERROR,
(errcode (ERRCODE_INTERNAL_ERROR),
errmsg("deposit fee query failed: error code %d \n", SPI_result_code())));
errmsg ("deposit fee query failed: error code %d \n",
SPI_result_code ())));
}
fees_ntuples = SPI_processed;
@ -301,11 +311,14 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
for (i = 0; i < fees_ntuples; i++)
{
Datum fee_val_datum =
SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &fee_null);
SPI_getbinval (SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1,
&fee_null);
Datum fee_frac_datum =
SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 2, &fee_null);
SPI_getbinval (SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 2,
&fee_null);
Datum deposit_id_datum =
SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 3, &deposit_null);
SPI_getbinval (SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 3,
&deposit_null);
if (! fee_null && ! deposit_null)
{
int64 fee_val = DatumGetInt64 (fee_val_datum);
@ -341,12 +354,18 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
result = HeapTupleGetDatum (tuple);
TupleDesc result_desc = CreateTemplateTupleDesc (6, false);
TupleDescInitEntry(result_desc, (AttrNumber) 1, "sum_deposit_value", INT8OID, -1, 0);
TupleDescInitEntry(result_desc, (AttrNumber) 2, "sum_deposit_fraction", INT4OID, -1, 0);
TupleDescInitEntry(result_desc, (AttrNumber) 3, "sum_refund_value", INT8OID, -1, 0);
TupleDescInitEntry(result_desc, (AttrNumber) 4, "sum_refund_fraction", INT4OID, -1, 0);
TupleDescInitEntry(result_desc, (AttrNumber) 5, "sum_fee_value", INT8OID, -1, 0);
TupleDescInitEntry(result_desc, (AttrNumber) 6, "sum_fee_fraction", INT4OID, -1, 0);
TupleDescInitEntry (result_desc, (AttrNumber) 1, "sum_deposit_value", INT8OID,
-1, 0);
TupleDescInitEntry (result_desc, (AttrNumber) 2, "sum_deposit_fraction",
INT4OID, -1, 0);
TupleDescInitEntry (result_desc, (AttrNumber) 3, "sum_refund_value", INT8OID,
-1, 0);
TupleDescInitEntry (result_desc, (AttrNumber) 4, "sum_refund_fraction",
INT4OID, -1, 0);
TupleDescInitEntry (result_desc, (AttrNumber) 5, "sum_fee_value", INT8OID, -1,
0);
TupleDescInitEntry (result_desc, (AttrNumber) 6, "sum_fee_fraction", INT4OID,
-1, 0);
int ret = SPI_prepare (sql, 4, argtypes);
if (ret != SPI_OK_PREPARE)
@ -366,17 +385,23 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
Datum values[6];
bool nulls[6] = {false};
values[0] =
SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &nulls[0]);
SPI_getbinval (SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1,
&nulls[0]);
values[1] =
SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &nulls[1]);
SPI_getbinval (SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2,
&nulls[1]);
values[2] =
SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3, &nulls[2]);
SPI_getbinval (SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3,
&nulls[2]);
values[3] =
SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 4, &nulls[3]);
SPI_getbinval (SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 4,
&nulls[3]);
values[4] =
SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 5, &nulls[4]);
SPI_getbinval (SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 5,
&nulls[4]);
values[5] =
SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 6, &nulls[5]);
SPI_getbinval (SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 6,
&nulls[5]);
tuple = heap_form_tuple (result_desc, values, nulls);
PG_RETURN_DATUM (HeapTupleGetDatum (tuple));
}
@ -384,6 +409,3 @@ Datum get_deposit_summary(PG_FUNCTION_ARGS)
PG_RETURN_NULL ();
}