head	1.7;
access;
symbols
	REL7_3_21:1.6
	REL7_3_20:1.6
	REL7_3_19:1.6
	REL7_3_18:1.6
	REL7_3_17:1.6
	REL7_3_16:1.6
	REL7_3_15:1.6
	REL7_3_14:1.6
	REL7_3_13:1.6
	REL7_3_12:1.6
	REL7_3_11:1.6
	REL7_2_8:1.5
	REL7_3_10:1.6
	REL7_2_7:1.5
	REL7_3_9:1.6
	REL7_3_8:1.6
	REL7_2_6:1.5
	REL7_2_5:1.5
	REL7_3_7:1.6
	REL7_3_6:1.6
	REL7_3_4:1.6
	REL7_3_2:1.6
	REL7_2_4:1.5
	REL7_3_STABLE:1.6.0.2
	REL7_2_3:1.5
	REL7_2_STABLE:1.5.0.4
	REL7_2:1.5
	REL7_2_RC2:1.5
	REL7_2_RC1:1.5
	REL7_2_BETA5:1.5
	REL7_2_BETA4:1.5
	REL7_2_BETA3:1.5
	REL7_2_BETA2:1.5
	REL7_2_BETA1:1.5
	REL7_1_2:1.5
	REL7_1_STABLE:1.5.0.2
	REL7_1_BETA:1.5
	REL7_1_BETA3:1.5
	REL7_1_BETA2:1.5
	REL7_1:1.5
	REL7_0_PATCHES:1.3.0.4
	REL7_0:1.3
	REL6_5_PATCHES:1.3.0.2
	REL6_5:1.3
	REL6_4:1.1.0.2
	release-6-3:1.1;
locks; strict;
comment	@# @;


1.7
date	2003.08.01.04.19.06;	author scrappy;	state dead;
branches;
next	1.6;

1.6
date	2002.08.15.03.31.45;	author momjian;	state Exp;
branches;
next	1.5;

1.5
date	2000.10.02.03.46.24;	author momjian;	state Exp;
branches;
next	1.4;

1.4
date	2000.10.02.03.27.33;	author momjian;	state dead;
branches;
next	1.3;

1.3
date	99.05.17.06.06.28;	author momjian;	state Exp;
branches;
next	1.2;

1.2
date	99.05.10.16.10.46;	author momjian;	state dead;
branches;
next	1.1;

1.1
date	98.01.25.06.09.21;	author scrappy;	state Exp;
branches;
next	;


desc
@@


1.7
log
@
remove python module, as its moved to http://www.pygresql.org
@
text
@#! /usr/bin/env python
# advanced.py - demo of advanced features of PostGres. Some may not be ANSI.
# inspired from the Postgres tutorial 
# adapted to Python 1995 by Pascal Andre

print """
__________________________________________________________________
MODULE ADVANCED.PY : ADVANCED POSTGRES SQL COMMANDS TUTORIAL

This module is designed for being imported from python prompt

In order to run the samples included here, first create a connection
using :                        cnx = advanced.DB(...)

The "..." should be replaced with whatever arguments you need to open an
existing database.  Usually all you need is the name of the database and,
in fact, if it is the same as your login name, you can leave it empty.

then start the demo with:      advanced.demo(cnx)
__________________________________________________________________
"""

from pg import DB
import sys

# waits for a key
def wait_key():
	print "Press <enter>"
	sys.stdin.read(1)

# inheritance features
def inherit_demo(pgcnx):
	print "-----------------------------"
	print "-- Inheritance:"
	print "--   a table can inherit from zero or more tables. A query"
	print "--   can reference either all rows of a table or all rows "
	print "--   of a table plus all of its descendants."
	print "-----------------------------"
	print
	print "-- For example, the capitals table inherits from cities table."
	print "-- (It inherits  all data fields from cities.)"
	print
	print "CREATE TABLE cities ("
	print "    name         text,"
	print "    population   float8,"
	print "    altitude     int"
	print ")"
	print
	print "CREATE TABLE capitals ("
	print "    state        varchar(2)"
	print ") INHERITS (cities)"
	pgcnx.query("""CREATE TABLE cities (
        name        text,
        population  float8,
        altitude    int)""")
	pgcnx.query("""CREATE TABLE capitals (
        state       varchar(2)) INHERITS (cities)""")
	wait_key()
	print
	print "-- now, let's populate the tables"
	print
	print "INSERT INTO cities VALUES ('San Francisco', 7.24E+5, 63)"
	print "INSERT INTO cities VALUES ('Las Vegas', 2.583E+5, 2174)"
	print "INSERT INTO cities VALUES ('Mariposa', 1200, 1953)"
	print
	print "INSERT INTO capitals VALUES ('Sacramento', 3.694E+5, 30, 'CA')"
	print "INSERT INTO capitals VALUES ('Madison', 1.913E+5, 845, 'WI')"
	print
	pgcnx.query("INSERT INTO cities VALUES ('San Francisco', 7.24E+5, 63)")
	pgcnx.query("INSERT INTO cities VALUES ('Las Vegas', 2.583E+5, 2174)")
	pgcnx.query("INSERT INTO cities VALUES ('Mariposa', 1200, 1953)")
	pgcnx.query("INSERT INTO capitals VALUES ('Sacramento',3.694E+5,30,'CA')")
	pgcnx.query("INSERT INTO capitals VALUES ('Madison', 1.913E+5, 845, 'WI')")
	print
	print "SELECT * FROM cities"
	print pgcnx.query("SELECT * FROM cities")
	print "SELECT * FROM capitals"
	print pgcnx.query("SELECT * FROM capitals")
	print
	print "-- like before, a regular query references rows of the base"
	print "-- table only"
	print
	print "SELECT name, altitude"
	print "FROM cities"
	print "WHERE altitude > 500;"
	print pgcnx.query("""SELECT name, altitude
        FROM cities
        WHERE altitude > 500""")
	print
	print "-- on the other hand, you can find all cities, including "
	print "-- capitals, that are located at an altitude of 500 'ft "
	print "-- or higher by:"
	print
	print "SELECT c.name, c.altitude"
	print "FROM cities* c"
	print "WHERE c.altitude > 500"
	print pgcnx.query("""SELECT c.name, c.altitude
        FROM cities* c
        WHERE c.altitude > 500""")

# arrays attributes 
def array_demo(pgcnx):
	print "----------------------"
	print "-- Arrays:"
	print "--      attributes can be arrays of base types or user-defined "
	print "--      types"
	print "----------------------"
	print
	print "CREATE TABLE sal_emp ("
	print "    name            text,"
	print "    pay_by_quarter  int4[],"
	print "    pay_by_extra_quarter  int8[],"
	print "    schedule        text[][]"
	print ")"
	pgcnx.query("""CREATE TABLE sal_emp (
        name              text,
        pay_by_quarter    int4[],
        pay_by_extra_quarter    int8[],
        schedule          text[][])""")
	wait_key()
	print
	print "-- insert instances with array attributes.  "
	print "   Note the use of braces"
	print
	print "INSERT INTO sal_emp VALUES ("
	print "    'Bill',"
	print "    '{10000,10000,10000,10000}',"
	print "    '{9223372036854775800,9223372036854775800,9223372036854775800}',"
	print "    '{{\"meeting\", \"lunch\"}, {}}')"
	print
	print "INSERT INTO sal_emp VALUES ("
	print "    'Carol',"
	print "    '{20000,25000,25000,25000}',"
	print "    '{9223372036854775807,9223372036854775807,9223372036854775807}',"
	print "    '{{\"talk\", \"consult\"}, {\"meeting\"}}')"
	print
	pgcnx.query("""INSERT INTO sal_emp VALUES (
        'Bill', '{10000,10000,10000,10000}',
	'{9223372036854775800,9223372036854775800,9223372036854775800}',
        '{{\"meeting\", \"lunch\"}, {}}')""")
	pgcnx.query("""INSERT INTO sal_emp VALUES (
        'Carol', '{20000,25000,25000,25000}',
	'{9223372036854775807,9223372036854775807,9223372036854775807}',
        '{{\"talk\", \"consult\"}, {\"meeting\"}}')""")
	wait_key()
	print
	print "----------------------"
	print "-- queries on array attributes"
	print "----------------------"
	print
	print "SELECT name FROM sal_emp WHERE"
	print "  sal_emp.pay_by_quarter[1] <> sal_emp.pay_by_quarter[2]"
	print
	print pgcnx.query("""SELECT name FROM sal_emp WHERE
        sal_emp.pay_by_quarter[1] <> sal_emp.pay_by_quarter[2]""")
	print
	print pgcnx.query("""SELECT name FROM sal_emp WHERE
        sal_emp.pay_by_extra_quarter[1] <> sal_emp.pay_by_extra_quarter[2]""")
	print
	print "-- retrieve third quarter pay of all employees"
	print 
	print "SELECT sal_emp.pay_by_quarter[3] FROM sal_emp"
	print
	print pgcnx.query("SELECT sal_emp.pay_by_quarter[3] FROM sal_emp")
	print
	print "-- retrieve third quarter extra pay of all employees"
	print 
	print "SELECT sal_emp.pay_by_extra_quarter[3] FROM sal_emp"
	print pgcnx.query("SELECT sal_emp.pay_by_extra_quarter[3] FROM sal_emp")
	print 
	print "-- retrieve first two quarters of extra quarter pay of all employees"
	print 
	print "SELECT sal_emp.pay_by_extra_quarter[1:2] FROM sal_emp"
	print
	print pgcnx.query("SELECT sal_emp.pay_by_extra_quarter[1:2] FROM sal_emp")
	print
	print "-- select subarrays"
	print 
	print "SELECT sal_emp.schedule[1:2][1:1] FROM sal_emp WHERE"
	print "     sal_emp.name = 'Bill'"
	print pgcnx.query("SELECT sal_emp.schedule[1:2][1:1] FROM sal_emp WHERE " \
        "sal_emp.name = 'Bill'")

# base cleanup
def demo_cleanup(pgcnx):
	print "-- clean up (you must remove the children first)"
	print "DROP TABLE sal_emp"
	print "DROP TABLE capitals"
	print "DROP TABLE cities;"
	pgcnx.query("DROP TABLE sal_emp")
	pgcnx.query("DROP TABLE capitals")
	pgcnx.query("DROP TABLE cities")

# main demo function
def demo(pgcnx):
	inherit_demo(pgcnx)
	array_demo(pgcnx)
	demo_cleanup(pgcnx)
@


1.6
log
@http://archives.postgresql.org/pgsql-bugs/2002-06/msg00086.php and never
saw a fix offered up.  Since I'm gearing up to use Postgres and Python
soon, I figured I'd have a hand at trying to get this sucker addressed.
Apologies if this has already been plugged.  I looked in the archives
and never saw a response.

At any rate, I must admit I don't think I fully understand the
implications of some of the changes I made even though they appear to be
straight forward.  We all know the devil is in the details.  Anyone more
knowledgeable is requested to review my changes. :(

I also updated the advanced.py script in a somewhat nonsensical fashion
to make use of an int8 field in an effort to test this change.  It seems
to run okay, however, this is by no means an all exhaustive test.  So,
it's possible that a bumpy road may lay ahead for some.  On the other
hand...overflows (hopefully) previously lurked (long -> int conversion).

Greg Copeland
@
text
@@


1.5
log
@Add missing python files.
@
text
@d112 1
d118 1
d128 1
d134 1
d139 1
d143 1
d157 3
d165 11
@


1.4
log
@Update for PyGreSQL 3.0, from D'Arcy J.M. Cain
@
text
@d1 1
a1 1
#! /usr/local/bin/python
d6 16
a21 9
print "__________________________________________________________________"
print "MODULE ADVANCED.PY : ADVANCED POSTGRES SQL COMMANDS TUTORIAL"
print
print "This module is designed for being imported from python prompt"
print
print "In order to run the samples included here, first create a connection"
print "using :                        cnx = advanced.DB(...)"
print "then start the demo with:      advanced.demo(cnx)"
print "__________________________________________________________________"
a22 1
from pgtools import *
d24 6
d35 3
a37 3
	print "--	a table can inherit from zero or more tables. A query"
	print "--	can reference either all rows of a table or all rows "
	print "--	of a table plus all of its descendants."
d44 3
a46 3
	print "    name		text,"
	print "	   population	float8,"
	print "    altitude	int"
d50 1
a50 1
	print "    state	varchar(2)"
d52 6
a57 6
	pgcnx.query("CREATE TABLE cities ("	\
		"name		text,"		\
		"population	float8,"	\
		"altitude	int)")
	pgcnx.query("CREATE TABLE capitals ("	\
		"state		varchar(2)) INHERITS (cities)")
d69 5
a73 10
	pgcnx.query(
		"INSERT INTO cities VALUES ('San Francisco', 7.24E+5, 63)")
	pgcnx.query(
		"INSERT INTO cities VALUES ('Las Vegas', 2.583E+5, 2174)")
	pgcnx.query(
		"INSERT INTO cities VALUES ('Mariposa', 1200, 1953)")
	pgcnx.query("INSERT INTO capitals"	\
		" VALUES ('Sacramento', 3.694E+5, 30, 'CA')")
	pgcnx.query("INSERT INTO capitals"	\
		" VALUES ('Madison', 1.913E+5, 845, 'WI')")
d86 3
a88 3
	print pgcnx.query("SELECT name, altitude "	\
		"FROM cities "			\
		"WHERE altitude > 500")
d97 3
a99 3
	print pgcnx.query("SELECT c.name, c.altitude "	\
		"FROM cities* c "			\
		"WHERE c.altitude > 500")
d110 3
a112 3
	print "    name			text,"
	print "    pay_by_quarter	int4[],"
	print "    schedule		text[][]"
d114 4
a117 4
	pgcnx.query("CREATE TABLE sal_emp ("		\
		"name	text,"				\
		"pay_by_quarter	int4[],"		\
		"schedule	text[][])")
d133 6
a138 6
	pgcnx.query("INSERT INTO sal_emp VALUES ("	\
		"'Bill', '{10000,10000,10000,10000}',"	\
		"'{{\"meeting\", \"lunch\"}, {}}')")
	pgcnx.query("INSERT INTO sal_emp VALUES ("	\
		"'Carol', '{20000,25000,25000,25000}',"	\
		"'{{\"talk\", \"consult\"}, {\"meeting\"}}')")
d148 2
a149 2
	print pgcnx.query("SELECT name FROM sal_emp WHERE "	\
		"sal_emp.pay_by_quarter[1] <> sal_emp.pay_by_quarter[2]")
d159 1
a159 1
	print "SELECT sal_emp.schedule[1:2][1:1] FROM sal_emp WHERE	"
d162 1
a162 1
		"sal_emp.name = 'Bill'")
@


1.3
log
@Updates for 6.5.
@
text
@@


1.2
log
@Update to PyGreSQL 2.3.
@
text
@d12 1
a12 1
print "using :                        cnx = advanced.connect(...)"
d17 1
a17 1
from pgext import *
d38 1
a38 1
	print "    state	char2"
d45 1
a45 1
		"state		char2) INHERITS (cities)")
d69 1
a69 2
	q = pgcnx.query("SELECT * FROM cities")
	display(q.listfields(), q.getresult())
d71 1
a71 2
	q = pgcnx.query("SELECT * FROM capitals")
	display(q.listfields(), q.getresult())
d79 1
a79 1
	q = pgcnx.query("SELECT name, altitude "	\
a81 1
	display(q.listfields(), q.getresult())
d90 1
a90 1
	q = pgcnx.query("SELECT c.name, c.altitude "	\
a92 36
	display(q.listfields(), q.getresult())

# time travel features
def time_travel(pgcnx):
	print "-----------------------------"
	print "-- Time Travel:"
	print "--	this feature allows you to run historical queries. "
	print "-----------------------------"
	print
	print "-- first, let's make some changes to the cities table (suppose"
	print "-- Mariposa's population grows 10% this year)"
	print
	print "UPDATE cities"
	print "SET population = population * 1.1"
	print "WHERE name = 'Mariposa';"
	pgcnx.query("UPDATE cities "			\
		"SET population = population * 1.1"	\
		"WHERE name = 'Mariposa'")
	wait_key()
	print
	print "-- the default time is the current time ('now'):"
	print
	print "SELECT * FROM cities WHERE name = 'Mariposa';"
	q = pgcnx.query("SELECT * FROM cities WHERE name = 'Mariposa'")
	display(q.listfields(), q.getresult())
	print
	print "-- we can also retrieve the population of Mariposa ever has. "
	print "-- ('epoch' is the earliest time representable by the system)"
	print
	print "SELECT name, population"
	print "FROM cities['epoch', 'now']  -- can be abbreviated to cities[,]"
	print "WHERE name = 'Mariposa';"
	q = pgcnx.query("SELECT name, population "
		"FROM cities['epoch', 'now'] "
		"WHERE name = 'Mariposa'")
	display(q.listfields(), q.getresult())
d105 1
a105 1
	print "    schedule		char16[][]"
d110 1
a110 1
		"schedule	char16[][])")
d141 1
a141 1
	q = pgcnx.query("SELECT name FROM sal_emp WHERE "	\
a142 1
	display(q.listfields(), q.getresult())
d148 1
a148 2
	q = pgcnx.query("SELECT sal_emp.pay_by_quarter[3] FROM sal_emp")
	display(q.listfields(), q.getresult())
d154 1
a154 1
	q = pgcnx.query("SELECT sal_emp.schedule[1:2][1:1] FROM sal_emp WHERE " \
a155 1
	display(q.listfields(), q.getresult())
a169 1
	time_travel(pgcnx)
@


1.1
log
@Merge in D'Arcy Cain's python interface (PyGreSQL 2.0)
@
text
@@
