Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Postgres FD Implementation
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abuhujair Javed
Postgres FD Implementation
Commits
439643a6
Commit
439643a6
authored
Oct 02, 2000
by
Bruce Momjian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add missing python files.
parent
f36e7ff0
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
826 additions
and
0 deletions
+826
-0
src/interfaces/python/tutorial/advanced.py
src/interfaces/python/tutorial/advanced.py
+178
-0
src/interfaces/python/tutorial/basics.py
src/interfaces/python/tutorial/basics.py
+296
-0
src/interfaces/python/tutorial/func.py
src/interfaces/python/tutorial/func.py
+205
-0
src/interfaces/python/tutorial/syscat.py
src/interfaces/python/tutorial/syscat.py
+147
-0
No files found.
src/interfaces/python/tutorial/advanced.py
0 → 100755
View file @
439643a6
#! /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
" schedule text[][]"
print
")"
pgcnx
.
query
(
"""CREATE TABLE sal_emp (
name text,
pay_by_quarter int4[],
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
" '{{
\"
meeting
\"
,
\"
lunch
\"
}, {}}')"
print
print
"INSERT INTO sal_emp VALUES ("
print
" 'Carol',"
print
" '{20000,25000,25000,25000}',"
print
" '{{
\"
talk
\"
,
\"
consult
\"
}, {
\"
meeting
\"
}}')"
print
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
\"
}}')"""
)
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
"-- 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
"-- 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
)
src/interfaces/python/tutorial/basics.py
0 → 100755
View file @
439643a6
This diff is collapsed.
Click to expand it.
src/interfaces/python/tutorial/func.py
0 → 100755
View file @
439643a6
# func.py - demonstrate the use of SQL functions
# inspired from the PostgreSQL tutorial
# adapted to Python 1995 by Pascal ANDRE
print
"""
__________________________________________________________________
MODULE FUNC.PY : SQL FUNCTION DEFINITION 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: func.demo(cnx)
__________________________________________________________________
"""
from
pg
import
DB
import
sys
# waits for a key
def
wait_key
():
print
"Press <enter>"
sys
.
stdin
.
read
(
1
)
# basic functions declaration
def
base_func
(
pgcnx
):
print
"-----------------------------"
print
"-- Creating SQL Functions on Base Types"
print
"-- a CREATE FUNCTION statement lets you create a new "
print
"-- function that can be used in expressions (in SELECT, "
print
"-- INSERT, etc.). We will start with functions that "
print
"-- return values of base types."
print
"-----------------------------"
print
print
"--"
print
"-- let's create a simple SQL function that takes no arguments"
print
"-- and returns 1"
print
print
"CREATE FUNCTION one() RETURNS int4"
print
" AS 'SELECT 1 as ONE' LANGUAGE 'sql'"
pgcnx
.
query
(
"""CREATE FUNCTION one() RETURNS int4
AS 'SELECT 1 as ONE' LANGUAGE 'sql'"""
)
wait_key
()
print
print
"--"
print
"-- functions can be used in any expressions (eg. in the target"
print
"-- list or qualifications)"
print
print
"SELECT one() AS answer"
print
pgcnx
.
query
(
"SELECT one() AS answer"
)
print
print
"--"
print
"-- here's how you create a function that takes arguments. The"
print
"-- following function returns the sum of its two arguments:"
print
print
"CREATE FUNCTION add_em(int4, int4) RETURNS int4"
print
" AS 'SELECT $1 + $2' LANGUAGE 'sql'"
pgcnx
.
query
(
"""CREATE FUNCTION add_em(int4, int4) RETURNS int4
AS 'SELECT $1 + $2' LANGUAGE 'sql'"""
)
print
print
"SELECT add_em(1, 2) AS answer"
print
pgcnx
.
query
(
"SELECT add_em(1, 2) AS answer"
)
# functions on composite types
def
comp_func
(
pgcnx
):
print
"-----------------------------"
print
"-- Creating SQL Functions on Composite Types"
print
"-- it is also possible to create functions that return"
print
"-- values of composite types."
print
"-----------------------------"
print
print
"-- before we create more sophisticated functions, let's "
print
"-- populate an EMP table"
print
print
"CREATE TABLE EMP ("
print
" name text,"
print
" salary int4,"
print
" age int4,"
print
" dept varchar(16)"
print
")"
pgcnx
.
query
(
"""CREATE TABLE EMP (
name text,
salary int4,
age int4,
dept varchar(16))"""
)
print
print
"INSERT INTO EMP VALUES ('Sam', 1200, 16, 'toy')"
print
"INSERT INTO EMP VALUES ('Claire', 5000, 32, 'shoe')"
print
"INSERT INTO EMP VALUES ('Andy', -1000, 2, 'candy')"
print
"INSERT INTO EMP VALUES ('Bill', 4200, 36, 'shoe')"
print
"INSERT INTO EMP VALUES ('Ginger', 4800, 30, 'candy')"
pgcnx
.
query
(
"INSERT INTO EMP VALUES ('Sam', 1200, 16, 'toy')"
)
pgcnx
.
query
(
"INSERT INTO EMP VALUES ('Claire', 5000, 32, 'shoe')"
)
pgcnx
.
query
(
"INSERT INTO EMP VALUES ('Andy', -1000, 2, 'candy')"
)
pgcnx
.
query
(
"INSERT INTO EMP VALUES ('Bill', 4200, 36, 'shoe')"
)
pgcnx
.
query
(
"INSERT INTO EMP VALUES ('Ginger', 4800, 30, 'candy')"
)
wait_key
()
print
print
"-- the argument of a function can also be a tuple. For "
print
"-- instance, double_salary takes a tuple of the EMP table"
print
print
"CREATE FUNCTION double_salary(EMP) RETURNS int4"
print
" AS 'SELECT $1.salary * 2 AS salary' LANGUAGE 'sql'"
pgcnx
.
query
(
"""CREATE FUNCTION double_salary(EMP) RETURNS int4
AS 'SELECT $1.salary * 2 AS salary' LANGUAGE 'sql'"""
)
print
print
"SELECT name, double_salary(EMP) AS dream"
print
"FROM EMP"
print
"WHERE EMP.dept = 'toy'"
print
pgcnx
.
query
(
"""SELECT name, double_salary(EMP) AS dream
FROM EMP WHERE EMP.dept = 'toy'"""
)
print
print
"-- the return value of a function can also be a tuple. However,"
print
"-- make sure that the expressions in the target list is in the "
print
"-- same order as the columns of EMP."
print
print
"CREATE FUNCTION new_emp() RETURNS EMP"
print
" AS 'SELECT
\'
None
\'
::text AS name,"
print
" 1000 AS salary,"
print
" 25 AS age,"
print
"
\'
none
\'
::varchar(16) AS dept'"
print
" LANGUAGE 'sql'"
pgcnx
.
query
(
"""CREATE FUNCTION new_emp() RETURNS EMP
AS 'SELECT
\\\'
None
\\\'
::text AS name,
1000 AS salary,
25 AS age,
\\\'
none
\\\'
::varchar(16) AS dept'
LANGUAGE 'sql'"""
)
wait_key
()
print
print
"-- you can then project a column out of resulting the tuple by"
print
"-- using the
\"
function notation
\"
for projection columns. "
print
"-- (ie. bar(foo) is equivalent to foo.bar) Note that we don't"
print
"-- support new_emp().name at this moment."
print
print
"SELECT name(new_emp()) AS nobody"
print
pgcnx
.
query
(
"SELECT name(new_emp()) AS nobody"
)
print
print
"-- let's try one more function that returns tuples"
print
"CREATE FUNCTION high_pay() RETURNS setof EMP"
print
" AS 'SELECT * FROM EMP where salary > 1500'"
print
" LANGUAGE 'sql'"
pgcnx
.
query
(
"""CREATE FUNCTION high_pay() RETURNS setof EMP
AS 'SELECT * FROM EMP where salary > 1500'
LANGUAGE 'sql'"""
)
print
print
"SELECT name(high_pay()) AS overpaid"
print
pgcnx
.
query
(
"SELECT name(high_pay()) AS overpaid"
)
# function with multiple SQL commands
def
mult_func
(
pgcnx
):
print
"-----------------------------"
print
"-- Creating SQL Functions with multiple SQL statements"
print
"-- you can also create functions that do more than just a"
print
"-- SELECT."
print
"-----------------------------"
print
print
"-- you may have noticed that Andy has a negative salary. We'll"
print
"-- create a function that removes employees with negative "
print
"-- salaries."
print
print
"SELECT * FROM EMP"
print
pgcnx
.
query
(
"SELECT * FROM EMP"
)
print
print
"CREATE FUNCTION clean_EMP () RETURNS int4"
print
" AS 'DELETE FROM EMP WHERE EMP.salary <= 0"
print
" SELECT 1 AS ignore_this'"
print
" LANGUAGE 'sql'"
pgcnx
.
query
(
"CREATE FUNCTION clean_EMP () RETURNS int4 AS 'DELETE FROM EMP WHERE EMP.salary <= 0; SELECT 1 AS ignore_this' LANGUAGE 'sql'"
)
print
print
"SELECT clean_EMP()"
print
pgcnx
.
query
(
"SELECT clean_EMP()"
)
print
print
"SELECT * FROM EMP"
print
pgcnx
.
query
(
"SELECT * FROM EMP"
)
# base cleanup
def
demo_cleanup
(
pgcnx
):
print
"-- remove functions that were created in this file"
print
print
"DROP FUNCTION clean_EMP()"
print
"DROP FUNCTION high_pay()"
print
"DROP FUNCTION new_emp()"
print
"DROP FUNCTION add_em(int4, int4)"
print
"DROP FUNCTION one()"
print
print
"DROP TABLE EMP"
pgcnx
.
query
(
"DROP FUNCTION clean_EMP()"
)
pgcnx
.
query
(
"DROP FUNCTION high_pay()"
)
pgcnx
.
query
(
"DROP FUNCTION new_emp()"
)
pgcnx
.
query
(
"DROP FUNCTION add_em(int4, int4)"
)
pgcnx
.
query
(
"DROP FUNCTION one()"
)
pgcnx
.
query
(
"DROP TABLE EMP"
)
# main demo function
def
demo
(
pgcnx
):
base_func
(
pgcnx
)
comp_func
(
pgcnx
)
mult_func
(
pgcnx
)
demo_cleanup
(
pgcnx
)
src/interfaces/python/tutorial/syscat.py
0 → 100755
View file @
439643a6
# syscat.py - parses some system catalogs
# inspired from the PostgreSQL tutorial
# adapted to Python 1995 by Pascal ANDRE
print
"""
__________________________________________________________________
MODULE SYSCAT.PY : PARSES SOME POSTGRESQL SYSTEM CATALOGS
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: syscat.demo(cnx)
Some results may be empty, depending on your base status."
__________________________________________________________________
"""
from
pg
import
DB
import
sys
# waits for a key
def
wait_key
():
print
"Press <enter>"
sys
.
stdin
.
read
(
1
)
# lists all simple indices
def
list_simple_ind
(
pgcnx
):
result
=
pgcnx
.
query
(
"""SELECT bc.relname AS class_name,
ic.relname AS index_name, a.attname
FROM pg_class bc, pg_class ic, pg_index i, pg_attribute a
WHERE i.indrelid = bc.oid AND i.indexrelid = bc.oid
AND i.indkey[0] = a.attnum AND a.attrelid = bc.oid
AND i.indproc = '0'::oid
ORDER BY class_name, index_name, attname"""
)
return
result
# list all user defined attributes and their type in user-defined classes
def
list_all_attr
(
pgcnx
):
result
=
pgcnx
.
query
(
"""SELECT c.relname, a.attname, t.typname
FROM pg_class c, pg_attribute a, pg_type t
WHERE c.relkind = 'r' and c.relname !~ '^pg_'
AND c.relname !~ '^Inv' and a.attnum > 0
AND a.attrelid = c.oid and a.atttypid = t.oid
ORDER BY relname, attname"""
)
return
result
# list all user defined base type
def
list_user_base_type
(
pgcnx
):
result
=
pgcnx
.
query
(
"""SELECT u.usename, t.typname
FROM pg_type t, pg_user u
WHERE u.usesysid = int2in(int4out(t.typowner))
AND t.typrelid = '0'::oid and t.typelem = '0'::oid
AND u.usename <> 'postgres' order by usename, typname"""
)
return
result
# list all right-unary operators
def
list_right_unary_operator
(
pgcnx
):
result
=
pgcnx
.
query
(
"""SELECT o.oprname AS right_unary,
lt.typname AS operand, result.typname AS return_type
FROM pg_operator o, pg_type lt, pg_type result
WHERE o.oprkind='r' and o.oprleft = lt.oid
AND o.oprresult = result.oid
ORDER BY operand"""
)
return
result
# list all left-unary operators
def
list_left_unary_operator
(
pgcnx
):
result
=
pgcnx
.
query
(
"""SELECT o.oprname AS left_unary,
rt.typname AS operand, result.typname AS return_type
FROM pg_operator o, pg_type rt, pg_type result
WHERE o.oprkind='l' AND o.oprright = rt.oid
AND o.oprresult = result.oid
ORDER BY operand"""
)
return
result
# list all binary operators
def
list_binary_operator
(
pgcnx
):
result
=
pgcnx
.
query
(
"""SELECT o.oprname AS binary_op,
rt.typname AS right_opr, lt.typname AS left_opr,
result.typname AS return_type
FROM pg_operator o, pg_type rt, pg_type lt, pg_type result
WHERE o.oprkind = 'b' AND o.oprright = rt.oid
AND o.oprleft = lt.oid AND o.oprresult = result.oid"""
)
return
result
# returns the name, args and return type from all function of lang l
def
list_lang_func
(
pgcnx
,
l
):
result
=
pgcnx
.
query
(
"""SELECT p.proname, p.pronargs, t.typname
FROM pg_proc p, pg_language l, pg_type t
WHERE p.prolang = l.oid AND p.prorettype = t.oid
AND l.lanname = '
%
s'
ORDER BY proname"""
%
l
)
return
result
# lists all the aggregate functions and the type to which they can be applied
def
list_agg_func
(
pgcnx
):
result
=
pgcnx
.
query
(
"""SELECT a.aggname, t.typname
FROM pg_aggregate a, pg_type t
WHERE a.aggbasetype = t.oid
ORDER BY aggname, typname"""
)
return
result
# lists all the operator classes that can be used with each access method as
# well as the operators that can be used with the respective operator classes
def
list_op_class
(
pgcnx
):
result
=
pgcnx
.
query
(
"""SELECT am.amname, opc.opcname, opr.oprname
FROM pg_am am, pg_amop amop, pg_opclass opc, pg_operator opr
WHERE amop.amopid = am.oid and amop.amopclaid = opc.oid
AND amop.amopopr = opr.oid order by amname, opcname, oprname"""
)
return
result
# demo function - runs all examples
def
demo
(
pgcnx
):
import
sys
,
os
save_stdout
=
sys
.
stdout
sys
.
stdout
=
os
.
popen
(
"more"
,
"w"
)
print
"Listing simple indices ..."
print
list_simple_ind
(
pgcnx
)
print
"Listing all attributes ..."
print
list_all_attr
(
pgcnx
)
print
"Listing all user-defined base types ..."
print
list_user_base_type
(
pgcnx
)
print
"Listing all left-unary operators defined ..."
print
list_left_unary_operator
(
pgcnx
)
print
"Listing all right-unary operators defined ..."
print
list_right_unary_operator
(
pgcnx
)
print
"Listing all binary operators ..."
print
list_binary_operator
(
pgcnx
)
print
"Listing C external function linked ..."
print
list_lang_func
(
pgcnx
,
'C'
)
print
"Listing C internal functions ..."
print
list_lang_func
(
pgcnx
,
'internal'
)
print
"Listing SQL functions defined ..."
print
list_lang_func
(
pgcnx
,
'sql'
)
print
"Listing 'aggregate functions' ..."
print
list_agg_func
(
pgcnx
)
print
"Listing 'operator classes' ..."
print
list_op_class
(
pgcnx
)
del
sys
.
stdout
sys
.
stdout
=
save_stdout
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment