1997-10-28 16:11:45 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* proclang.c
|
1997-10-28 16:11:45 +01:00
|
|
|
* PostgreSQL PROCEDURAL LANGUAGE support code.
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include <ctype.h>
|
1998-04-27 06:08:07 +02:00
|
|
|
|
1997-10-28 16:11:45 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "catalog/catname.h"
|
1999-11-22 18:56:41 +01:00
|
|
|
#include "catalog/indexing.h"
|
1997-10-28 16:11:45 +01:00
|
|
|
#include "catalog/pg_language.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "catalog/pg_proc.h"
|
|
|
|
#include "catalog/pg_shadow.h"
|
1997-10-28 16:11:45 +01:00
|
|
|
#include "commands/proclang.h"
|
|
|
|
#include "fmgr.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "utils/syscache.h"
|
1997-10-28 16:11:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
case_translate_language_name(const char *input, char *output)
|
|
|
|
{
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
Translate the input language name to lower case, except if it's C,
|
|
|
|
translate to upper case.
|
|
|
|
--------------------------------------------------------------------------*/
|
|
|
|
int i;
|
|
|
|
|
1997-12-05 02:13:24 +01:00
|
|
|
for (i = 0; i < NAMEDATALEN && input[i]; ++i)
|
1997-10-28 16:11:45 +01:00
|
|
|
output[i] = tolower(input[i]);
|
|
|
|
|
|
|
|
output[i] = '\0';
|
|
|
|
|
|
|
|
if (strcmp(output, "c") == 0)
|
|
|
|
output[0] = 'C';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------
|
|
|
|
* CREATE PROCEDURAL LANGUAGE
|
|
|
|
* ---------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
1998-02-26 05:46:47 +01:00
|
|
|
CreateProceduralLanguage(CreatePLangStmt *stmt)
|
1997-10-28 16:11:45 +01:00
|
|
|
{
|
|
|
|
char languageName[NAMEDATALEN];
|
|
|
|
HeapTuple langTup;
|
|
|
|
HeapTuple procTup;
|
|
|
|
|
2000-01-10 18:14:46 +01:00
|
|
|
Oid typev[FUNC_MAX_ARGS];
|
1997-10-28 16:11:45 +01:00
|
|
|
char nulls[Natts_pg_language];
|
|
|
|
Datum values[Natts_pg_language];
|
1998-08-19 04:04:17 +02:00
|
|
|
Relation rel;
|
1997-10-28 16:11:45 +01:00
|
|
|
HeapTuple tup;
|
|
|
|
TupleDesc tupDesc;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* Check permission
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (!superuser())
|
|
|
|
{
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Only users with Postgres superuser privilege are "
|
1997-10-28 16:11:45 +01:00
|
|
|
"permitted to create procedural languages");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* Translate the language name and check that
|
|
|
|
* this language doesn't already exist
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
case_translate_language_name(stmt->plname, languageName);
|
|
|
|
|
1999-11-22 18:56:41 +01:00
|
|
|
langTup = SearchSysCacheTuple(LANGNAME,
|
1997-10-28 16:11:45 +01:00
|
|
|
PointerGetDatum(languageName),
|
|
|
|
0, 0, 0);
|
|
|
|
if (HeapTupleIsValid(langTup))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Language %s already exists", languageName);
|
1997-10-28 16:11:45 +01:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* Lookup the PL handler function and check that it is
|
|
|
|
* of return type Opaque
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
memset(typev, 0, sizeof(typev));
|
1999-11-22 18:56:41 +01:00
|
|
|
procTup = SearchSysCacheTuple(PROCNAME,
|
1997-10-28 16:11:45 +01:00
|
|
|
PointerGetDatum(stmt->plhandler),
|
1998-08-19 04:04:17 +02:00
|
|
|
Int32GetDatum(0),
|
1997-10-28 16:11:45 +01:00
|
|
|
PointerGetDatum(typev),
|
|
|
|
0);
|
|
|
|
if (!HeapTupleIsValid(procTup))
|
|
|
|
{
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "PL handler function %s() doesn't exist",
|
1997-10-28 16:11:45 +01:00
|
|
|
stmt->plhandler);
|
|
|
|
}
|
|
|
|
if (((Form_pg_proc) GETSTRUCT(procTup))->prorettype != InvalidOid)
|
|
|
|
{
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "PL handler function %s() isn't of return type Opaque",
|
1997-10-28 16:11:45 +01:00
|
|
|
stmt->plhandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* Insert the new language into pg_language
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
for (i = 0; i < Natts_pg_language; i++)
|
|
|
|
{
|
|
|
|
nulls[i] = ' ';
|
|
|
|
values[i] = (Datum) NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
values[i++] = PointerGetDatum(languageName);
|
|
|
|
values[i++] = Int8GetDatum((bool) 1);
|
|
|
|
values[i++] = Int8GetDatum(stmt->pltrusted);
|
1998-11-27 20:52:36 +01:00
|
|
|
values[i++] = ObjectIdGetDatum(procTup->t_data->t_oid);
|
1998-04-27 06:08:07 +02:00
|
|
|
values[i++] = (Datum) fmgr(F_TEXTIN, stmt->plcompiler);
|
1997-10-28 16:11:45 +01:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
rel = heap_openr(LanguageRelationName, RowExclusiveLock);
|
1997-10-28 16:11:45 +01:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
tupDesc = rel->rd_att;
|
1997-10-28 16:11:45 +01:00
|
|
|
tup = heap_formtuple(tupDesc, values, nulls);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
heap_insert(rel, tup);
|
1997-10-28 16:11:45 +01:00
|
|
|
|
1999-11-22 18:56:41 +01:00
|
|
|
if (RelationGetForm(rel)->relhasindex)
|
|
|
|
{
|
|
|
|
Relation idescs[Num_pg_language_indices];
|
|
|
|
|
|
|
|
CatalogOpenIndices(Num_pg_language_indices, Name_pg_language_indices, idescs);
|
|
|
|
CatalogIndexInsert(idescs, Num_pg_language_indices, rel, tup);
|
|
|
|
CatalogCloseIndices(Num_pg_language_indices, idescs);
|
|
|
|
}
|
2000-01-10 18:14:46 +01:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(rel, RowExclusiveLock);
|
1997-10-28 16:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------
|
|
|
|
* DROP PROCEDURAL LANGUAGE
|
|
|
|
* ---------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
1998-02-26 05:46:47 +01:00
|
|
|
DropProceduralLanguage(DropPLangStmt *stmt)
|
1997-10-28 16:11:45 +01:00
|
|
|
{
|
|
|
|
char languageName[NAMEDATALEN];
|
|
|
|
HeapTuple langTup;
|
1998-08-19 04:04:17 +02:00
|
|
|
Relation rel;
|
1997-10-28 16:11:45 +01:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* Check permission
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (!superuser())
|
|
|
|
{
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Only users with Postgres superuser privilege are "
|
1997-10-28 16:11:45 +01:00
|
|
|
"permitted to drop procedural languages");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* Translate the language name, check that
|
|
|
|
* this language exist and is a PL
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
case_translate_language_name(stmt->plname, languageName);
|
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
rel = heap_openr(LanguageRelationName, RowExclusiveLock);
|
|
|
|
|
1999-11-22 18:56:41 +01:00
|
|
|
langTup = SearchSysCacheTupleCopy(LANGNAME,
|
1998-09-01 06:40:42 +02:00
|
|
|
PointerGetDatum(languageName),
|
|
|
|
0, 0, 0);
|
1997-10-28 16:11:45 +01:00
|
|
|
if (!HeapTupleIsValid(langTup))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Language %s doesn't exist", languageName);
|
1997-10-28 16:11:45 +01:00
|
|
|
|
|
|
|
if (!((Form_pg_language) GETSTRUCT(langTup))->lanispl)
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Language %s isn't a created procedural language",
|
1997-10-28 16:11:45 +01:00
|
|
|
languageName);
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
heap_delete(rel, &langTup->t_self, NULL);
|
1997-10-28 16:11:45 +01:00
|
|
|
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(langTup);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(rel, RowExclusiveLock);
|
1997-10-28 16:11:45 +01:00
|
|
|
}
|