Esempio di linguaggio C
Questo esempio utilizza il seguente nome di file: 'env.c.
Codice
Le classi ambiente e libreria condivisa forniscono informazioni sull'ambiente e sulle librerie associate a un AE. Questo esempio richiede un solo argomento, un numero intero. Se l'intero è uguale a 0, l'AE emette l'ambiente che vede. Se l'intero è uguale a 1, l'AE cerca l'ambiente come libreria. Se viene trovata, viene restituito il percorso della libreria, altrimenti viene restituito NULL:
#include <stdio.h>
#include <stdlib.h>
#include "nzaeapis.h"
static int run(NZAE_HANDLE h);
int main(int argc, char * argv[])
{
if (nzaeIsLocal())
{
NzaeInitialization arg;
memset(&arg, 0, sizeof(arg));
arg.ldkVersion = NZAE_LDK_VERSION;
if (nzaeInitialize(&arg))
{
fprintf(stderr, "initialization failed\n");
return -1;
}
run(arg.handle);
nzaeClose(arg.handle);
}
else {
NZAECONPT_HANDLE hConpt = nzaeconptCreate();
if (!hConpt)
{
fprintf(stderr, "error creating connection point\n");
fflush(stderr);
return -1;
}
const char * conPtName = nzaeRemprotGetRemoteName();
if (!conPtName)
{
fprintf(stderr, "error getting connection point name\n");
fflush(stderr);
exit(-1);
}
if (nzaeconptSetName(hConpt, conPtName))
{
fprintf(stderr, "error setting connection point name\n");
fflush(stderr);
nzaeconptClose(hConpt);
return -1;
}
NzaeremprotInitialization args;
memset(&args, 0, sizeof(args));
args.ldkVersion = NZAE_LDK_VERSION;
args.hConpt = hConpt;
if (nzaeRemprotCreateListener(&args))
{
fprintf(stderr, "unable to create listener - %s\n", \
args.errorMessage);
fflush(stderr);
nzaeconptClose(hConpt);
return -1;
}
NZAEREMPROT_HANDLE hRemprot = args.handle;
NzaeApi api;
int i;
for (i = 0; i < 5; i++)
{
if (nzaeRemprotAcceptApi(hRemprot, &api))
{
fprintf(stderr, "unable to accept API - %s\n", \
nzaeRemprotGetLastErrorText(hRemprot));
fflush(stderr);
nzaeconptClose(hConpt);
nzaeRemprotClose(hRemprot);
return -1;
}
if (api.apiType != NZAE_API_FUNCTION)
{
fprintf(stderr, "unexpected API returned\n");
fflush(stderr);
nzaeconptClose(hConpt);
nzaeRemprotClose(hRemprot);
return -1;
}
printf("testcapi: accepted a remote request\n");
fflush(stdout);
run(api.handle.function);
}
nzaeRemprotClose(hRemprot);
nzaeconptClose(hConpt);
}
return 0;
}
static int run(NZAE_HANDLE h)
{
NzaeMetadata metadata;
if (nzaeGetMetadata(h, &metadata))
{
fprintf(stderr, "get metadata failed\n");
return -1;
}
#define CHECK(value) \
{ \
NzaeRcCode rc = value; \
if (rc) \
{ \
const char * format = "%s in %s at %d"; \
fprintf(stderr, format, \
nzaeGetLastErrorText(h), __FILE__, __LINE__); \
nzaeUserError(h, format, \
nzaeGetLastErrorText(h), __FILE__, __LINE__); \
exit(-1); \
} \
}
for (;;)
{
int64_t ar[6];
int i;
double result = 0;
NzaeRcCode rc = nzaeGetNext(h);
if (rc == NZAE_RC_END)
{
break;
}
NzudsData * input = NULL;
CHECK(nzaeGetInputColumn(h, 1, &input));
int type = *input->data.pInt32;
CHECK(nzaeGetInputColumn(h, 0, &input));
if (type == 0) {
const char *result;
nzaeGetEnv(h, input->data.pVariableString, &result);
if (!result) {
CHECK(nzaeSetOutputNull(h, 0));
}
else
CHECK(nzaeSetOutputString(h, 0, result));
}
else {
const char *result = nzaeGetLibraryFullPath(h, \
input->data.pVariableString, false);
if (!result) {
CHECK(nzaeSetOutputNull(h, 0));
}
else
CHECK(nzaeSetOutputString(h, 0, result));
}
CHECK(nzaeOutputResult(h));
}
nzaeDone(h);
return 0;
}Compilazione
Utilizzare la compilazione standard:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language system --version 3 \
--template compile env.c --exe envRegistrazione
Registrare l'esempio:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language system --version 3 \
--template udtf --exe env --sig "env_c(varchar(any),int4)" \
--return "table(output varchar(1000))" --deps inza..LIBNZAEADAPTERS
Questo esempio presuppone che sia stata aggiunta la libreria condivisa LIBNZAEADAPTERS.
In esecuzione
Si noti che l'output di questo esempio è specifico dell'ambiente. Pertanto, il risultato effettivo sarà simile, ma non uguale, al testo seguente.
SELECT * FROM TABLE WITH FINAL(env_c('inza..libnzaeadapters', 1));
OUTPUT
---------------------------------------------------------------------
/nz/data.1.0/base/1/library/237951/host/libnzaeadapters.so
(1 row)
SELECT * FROM TABLE WITH FINAL(env_c('NZAE_DYNAMIC_ENVIRONMENT', 0));
OUTPUT
--------
0
(1 row)
SELECT * FROM TABLE WITH FINAL(env_c('NZAE_DYNAMIC_ENVIRONMENT2', 0));
OUTPUT
--------
(1 row)