⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi.c

📁 关系型数据库 Postgresql 6.5.2
💻 C
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------------- * * spi.c *				Server Programming Interface * * $Id: spi.c,v 1.39 1999/05/25 22:41:02 momjian Exp $ * *------------------------------------------------------------------------- */#include "executor/spi.h"#include "executor/spi_priv.h"#include "catalog/pg_type.h"#include "access/printtup.h"#include "fmgr.h"static Portal _SPI_portal = (Portal) NULL;static _SPI_connection *_SPI_stack = NULL;static _SPI_connection *_SPI_current = NULL;static int	_SPI_connected = -1;static int	_SPI_curid = -1;DLLIMPORT uint32 SPI_processed = 0;DLLIMPORT SPITupleTable *SPI_tuptable;DLLIMPORT int SPI_result;static int	_SPI_execute(char *src, int tcount, _SPI_plan *plan);static int	_SPI_pquery(QueryDesc *queryDesc, EState *state, int tcount);static int _SPI_execute_plan(_SPI_plan *plan,				  Datum *Values, char *Nulls, int tcount);static _SPI_plan *_SPI_copy_plan(_SPI_plan *plan, int location);static int	_SPI_begin_call(bool execmem);static int	_SPI_end_call(bool procmem);static MemoryContext _SPI_execmem(void);static MemoryContext _SPI_procmem(void);static bool _SPI_checktuples(void);#ifdef SPI_EXECUTOR_STATSextern int	ShowExecutorStats;extern void ResetUsage(void);extern void ShowUsage(void);#endif/* =================== interface functions =================== */intSPI_connect(){	char		pname[64];	PortalVariableMemory pvmem;	/*	 * It's possible on startup and after commit/abort. In future we'll	 * catch commit/abort in some way...	 */	strcpy(pname, "<SPI manager>");	_SPI_portal = GetPortalByName(pname);	if (!PortalIsValid(_SPI_portal))	{		if (_SPI_stack != NULL) /* there was abort */			free(_SPI_stack);		_SPI_current = _SPI_stack = NULL;		_SPI_connected = _SPI_curid = -1;		SPI_processed = 0;		SPI_tuptable = NULL;		_SPI_portal = CreatePortal(pname);		if (!PortalIsValid(_SPI_portal))			elog(FATAL, "SPI_connect: global initialization failed");	}	/*	 * When procedure called by Executor _SPI_curid expected to be equal	 * to _SPI_connected	 */	if (_SPI_curid != _SPI_connected)		return SPI_ERROR_CONNECT;	if (_SPI_stack == NULL)	{		if (_SPI_connected != -1)			elog(FATAL, "SPI_connect: no connection(s) expected");		_SPI_stack = (_SPI_connection *) malloc(sizeof(_SPI_connection));	}	else	{		if (_SPI_connected <= -1)			elog(FATAL, "SPI_connect: some connection(s) expected");		_SPI_stack = (_SPI_connection *) realloc(_SPI_stack,						 (_SPI_connected + 2) * sizeof(_SPI_connection));	}	/*	 * We' returning to procedure where _SPI_curid == _SPI_connected - 1	 */	_SPI_connected++;	_SPI_current = &(_SPI_stack[_SPI_connected]);	_SPI_current->qtlist = NULL;	_SPI_current->processed = 0;	_SPI_current->tuptable = NULL;	/* Create Portal for this procedure ... */	snprintf(pname, 64, "<SPI %d>", _SPI_connected);	_SPI_current->portal = CreatePortal(pname);	if (!PortalIsValid(_SPI_current->portal))		elog(FATAL, "SPI_connect: initialization failed");	/* ... and switch to Portal' Variable memory - procedure' context */	pvmem = PortalGetVariableMemory(_SPI_current->portal);	_SPI_current->savedcxt = MemoryContextSwitchTo((MemoryContext) pvmem);	_SPI_current->savedId = GetScanCommandId();	SetScanCommandId(GetCurrentCommandId());	return SPI_OK_CONNECT;}intSPI_finish(){	int			res;	res = _SPI_begin_call(false);		/* live in procedure memory */	if (res < 0)		return res;	/* Restore memory context as it was before procedure call */	MemoryContextSwitchTo(_SPI_current->savedcxt);	PortalDestroy(&(_SPI_current->portal));	SetScanCommandId(_SPI_current->savedId);	/*	 * After _SPI_begin_call _SPI_connected == _SPI_curid. Now we are	 * closing connection to SPI and returning to upper Executor and so	 * _SPI_connected must be equal to _SPI_curid.	 */	_SPI_connected--;	_SPI_curid--;	if (_SPI_connected == -1)	{		free(_SPI_stack);		_SPI_stack = NULL;	}	else	{		_SPI_stack = (_SPI_connection *) realloc(_SPI_stack,						 (_SPI_connected + 1) * sizeof(_SPI_connection));		_SPI_current = &(_SPI_stack[_SPI_connected]);	}	return SPI_OK_FINISH;}voidSPI_push(void){	_SPI_curid++;}voidSPI_pop(void){	_SPI_curid--;}intSPI_exec(char *src, int tcount){	int			res;	if (src == NULL || tcount < 0)		return SPI_ERROR_ARGUMENT;	res = _SPI_begin_call(true);	if (res < 0)		return res;	res = _SPI_execute(src, tcount, NULL);	_SPI_end_call(true);	return res;}intSPI_execp(void *plan, Datum *Values, char *Nulls, int tcount){	int			res;	if (plan == NULL || tcount < 0)		return SPI_ERROR_ARGUMENT;	if (((_SPI_plan *) plan)->nargs > 0 && Values == NULL)		return SPI_ERROR_PARAM;	res = _SPI_begin_call(true);	if (res < 0)		return res;	/* copy plan to current (executor) context */	plan = (void *) _SPI_copy_plan(plan, _SPI_CPLAN_CURCXT);	res = _SPI_execute_plan((_SPI_plan *) plan, Values, Nulls, tcount);	_SPI_end_call(true);	return res;}void *SPI_prepare(char *src, int nargs, Oid *argtypes){	_SPI_plan  *plan;	if (src == NULL || nargs < 0 || (nargs > 0 && argtypes == NULL))	{		SPI_result = SPI_ERROR_ARGUMENT;		return NULL;	}	SPI_result = _SPI_begin_call(true);	if (SPI_result < 0)		return NULL;	plan = (_SPI_plan *) palloc(sizeof(_SPI_plan));		/* Executor context */	plan->argtypes = argtypes;	plan->nargs = nargs;	SPI_result = _SPI_execute(src, 0, plan);	if (SPI_result >= 0)		/* copy plan to procedure context */		plan = _SPI_copy_plan(plan, _SPI_CPLAN_PROCXT);	else		plan = NULL;	_SPI_end_call(true);	return (void *) plan;}void *SPI_saveplan(void *plan){	_SPI_plan  *newplan;	if (plan == NULL)	{		SPI_result = SPI_ERROR_ARGUMENT;		return NULL;	}	SPI_result = _SPI_begin_call(false);		/* don't change context */	if (SPI_result < 0)		return NULL;	newplan = _SPI_copy_plan((_SPI_plan *) plan, _SPI_CPLAN_TOPCXT);	_SPI_curid--;	SPI_result = 0;	return (void *) newplan;}HeapTupleSPI_copytuple(HeapTuple tuple){	MemoryContext oldcxt = NULL;	HeapTuple	ctuple;	if (tuple == NULL)	{		SPI_result = SPI_ERROR_ARGUMENT;		return NULL;	}	if (_SPI_curid + 1 == _SPI_connected)		/* connected */	{		if (_SPI_current != &(_SPI_stack[_SPI_curid + 1]))			elog(FATAL, "SPI: stack corrupted");		oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);	}	ctuple = heap_copytuple(tuple);	if (oldcxt)		MemoryContextSwitchTo(oldcxt);	return ctuple;}HeapTupleSPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum,				Datum *Values, char *Nulls){	MemoryContext oldcxt = NULL;	HeapTuple	mtuple;	int			numberOfAttributes;	uint8		infomask;	Datum	   *v;	char	   *n;	bool		isnull;	int			i;	if (rel == NULL || tuple == NULL || natts <= 0 || attnum == NULL || Values == NULL)	{		SPI_result = SPI_ERROR_ARGUMENT;		return NULL;	}	if (_SPI_curid + 1 == _SPI_connected)		/* connected */	{		if (_SPI_current != &(_SPI_stack[_SPI_curid + 1]))			elog(FATAL, "SPI: stack corrupted");		oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);	}	SPI_result = 0;	numberOfAttributes = rel->rd_att->natts;	v = (Datum *) palloc(numberOfAttributes * sizeof(Datum));	n = (char *) palloc(numberOfAttributes * sizeof(char));	/* fetch old values and nulls */	for (i = 0; i < numberOfAttributes; i++)	{		v[i] = heap_getattr(tuple, i + 1, rel->rd_att, &isnull);		n[i] = (isnull) ? 'n' : ' ';	}	/* replace values and nulls */	for (i = 0; i < natts; i++)	{		if (attnum[i] <= 0 || attnum[i] > numberOfAttributes)			break;		v[attnum[i] - 1] = Values[i];		n[attnum[i] - 1] = (Nulls && Nulls[i] == 'n') ? 'n' : ' ';	}	if (i == natts)				/* no errors in *attnum */	{		mtuple = heap_formtuple(rel->rd_att, v, n);		infomask = mtuple->t_data->t_infomask;		memmove(&(mtuple->t_data->t_oid), &(tuple->t_data->t_oid),				((char *) &(tuple->t_data->t_hoff) -				 (char *) &(tuple->t_data->t_oid)));		mtuple->t_data->t_infomask = infomask;		mtuple->t_data->t_natts = numberOfAttributes;	}	else	{		mtuple = NULL;		SPI_result = SPI_ERROR_NOATTRIBUTE;	}	pfree(v);	pfree(n);	if (oldcxt)		MemoryContextSwitchTo(oldcxt);	return mtuple;}intSPI_fnumber(TupleDesc tupdesc, char *fname){	int			res;	for (res = 0; res < tupdesc->natts; res++)	{		if (strcasecmp(tupdesc->attrs[res]->attname.data, fname) == 0)			return res + 1;	}	return SPI_ERROR_NOATTRIBUTE;}char *SPI_fname(TupleDesc tupdesc, int fnumber){	SPI_result = 0;	if (tupdesc->natts < fnumber || fnumber <= 0)	{		SPI_result = SPI_ERROR_NOATTRIBUTE;		return NULL;	}	return nameout(&(tupdesc->attrs[fnumber - 1]->attname));}char *SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber){	Datum		val;	bool		isnull;	Oid			foutoid,				typelem;	SPI_result = 0;	if (tuple->t_data->t_natts < fnumber || fnumber <= 0)	{		SPI_result = SPI_ERROR_NOATTRIBUTE;		return NULL;	}	val = heap_getattr(tuple, fnumber, tupdesc, &isnull);	if (isnull)		return NULL;	if (!getTypeOutAndElem((Oid) tupdesc->attrs[fnumber - 1]->atttypid,						   &foutoid, &typelem))	{		SPI_result = SPI_ERROR_NOOUTFUNC;		return NULL;	}	return (fmgr(foutoid, val, typelem,				 tupdesc->attrs[fnumber - 1]->atttypmod));}DatumSPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull){	Datum		val;	*isnull = true;	SPI_result = 0;	if (tuple->t_data->t_natts < fnumber || fnumber <= 0)	{		SPI_result = SPI_ERROR_NOATTRIBUTE;		return (Datum) NULL;	}	val = heap_getattr(tuple, fnumber, tupdesc, isnull);	return val;}char *SPI_gettype(TupleDesc tupdesc, int fnumber){	HeapTuple	typeTuple;	SPI_result = 0;	if (tupdesc->natts < fnumber || fnumber <= 0)	{		SPI_result = SPI_ERROR_NOATTRIBUTE;		return NULL;	}	typeTuple = SearchSysCacheTuple(TYPOID,				 ObjectIdGetDatum(tupdesc->attrs[fnumber - 1]->atttypid),									0, 0, 0);	if (!HeapTupleIsValid(typeTuple))	{		SPI_result = SPI_ERROR_TYPUNKNOWN;		return NULL;	}	return pstrdup(((Form_pg_type) GETSTRUCT(typeTuple))->typname.data);}OidSPI_gettypeid(TupleDesc tupdesc, int fnumber){	SPI_result = 0;	if (tupdesc->natts < fnumber || fnumber <= 0)	{		SPI_result = SPI_ERROR_NOATTRIBUTE;		return InvalidOid;	}	return tupdesc->attrs[fnumber - 1]->atttypid;}char *SPI_getrelname(Relation rel){	return pstrdup(rel->rd_rel->relname.data);}void *SPI_palloc(Size size){	MemoryContext oldcxt = NULL;	void	   *pointer;	if (_SPI_curid + 1 == _SPI_connected)		/* connected */	{		if (_SPI_current != &(_SPI_stack[_SPI_curid + 1]))			elog(FATAL, "SPI: stack corrupted");		oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);	}	pointer = palloc(size);	if (oldcxt)		MemoryContextSwitchTo(oldcxt);	return pointer;}void *SPI_repalloc(void *pointer, Size size){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -