Skip to content

Instantly share code, notes, and snippets.

@meska
Created July 13, 2023 14:57
Show Gist options
  • Save meska/1cfdb0abe6a011127c26dfe8dad02cd3 to your computer and use it in GitHub Desktop.
Save meska/1cfdb0abe6a011127c26dfe8dad02cd3 to your computer and use it in GitHub Desktop.
upsert to couchdb from oracle
create or replace procedure send_to_couchdb(
p_db in varchar2,
p_id in varchar2,
p_doc in varchar2
) is
req UTL_HTTP.REQ;
headreq UTL_HTTP.REQ;
resp UTL_HTTP.RESP;
headresp UTL_HTTP.RESP;
base_url VARCHAR2(1024) := 'http://couchdb.test.com/' || p_db || '/' || p_id;
url VARCHAR2(1024);
etag VARCHAR2(1024);
BEGIN
DBMS_OUTPUT.ENABLE (buffer_size => NULL);
headreq := UTL_HTTP.BEGIN_REQUEST(base_url, 'HEAD','HTTP/1.1');
UTL_HTTP.SET_HEADER(headreq, 'User-Agent', 'mozilla/4.0');
UTL_HTTP.SET_HEADER(headreq, 'Authorization', 'Basic xxxxxxxxxxxxx');
headresp := UTL_HTTP.GET_RESPONSE(headreq);
begin
UTL_HTTP.GET_HEADER_BY_NAME(headresp, 'Etag', etag);
exception when others then
etag := null;
end;
-- INSERT INTO YHTTPLOG (HEADERS) VALUES (etag);
UTL_HTTP.END_RESPONSE(headresp);
CASE WHEN etag IS NULL THEN
url := base_url;
ELSE
url := base_url || '?rev=' || SUBSTR(etag,2,LENGTH(etag)-2);
END CASE;
req := UTL_HTTP.BEGIN_REQUEST(url, 'PUT','HTTP/1.1');
UTL_HTTP.SET_HEADER(req, 'User-Agent', 'mozilla/4.0');
UTL_HTTP.SET_HEADER(req, 'Authorization', 'Basic xxxxxxxxxxxxx');
utl_http.set_header(req, 'content-type', 'application/json');
utl_http.set_header(req, 'Content-Length', length(p_doc));
UTL_HTTP.WRITE_TEXT(req,p_doc);
resp := UTL_HTTP.GET_RESPONSE(req);
UTL_HTTP.END_RESPONSE(resp);
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY THEN
UTL_HTTP.END_RESPONSE(resp);
END send_to_couchdb;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment