27 |
#include "utils/array.h" |
#include "utils/array.h" |
28 |
#include "utils/lsyscache.h" |
#include "utils/lsyscache.h" |
29 |
#include "miscadmin.h" |
#include "miscadmin.h" |
30 |
|
#include "commands/trigger.h" |
31 |
|
#include "executor/spi.h" |
32 |
|
|
33 |
#include <estraier.h> |
#include <estraier.h> |
34 |
#include <cabin.h> |
#include <cabin.h> |
35 |
#include <estnode.h> |
#include <estnode.h> |
739 |
attr = next; |
attr = next; |
740 |
} |
} |
741 |
} |
} |
742 |
|
|
743 |
|
/* trigger to keep data in Hyper Estraier index up-to-date */ |
744 |
|
/* CREATE FUNCTION pgest_trigger() RETURNS TRIGGER AS ... */ |
745 |
|
|
746 |
|
/* |
747 |
|
* UPDATE, INSERT and DELETE triggers are like this: |
748 |
|
|
749 |
|
CREATE TRIGGER pgest_trigger_update AFTER UPDATE |
750 |
|
ON table_name FOR EACH ROW |
751 |
|
EXECUTE PROCEDURE pgest_trigger('http://localhost:1978/node/trivia','admin','admin', |
752 |
|
'name_of_pk', 'column', 'another_column', 'and_so_on' |
753 |
|
) |
754 |
|
|
755 |
|
*/ |
756 |
|
|
757 |
|
PG_FUNCTION_INFO_V1(pgest_trigger); |
758 |
|
Datum pgest_trigger(PG_FUNCTION_ARGS) { |
759 |
|
|
760 |
|
TriggerData *data; |
761 |
|
TupleDesc tupdesc; |
762 |
|
HeapTuple ret; |
763 |
|
|
764 |
|
char **args; |
765 |
|
char *keycol = NULL; |
766 |
|
char *key = NULL; |
767 |
|
char *col_data = NULL; |
768 |
|
int knumber; |
769 |
|
int i; |
770 |
|
int create_doc = 0; |
771 |
|
|
772 |
|
ESTNODE *node; |
773 |
|
ESTDOC *doc; |
774 |
|
|
775 |
|
|
776 |
|
|
777 |
|
if (! CALLED_AS_TRIGGER(fcinfo)) { |
778 |
|
elog(ERROR, "pgest_trigger() must be called as a trigger"); |
779 |
|
} |
780 |
|
|
781 |
|
data = (TriggerData *) fcinfo->context; |
782 |
|
|
783 |
|
if (data->tg_trigger->tgnargs < 5) |
784 |
|
elog(ERROR, "pgest_trigger() requires at least 5 parameters ('http://localhost:1978/node/trivia', 'user', 'passwd', 'pk_column', 'column', ... )"); |
785 |
|
|
786 |
|
args = data->tg_trigger->tgargs; |
787 |
|
keycol = args[3]; |
788 |
|
|
789 |
|
tupdesc = data->tg_relation->rd_att; |
790 |
|
|
791 |
|
knumber = SPI_fnumber(tupdesc, keycol); |
792 |
|
key = SPI_getvalue(data->tg_trigtuple, tupdesc, knumber); |
793 |
|
|
794 |
|
|
795 |
|
/* initialize the network environment */ |
796 |
|
if( ! est_init_net_env() ) |
797 |
|
elog(ERROR, "pgest_trigger: network is unavailable\n"); |
798 |
|
|
799 |
|
/* create and configure the node connection object */ |
800 |
|
node = est_node_new( args[0] ); |
801 |
|
est_node_set_auth(node, args[1], args[2]); |
802 |
|
|
803 |
|
|
804 |
|
if (TRIGGER_FIRED_BY_INSERT(data->tg_event)) { |
805 |
|
/* There is no old data */ |
806 |
|
ret = data->tg_trigtuple; |
807 |
|
|
808 |
|
create_doc++; |
809 |
|
|
810 |
|
} else if (TRIGGER_FIRED_BY_UPDATE(data->tg_event)) { |
811 |
|
ret = data->tg_newtuple; |
812 |
|
|
813 |
|
create_doc++; |
814 |
|
|
815 |
|
} else if (TRIGGER_FIRED_BY_DELETE(data->tg_event)) { |
816 |
|
/* There is no new data */ |
817 |
|
ret = data->tg_trigtuple; |
818 |
|
|
819 |
|
if (! est_node_out_doc_by_uri(node, key) ) |
820 |
|
elog(ERROR, "est_node_doc_by_uri(%s): %d\n", key, est_node_status(node)); |
821 |
|
|
822 |
|
} else { |
823 |
|
elog(ERROR, "pgest_trigger() not called from INSERT/UPDATE/DELETE"); |
824 |
|
} |
825 |
|
|
826 |
|
if ( create_doc ) { |
827 |
|
|
828 |
|
/* create a document object */ |
829 |
|
doc = est_doc_new(); |
830 |
|
est_doc_add_attr(doc, "@uri", key); |
831 |
|
|
832 |
|
elog(DEBUG1, "est_doc_new @uri=%s", key); |
833 |
|
|
834 |
|
for( i = 4; i < data->tg_trigger->tgnargs; i++ ) { |
835 |
|
|
836 |
|
col_data = SPI_getvalue(ret, tupdesc, SPI_fnumber(tupdesc, args[i])); |
837 |
|
|
838 |
|
if (data) { |
839 |
|
elog(DEBUG1, " + %s = %s", args[i], col_data); |
840 |
|
est_doc_add_attr(doc, args[i], col_data); |
841 |
|
est_doc_add_text(doc, col_data); |
842 |
|
} |
843 |
|
|
844 |
|
} |
845 |
|
|
846 |
|
/* register the document object to the node */ |
847 |
|
if( ! est_node_put_doc(node, doc) ) |
848 |
|
elog(ERROR, "est_node_put_doc: %d\n", est_node_status(node)); |
849 |
|
|
850 |
|
/* destroy the document object */ |
851 |
|
est_doc_delete(doc); |
852 |
|
|
853 |
|
} |
854 |
|
|
855 |
|
/* destroy the node object */ |
856 |
|
est_node_delete(node); |
857 |
|
/* free the networking environment */ |
858 |
|
est_free_net_env(); |
859 |
|
|
860 |
|
|
861 |
|
return PointerGetDatum(ret); |
862 |
|
} |
863 |
|
|