mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Massive commit to run PGINDENT on all *.c and *.h files.
This commit is contained in:
@ -8,8 +8,8 @@
|
||||
* For example array_int4eq returns true if some of the elements
|
||||
* of an array of int4 is equal to the given value:
|
||||
*
|
||||
* array_int4eq({1,2,3}, 1) --> true
|
||||
* array_int4eq({1,2,3}, 4) --> false
|
||||
* array_int4eq({1,2,3}, 1) --> true
|
||||
* array_int4eq({1,2,3}, 4) --> false
|
||||
*
|
||||
* If we have defined T array types and O scalar operators
|
||||
* we can define T x O array operators, each of them has a name
|
||||
@ -19,10 +19,10 @@
|
||||
* the array_int4_like because there is no like operator for int4.
|
||||
* It is now possible to write queries which look inside the arrays:
|
||||
*
|
||||
* create table t(id int4[], txt text[]);
|
||||
* select * from t where t.id *= 123;
|
||||
* select * from t where t.txt *~ '[a-z]';
|
||||
* select * from t where t.txt[1:3] **~ '[a-z]';
|
||||
* create table t(id int4[], txt text[]);
|
||||
* select * from t where t.id *= 123;
|
||||
* select * from t where t.txt *~ '[a-z]';
|
||||
* select * from t where t.txt[1:3] **~ '[a-z]';
|
||||
*
|
||||
* Copyright (c) 1996, Massimo Dal Zotto <dz@cs.unitn.it>
|
||||
*/
|
||||
@ -40,93 +40,116 @@
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/elog.h"
|
||||
|
||||
static int32
|
||||
array_iterator(Oid elemtype, Oid proc, int and, ArrayType *array, Datum value)
|
||||
static int32
|
||||
array_iterator(Oid elemtype, Oid proc, int and, ArrayType * array, Datum value)
|
||||
{
|
||||
HeapTuple typ_tuple;
|
||||
TypeTupleForm typ_struct;
|
||||
bool typbyval;
|
||||
int typlen;
|
||||
func_ptr proc_fn;
|
||||
int pronargs;
|
||||
int nitems, i, result;
|
||||
int ndim, *dim;
|
||||
char *p;
|
||||
HeapTuple typ_tuple;
|
||||
TypeTupleForm typ_struct;
|
||||
bool typbyval;
|
||||
int typlen;
|
||||
func_ptr proc_fn;
|
||||
int pronargs;
|
||||
int nitems,
|
||||
i,
|
||||
result;
|
||||
int ndim,
|
||||
*dim;
|
||||
char *p;
|
||||
|
||||
/* Sanity checks */
|
||||
if ((array == (ArrayType *) NULL)
|
||||
|| (ARR_IS_LO(array) == true)) {
|
||||
/* elog(NOTICE, "array_iterator: array is null"); */
|
||||
return (0);
|
||||
}
|
||||
ndim = ARR_NDIM(array);
|
||||
dim = ARR_DIMS(array);
|
||||
nitems = getNitems(ndim, dim);
|
||||
if (nitems == 0) {
|
||||
/* elog(NOTICE, "array_iterator: nitems = 0"); */
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Lookup element type information */
|
||||
typ_tuple = SearchSysCacheTuple(TYPOID, ObjectIdGetDatum(elemtype),0,0,0);
|
||||
if (!HeapTupleIsValid(typ_tuple)) {
|
||||
elog(WARN,"array_iterator: cache lookup failed for type %d", elemtype);
|
||||
return 0;
|
||||
}
|
||||
typ_struct = (TypeTupleForm) GETSTRUCT(typ_tuple);
|
||||
typlen = typ_struct->typlen;
|
||||
typbyval = typ_struct->typbyval;
|
||||
|
||||
/* Lookup the function entry point */
|
||||
proc_fn == (func_ptr) NULL;
|
||||
fmgr_info(proc, &proc_fn, &pronargs);
|
||||
if ((proc_fn == NULL) || (pronargs != 2)) {
|
||||
elog(WARN, "array_iterator: fmgr_info lookup failed for oid %d", proc);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Scan the array and apply the operator to each element */
|
||||
result = 0;
|
||||
p = ARR_DATA_PTR(array);
|
||||
for (i = 0; i < nitems; i++) {
|
||||
if (typbyval) {
|
||||
switch(typlen) {
|
||||
case 1:
|
||||
result = (int) (*proc_fn)(*p, value);
|
||||
break;
|
||||
case 2:
|
||||
result = (int) (*proc_fn)(* (int16 *) p, value);
|
||||
break;
|
||||
case 3:
|
||||
case 4:
|
||||
result = (int) (*proc_fn)(* (int32 *) p, value);
|
||||
break;
|
||||
}
|
||||
p += typlen;
|
||||
} else {
|
||||
result = (int) (*proc_fn)(p, value);
|
||||
if (typlen > 0) {
|
||||
p += typlen;
|
||||
} else {
|
||||
p += INTALIGN(* (int32 *) p);
|
||||
}
|
||||
}
|
||||
if (result) {
|
||||
if (!and) {
|
||||
return (1);
|
||||
}
|
||||
} else {
|
||||
if (and) {
|
||||
/* Sanity checks */
|
||||
if ((array == (ArrayType *) NULL)
|
||||
|| (ARR_IS_LO(array) == true))
|
||||
{
|
||||
/* elog(NOTICE, "array_iterator: array is null"); */
|
||||
return (0);
|
||||
}
|
||||
ndim = ARR_NDIM(array);
|
||||
dim = ARR_DIMS(array);
|
||||
nitems = getNitems(ndim, dim);
|
||||
if (nitems == 0)
|
||||
{
|
||||
/* elog(NOTICE, "array_iterator: nitems = 0"); */
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (and && result) {
|
||||
return (1);
|
||||
} else {
|
||||
return (0);
|
||||
}
|
||||
/* Lookup element type information */
|
||||
typ_tuple = SearchSysCacheTuple(TYPOID, ObjectIdGetDatum(elemtype), 0, 0, 0);
|
||||
if (!HeapTupleIsValid(typ_tuple))
|
||||
{
|
||||
elog(WARN, "array_iterator: cache lookup failed for type %d", elemtype);
|
||||
return 0;
|
||||
}
|
||||
typ_struct = (TypeTupleForm) GETSTRUCT(typ_tuple);
|
||||
typlen = typ_struct->typlen;
|
||||
typbyval = typ_struct->typbyval;
|
||||
|
||||
/* Lookup the function entry point */
|
||||
proc_fn == (func_ptr) NULL;
|
||||
fmgr_info(proc, &proc_fn, &pronargs);
|
||||
if ((proc_fn == NULL) || (pronargs != 2))
|
||||
{
|
||||
elog(WARN, "array_iterator: fmgr_info lookup failed for oid %d", proc);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Scan the array and apply the operator to each element */
|
||||
result = 0;
|
||||
p = ARR_DATA_PTR(array);
|
||||
for (i = 0; i < nitems; i++)
|
||||
{
|
||||
if (typbyval)
|
||||
{
|
||||
switch (typlen)
|
||||
{
|
||||
case 1:
|
||||
result = (int) (*proc_fn) (*p, value);
|
||||
break;
|
||||
case 2:
|
||||
result = (int) (*proc_fn) (*(int16 *) p, value);
|
||||
break;
|
||||
case 3:
|
||||
case 4:
|
||||
result = (int) (*proc_fn) (*(int32 *) p, value);
|
||||
break;
|
||||
}
|
||||
p += typlen;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = (int) (*proc_fn) (p, value);
|
||||
if (typlen > 0)
|
||||
{
|
||||
p += typlen;
|
||||
}
|
||||
else
|
||||
{
|
||||
p += INTALIGN(*(int32 *) p);
|
||||
}
|
||||
}
|
||||
if (result)
|
||||
{
|
||||
if (!and)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (and)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (and && result)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -134,39 +157,39 @@ array_iterator(Oid elemtype, Oid proc, int and, ArrayType *array, Datum value)
|
||||
*/
|
||||
|
||||
int32
|
||||
array_texteq(ArrayType *array, char* value)
|
||||
array_texteq(ArrayType * array, char *value)
|
||||
{
|
||||
return array_iterator((Oid) 25, /* text */
|
||||
(Oid) 67, /* texteq */
|
||||
0, /* logical or */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 25, /* text */
|
||||
(Oid) 67, /* texteq */
|
||||
0, /* logical or */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
int32
|
||||
array_all_texteq(ArrayType *array, char* value)
|
||||
array_all_texteq(ArrayType * array, char *value)
|
||||
{
|
||||
return array_iterator((Oid) 25, /* text */
|
||||
(Oid) 67, /* texteq */
|
||||
1, /* logical and */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 25, /* text */
|
||||
(Oid) 67, /* texteq */
|
||||
1, /* logical and */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
int32
|
||||
array_textregexeq(ArrayType *array, char* value)
|
||||
array_textregexeq(ArrayType * array, char *value)
|
||||
{
|
||||
return array_iterator((Oid) 25, /* text */
|
||||
(Oid) 81, /* textregexeq */
|
||||
0, /* logical or */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 25, /* text */
|
||||
(Oid) 81, /* textregexeq */
|
||||
0, /* logical or */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
int32
|
||||
array_all_textregexeq(ArrayType *array, char* value)
|
||||
array_all_textregexeq(ArrayType * array, char *value)
|
||||
{
|
||||
return array_iterator((Oid) 25, /* text */
|
||||
(Oid) 81, /* textregexeq */
|
||||
1, /* logical and */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 25, /* text */
|
||||
(Oid) 81, /* textregexeq */
|
||||
1, /* logical and */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -175,39 +198,39 @@ array_all_textregexeq(ArrayType *array, char* value)
|
||||
*/
|
||||
|
||||
int32
|
||||
array_char16eq(ArrayType *array, char* value)
|
||||
array_char16eq(ArrayType * array, char *value)
|
||||
{
|
||||
return array_iterator((Oid) 20, /* char16 */
|
||||
(Oid) 490, /* char16eq */
|
||||
0, /* logical or */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 20, /* char16 */
|
||||
(Oid) 490, /* char16eq */
|
||||
0, /* logical or */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
int32
|
||||
array_all_char16eq(ArrayType *array, char* value)
|
||||
array_all_char16eq(ArrayType * array, char *value)
|
||||
{
|
||||
return array_iterator((Oid) 20, /* char16 */
|
||||
(Oid) 490, /* char16eq */
|
||||
1, /* logical and */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 20, /* char16 */
|
||||
(Oid) 490, /* char16eq */
|
||||
1, /* logical and */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
int32
|
||||
array_char16regexeq(ArrayType *array, char* value)
|
||||
array_char16regexeq(ArrayType * array, char *value)
|
||||
{
|
||||
return array_iterator((Oid) 20, /* char16 */
|
||||
(Oid) 700, /* char16regexeq */
|
||||
0, /* logical or */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 20, /* char16 */
|
||||
(Oid) 700, /* char16regexeq */
|
||||
0, /* logical or */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
int32
|
||||
array_all_char16regexeq(ArrayType *array, char* value)
|
||||
array_all_char16regexeq(ArrayType * array, char *value)
|
||||
{
|
||||
return array_iterator((Oid) 20, /* char16 */
|
||||
(Oid) 700, /* char16regexeq */
|
||||
1, /* logical and */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 20, /* char16 */
|
||||
(Oid) 700, /* char16regexeq */
|
||||
1, /* logical and */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -215,37 +238,37 @@ array_all_char16regexeq(ArrayType *array, char* value)
|
||||
*/
|
||||
|
||||
int32
|
||||
array_int4eq(ArrayType *array, int4 value)
|
||||
array_int4eq(ArrayType * array, int4 value)
|
||||
{
|
||||
return array_iterator((Oid) 23, /* int4 */
|
||||
(Oid) 65, /* int4eq */
|
||||
0, /* logical or */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 23, /* int4 */
|
||||
(Oid) 65, /* int4eq */
|
||||
0, /* logical or */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
int32
|
||||
array_all_int4eq(ArrayType *array, int4 value)
|
||||
array_all_int4eq(ArrayType * array, int4 value)
|
||||
{
|
||||
return array_iterator((Oid) 23, /* int4 */
|
||||
(Oid) 65, /* int4eq */
|
||||
1, /* logical and */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 23, /* int4 */
|
||||
(Oid) 65, /* int4eq */
|
||||
1, /* logical and */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
int32
|
||||
array_int4gt(ArrayType *array, int4 value)
|
||||
array_int4gt(ArrayType * array, int4 value)
|
||||
{
|
||||
return array_iterator((Oid) 23, /* int4 */
|
||||
(Oid) 147, /* int4gt */
|
||||
0, /* logical or */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 23, /* int4 */
|
||||
(Oid) 147, /* int4gt */
|
||||
0, /* logical or */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
||||
int32
|
||||
array_all_int4gt(ArrayType *array, int4 value)
|
||||
array_all_int4gt(ArrayType * array, int4 value)
|
||||
{
|
||||
return array_iterator((Oid) 23, /* int4 */
|
||||
(Oid) 147, /* int4gt */
|
||||
1, /* logical and */
|
||||
array, (Datum)value);
|
||||
return array_iterator((Oid) 23, /* int4 */
|
||||
(Oid) 147, /* int4gt */
|
||||
1, /* logical and */
|
||||
array, (Datum) value);
|
||||
}
|
||||
|
@ -13,86 +13,99 @@
|
||||
#include "utils/datetime.h"
|
||||
|
||||
|
||||
TimeADT *time_difference(TimeADT * time1, TimeADT * time2)
|
||||
TimeADT *
|
||||
time_difference(TimeADT * time1, TimeADT * time2)
|
||||
{
|
||||
TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT));
|
||||
*result = *time1 - *time2;
|
||||
return (result);
|
||||
TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT));
|
||||
|
||||
*result = *time1 - *time2;
|
||||
return (result);
|
||||
}
|
||||
|
||||
TimeADT *currenttime()
|
||||
TimeADT *
|
||||
currenttime()
|
||||
{
|
||||
time_t current_time;
|
||||
struct tm *tm;
|
||||
TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT));
|
||||
time_t current_time;
|
||||
struct tm *tm;
|
||||
TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT));
|
||||
|
||||
current_time = time(NULL);
|
||||
tm = localtime(¤t_time);
|
||||
*result = ((((tm->tm_hour*60)+tm->tm_min)*60)+tm->tm_sec);
|
||||
return (result);
|
||||
current_time = time(NULL);
|
||||
tm = localtime(¤t_time);
|
||||
*result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec);
|
||||
return (result);
|
||||
}
|
||||
DateADT currentdate()
|
||||
DateADT
|
||||
currentdate()
|
||||
{
|
||||
time_t current_time;
|
||||
struct tm *tm;
|
||||
DateADT result;
|
||||
current_time = time(NULL);
|
||||
tm = localtime(¤t_time);
|
||||
time_t current_time;
|
||||
struct tm *tm;
|
||||
DateADT result;
|
||||
|
||||
result = date2j(tm->tm_year,tm->tm_mon + 1,tm->tm_mday) -
|
||||
date2j(100,1,1);
|
||||
return (result);
|
||||
current_time = time(NULL);
|
||||
tm = localtime(¤t_time);
|
||||
|
||||
result = date2j(tm->tm_year, tm->tm_mon + 1, tm->tm_mday) -
|
||||
date2j(100, 1, 1);
|
||||
return (result);
|
||||
}
|
||||
int4 hours(TimeADT * time)
|
||||
int4
|
||||
hours(TimeADT * time)
|
||||
{
|
||||
return(*time / (60*60));
|
||||
return (*time / (60 * 60));
|
||||
}
|
||||
|
||||
int4 minutes(TimeADT * time)
|
||||
int4
|
||||
minutes(TimeADT * time)
|
||||
{
|
||||
return(((int) (*time / 60)) % 60);
|
||||
return (((int) (*time / 60)) % 60);
|
||||
}
|
||||
|
||||
int4 seconds(TimeADT * time)
|
||||
int4
|
||||
seconds(TimeADT * time)
|
||||
{
|
||||
return(((int) *time) % 60);
|
||||
return (((int) *time) % 60);
|
||||
}
|
||||
int4 day(DateADT *date)
|
||||
int4
|
||||
day(DateADT * date)
|
||||
{
|
||||
struct tm tm;
|
||||
struct tm tm;
|
||||
|
||||
j2date( (*date + date2j(2000,1,1)),
|
||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
||||
j2date((*date + date2j(2000, 1, 1)),
|
||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
||||
|
||||
return (tm.tm_mday);
|
||||
return (tm.tm_mday);
|
||||
}
|
||||
int4 month(DateADT *date)
|
||||
int4
|
||||
month(DateADT * date)
|
||||
{
|
||||
struct tm tm;
|
||||
struct tm tm;
|
||||
|
||||
j2date( (*date + date2j(2000,1,1)),
|
||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
||||
j2date((*date + date2j(2000, 1, 1)),
|
||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
||||
|
||||
return (tm.tm_mon);
|
||||
return (tm.tm_mon);
|
||||
}
|
||||
int4 year(DateADT *date)
|
||||
int4
|
||||
year(DateADT * date)
|
||||
{
|
||||
struct tm tm;
|
||||
struct tm tm;
|
||||
|
||||
j2date( (*date + date2j(2000,1,1)),
|
||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
||||
j2date((*date + date2j(2000, 1, 1)),
|
||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
||||
|
||||
return (tm.tm_year);
|
||||
return (tm.tm_year);
|
||||
}
|
||||
int4 asminutes(TimeADT * time)
|
||||
int4
|
||||
asminutes(TimeADT * time)
|
||||
{
|
||||
int seconds = (int) *time;
|
||||
int seconds = (int) *time;
|
||||
|
||||
return (seconds / 60);
|
||||
return (seconds / 60);
|
||||
}
|
||||
int4 asseconds(TimeADT * time)
|
||||
int4
|
||||
asseconds(TimeADT * time)
|
||||
{
|
||||
int seconds = (int) *time;
|
||||
int seconds = (int) *time;
|
||||
|
||||
return (seconds);
|
||||
return (seconds);
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* int8.c--
|
||||
* Internal 64-bit integer operations
|
||||
* Internal 64-bit integer operations
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include <stdio.h> /* for sprintf proto, etc. */
|
||||
#include <stdlib.h> /* for strtod, etc. */
|
||||
#include <stdio.h> /* for sprintf proto, etc. */
|
||||
#include <stdlib.h> /* for strtod, etc. */
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
@ -17,7 +17,7 @@
|
||||
#include "postgres.h"
|
||||
#include "utils/palloc.h"
|
||||
|
||||
#define MAXINT8LEN 25
|
||||
#define MAXINT8LEN 25
|
||||
|
||||
#define USE_LOCAL_CODE 1
|
||||
|
||||
@ -26,53 +26,58 @@
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_64BIT_INTS
|
||||
typedef char[8] int64;
|
||||
typedef char [8] int64;
|
||||
|
||||
#elif defined(__alpha)
|
||||
typedef long int int64;
|
||||
|
||||
#define INT64_FORMAT "%ld"
|
||||
|
||||
#elif defined(__GNUC__)
|
||||
typedef long long int int64;
|
||||
|
||||
#define INT64_FORMAT "%Ld"
|
||||
|
||||
#else
|
||||
typedef long int int64;
|
||||
|
||||
#define INT64_FORMAT "%ld"
|
||||
#endif
|
||||
|
||||
int64 *int8in(char *str);
|
||||
char *int8out(int64 *val);
|
||||
int64 *int8in(char *str);
|
||||
char *int8out(int64 * val);
|
||||
|
||||
bool int8eq(int64 *val1, int64 *val2);
|
||||
bool int8ne(int64 *val1, int64 *val2);
|
||||
bool int8lt(int64 *val1, int64 *val2);
|
||||
bool int8gt(int64 *val1, int64 *val2);
|
||||
bool int8le(int64 *val1, int64 *val2);
|
||||
bool int8ge(int64 *val1, int64 *val2);
|
||||
bool int8eq(int64 * val1, int64 * val2);
|
||||
bool int8ne(int64 * val1, int64 * val2);
|
||||
bool int8lt(int64 * val1, int64 * val2);
|
||||
bool int8gt(int64 * val1, int64 * val2);
|
||||
bool int8le(int64 * val1, int64 * val2);
|
||||
bool int8ge(int64 * val1, int64 * val2);
|
||||
|
||||
bool int84eq(int64 *val1, int32 val2);
|
||||
bool int84ne(int64 *val1, int32 val2);
|
||||
bool int84lt(int64 *val1, int32 val2);
|
||||
bool int84gt(int64 *val1, int32 val2);
|
||||
bool int84le(int64 *val1, int32 val2);
|
||||
bool int84ge(int64 *val1, int32 val2);
|
||||
bool int84eq(int64 * val1, int32 val2);
|
||||
bool int84ne(int64 * val1, int32 val2);
|
||||
bool int84lt(int64 * val1, int32 val2);
|
||||
bool int84gt(int64 * val1, int32 val2);
|
||||
bool int84le(int64 * val1, int32 val2);
|
||||
bool int84ge(int64 * val1, int32 val2);
|
||||
|
||||
int64 *int8um(int64 *val);
|
||||
int64 *int8pl(int64 *val1, int64 *val2);
|
||||
int64 *int8mi(int64 *val1, int64 *val2);
|
||||
int64 *int8mul(int64 *val1, int64 *val2);
|
||||
int64 *int8div(int64 *val1, int64 *val2);
|
||||
int64 *int8um(int64 * val);
|
||||
int64 *int8pl(int64 * val1, int64 * val2);
|
||||
int64 *int8mi(int64 * val1, int64 * val2);
|
||||
int64 *int8mul(int64 * val1, int64 * val2);
|
||||
int64 *int8div(int64 * val1, int64 * val2);
|
||||
|
||||
int64 *int48(int32 val);
|
||||
int32 int84(int64 * val);
|
||||
|
||||
int64 *int48(int32 val);
|
||||
int32 int84(int64 *val);
|
||||
#if FALSE
|
||||
int64 *int28(int16 val);
|
||||
int16 int82(int64 *val);
|
||||
int64 *int28(int16 val);
|
||||
int16 int82(int64 * val);
|
||||
|
||||
#endif
|
||||
|
||||
float64 i8tod(int64 *val);
|
||||
int64 *dtoi8(float64 val);
|
||||
float64 i8tod(int64 * val);
|
||||
int64 *dtoi8(float64 val);
|
||||
|
||||
#if USE_LOCAL_CODE
|
||||
|
||||
@ -88,7 +93,7 @@ int64 *dtoi8(float64 val);
|
||||
|
||||
/***********************************************************************
|
||||
**
|
||||
** Routines for 64-bit integers.
|
||||
** Routines for 64-bit integers.
|
||||
**
|
||||
***********************************************************************/
|
||||
|
||||
@ -98,264 +103,289 @@ int64 *dtoi8(float64 val);
|
||||
|
||||
/* int8in()
|
||||
*/
|
||||
int64 *int8in(char *str)
|
||||
int64 *
|
||||
int8in(char *str)
|
||||
{
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
|
||||
#if HAVE_64BIT_INTS
|
||||
if (!PointerIsValid(str))
|
||||
elog (WARN,"Bad (null) int8 external representation",NULL);
|
||||
if (!PointerIsValid(str))
|
||||
elog(WARN, "Bad (null) int8 external representation", NULL);
|
||||
|
||||
if (sscanf(str, INT64_FORMAT, result) != 1)
|
||||
elog(WARN,"Bad int8 external representation '%s'",str);
|
||||
if (sscanf(str, INT64_FORMAT, result) != 1)
|
||||
elog(WARN, "Bad int8 external representation '%s'", str);
|
||||
|
||||
#else
|
||||
elog(WARN,"64-bit integers are not supported",NULL);
|
||||
result = NULL;
|
||||
elog(WARN, "64-bit integers are not supported", NULL);
|
||||
result = NULL;
|
||||
#endif
|
||||
|
||||
return(result);
|
||||
} /* int8in() */
|
||||
return (result);
|
||||
} /* int8in() */
|
||||
|
||||
|
||||
/* int8out()
|
||||
*/
|
||||
char *int8out(int64 *val)
|
||||
char *
|
||||
int8out(int64 * val)
|
||||
{
|
||||
char *result;
|
||||
char *result;
|
||||
|
||||
int len;
|
||||
char buf[MAXINT8LEN+1];
|
||||
int len;
|
||||
char buf[MAXINT8LEN + 1];
|
||||
|
||||
#if HAVE_64BIT_INTS
|
||||
if (!PointerIsValid(val))
|
||||
return(NULL);
|
||||
if (!PointerIsValid(val))
|
||||
return (NULL);
|
||||
|
||||
if ((len = snprintf( buf, MAXINT8LEN, INT64_FORMAT, *val)) < 0)
|
||||
elog (WARN,"Unable to format int8",NULL);
|
||||
if ((len = snprintf(buf, MAXINT8LEN, INT64_FORMAT, *val)) < 0)
|
||||
elog(WARN, "Unable to format int8", NULL);
|
||||
|
||||
result = PALLOC(len+1);
|
||||
result = PALLOC(len + 1);
|
||||
|
||||
strcpy(result, buf);
|
||||
strcpy(result, buf);
|
||||
|
||||
#else
|
||||
elog(WARN,"64-bit integers are not supported",NULL);
|
||||
result = NULL;
|
||||
elog(WARN, "64-bit integers are not supported", NULL);
|
||||
result = NULL;
|
||||
#endif
|
||||
|
||||
return( result);
|
||||
} /* int8out() */
|
||||
return (result);
|
||||
} /* int8out() */
|
||||
|
||||
|
||||
/*----------------------------------------------------------
|
||||
* Relational operators for int8s.
|
||||
* Relational operators for int8s.
|
||||
*---------------------------------------------------------*/
|
||||
|
||||
/* int8relop()
|
||||
* Is val1 relop val2?
|
||||
*/
|
||||
bool int8eq(int64 *val1, int64 *val2)
|
||||
bool
|
||||
int8eq(int64 * val1, int64 * val2)
|
||||
{
|
||||
return(*val1 == *val2);
|
||||
} /* int8eq() */
|
||||
return (*val1 == *val2);
|
||||
} /* int8eq() */
|
||||
|
||||
bool int8ne(int64 *val1, int64 *val2)
|
||||
bool
|
||||
int8ne(int64 * val1, int64 * val2)
|
||||
{
|
||||
return(*val1 != *val2);
|
||||
} /* int8ne() */
|
||||
return (*val1 != *val2);
|
||||
} /* int8ne() */
|
||||
|
||||
bool int8lt(int64 *val1, int64 *val2)
|
||||
bool
|
||||
int8lt(int64 * val1, int64 * val2)
|
||||
{
|
||||
return(*val1 < *val2);
|
||||
} /* int8lt() */
|
||||
return (*val1 < *val2);
|
||||
} /* int8lt() */
|
||||
|
||||
bool int8gt(int64 *val1, int64 *val2)
|
||||
bool
|
||||
int8gt(int64 * val1, int64 * val2)
|
||||
{
|
||||
return(*val1 > *val2);
|
||||
} /* int8gt() */
|
||||
return (*val1 > *val2);
|
||||
} /* int8gt() */
|
||||
|
||||
bool int8le(int64 *val1, int64 *val2)
|
||||
bool
|
||||
int8le(int64 * val1, int64 * val2)
|
||||
{
|
||||
return(*val1 <= *val2);
|
||||
} /* int8le() */
|
||||
return (*val1 <= *val2);
|
||||
} /* int8le() */
|
||||
|
||||
bool int8ge(int64 *val1, int64 *val2)
|
||||
bool
|
||||
int8ge(int64 * val1, int64 * val2)
|
||||
{
|
||||
return(*val1 >= *val2);
|
||||
} /* int8ge() */
|
||||
return (*val1 >= *val2);
|
||||
} /* int8ge() */
|
||||
|
||||
|
||||
/* int84relop()
|
||||
* Is 64-bit val1 relop 32-bit val2?
|
||||
*/
|
||||
bool int84eq(int64 *val1, int32 val2)
|
||||
bool
|
||||
int84eq(int64 * val1, int32 val2)
|
||||
{
|
||||
return(*val1 == val2);
|
||||
} /* int84eq() */
|
||||
return (*val1 == val2);
|
||||
} /* int84eq() */
|
||||
|
||||
bool int84ne(int64 *val1, int32 val2)
|
||||
bool
|
||||
int84ne(int64 * val1, int32 val2)
|
||||
{
|
||||
return(*val1 != val2);
|
||||
} /* int84ne() */
|
||||
return (*val1 != val2);
|
||||
} /* int84ne() */
|
||||
|
||||
bool int84lt(int64 *val1, int32 val2)
|
||||
bool
|
||||
int84lt(int64 * val1, int32 val2)
|
||||
{
|
||||
return(*val1 < val2);
|
||||
} /* int84lt() */
|
||||
return (*val1 < val2);
|
||||
} /* int84lt() */
|
||||
|
||||
bool int84gt(int64 *val1, int32 val2)
|
||||
bool
|
||||
int84gt(int64 * val1, int32 val2)
|
||||
{
|
||||
return(*val1 > val2);
|
||||
} /* int84gt() */
|
||||
return (*val1 > val2);
|
||||
} /* int84gt() */
|
||||
|
||||
bool int84le(int64 *val1, int32 val2)
|
||||
bool
|
||||
int84le(int64 * val1, int32 val2)
|
||||
{
|
||||
return(*val1 <= val2);
|
||||
} /* int84le() */
|
||||
return (*val1 <= val2);
|
||||
} /* int84le() */
|
||||
|
||||
bool int84ge(int64 *val1, int32 val2)
|
||||
bool
|
||||
int84ge(int64 * val1, int32 val2)
|
||||
{
|
||||
return(*val1 >= val2);
|
||||
} /* int84ge() */
|
||||
return (*val1 >= val2);
|
||||
} /* int84ge() */
|
||||
|
||||
|
||||
/*----------------------------------------------------------
|
||||
* Arithmetic operators on 64-bit integers.
|
||||
* Arithmetic operators on 64-bit integers.
|
||||
*---------------------------------------------------------*/
|
||||
|
||||
int64 *int8um(int64 *val)
|
||||
int64 *
|
||||
int8um(int64 * val)
|
||||
{
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
|
||||
if (!PointerIsValid(val))
|
||||
return NULL;
|
||||
if (!PointerIsValid(val))
|
||||
return NULL;
|
||||
|
||||
*result = (- *val);
|
||||
*result = (-*val);
|
||||
|
||||
return(result);
|
||||
} /* int8um() */
|
||||
return (result);
|
||||
} /* int8um() */
|
||||
|
||||
int64 *int8pl(int64 *val1, int64 *val2)
|
||||
int64 *
|
||||
int8pl(int64 * val1, int64 * val2)
|
||||
{
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 + *val2;
|
||||
*result = *val1 + *val2;
|
||||
|
||||
return(result);
|
||||
} /* int8pl() */
|
||||
return (result);
|
||||
} /* int8pl() */
|
||||
|
||||
int64 *int8mi(int64 *val1, int64 *val2)
|
||||
int64 *
|
||||
int8mi(int64 * val1, int64 * val2)
|
||||
{
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 - *val2;
|
||||
*result = *val1 - *val2;
|
||||
|
||||
return(result);
|
||||
} /* int8mi() */
|
||||
return (result);
|
||||
} /* int8mi() */
|
||||
|
||||
int64 *int8mul(int64 *val1, int64 *val2)
|
||||
int64 *
|
||||
int8mul(int64 * val1, int64 * val2)
|
||||
{
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 * *val2;
|
||||
*result = *val1 * *val2;
|
||||
|
||||
return(result);
|
||||
} /* int8mul() */
|
||||
return (result);
|
||||
} /* int8mul() */
|
||||
|
||||
int64 *int8div(int64 *val1, int64 *val2)
|
||||
int64 *
|
||||
int8div(int64 * val1, int64 * val2)
|
||||
{
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 / *val2;
|
||||
*result = *val1 / *val2;
|
||||
|
||||
return(result);
|
||||
} /* int8div() */
|
||||
return (result);
|
||||
} /* int8div() */
|
||||
|
||||
|
||||
/*----------------------------------------------------------
|
||||
* Conversion operators.
|
||||
* Conversion operators.
|
||||
*---------------------------------------------------------*/
|
||||
|
||||
int64 *int48(int32 val)
|
||||
int64 *
|
||||
int48(int32 val)
|
||||
{
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
|
||||
*result = val;
|
||||
*result = val;
|
||||
|
||||
return(result);
|
||||
} /* int48() */
|
||||
return (result);
|
||||
} /* int48() */
|
||||
|
||||
int32 int84(int64 *val)
|
||||
int32
|
||||
int84(int64 * val)
|
||||
{
|
||||
int32 result;
|
||||
int32 result;
|
||||
|
||||
if (!PointerIsValid(val))
|
||||
elog(WARN,"Invalid (null) int64, can't convert int8 to int4",NULL);
|
||||
if (!PointerIsValid(val))
|
||||
elog(WARN, "Invalid (null) int64, can't convert int8 to int4", NULL);
|
||||
|
||||
if ((*val < INT_MIN) || (*val > INT_MAX))
|
||||
elog(WARN,"int8 conversion to int4 is out of range",NULL);
|
||||
if ((*val < INT_MIN) || (*val > INT_MAX))
|
||||
elog(WARN, "int8 conversion to int4 is out of range", NULL);
|
||||
|
||||
result = *val;
|
||||
result = *val;
|
||||
|
||||
return(result);
|
||||
} /* int84() */
|
||||
return (result);
|
||||
} /* int84() */
|
||||
|
||||
#if FALSE
|
||||
int64 *int28(int16 val)
|
||||
int64 *
|
||||
int28(int16 val)
|
||||
{
|
||||
int64 *result;
|
||||
int64 *result;
|
||||
|
||||
if (!PointerIsValid(result = PALLOCTYPE(int64)))
|
||||
elog(WARN,"Memory allocation failed, can't convert int8 to int2",NULL);
|
||||
if (!PointerIsValid(result = PALLOCTYPE(int64)))
|
||||
elog(WARN, "Memory allocation failed, can't convert int8 to int2", NULL);
|
||||
|
||||
*result = val;
|
||||
*result = val;
|
||||
|
||||
return(result);
|
||||
} /* int28() */
|
||||
return (result);
|
||||
} /* int28() */
|
||||
|
||||
int16 int82(int64 *val)
|
||||
int16
|
||||
int82(int64 * val)
|
||||
{
|
||||
int16 result;
|
||||
int16 result;
|
||||
|
||||
if (!PointerIsValid(val))
|
||||
elog(WARN,"Invalid (null) int8, can't convert to int2",NULL);
|
||||
if (!PointerIsValid(val))
|
||||
elog(WARN, "Invalid (null) int8, can't convert to int2", NULL);
|
||||
|
||||
result = *val;
|
||||
result = *val;
|
||||
|
||||
return (result);
|
||||
} /* int82() */
|
||||
|
||||
return(result);
|
||||
} /* int82() */
|
||||
#endif
|
||||
|
||||
float64 i8tod(int64 *val)
|
||||
float64
|
||||
i8tod(int64 * val)
|
||||
{
|
||||
float64 result = PALLOCTYPE(float64data);
|
||||
float64 result = PALLOCTYPE(float64data);
|
||||
|
||||
*result = *val;
|
||||
*result = *val;
|
||||
|
||||
return(result);
|
||||
} /* i8tod() */
|
||||
return (result);
|
||||
} /* i8tod() */
|
||||
|
||||
int64 *dtoi8(float64 val)
|
||||
int64 *
|
||||
dtoi8(float64 val)
|
||||
{
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
int64 *result = PALLOCTYPE(int64);
|
||||
|
||||
if ((*val < (-pow(2,64)+1)) || (*val > (pow(2,64)-1)))
|
||||
elog(WARN,"Floating point conversion to int64 is out of range",NULL);
|
||||
if ((*val < (-pow(2, 64) + 1)) || (*val > (pow(2, 64) - 1)))
|
||||
elog(WARN, "Floating point conversion to int64 is out of range", NULL);
|
||||
|
||||
*result = *val;
|
||||
|
||||
return(result);
|
||||
} /* dtoi8() */
|
||||
*result = *val;
|
||||
|
||||
return (result);
|
||||
} /* dtoi8() */
|
||||
|
@ -1,8 +1,8 @@
|
||||
/*
|
||||
**
|
||||
** halt.c
|
||||
** halt.c
|
||||
**
|
||||
** This is used to print out error messages and exit
|
||||
** This is used to print out error messages and exit
|
||||
*/
|
||||
|
||||
#include <varargs.h>
|
||||
@ -15,44 +15,46 @@
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
**
|
||||
** halt - print error message, and call clean up routine or exit
|
||||
** halt - print error message, and call clean up routine or exit
|
||||
**
|
||||
**------------------------------------------------------------------------*/
|
||||
|
||||
/*VARARGS*/
|
||||
void halt(va_alist)
|
||||
void
|
||||
halt(va_alist)
|
||||
va_dcl
|
||||
{
|
||||
va_list arg_ptr;
|
||||
char *format, *pstr;
|
||||
void (*sig_func)();
|
||||
va_list arg_ptr;
|
||||
char *format,
|
||||
*pstr;
|
||||
void (*sig_func) ();
|
||||
|
||||
va_start(arg_ptr);
|
||||
format = va_arg(arg_ptr,char *);
|
||||
if (strncmp(format,"PERROR", 6) != 0)
|
||||
vfprintf(stderr,format,arg_ptr);
|
||||
format = va_arg(arg_ptr, char *);
|
||||
if (strncmp(format, "PERROR", 6) != 0)
|
||||
vfprintf(stderr, format, arg_ptr);
|
||||
else
|
||||
{
|
||||
for (pstr=format+6; *pstr == ' ' || *pstr == ':'; pstr++)
|
||||
for (pstr = format + 6; *pstr == ' ' || *pstr == ':'; pstr++)
|
||||
;
|
||||
vfprintf(stderr,pstr,arg_ptr);
|
||||
vfprintf(stderr, pstr, arg_ptr);
|
||||
perror("");
|
||||
}
|
||||
}
|
||||
va_end(arg_ptr);
|
||||
fflush(stderr);
|
||||
|
||||
/* call one clean up function if defined */
|
||||
if ( (sig_func = signal(SIGTERM, SIG_DFL)) != SIG_DFL &&
|
||||
sig_func != SIG_IGN)
|
||||
(*sig_func)(0);
|
||||
else if ( (sig_func = signal(SIGHUP, SIG_DFL)) != SIG_DFL &&
|
||||
sig_func != SIG_IGN)
|
||||
(*sig_func)(0);
|
||||
else if ( (sig_func = signal(SIGINT, SIG_DFL)) != SIG_DFL &&
|
||||
sig_func != SIG_IGN)
|
||||
(*sig_func)(0);
|
||||
else if ( (sig_func = signal(SIGQUIT, SIG_DFL)) != SIG_DFL &&
|
||||
sig_func != SIG_IGN)
|
||||
(*sig_func)(0);
|
||||
/* call one clean up function if defined */
|
||||
if ((sig_func = signal(SIGTERM, SIG_DFL)) != SIG_DFL &&
|
||||
sig_func != SIG_IGN)
|
||||
(*sig_func) (0);
|
||||
else if ((sig_func = signal(SIGHUP, SIG_DFL)) != SIG_DFL &&
|
||||
sig_func != SIG_IGN)
|
||||
(*sig_func) (0);
|
||||
else if ((sig_func = signal(SIGINT, SIG_DFL)) != SIG_DFL &&
|
||||
sig_func != SIG_IGN)
|
||||
(*sig_func) (0);
|
||||
else if ((sig_func = signal(SIGQUIT, SIG_DFL)) != SIG_DFL &&
|
||||
sig_func != SIG_IGN)
|
||||
(*sig_func) (0);
|
||||
exit(1);
|
||||
}
|
||||
|
@ -3,5 +3,4 @@
|
||||
**
|
||||
*/
|
||||
|
||||
void halt();
|
||||
|
||||
void halt();
|
||||
|
@ -10,20 +10,25 @@
|
||||
#include "halt.h"
|
||||
#include "pginterface.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char query[4000];
|
||||
int row =1;
|
||||
int aint;
|
||||
float afloat;
|
||||
double adouble;
|
||||
char achar[11], achar16[17], abpchar[11], avarchar[51], atext[51];
|
||||
time_t aabstime;
|
||||
|
||||
if (argc != 2)
|
||||
halt("Usage: %s database\n",argv[0]);
|
||||
char query[4000];
|
||||
int row = 1;
|
||||
int aint;
|
||||
float afloat;
|
||||
double adouble;
|
||||
char achar[11],
|
||||
achar16[17],
|
||||
abpchar[11],
|
||||
avarchar[51],
|
||||
atext[51];
|
||||
time_t aabstime;
|
||||
|
||||
connectdb(argv[1],NULL,NULL,NULL,NULL);
|
||||
if (argc != 2)
|
||||
halt("Usage: %s database\n", argv[0]);
|
||||
|
||||
connectdb(argv[1], NULL, NULL, NULL, NULL);
|
||||
|
||||
on_error_continue();
|
||||
doquery("DROP TABLE testfetch");
|
||||
@ -42,9 +47,9 @@ int main(int argc, char **argv)
|
||||
aabstime abstime) \
|
||||
");
|
||||
|
||||
while(1)
|
||||
while (1)
|
||||
{
|
||||
sprintf(query,"INSERT INTO testfetch VALUES ( \
|
||||
sprintf(query, "INSERT INTO testfetch VALUES ( \
|
||||
%d, \
|
||||
2322.12, \
|
||||
'923121.0323'::float8, \
|
||||
@ -55,44 +60,43 @@ int main(int argc, char **argv)
|
||||
'Ernie', \
|
||||
'now' )", row);
|
||||
doquery(query);
|
||||
|
||||
|
||||
doquery("BEGIN WORK");
|
||||
doquery("DECLARE c_testfetch BINARY CURSOR FOR \
|
||||
doquery("DECLARE c_testfetch BINARY CURSOR FOR \
|
||||
SELECT * FROM testfetch");
|
||||
|
||||
doquery("FETCH ALL IN c_testfetch");
|
||||
|
||||
while (fetch(
|
||||
&aint,
|
||||
&afloat,
|
||||
&adouble,
|
||||
achar,
|
||||
achar16,
|
||||
abpchar,
|
||||
avarchar,
|
||||
atext,
|
||||
&aabstime) != END_OF_TUPLES)
|
||||
printf("int %d\nfloat %f\ndouble %f\nchar %s\nchar16 %s\n\
|
||||
&aint,
|
||||
&afloat,
|
||||
&adouble,
|
||||
achar,
|
||||
achar16,
|
||||
abpchar,
|
||||
avarchar,
|
||||
atext,
|
||||
&aabstime) != END_OF_TUPLES)
|
||||
printf("int %d\nfloat %f\ndouble %f\nchar %s\nchar16 %s\n\
|
||||
bpchar %s\nvarchar %s\ntext %s\nabstime %s",
|
||||
aint,
|
||||
afloat,
|
||||
adouble,
|
||||
achar,
|
||||
achar16,
|
||||
abpchar,
|
||||
avarchar,
|
||||
atext,
|
||||
ctime(&aabstime));
|
||||
aint,
|
||||
afloat,
|
||||
adouble,
|
||||
achar,
|
||||
achar16,
|
||||
abpchar,
|
||||
avarchar,
|
||||
atext,
|
||||
ctime(&aabstime));
|
||||
|
||||
|
||||
|
||||
doquery("CLOSE c_testfetch");
|
||||
doquery("COMMIT WORK");
|
||||
printf("--- %-d rows inserted so far\n",row);
|
||||
|
||||
printf("--- %-d rows inserted so far\n", row);
|
||||
|
||||
row++;
|
||||
}
|
||||
|
||||
disconnectdb();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -12,77 +12,82 @@
|
||||
#include "halt.h"
|
||||
#include "pginterface.h"
|
||||
|
||||
static void sig_disconnect();
|
||||
static void set_signals();
|
||||
static void sig_disconnect();
|
||||
static void set_signals();
|
||||
|
||||
#define NUL '\0'
|
||||
|
||||
/* GLOBAL VARIABLES */
|
||||
static PGconn* conn;
|
||||
static PGresult* res = NULL;
|
||||
static PGconn *conn;
|
||||
static PGresult *res = NULL;
|
||||
|
||||
#define ON_ERROR_STOP 0
|
||||
#define ON_ERROR_CONTINUE 1
|
||||
#define ON_ERROR_CONTINUE 1
|
||||
|
||||
static int on_error_state = ON_ERROR_STOP;
|
||||
static int on_error_state = ON_ERROR_STOP;
|
||||
|
||||
/* LOCAL VARIABLES */
|
||||
static sigset_t block_sigs, unblock_sigs;
|
||||
static int tuple;
|
||||
static sigset_t block_sigs,
|
||||
unblock_sigs;
|
||||
static int tuple;
|
||||
|
||||
/*
|
||||
**
|
||||
** connectdb - returns PGconn structure
|
||||
** connectdb - returns PGconn structure
|
||||
**
|
||||
*/
|
||||
PGconn *connectdb( char *dbName,
|
||||
char *pghost,
|
||||
char *pgport,
|
||||
char *pgoptions,
|
||||
char *pgtty)
|
||||
PGconn *
|
||||
connectdb(char *dbName,
|
||||
char *pghost,
|
||||
char *pgport,
|
||||
char *pgoptions,
|
||||
char *pgtty)
|
||||
{
|
||||
/* make a connection to the database */
|
||||
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
|
||||
if (PQstatus(conn) == CONNECTION_BAD)
|
||||
halt("Connection to database '%s' failed.\n%s\n", dbName,
|
||||
PQerrorMessage(conn));
|
||||
PQerrorMessage(conn));
|
||||
set_signals();
|
||||
return conn;
|
||||
}
|
||||
|
||||
/*
|
||||
**
|
||||
** disconnectdb
|
||||
** disconnectdb
|
||||
**
|
||||
*/
|
||||
void disconnectdb()
|
||||
void
|
||||
disconnectdb()
|
||||
{
|
||||
PQfinish(conn);
|
||||
}
|
||||
|
||||
/*
|
||||
**
|
||||
** doquery - returns PGresult structure
|
||||
** doquery - returns PGresult structure
|
||||
**
|
||||
*/
|
||||
PGresult *doquery(char *query)
|
||||
PGresult *
|
||||
doquery(char *query)
|
||||
{
|
||||
if (res != NULL)
|
||||
PQclear(res);
|
||||
|
||||
sigprocmask(SIG_SETMASK,&block_sigs,NULL);
|
||||
sigprocmask(SIG_SETMASK, &block_sigs, NULL);
|
||||
res = PQexec(conn, query);
|
||||
sigprocmask(SIG_SETMASK,&unblock_sigs,NULL);
|
||||
sigprocmask(SIG_SETMASK, &unblock_sigs, NULL);
|
||||
|
||||
if (on_error_state == ON_ERROR_STOP &&
|
||||
(res == NULL ||
|
||||
PQresultStatus(res) == PGRES_BAD_RESPONSE ||
|
||||
PQresultStatus(res) == PGRES_NONFATAL_ERROR ||
|
||||
PQresultStatus(res) == PGRES_FATAL_ERROR))
|
||||
PQresultStatus(res) == PGRES_BAD_RESPONSE ||
|
||||
PQresultStatus(res) == PGRES_NONFATAL_ERROR ||
|
||||
PQresultStatus(res) == PGRES_FATAL_ERROR))
|
||||
{
|
||||
if (res != NULL)
|
||||
fprintf(stderr,"query error: %s\n",PQcmdStatus(res));
|
||||
else fprintf(stderr,"connection error: %s\n",PQerrorMessage(conn));
|
||||
fprintf(stderr, "query error: %s\n", PQcmdStatus(res));
|
||||
else
|
||||
fprintf(stderr, "connection error: %s\n", PQerrorMessage(conn));
|
||||
PQfinish(conn);
|
||||
halt("failed request: %s\n", query);
|
||||
}
|
||||
@ -92,14 +97,16 @@ PGresult *doquery(char *query)
|
||||
|
||||
/*
|
||||
**
|
||||
** fetch - returns tuple number (starts at 0), or the value END_OF_TUPLES
|
||||
** NULL pointers are skipped
|
||||
** fetch - returns tuple number (starts at 0), or the value END_OF_TUPLES
|
||||
** NULL pointers are skipped
|
||||
**
|
||||
*/
|
||||
int fetch(void *param, ...)
|
||||
int
|
||||
fetch(void *param,...)
|
||||
{
|
||||
va_list ap;
|
||||
int arg, num_fields;
|
||||
va_list ap;
|
||||
int arg,
|
||||
num_fields;
|
||||
|
||||
num_fields = PQnfields(res);
|
||||
|
||||
@ -113,11 +120,11 @@ int fetch(void *param, ...)
|
||||
{
|
||||
if (PQfsize(res, arg) == -1)
|
||||
{
|
||||
memcpy(param,PQgetvalue(res,tuple,arg),PQgetlength(res,tuple,arg));
|
||||
((char *)param)[PQgetlength(res,tuple,arg)] = NUL;
|
||||
memcpy(param, PQgetvalue(res, tuple, arg), PQgetlength(res, tuple, arg));
|
||||
((char *) param)[PQgetlength(res, tuple, arg)] = NUL;
|
||||
}
|
||||
else
|
||||
memcpy(param,PQgetvalue(res,tuple,arg),PQfsize(res,arg));
|
||||
memcpy(param, PQgetvalue(res, tuple, arg), PQfsize(res, arg));
|
||||
}
|
||||
param = va_arg(ap, char *);
|
||||
}
|
||||
@ -127,15 +134,17 @@ int fetch(void *param, ...)
|
||||
|
||||
/*
|
||||
**
|
||||
** fetchwithnulls - returns tuple number (starts at 0),
|
||||
** or the value END_OF_TUPLES
|
||||
** Returns true or false into null indicator variables
|
||||
** NULL pointers are skipped
|
||||
** fetchwithnulls - returns tuple number (starts at 0),
|
||||
** or the value END_OF_TUPLES
|
||||
** Returns true or false into null indicator variables
|
||||
** NULL pointers are skipped
|
||||
*/
|
||||
int fetchwithnulls(void *param, ...)
|
||||
int
|
||||
fetchwithnulls(void *param,...)
|
||||
{
|
||||
va_list ap;
|
||||
int arg, num_fields;
|
||||
va_list ap;
|
||||
int arg,
|
||||
num_fields;
|
||||
|
||||
num_fields = PQnfields(res);
|
||||
|
||||
@ -149,17 +158,17 @@ int fetchwithnulls(void *param, ...)
|
||||
{
|
||||
if (PQfsize(res, arg) == -1)
|
||||
{
|
||||
memcpy(param,PQgetvalue(res,tuple,arg),PQgetlength(res,tuple,arg));
|
||||
((char *)param)[PQgetlength(res,tuple,arg)] = NUL;
|
||||
memcpy(param, PQgetvalue(res, tuple, arg), PQgetlength(res, tuple, arg));
|
||||
((char *) param)[PQgetlength(res, tuple, arg)] = NUL;
|
||||
}
|
||||
else
|
||||
memcpy(param,PQgetvalue(res,tuple,arg),PQfsize(res,arg));
|
||||
memcpy(param, PQgetvalue(res, tuple, arg), PQfsize(res, arg));
|
||||
}
|
||||
param = va_arg(ap, char *);
|
||||
if (PQgetisnull(res,tuple,arg) != 0)
|
||||
*(int *)param = 1;
|
||||
if (PQgetisnull(res, tuple, arg) != 0)
|
||||
*(int *) param = 1;
|
||||
else
|
||||
*(int *)param = 0;
|
||||
*(int *) param = 0;
|
||||
param = va_arg(ap, char *);
|
||||
}
|
||||
va_end(ap);
|
||||
@ -168,52 +177,56 @@ int fetchwithnulls(void *param, ...)
|
||||
|
||||
/*
|
||||
**
|
||||
** on_error_stop
|
||||
** on_error_stop
|
||||
**
|
||||
*/
|
||||
void on_error_stop()
|
||||
void
|
||||
on_error_stop()
|
||||
{
|
||||
on_error_state = ON_ERROR_STOP;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
**
|
||||
** on_error_continue
|
||||
** on_error_continue
|
||||
**
|
||||
*/
|
||||
void on_error_continue()
|
||||
void
|
||||
on_error_continue()
|
||||
{
|
||||
on_error_state = ON_ERROR_CONTINUE;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
**
|
||||
** sig_disconnect
|
||||
** sig_disconnect
|
||||
**
|
||||
*/
|
||||
static void sig_disconnect()
|
||||
static void
|
||||
sig_disconnect()
|
||||
{
|
||||
fprintf(stderr,"exiting...\n");
|
||||
fprintf(stderr, "exiting...\n");
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
**
|
||||
** set_signals
|
||||
** set_signals
|
||||
**
|
||||
*/
|
||||
static void set_signals()
|
||||
static void
|
||||
set_signals()
|
||||
{
|
||||
sigemptyset(&block_sigs);
|
||||
sigemptyset(&unblock_sigs);
|
||||
sigaddset(&block_sigs,SIGTERM);
|
||||
sigaddset(&block_sigs,SIGHUP);
|
||||
sigaddset(&block_sigs,SIGINT);
|
||||
/* sigaddset(&block_sigs,SIGQUIT); no block */
|
||||
sigprocmask(SIG_SETMASK,&unblock_sigs,NULL);
|
||||
signal(SIGTERM,sig_disconnect);
|
||||
signal(SIGHUP,sig_disconnect);
|
||||
signal(SIGINT,sig_disconnect);
|
||||
signal(SIGQUIT,sig_disconnect);
|
||||
sigaddset(&block_sigs, SIGTERM);
|
||||
sigaddset(&block_sigs, SIGHUP);
|
||||
sigaddset(&block_sigs, SIGINT);
|
||||
/* sigaddset(&block_sigs,SIGQUIT); no block */
|
||||
sigprocmask(SIG_SETMASK, &unblock_sigs, NULL);
|
||||
signal(SIGTERM, sig_disconnect);
|
||||
signal(SIGHUP, sig_disconnect);
|
||||
signal(SIGINT, sig_disconnect);
|
||||
signal(SIGQUIT, sig_disconnect);
|
||||
}
|
||||
|
@ -3,12 +3,12 @@
|
||||
*
|
||||
*/
|
||||
|
||||
PGresult *doquery(char *query);
|
||||
PGconn *connectdb();
|
||||
void disconnectdb();
|
||||
int fetch(void *param, ...);
|
||||
int fetchwithnulls(void *param, ...);
|
||||
void on_error_continue();
|
||||
void on_error_stop();
|
||||
PGresult *doquery(char *query);
|
||||
PGconn *connectdb();
|
||||
void disconnectdb();
|
||||
int fetch(void *param,...);
|
||||
int fetchwithnulls(void *param,...);
|
||||
void on_error_continue();
|
||||
void on_error_stop();
|
||||
|
||||
#define END_OF_TUPLES (-1)
|
||||
|
@ -12,29 +12,34 @@
|
||||
#include <libpq-fe.h>
|
||||
#include <pginterface.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char query[4000];
|
||||
int row =1;
|
||||
int aint;
|
||||
float afloat;
|
||||
double adouble;
|
||||
char achar[11], achar16[17], abpchar[11], avarchar[51], atext[51];
|
||||
time_t aabstime;
|
||||
int aint_null,
|
||||
afloat_null,
|
||||
adouble_null,
|
||||
achar_null,
|
||||
achar16_null,
|
||||
abpchar_null,
|
||||
avarchar_null,
|
||||
atext_null,
|
||||
aabstime_null;
|
||||
char query[4000];
|
||||
int row = 1;
|
||||
int aint;
|
||||
float afloat;
|
||||
double adouble;
|
||||
char achar[11],
|
||||
achar16[17],
|
||||
abpchar[11],
|
||||
avarchar[51],
|
||||
atext[51];
|
||||
time_t aabstime;
|
||||
int aint_null,
|
||||
afloat_null,
|
||||
adouble_null,
|
||||
achar_null,
|
||||
achar16_null,
|
||||
abpchar_null,
|
||||
avarchar_null,
|
||||
atext_null,
|
||||
aabstime_null;
|
||||
|
||||
if (argc != 2)
|
||||
halt("Usage: %s database\n",argv[0]);
|
||||
halt("Usage: %s database\n", argv[0]);
|
||||
|
||||
connectdb(argv[1],NULL,NULL,NULL,NULL);
|
||||
connectdb(argv[1], NULL, NULL, NULL, NULL);
|
||||
|
||||
on_error_continue();
|
||||
doquery("DROP TABLE testfetch");
|
||||
@ -54,7 +59,7 @@ int main(int argc, char **argv)
|
||||
");
|
||||
|
||||
#ifdef TEST_NON_NULLS
|
||||
sprintf(query,"INSERT INTO testfetch VALUES ( \
|
||||
sprintf(query, "INSERT INTO testfetch VALUES ( \
|
||||
0, \
|
||||
0, \
|
||||
0, \
|
||||
@ -65,7 +70,7 @@ int main(int argc, char **argv)
|
||||
'', \
|
||||
'');");
|
||||
#else
|
||||
sprintf(query,"INSERT INTO testfetch VALUES ( \
|
||||
sprintf(query, "INSERT INTO testfetch VALUES ( \
|
||||
NULL, \
|
||||
NULL, \
|
||||
NULL, \
|
||||
@ -85,55 +90,54 @@ int main(int argc, char **argv)
|
||||
doquery("FETCH ALL IN c_testfetch");
|
||||
|
||||
if (fetchwithnulls(
|
||||
&aint,
|
||||
&aint_null,
|
||||
&afloat,
|
||||
&afloat_null,
|
||||
&adouble,
|
||||
&adouble_null,
|
||||
achar,
|
||||
&achar_null,
|
||||
achar16,
|
||||
&achar16_null,
|
||||
abpchar,
|
||||
&abpchar_null,
|
||||
avarchar,
|
||||
&avarchar_null,
|
||||
atext,
|
||||
&atext_null,
|
||||
&aabstime,
|
||||
&aabstime_null) != END_OF_TUPLES)
|
||||
printf("int %d\nfloat %f\ndouble %f\nchar %s\nchar16 %s\n\
|
||||
&aint,
|
||||
&aint_null,
|
||||
&afloat,
|
||||
&afloat_null,
|
||||
&adouble,
|
||||
&adouble_null,
|
||||
achar,
|
||||
&achar_null,
|
||||
achar16,
|
||||
&achar16_null,
|
||||
abpchar,
|
||||
&abpchar_null,
|
||||
avarchar,
|
||||
&avarchar_null,
|
||||
atext,
|
||||
&atext_null,
|
||||
&aabstime,
|
||||
&aabstime_null) != END_OF_TUPLES)
|
||||
printf("int %d\nfloat %f\ndouble %f\nchar %s\nchar16 %s\n\
|
||||
bpchar %s\nvarchar %s\ntext %s\nabstime %s\n",
|
||||
aint,
|
||||
afloat,
|
||||
adouble,
|
||||
achar,
|
||||
achar16,
|
||||
abpchar,
|
||||
avarchar,
|
||||
atext,
|
||||
ctime(&aabstime));
|
||||
printf("NULL:\nint %d\nfloat %d\ndouble %d\nchar %d\nchar16 %d\n\
|
||||
aint,
|
||||
afloat,
|
||||
adouble,
|
||||
achar,
|
||||
achar16,
|
||||
abpchar,
|
||||
avarchar,
|
||||
atext,
|
||||
ctime(&aabstime));
|
||||
printf("NULL:\nint %d\nfloat %d\ndouble %d\nchar %d\nchar16 %d\n\
|
||||
bpchar %d\nvarchar %d\ntext %d\nabstime %d\n",
|
||||
aint_null,
|
||||
afloat_null,
|
||||
adouble_null,
|
||||
achar_null,
|
||||
achar16_null,
|
||||
abpchar_null,
|
||||
avarchar_null,
|
||||
atext_null,
|
||||
aabstime_null);
|
||||
aint_null,
|
||||
afloat_null,
|
||||
adouble_null,
|
||||
achar_null,
|
||||
achar16_null,
|
||||
abpchar_null,
|
||||
avarchar_null,
|
||||
atext_null,
|
||||
aabstime_null);
|
||||
|
||||
|
||||
doquery("CLOSE c_testfetch");
|
||||
doquery("COMMIT WORK");
|
||||
printf("--- %-d rows inserted so far\n",row);
|
||||
printf("--- %-d rows inserted so far\n", row);
|
||||
|
||||
row++;
|
||||
|
||||
disconnectdb();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -10,17 +10,18 @@
|
||||
#include <libpq-fe.h>
|
||||
#include "pginterface.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char query[4000];
|
||||
int row = 0;
|
||||
int count;
|
||||
char line[4000];
|
||||
|
||||
if (argc != 2)
|
||||
halt("Usage: %s database\n",argv[0]);
|
||||
char query[4000];
|
||||
int row = 0;
|
||||
int count;
|
||||
char line[4000];
|
||||
|
||||
connectdb(argv[1],NULL,NULL,NULL,NULL);
|
||||
if (argc != 2)
|
||||
halt("Usage: %s database\n", argv[0]);
|
||||
|
||||
connectdb(argv[1], NULL, NULL, NULL, NULL);
|
||||
on_error_continue();
|
||||
doquery("DROP TABLE words");
|
||||
on_error_stop();
|
||||
@ -35,33 +36,33 @@ int main(int argc, char **argv)
|
||||
word text_ops )\
|
||||
");
|
||||
|
||||
while(1)
|
||||
while (1)
|
||||
{
|
||||
if (scanf("%s",line) != 1)
|
||||
if (scanf("%s", line) != 1)
|
||||
break;
|
||||
doquery("BEGIN WORK");
|
||||
sprintf(query,"\
|
||||
sprintf(query, "\
|
||||
DECLARE c_words BINARY CURSOR FOR \
|
||||
SELECT count(*) \
|
||||
FROM words \
|
||||
WHERE word = '%s'", line);
|
||||
doquery(query);
|
||||
doquery("FETCH ALL IN c_words");
|
||||
|
||||
|
||||
while (fetch(&count) == END_OF_TUPLES)
|
||||
count = 0;
|
||||
doquery("CLOSE c_words");
|
||||
doquery("COMMIT WORK");
|
||||
|
||||
if (count == 0)
|
||||
sprintf(query,"\
|
||||
sprintf(query, "\
|
||||
INSERT INTO words \
|
||||
VALUES (1, '%s')", line);
|
||||
VALUES (1, '%s')", line);
|
||||
else
|
||||
sprintf(query,"\
|
||||
sprintf(query, "\
|
||||
UPDATE words \
|
||||
SET matches = matches + 1 \
|
||||
WHERE word = '%s'", line);
|
||||
WHERE word = '%s'", line);
|
||||
doquery(query);
|
||||
row++;
|
||||
}
|
||||
@ -69,4 +70,3 @@ int main(int argc, char **argv)
|
||||
disconnectdb();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -4,80 +4,86 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "postgres.h" /* for char16, etc. */
|
||||
#include "utils/palloc.h" /* for palloc */
|
||||
#include "libpq-fe.h" /* for TUPLE */
|
||||
#include "postgres.h" /* for char16, etc. */
|
||||
#include "utils/palloc.h" /* for palloc */
|
||||
#include "libpq-fe.h" /* for TUPLE */
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/* prototype for soundex function */
|
||||
char *soundex(char *instr, char *outstr);
|
||||
char *soundex(char *instr, char *outstr);
|
||||
|
||||
text *text_soundex(text *t)
|
||||
text *
|
||||
text_soundex(text * t)
|
||||
{
|
||||
/* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
||||
char *table = "01230120022455012623010202";
|
||||
int count = 0;
|
||||
text *new_t;
|
||||
/* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
||||
char *table = "01230120022455012623010202";
|
||||
int count = 0;
|
||||
text *new_t;
|
||||
|
||||
char outstr[6+1]; /* max length of soundex is 6 */
|
||||
char *instr;
|
||||
char outstr[6 + 1]; /* max length of soundex is 6 */
|
||||
char *instr;
|
||||
|
||||
/* make a null-terminated string */
|
||||
instr=palloc(VARSIZE(t)+1);
|
||||
memcpy(instr,VARDATA(t),VARSIZE(t)-VARHDRSZ);
|
||||
instr[VARSIZE(t)-VARHDRSZ] = (char)0;
|
||||
/* make a null-terminated string */
|
||||
instr = palloc(VARSIZE(t) + 1);
|
||||
memcpy(instr, VARDATA(t), VARSIZE(t) - VARHDRSZ);
|
||||
instr[VARSIZE(t) - VARHDRSZ] = (char) 0;
|
||||
|
||||
/* load soundex into outstr */
|
||||
soundex(instr, outstr);
|
||||
/* load soundex into outstr */
|
||||
soundex(instr, outstr);
|
||||
|
||||
/* Now the outstr contains the soundex of instr */
|
||||
/* copy outstr to new_t */
|
||||
new_t = (text *) palloc(strlen(outstr)+VARHDRSZ);
|
||||
memset(new_t, 0, strlen(outstr)+1);
|
||||
VARSIZE(new_t) = strlen(outstr)+VARHDRSZ;
|
||||
memcpy((void *) VARDATA(new_t),
|
||||
(void *) outstr,
|
||||
strlen(outstr));
|
||||
/* Now the outstr contains the soundex of instr */
|
||||
/* copy outstr to new_t */
|
||||
new_t = (text *) palloc(strlen(outstr) + VARHDRSZ);
|
||||
memset(new_t, 0, strlen(outstr) + 1);
|
||||
VARSIZE(new_t) = strlen(outstr) + VARHDRSZ;
|
||||
memcpy((void *) VARDATA(new_t),
|
||||
(void *) outstr,
|
||||
strlen(outstr));
|
||||
|
||||
/* free instr */
|
||||
pfree(instr);
|
||||
/* free instr */
|
||||
pfree(instr);
|
||||
|
||||
return(new_t);
|
||||
return (new_t);
|
||||
}
|
||||
|
||||
char *soundex(char *instr, char *outstr)
|
||||
{ /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
||||
char *table = "01230120022455012623010202";
|
||||
int count = 0;
|
||||
char *
|
||||
soundex(char *instr, char *outstr)
|
||||
{ /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
||||
char *table = "01230120022455012623010202";
|
||||
int count = 0;
|
||||
|
||||
while(!isalpha(instr[0]) && instr[0])
|
||||
++instr;
|
||||
while (!isalpha(instr[0]) && instr[0])
|
||||
++instr;
|
||||
|
||||
if(!instr[0]) { /* Hey! Where'd the string go? */
|
||||
outstr[0]=(char)0;
|
||||
return outstr;
|
||||
}
|
||||
if (!instr[0])
|
||||
{ /* Hey! Where'd the string go? */
|
||||
outstr[0] = (char) 0;
|
||||
return outstr;
|
||||
}
|
||||
|
||||
if(toupper(instr[0]) == 'P' && toupper(instr[1]) == 'H') {
|
||||
instr[0] = 'F';
|
||||
instr[1] = 'A';
|
||||
}
|
||||
if (toupper(instr[0]) == 'P' && toupper(instr[1]) == 'H')
|
||||
{
|
||||
instr[0] = 'F';
|
||||
instr[1] = 'A';
|
||||
}
|
||||
|
||||
*outstr++ = (char)toupper(*instr++);
|
||||
*outstr++ = (char) toupper(*instr++);
|
||||
|
||||
while(*instr && count < 5) {
|
||||
if(isalpha(*instr) && *instr != *(instr-1)) {
|
||||
*outstr = table[toupper(instr[0]) - 'A'];
|
||||
if(*outstr != '0') {
|
||||
++outstr;
|
||||
++count;
|
||||
}
|
||||
}
|
||||
++instr;
|
||||
}
|
||||
while (*instr && count < 5)
|
||||
{
|
||||
if (isalpha(*instr) && *instr != *(instr - 1))
|
||||
{
|
||||
*outstr = table[toupper(instr[0]) - 'A'];
|
||||
if (*outstr != '0')
|
||||
{
|
||||
++outstr;
|
||||
++count;
|
||||
}
|
||||
}
|
||||
++instr;
|
||||
}
|
||||
|
||||
*outstr = '\0';
|
||||
return(outstr);
|
||||
*outstr = '\0';
|
||||
return (outstr);
|
||||
}
|
||||
|
||||
|
@ -17,14 +17,14 @@
|
||||
/* define this if you want to see iso-8859 characters */
|
||||
#define ISO8859
|
||||
|
||||
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
||||
#define VALUE(char) ((char) - '0')
|
||||
#define DIGIT(val) ((val) + '0')
|
||||
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
|
||||
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
||||
#define VALUE(char) ((char) - '0')
|
||||
#define DIGIT(val) ((val) + '0')
|
||||
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
|
||||
#ifndef ISO8859
|
||||
#define NOTPRINTABLE(c) (!isprint(c))
|
||||
#define NOTPRINTABLE(c) (!isprint(c))
|
||||
#else
|
||||
#define NOTPRINTABLE(c) (!isprint(c) && ((c) < 0xa0))
|
||||
#define NOTPRINTABLE(c) (!isprint(c) && ((c) < 0xa0))
|
||||
#endif
|
||||
|
||||
/*
|
||||
@ -36,115 +36,129 @@
|
||||
* The function is used by output methods of various string types.
|
||||
*
|
||||
* Arguments:
|
||||
* data - input data (can be NULL)
|
||||
* size - optional size of data. A negative value indicates
|
||||
* that data is a null terminated string.
|
||||
* data - input data (can be NULL)
|
||||
* size - optional size of data. A negative value indicates
|
||||
* that data is a null terminated string.
|
||||
*
|
||||
* Returns:
|
||||
* a pointer to a new string containing the printable
|
||||
* representation of data.
|
||||
* a pointer to a new string containing the printable
|
||||
* representation of data.
|
||||
*/
|
||||
|
||||
char *
|
||||
char *
|
||||
string_output(char *data, int size)
|
||||
{
|
||||
register unsigned char c, *p, *r, *result;
|
||||
register int l, len;
|
||||
register unsigned char c,
|
||||
*p,
|
||||
*r,
|
||||
*result;
|
||||
register int l,
|
||||
len;
|
||||
|
||||
if (data == NULL) {
|
||||
result = (char *) palloc(2);
|
||||
result[0] = '-';
|
||||
result[1] = '\0';
|
||||
return (result);
|
||||
}
|
||||
|
||||
if (size < 0) {
|
||||
size = strlen(data);
|
||||
}
|
||||
|
||||
/* adjust string length for escapes */
|
||||
len = size;
|
||||
for (p=data,l=size; l>0; p++,l--) {
|
||||
switch (*p) {
|
||||
case '\\':
|
||||
case '"' :
|
||||
case '{':
|
||||
case '}':
|
||||
case '\b':
|
||||
case '\f':
|
||||
case '\n':
|
||||
case '\r':
|
||||
case '\t':
|
||||
case '\v':
|
||||
len++;
|
||||
break;
|
||||
default:
|
||||
if (NOTPRINTABLE(*p)) {
|
||||
len += 3;
|
||||
}
|
||||
if (data == NULL)
|
||||
{
|
||||
result = (char *) palloc(2);
|
||||
result[0] = '-';
|
||||
result[1] = '\0';
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
len++;
|
||||
|
||||
result = (char *) palloc(len);
|
||||
|
||||
for (p=data,r=result,l=size; (l > 0) && (c = *p); p++,l--) {
|
||||
switch (c) {
|
||||
case '\\':
|
||||
case '"' :
|
||||
case '{':
|
||||
case '}':
|
||||
*r++ = '\\';
|
||||
*r++ = c;
|
||||
break;
|
||||
case '\b':
|
||||
*r++ = '\\';
|
||||
*r++ = 'b';
|
||||
break;
|
||||
case '\f':
|
||||
*r++ = '\\';
|
||||
*r++ = 'f';
|
||||
break;
|
||||
case '\n':
|
||||
*r++ = '\\';
|
||||
*r++ = 'n';
|
||||
break;
|
||||
case '\r':
|
||||
*r++ = '\\';
|
||||
*r++ = 'r';
|
||||
break;
|
||||
case '\t':
|
||||
*r++ = '\\';
|
||||
*r++ = 't';
|
||||
break;
|
||||
case '\v':
|
||||
*r++ = '\\';
|
||||
*r++ = 'v';
|
||||
break;
|
||||
default:
|
||||
if (NOTPRINTABLE(c)) {
|
||||
*r = '\\';
|
||||
r += 3;
|
||||
*r-- = DIGIT(c & 07);
|
||||
c >>= 3;
|
||||
*r-- = DIGIT(c & 07);
|
||||
c >>= 3;
|
||||
*r = DIGIT(c & 03);
|
||||
r += 3;
|
||||
} else {
|
||||
*r++ = c;
|
||||
}
|
||||
if (size < 0)
|
||||
{
|
||||
size = strlen(data);
|
||||
}
|
||||
}
|
||||
*r = '\0';
|
||||
|
||||
return((char *) result);
|
||||
/* adjust string length for escapes */
|
||||
len = size;
|
||||
for (p = data, l = size; l > 0; p++, l--)
|
||||
{
|
||||
switch (*p)
|
||||
{
|
||||
case '\\':
|
||||
case '"':
|
||||
case '{':
|
||||
case '}':
|
||||
case '\b':
|
||||
case '\f':
|
||||
case '\n':
|
||||
case '\r':
|
||||
case '\t':
|
||||
case '\v':
|
||||
len++;
|
||||
break;
|
||||
default:
|
||||
if (NOTPRINTABLE(*p))
|
||||
{
|
||||
len += 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
len++;
|
||||
|
||||
result = (char *) palloc(len);
|
||||
|
||||
for (p = data, r = result, l = size; (l > 0) && (c = *p); p++, l--)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '\\':
|
||||
case '"':
|
||||
case '{':
|
||||
case '}':
|
||||
*r++ = '\\';
|
||||
*r++ = c;
|
||||
break;
|
||||
case '\b':
|
||||
*r++ = '\\';
|
||||
*r++ = 'b';
|
||||
break;
|
||||
case '\f':
|
||||
*r++ = '\\';
|
||||
*r++ = 'f';
|
||||
break;
|
||||
case '\n':
|
||||
*r++ = '\\';
|
||||
*r++ = 'n';
|
||||
break;
|
||||
case '\r':
|
||||
*r++ = '\\';
|
||||
*r++ = 'r';
|
||||
break;
|
||||
case '\t':
|
||||
*r++ = '\\';
|
||||
*r++ = 't';
|
||||
break;
|
||||
case '\v':
|
||||
*r++ = '\\';
|
||||
*r++ = 'v';
|
||||
break;
|
||||
default:
|
||||
if (NOTPRINTABLE(c))
|
||||
{
|
||||
*r = '\\';
|
||||
r += 3;
|
||||
*r-- = DIGIT(c & 07);
|
||||
c >>= 3;
|
||||
*r-- = DIGIT(c & 07);
|
||||
c >>= 3;
|
||||
*r = DIGIT(c & 03);
|
||||
r += 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
*r++ = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
*r = '\0';
|
||||
|
||||
return ((char *) result);
|
||||
}
|
||||
|
||||
/*
|
||||
* string_input() --
|
||||
*
|
||||
* This function accepts a C string in input and copies it into a new
|
||||
* This function accepts a C string in input and copies it into a new
|
||||
* object allocated with palloc() translating all escape sequences.
|
||||
* An optional header can be allocatd before the string, for example
|
||||
* to hold the length of a varlena object.
|
||||
@ -153,209 +167,231 @@ string_output(char *data, int size)
|
||||
* receive strings in internal form.
|
||||
*
|
||||
* Arguments:
|
||||
* str - input string possibly with escapes
|
||||
* size - the required size of new data. A value of 0
|
||||
* indicates a variable size string, while a
|
||||
* negative value indicates a variable size string
|
||||
* of size not greater than this absolute value.
|
||||
* hdrsize - size of an optional header to be allocated before
|
||||
* the data. It must then be filled by the caller.
|
||||
* rtn_size - an optional pointer to an int variable where the
|
||||
* size of the new string is stored back.
|
||||
* str - input string possibly with escapes
|
||||
* size - the required size of new data. A value of 0
|
||||
* indicates a variable size string, while a
|
||||
* negative value indicates a variable size string
|
||||
* of size not greater than this absolute value.
|
||||
* hdrsize - size of an optional header to be allocated before
|
||||
* the data. It must then be filled by the caller.
|
||||
* rtn_size - an optional pointer to an int variable where the
|
||||
* size of the new string is stored back.
|
||||
*
|
||||
* Returns:
|
||||
* a pointer to the new string or the header.
|
||||
* a pointer to the new string or the header.
|
||||
*/
|
||||
|
||||
char *
|
||||
char *
|
||||
string_input(char *str, int size, int hdrsize, int *rtn_size)
|
||||
{
|
||||
register unsigned char *p, *r;
|
||||
unsigned char *result;
|
||||
int len;
|
||||
register unsigned char *p,
|
||||
*r;
|
||||
unsigned char *result;
|
||||
int len;
|
||||
|
||||
if ((str == NULL) || (hdrsize < 0)) {
|
||||
return (char *) NULL;
|
||||
}
|
||||
|
||||
/* Compute result size */
|
||||
len = strlen(str);
|
||||
for (p=str; *p; ) {
|
||||
if (*p++ == '\\') {
|
||||
if (ISOCTAL(*p)) {
|
||||
if (ISOCTAL(*(p+1))) {
|
||||
p++;
|
||||
len--;
|
||||
}
|
||||
if (ISOCTAL(*(p+1))) {
|
||||
p++;
|
||||
len--;
|
||||
}
|
||||
}
|
||||
if (*p) p++;
|
||||
len--;
|
||||
if ((str == NULL) || (hdrsize < 0))
|
||||
{
|
||||
return (char *) NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* result has variable length */
|
||||
if (size == 0) {
|
||||
size = len+1;
|
||||
} else
|
||||
|
||||
/* result has variable length with maximum size */
|
||||
if (size < 0) {
|
||||
size = MIN(len, - size)+1;
|
||||
}
|
||||
|
||||
result = (char *) palloc(hdrsize+size);
|
||||
memset(result, 0, hdrsize+size);
|
||||
if (rtn_size) {
|
||||
*rtn_size = size;
|
||||
}
|
||||
|
||||
r = result + hdrsize;
|
||||
for (p=str; *p; ) {
|
||||
register unsigned char c;
|
||||
if ((c = *p++) == '\\') {
|
||||
switch (c = *p++) {
|
||||
case '\0':
|
||||
p--;
|
||||
break;
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
c = VALUE(c);
|
||||
if (isdigit(*p)) {
|
||||
c = (c<<3) + VALUE(*p++);
|
||||
/* Compute result size */
|
||||
len = strlen(str);
|
||||
for (p = str; *p;)
|
||||
{
|
||||
if (*p++ == '\\')
|
||||
{
|
||||
if (ISOCTAL(*p))
|
||||
{
|
||||
if (ISOCTAL(*(p + 1)))
|
||||
{
|
||||
p++;
|
||||
len--;
|
||||
}
|
||||
if (ISOCTAL(*(p + 1)))
|
||||
{
|
||||
p++;
|
||||
len--;
|
||||
}
|
||||
}
|
||||
if (*p)
|
||||
p++;
|
||||
len--;
|
||||
}
|
||||
if (isdigit(*p)) {
|
||||
c = (c<<3) + VALUE(*p++);
|
||||
}
|
||||
*r++ = c;
|
||||
break;
|
||||
case 'b':
|
||||
*r++ = '\b';
|
||||
break;
|
||||
case 'f':
|
||||
*r++ = '\f';
|
||||
break;
|
||||
case 'n':
|
||||
*r++ = '\n';
|
||||
break;
|
||||
case 'r':
|
||||
*r++ = '\r';
|
||||
break;
|
||||
case 't':
|
||||
*r++ = '\t';
|
||||
break;
|
||||
case 'v':
|
||||
*r++ = '\v';
|
||||
break;
|
||||
default:
|
||||
*r++ = c;
|
||||
}
|
||||
} else {
|
||||
*r++ = c;
|
||||
}
|
||||
}
|
||||
|
||||
return((char *) result);
|
||||
/* result has variable length */
|
||||
if (size == 0)
|
||||
{
|
||||
size = len + 1;
|
||||
}
|
||||
else
|
||||
/* result has variable length with maximum size */
|
||||
if (size < 0)
|
||||
{
|
||||
size = MIN(len, -size) + 1;
|
||||
}
|
||||
|
||||
result = (char *) palloc(hdrsize + size);
|
||||
memset(result, 0, hdrsize + size);
|
||||
if (rtn_size)
|
||||
{
|
||||
*rtn_size = size;
|
||||
}
|
||||
|
||||
r = result + hdrsize;
|
||||
for (p = str; *p;)
|
||||
{
|
||||
register unsigned char c;
|
||||
|
||||
if ((c = *p++) == '\\')
|
||||
{
|
||||
switch (c = *p++)
|
||||
{
|
||||
case '\0':
|
||||
p--;
|
||||
break;
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
c = VALUE(c);
|
||||
if (isdigit(*p))
|
||||
{
|
||||
c = (c << 3) + VALUE(*p++);
|
||||
}
|
||||
if (isdigit(*p))
|
||||
{
|
||||
c = (c << 3) + VALUE(*p++);
|
||||
}
|
||||
*r++ = c;
|
||||
break;
|
||||
case 'b':
|
||||
*r++ = '\b';
|
||||
break;
|
||||
case 'f':
|
||||
*r++ = '\f';
|
||||
break;
|
||||
case 'n':
|
||||
*r++ = '\n';
|
||||
break;
|
||||
case 'r':
|
||||
*r++ = '\r';
|
||||
break;
|
||||
case 't':
|
||||
*r++ = '\t';
|
||||
break;
|
||||
case 'v':
|
||||
*r++ = '\v';
|
||||
break;
|
||||
default:
|
||||
*r++ = c;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*r++ = c;
|
||||
}
|
||||
}
|
||||
|
||||
return ((char *) result);
|
||||
}
|
||||
|
||||
char *
|
||||
char *
|
||||
c_charout(int32 c)
|
||||
{
|
||||
char str[2];
|
||||
char str[2];
|
||||
|
||||
str[0] = (char) c;
|
||||
str[1] = '\0';
|
||||
str[0] = (char) c;
|
||||
str[1] = '\0';
|
||||
|
||||
return (string_output(str, 1));
|
||||
return (string_output(str, 1));
|
||||
}
|
||||
|
||||
char *
|
||||
char *
|
||||
c_char2out(uint16 s)
|
||||
{
|
||||
return (string_output((char *) &s, 2));
|
||||
return (string_output((char *) &s, 2));
|
||||
}
|
||||
|
||||
char *
|
||||
char *
|
||||
c_char4out(uint32 s)
|
||||
{
|
||||
return (string_output((char *) &s, 4));
|
||||
return (string_output((char *) &s, 4));
|
||||
}
|
||||
|
||||
char *
|
||||
char *
|
||||
c_char8out(char *s)
|
||||
{
|
||||
return (string_output(s, 8));
|
||||
return (string_output(s, 8));
|
||||
}
|
||||
|
||||
char *
|
||||
char *
|
||||
c_char16out(char *s)
|
||||
{
|
||||
return (string_output(s, 16));
|
||||
return (string_output(s, 16));
|
||||
}
|
||||
|
||||
/*
|
||||
* This can be used for text, bytea, SET and unknown data types
|
||||
*/
|
||||
|
||||
char *
|
||||
c_textout(struct varlena *vlena)
|
||||
char *
|
||||
c_textout(struct varlena * vlena)
|
||||
{
|
||||
int len = 0;
|
||||
char *s = NULL;
|
||||
int len = 0;
|
||||
char *s = NULL;
|
||||
|
||||
if (vlena) {
|
||||
len = VARSIZE(vlena) - VARHDRSZ;
|
||||
s = VARDATA(vlena);
|
||||
}
|
||||
return (string_output(s, len));
|
||||
if (vlena)
|
||||
{
|
||||
len = VARSIZE(vlena) - VARHDRSZ;
|
||||
s = VARDATA(vlena);
|
||||
}
|
||||
return (string_output(s, len));
|
||||
}
|
||||
|
||||
/*
|
||||
* This can be used for varchar and bpchar strings
|
||||
*/
|
||||
|
||||
char *
|
||||
char *
|
||||
c_varcharout(char *s)
|
||||
{
|
||||
int len;
|
||||
int len;
|
||||
|
||||
if (s) {
|
||||
len = *(int32*)s - 4;
|
||||
s += 4;
|
||||
}
|
||||
return (string_output(s, len));
|
||||
if (s)
|
||||
{
|
||||
len = *(int32 *) s - 4;
|
||||
s += 4;
|
||||
}
|
||||
return (string_output(s, len));
|
||||
}
|
||||
|
||||
#ifdef 0
|
||||
struct varlena *
|
||||
c_textin(char *str)
|
||||
{
|
||||
struct varlena *result;
|
||||
int len;
|
||||
struct varlena *result;
|
||||
int len;
|
||||
|
||||
if (str == NULL) {
|
||||
return ((struct varlena *) NULL);
|
||||
}
|
||||
if (str == NULL)
|
||||
{
|
||||
return ((struct varlena *) NULL);
|
||||
}
|
||||
|
||||
result = (struct varlena *) string_input(str, 0, VARHDRSZ, &len);
|
||||
VARSIZE(result) = len;
|
||||
result = (struct varlena *) string_input(str, 0, VARHDRSZ, &len);
|
||||
VARSIZE(result) = len;
|
||||
|
||||
return (result);
|
||||
return (result);
|
||||
}
|
||||
|
||||
char *
|
||||
char *
|
||||
c_char16in(char *str)
|
||||
{
|
||||
return (string_input(str, 16, 0, NULL));
|
||||
return (string_input(str, 16, 0, NULL));
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user