📄 regress.c
字号:
/* * $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.64 2005/10/15 02:49:51 momjian Exp $ */#include "postgres.h"#include <float.h> /* faked on sunos */#include "utils/geo_decls.h" /* includes <math.h> */#include "executor/executor.h" /* For GetAttributeByName */#include "commands/sequence.h" /* for nextval() */#define P_MAXDIG 12#define LDELIM '('#define RDELIM ')'#define DELIM ','extern Datum regress_dist_ptpath(PG_FUNCTION_ARGS);extern Datum regress_path_dist(PG_FUNCTION_ARGS);extern PATH *poly2path(POLYGON *poly);extern Datum interpt_pp(PG_FUNCTION_ARGS);extern void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2);extern Datum overpaid(PG_FUNCTION_ARGS);extern Datum boxarea(PG_FUNCTION_ARGS);extern char *reverse_name(char *string);extern int oldstyle_length(int n, text *t);extern Datum int44in(PG_FUNCTION_ARGS);extern Datum int44out(PG_FUNCTION_ARGS);extern Datum do_sleep(PG_FUNCTION_ARGS);/* * Distance from a point to a path */PG_FUNCTION_INFO_V1(regress_dist_ptpath);Datumregress_dist_ptpath(PG_FUNCTION_ARGS){ Point *pt = PG_GETARG_POINT_P(0); PATH *path = PG_GETARG_PATH_P(1); float8 result = 0.0; /* keep compiler quiet */ float8 tmp; int i; LSEG lseg; switch (path->npts) { case 0: PG_RETURN_NULL(); case 1: result = point_dt(pt, &path->p[0]); break; default: /* * the distance from a point to a path is the smallest distance * from the point to any of its constituent segments. */ Assert(path->npts > 1); for (i = 0; i < path->npts - 1; ++i) { regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]); tmp = DatumGetFloat8(DirectFunctionCall2(dist_ps, PointPGetDatum(pt), LsegPGetDatum(&lseg))); if (i == 0 || tmp < result) result = tmp; } break; } PG_RETURN_FLOAT8(result);}/* * this essentially does a cartesian product of the lsegs in the * two paths, and finds the min distance between any two lsegs */PG_FUNCTION_INFO_V1(regress_path_dist);Datumregress_path_dist(PG_FUNCTION_ARGS){ PATH *p1 = PG_GETARG_PATH_P(0); PATH *p2 = PG_GETARG_PATH_P(1); bool have_min = false; float8 min = 0.0; /* initialize to keep compiler quiet */ float8 tmp; int i, j; LSEG seg1, seg2; for (i = 0; i < p1->npts - 1; i++) { for (j = 0; j < p2->npts - 1; j++) { regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]); regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]); tmp = DatumGetFloat8(DirectFunctionCall2(lseg_distance, LsegPGetDatum(&seg1), LsegPGetDatum(&seg2))); if (!have_min || tmp < min) { min = tmp; have_min = true; } } } if (!have_min) PG_RETURN_NULL(); PG_RETURN_FLOAT8(min);}PATH *poly2path(POLYGON *poly){ int i; char *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64); char buf[2 * (P_MAXDIG) + 20]; sprintf(output, "(1, %*d", P_MAXDIG, poly->npts); for (i = 0; i < poly->npts; i++) { snprintf(buf, sizeof(buf), ",%*g,%*g", P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y); strcat(output, buf); } snprintf(buf, sizeof(buf), "%c", RDELIM); strcat(output, buf); return DatumGetPathP(DirectFunctionCall1(path_in, CStringGetDatum(output)));}/* return the point where two paths intersect, or NULL if no intersection. */PG_FUNCTION_INFO_V1(interpt_pp);Datuminterpt_pp(PG_FUNCTION_ARGS){ PATH *p1 = PG_GETARG_PATH_P(0); PATH *p2 = PG_GETARG_PATH_P(1); int i, j; LSEG seg1, seg2; bool found; /* We've found the intersection */ found = false; /* Haven't found it yet */ for (i = 0; i < p1->npts - 1 && !found; i++) { regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]); for (j = 0; j < p2->npts - 1 && !found; j++) { regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]); if (DatumGetBool(DirectFunctionCall2(lseg_intersect, LsegPGetDatum(&seg1), LsegPGetDatum(&seg2)))) found = true; } } if (!found) PG_RETURN_NULL(); /* * Note: DirectFunctionCall2 will kick out an error if lseg_interpt() * returns NULL, but that should be impossible since we know the two * segments intersect. */ PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt, LsegPGetDatum(&seg1), LsegPGetDatum(&seg2)));}/* like lseg_construct, but assume space already allocated */voidregress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2){ lseg->p[0].x = pt1->x; lseg->p[0].y = pt1->y; lseg->p[1].x = pt2->x; lseg->p[1].y = pt2->y; lseg->m = point_sl(pt1, pt2);}PG_FUNCTION_INFO_V1(overpaid);Datumoverpaid(PG_FUNCTION_ARGS){ HeapTupleHeader tuple = PG_GETARG_HEAPTUPLEHEADER(0); bool isnull; int32 salary; salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull)); if (isnull) PG_RETURN_NULL(); PG_RETURN_BOOL(salary > 699);}/* New type "widget" * This used to be "circle", but I added circle to builtins, * so needed to make sure the names do not collide. - tgl 97/04/21 */typedef struct{ Point center; double radius;} WIDGET;WIDGET *widget_in(char *str);char *widget_out(WIDGET * widget);extern Datum pt_in_widget(PG_FUNCTION_ARGS);#define NARGS 3WIDGET *widget_in(char *str){ char *p, *coord[NARGS], buf2[1000]; int i; WIDGET *result; if (str == NULL) return NULL; for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++) if (*p == ',' || (*p == LDELIM && !i)) coord[i++] = p + 1; if (i < NARGS - 1) return NULL; result = (WIDGET *) palloc(sizeof(WIDGET)); result->center.x = atof(coord[0]); result->center.y = atof(coord[1]); result->radius = atof(coord[2]); snprintf(buf2, sizeof(buf2), "widget_in: read (%f, %f, %f)\n", result->center.x, result->center.y, result->radius); return result;}char *widget_out(WIDGET * widget){ char *result; if (widget == NULL) return NULL; result = (char *) palloc(60); sprintf(result, "(%g,%g,%g)", widget->center.x, widget->center.y, widget->radius); return result;}PG_FUNCTION_INFO_V1(pt_in_widget);Datumpt_in_widget(PG_FUNCTION_ARGS){ Point *point = PG_GETARG_POINT_P(0); WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1); PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);}PG_FUNCTION_INFO_V1(boxarea);Datumboxarea(PG_FUNCTION_ARGS){ BOX *box = PG_GETARG_BOX_P(0); double width, height; width = Abs(box->high.x - box->low.x); height = Abs(box->high.y - box->low.y); PG_RETURN_FLOAT8(width * height);}char *reverse_name(char *string){ int i; int len; char *new_string; new_string = palloc0(NAMEDATALEN); for (i = 0; i < NAMEDATALEN && string[i]; ++i) ; if (i == NAMEDATALEN || !string[i]) --i; len = i; for (; i >= 0; --i) new_string[len - i] = string[i]; return new_string;}/* * This rather silly function is just to test that oldstyle functions * work correctly on toast-able inputs. */intoldstyle_length(int n, text *t){ int len = 0; if (t) len = VARSIZE(t) - VARHDRSZ; return n + len;}#include "executor/spi.h" /* this is what you need to work with SPI */#include "commands/trigger.h" /* -"- and triggers */static TransactionId fd17b_xid = InvalidTransactionId;static TransactionId fd17a_xid = InvalidTransactionId;static int fd17b_level = 0;static int fd17a_level = 0;static bool fd17b_recursion = true;static bool fd17a_recursion = true;extern Datum funny_dup17(PG_FUNCTION_ARGS);PG_FUNCTION_INFO_V1(funny_dup17);Datumfunny_dup17(PG_FUNCTION_ARGS){ TriggerData *trigdata = (TriggerData *) fcinfo->context; TransactionId *xid; int *level; bool *recursion; Relation rel; TupleDesc tupdesc; HeapTuple tuple; char *query, *fieldval, *fieldtype; char *when; int inserted; int selected = 0; int ret; if (!CALLED_AS_TRIGGER(fcinfo)) elog(ERROR, "funny_dup17: not fired by trigger manager"); tuple = trigdata->tg_trigtuple; rel = trigdata->tg_relation; tupdesc = rel->rd_att; if (TRIGGER_FIRED_BEFORE(trigdata->tg_event)) { xid = &fd17b_xid; level = &fd17b_level; recursion = &fd17b_recursion; when = "BEFORE"; } else { xid = &fd17a_xid; level = &fd17a_level; recursion = &fd17a_recursion; when = "AFTER "; } if (!TransactionIdIsCurrentTransactionId(*xid)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -