aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpgeasy/examples
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>1999-10-11 18:03:04 +0000
committerBruce Momjian <bruce@momjian.us>1999-10-11 18:03:04 +0000
commitad869fcb88ac46d164cce0abc88a0b01cb7bcefc (patch)
tree5250f6633ef7bb842d9a88aa99af01e4b9c72be7 /src/interfaces/libpgeasy/examples
parent0e839dbbdde7189fc145e34e7fdf884c325c26d5 (diff)
downloadpostgresql-ad869fcb88ac46d164cce0abc88a0b01cb7bcefc.tar.gz
postgresql-ad869fcb88ac46d164cce0abc88a0b01cb7bcefc.zip
pgeasy update.
Diffstat (limited to 'src/interfaces/libpgeasy/examples')
-rw-r--r--src/interfaces/libpgeasy/examples/Makefile27
-rw-r--r--src/interfaces/libpgeasy/examples/pginsert.c101
-rw-r--r--src/interfaces/libpgeasy/examples/pgnulltest.c142
-rw-r--r--src/interfaces/libpgeasy/examples/pgwordcount.c70
4 files changed, 340 insertions, 0 deletions
diff --git a/src/interfaces/libpgeasy/examples/Makefile b/src/interfaces/libpgeasy/examples/Makefile
new file mode 100644
index 00000000000..db88f1b9ebc
--- /dev/null
+++ b/src/interfaces/libpgeasy/examples/Makefile
@@ -0,0 +1,27 @@
+#-------------------------------------------------------------------------
+#
+# Makefile
+# Makefile for pgeasy examples
+#
+# IDENTIFICATION
+# $Header: /cvsroot/pgsql/src/interfaces/libpgeasy/examples/Attic/Makefile,v 1.1 1999/10/11 18:03:01 momjian Exp $
+#
+#-------------------------------------------------------------------------
+
+TARGET = pginsert pgwordcount pgnulltest
+LDFLAGS = -lpgeasy
+
+all : $(TARGET)
+
+pginsert:
+ gcc -o $@ $(CFLAGS) $@.c $(PGEASY) $(LDFLAGS)
+
+pgwordcount:
+ gcc -o $@ $(CFLAGS) $@.c $(PGEASY) $(LDFLAGS)
+
+pgnulltest:
+ gcc -o $@ $(CFLAGS) $@.c $(PGEASY) $(LDFLAGS)
+
+clean:
+ rm -f *.o $(TARGET) log core
+
diff --git a/src/interfaces/libpgeasy/examples/pginsert.c b/src/interfaces/libpgeasy/examples/pginsert.c
new file mode 100644
index 00000000000..7cb2cf5b268
--- /dev/null
+++ b/src/interfaces/libpgeasy/examples/pginsert.c
@@ -0,0 +1,101 @@
+/*
+ * insert.c
+ *
+*/
+
+#include <stdio.h>
+#include <time.h>
+#include <libpq-fe.h>
+#include "halt.h"
+#include "pgeasy.h"
+
+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]);
+
+ connectdb(argv[1], NULL, NULL, NULL, NULL);
+
+ on_error_continue();
+ doquery("DROP TABLE testfetch");
+ on_error_stop();
+
+ doquery("\
+ CREATE TABLE testfetch( \
+ aint int4, \
+ afloat float4, \
+ adouble float8, \
+ achar char, \
+ achar16 char16, \
+ abpchar char(10), \
+ avarchar varchar(50), \
+ atext text, \
+ aabstime abstime) \
+ ");
+
+ while (1)
+ {
+ sprintf(query, "INSERT INTO testfetch VALUES ( \
+ %d, \
+ 2322.12, \
+ '923121.0323'::float8, \
+ 'A', \
+ 'Betty', \
+ 'Charley', \
+ 'Doug', \
+ 'Ernie', \
+ 'now' )", row);
+ doquery(query);
+
+ doquery("BEGIN WORK");
+ 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\
+bpchar %s\nvarchar %s\ntext %s\nabstime %s",
+ 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);
+
+ row++;
+ }
+
+ disconnectdb();
+ return 0;
+}
diff --git a/src/interfaces/libpgeasy/examples/pgnulltest.c b/src/interfaces/libpgeasy/examples/pgnulltest.c
new file mode 100644
index 00000000000..4651d77bac8
--- /dev/null
+++ b/src/interfaces/libpgeasy/examples/pgnulltest.c
@@ -0,0 +1,142 @@
+/*
+ * pgnulltest.c
+ *
+*/
+
+#define TEST_NON_NULLS
+
+#include <stdio.h>
+#include <time.h>
+#include <halt.h>
+#include <libpq-fe.h>
+#include <pgeasy.h>
+
+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;
+
+ if (argc != 2)
+ halt("Usage: %s database\n", argv[0]);
+
+ connectdb(argv[1], NULL, NULL, NULL, NULL);
+
+ on_error_continue();
+ doquery("DROP TABLE testfetch");
+ on_error_stop();
+
+ doquery("\
+ CREATE TABLE testfetch( \
+ aint int4, \
+ afloat float4, \
+ adouble float8, \
+ achar char, \
+ achar16 char16, \
+ abpchar char(10), \
+ avarchar varchar(50), \
+ atext text, \
+ aabstime abstime) \
+ ");
+
+#ifdef TEST_NON_NULLS
+ sprintf(query, "INSERT INTO testfetch VALUES ( \
+ 0, \
+ 0, \
+ 0, \
+ '', \
+ '', \
+ '', \
+ '', \
+ '', \
+ '');");
+#else
+ sprintf(query, "INSERT INTO testfetch VALUES ( \
+ NULL, \
+ NULL, \
+ NULL, \
+ NULL, \
+ NULL, \
+ NULL, \
+ NULL, \
+ NULL, \
+ NULL);");
+#endif
+ doquery(query);
+
+ doquery("BEGIN WORK");
+ doquery("DECLARE c_testfetch BINARY CURSOR FOR \
+ SELECT * FROM testfetch");
+
+ 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\
+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\
+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);
+
+
+ doquery("CLOSE c_testfetch");
+ doquery("COMMIT WORK");
+ printf("--- %-d rows inserted so far\n", row);
+
+ row++;
+
+ disconnectdb();
+ return 0;
+}
diff --git a/src/interfaces/libpgeasy/examples/pgwordcount.c b/src/interfaces/libpgeasy/examples/pgwordcount.c
new file mode 100644
index 00000000000..4715f88f438
--- /dev/null
+++ b/src/interfaces/libpgeasy/examples/pgwordcount.c
@@ -0,0 +1,70 @@
+/*
+ * wordcount.c
+ *
+*/
+
+#include <stdio.h>
+#include "halt.h"
+#include <libpq-fe.h>
+#include "pgeasy.h"
+
+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]);
+
+ connectdb(argv[1], NULL, NULL, NULL, NULL);
+ on_error_continue();
+ doquery("DROP TABLE words");
+ on_error_stop();
+
+ doquery("\
+ CREATE TABLE words( \
+ matches int4, \
+ word text ) \
+ ");
+ doquery("\
+ CREATE INDEX i_words_1 ON words USING btree ( \
+ word text_ops )\
+ ");
+
+ while (1)
+ {
+ if (scanf("%s", line) != 1)
+ break;
+ doquery("BEGIN WORK");
+ 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, "\
+ INSERT INTO words \
+ VALUES (1, '%s')", line);
+ else
+ sprintf(query, "\
+ UPDATE words \
+ SET matches = matches + 1 \
+ WHERE word = '%s'", line);
+ doquery(query);
+ row++;
+ }
+
+ disconnectdb();
+ return 0;
+}