Datum plvsubst_string_string(PG_FUNCTION_ARGS) { Datum r; ArrayType *array; FunctionCallInfoData locfcinfo; init_c_subst(); if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL(); /* * I can't use DirectFunctionCall2 */ InitFunctionCallInfoData(locfcinfo, fcinfo->flinfo, 2, NULL, NULL); locfcinfo.arg[0] = PG_GETARG_DATUM(1); locfcinfo.arg[1] = PG_GETARG_IF_EXISTS(2, DATUM, CStringGetTextDatum(",")); locfcinfo.argnull[0] = false; locfcinfo.argnull[1] = false; r = text_to_array(&locfcinfo); if (locfcinfo.isnull || r == (Datum) 0) array = NULL; else array = DatumGetArrayTypeP(r); PG_RETURN_TEXT_P(plvsubst_string(PG_GETARG_TEXT_P(0), array, PG_GETARG_IF_EXISTS(3, TEXT_P, c_subst), fcinfo)); }
/* * CREATE FUNCTION utl_file.fcopy( * src_location text, * src_filename text, * dest_location text, * dest_filename text, * start_line integer DEFAULT NULL * end_line integer DEFAULT NULL) */ Datum utl_file_fcopy(PG_FUNCTION_ARGS) { char *srcpath; char *dstpath; int start_line; int end_line; FILE *srcfile; FILE *dstfile; NOT_NULL_ARG(0); NOT_NULL_ARG(1); NOT_NULL_ARG(2); NOT_NULL_ARG(3); srcpath = get_safe_path(PG_GETARG_TEXT_P(0), PG_GETARG_TEXT_P(1)); dstpath = get_safe_path(PG_GETARG_TEXT_P(2), PG_GETARG_TEXT_P(3)); start_line = PG_GETARG_IF_EXISTS(4, INT32, 1); if (start_line <= 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("start_line must be positive (%d passed)", start_line))); end_line = PG_GETARG_IF_EXISTS(5, INT32, INT_MAX); if (end_line <= 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("end_line must be positive (%d passed)", end_line))); srcfile = AllocateFile(srcpath, "rt"); if (srcfile == NULL) { /* failed to open src file. */ IO_EXCEPTION(); } dstfile = AllocateFile(dstpath, "wt"); if (dstfile == NULL) { /* failed to open dst file. */ fclose(srcfile); IO_EXCEPTION(); } if (copy_text_file(srcfile, dstfile, start_line, end_line)) IO_EXCEPTION(); FreeFile(srcfile); FreeFile(dstfile); PG_RETURN_VOID(); }
/* * CREATE FUNCTION utl_file.frename( * location text, * filename text, * dest_dir text, * dest_file text, * overwrite boolean DEFAULT false) */ Datum utl_file_frename(PG_FUNCTION_ARGS) { char *srcpath; char *dstpath; bool overwrite; NOT_NULL_ARG(0); NOT_NULL_ARG(1); NOT_NULL_ARG(2); NOT_NULL_ARG(3); overwrite = PG_GETARG_IF_EXISTS(4, BOOL, false); srcpath = get_safe_path(PG_GETARG_TEXT_P(0), PG_GETARG_TEXT_P(1)); dstpath = get_safe_path(PG_GETARG_TEXT_P(2), PG_GETARG_TEXT_P(3)); if (!overwrite) { struct stat st; if (stat(dstpath, &st) == 0) CUSTOM_EXCEPTION(WRITE_ERROR, "File exists"); else if (errno != ENOENT) IO_EXCEPTION(); } /* rename() overwrites existing files. */ if (rename(srcpath, dstpath) != 0) IO_EXCEPTION(); PG_RETURN_VOID(); }
Datum orafce_dump(PG_FUNCTION_ARGS) { Oid valtype = get_fn_expr_argtype(fcinfo->flinfo, 0); List *args; int16 typlen; bool typbyval; Size length; Datum value; int format; StringInfoData str; if (!fcinfo->flinfo || !fcinfo->flinfo->fn_expr) elog(ERROR, "function is called from invalid context"); if (PG_ARGISNULL(0)) elog(ERROR, "argument is NULL"); value = PG_GETARG_DATUM(0); format = PG_GETARG_IF_EXISTS(1, INT32, 10); args = ((FuncExpr *) fcinfo->flinfo->fn_expr)->args; valtype = exprType((Node *) list_nth(args, 0)); get_typlenbyval(valtype, &typlen, &typbyval); length = datumGetSize(value, typbyval, typlen); initStringInfo(&str); appendStringInfo(&str, "Typ=%d Len=%d: ", valtype, (int) length); if (!typbyval) { appendDatum(&str, DatumGetPointer(value), length, format); } else if (length <= 1) { char v = DatumGetChar(value); appendDatum(&str, &v, sizeof(char), format); } else if (length <= 2) { int16 v = DatumGetInt16(value); appendDatum(&str, &v, sizeof(int16), format); } else if (length <= 4) { int32 v = DatumGetInt32(value); appendDatum(&str, &v, sizeof(int32), format); } else { int64 v = DatumGetInt64(value); appendDatum(&str, &v, sizeof(int64), format); } PG_RETURN_TEXT_P(cstring_to_text(str.data)); }
Datum utl_file_new_line(PG_FUNCTION_ARGS) { FILE *f; int lines; CHECK_FILE_HANDLE(); f = get_stream(PG_GETARG_INT32(0), NULL, NULL); lines = PG_GETARG_IF_EXISTS(1, INT32, 1); do_new_line(f, lines); PG_RETURN_BOOL(true); }
Datum utl_file_put_line(PG_FUNCTION_ARGS) { FILE *f; bool autoflush; f = do_put(fcinfo); autoflush = PG_GETARG_IF_EXISTS(2, BOOL, false); do_new_line(f, 1); if (autoflush) do_flush(f); PG_RETURN_BOOL(true); }