|
53 | 53 | #include "pgstat.h"
|
54 | 54 | #include "utils/acl.h"
|
55 | 55 | #include "utils/builtins.h"
|
| 56 | +#include "utils/date.h" |
56 | 57 | #include "utils/lsyscache.h"
|
57 | 58 | #include "utils/memutils.h"
|
| 59 | +#include "utils/timestamp.h" |
58 | 60 | #include "utils/typcache.h"
|
59 | 61 | #include "utils/xml.h"
|
60 | 62 |
|
@@ -147,6 +149,9 @@ static Datum ExecEvalCoalesce(CoalesceExprState *coalesceExpr,
|
147 | 149 | static Datum ExecEvalMinMax(MinMaxExprState *minmaxExpr,
|
148 | 150 | ExprContext *econtext,
|
149 | 151 | bool *isNull, ExprDoneCond *isDone);
|
| 152 | +static Datum ExecEvalSQLValueFunction(ExprState *svfExpr, |
| 153 | + ExprContext *econtext, |
| 154 | + bool *isNull, ExprDoneCond *isDone); |
150 | 155 | static Datum ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
|
151 | 156 | bool *isNull, ExprDoneCond *isDone);
|
152 | 157 | static Datum ExecEvalNullIf(FuncExprState *nullIfExpr,
|
@@ -3530,6 +3535,75 @@ ExecEvalMinMax(MinMaxExprState *minmaxExpr, ExprContext *econtext,
|
3530 | 3535 | return result;
|
3531 | 3536 | }
|
3532 | 3537 |
|
| 3538 | +/* ---------------------------------------------------------------- |
| 3539 | + * ExecEvalSQLValueFunction |
| 3540 | + * ---------------------------------------------------------------- |
| 3541 | + */ |
| 3542 | +static Datum |
| 3543 | +ExecEvalSQLValueFunction(ExprState *svfExpr, |
| 3544 | + ExprContext *econtext, |
| 3545 | + bool *isNull, ExprDoneCond *isDone) |
| 3546 | +{ |
| 3547 | + Datum result = (Datum) 0; |
| 3548 | + SQLValueFunction *svf = (SQLValueFunction *) svfExpr->expr; |
| 3549 | + FunctionCallInfoData fcinfo; |
| 3550 | + |
| 3551 | + if (isDone) |
| 3552 | + *isDone = ExprSingleResult; |
| 3553 | + *isNull = false; |
| 3554 | + |
| 3555 | + /* |
| 3556 | + * Note: current_schema() can return NULL. current_user() etc currently |
| 3557 | + * cannot, but might as well code those cases the same way for safety. |
| 3558 | + */ |
| 3559 | + switch (svf->op) |
| 3560 | + { |
| 3561 | + case SVFOP_CURRENT_DATE: |
| 3562 | + result = DateADTGetDatum(GetSQLCurrentDate()); |
| 3563 | + break; |
| 3564 | + case SVFOP_CURRENT_TIME: |
| 3565 | + case SVFOP_CURRENT_TIME_N: |
| 3566 | + result = TimeTzADTPGetDatum(GetSQLCurrentTime(svf->typmod)); |
| 3567 | + break; |
| 3568 | + case SVFOP_CURRENT_TIMESTAMP: |
| 3569 | + case SVFOP_CURRENT_TIMESTAMP_N: |
| 3570 | + result = TimestampTzGetDatum(GetSQLCurrentTimestamp(svf->typmod)); |
| 3571 | + break; |
| 3572 | + case SVFOP_LOCALTIME: |
| 3573 | + case SVFOP_LOCALTIME_N: |
| 3574 | + result = TimeADTGetDatum(GetSQLLocalTime(svf->typmod)); |
| 3575 | + break; |
| 3576 | + case SVFOP_LOCALTIMESTAMP: |
| 3577 | + case SVFOP_LOCALTIMESTAMP_N: |
| 3578 | + result = TimestampGetDatum(GetSQLLocalTimestamp(svf->typmod)); |
| 3579 | + break; |
| 3580 | + case SVFOP_CURRENT_ROLE: |
| 3581 | + case SVFOP_CURRENT_USER: |
| 3582 | + case SVFOP_USER: |
| 3583 | + InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
| 3584 | + result = current_user(&fcinfo); |
| 3585 | + *isNull = fcinfo.isnull; |
| 3586 | + break; |
| 3587 | + case SVFOP_SESSION_USER: |
| 3588 | + InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
| 3589 | + result = session_user(&fcinfo); |
| 3590 | + *isNull = fcinfo.isnull; |
| 3591 | + break; |
| 3592 | + case SVFOP_CURRENT_CATALOG: |
| 3593 | + InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
| 3594 | + result = current_database(&fcinfo); |
| 3595 | + *isNull = fcinfo.isnull; |
| 3596 | + break; |
| 3597 | + case SVFOP_CURRENT_SCHEMA: |
| 3598 | + InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
| 3599 | + result = current_schema(&fcinfo); |
| 3600 | + *isNull = fcinfo.isnull; |
| 3601 | + break; |
| 3602 | + } |
| 3603 | + |
| 3604 | + return result; |
| 3605 | +} |
| 3606 | + |
3533 | 3607 | /* ----------------------------------------------------------------
|
3534 | 3608 | * ExecEvalXml
|
3535 | 3609 | * ----------------------------------------------------------------
|
@@ -5086,6 +5160,10 @@ ExecInitExpr(Expr *node, PlanState *parent)
|
5086 | 5160 | state = (ExprState *) mstate;
|
5087 | 5161 | }
|
5088 | 5162 | break;
|
| 5163 | + case T_SQLValueFunction: |
| 5164 | + state = (ExprState *) makeNode(ExprState); |
| 5165 | + state->evalfunc = ExecEvalSQLValueFunction; |
| 5166 | + break; |
5089 | 5167 | case T_XmlExpr:
|
5090 | 5168 | {
|
5091 | 5169 | XmlExpr *xexpr = (XmlExpr *) node;
|
|
0 commit comments