Embedded SQL with C is also an option (we'd have to ask for it to be
installed). [See first.pgc for a "simple"
example.] It solves a problem with the dash in cs-linux and checks for
some null values resulting from a left join. Sample
output below.
Java is no longer a choice, because the postgresql interface does not do "UNIX socket connections" I am sorry to learn this.
By now you have a concept of an "order" consisting of several items.
It could be identified by (key) customer and date, or have a unique
order number, for example.
GRANT SELECT ON tablename TO stu;
Rules will always work, because queries invoked by the rule use the permissions of the rule owner (you). For example, if a student has permission to insert an order_line, and your rule updates the inventory, the student does not need any permission for inventory.
Do not put user= in any of your programs unless you want to prevent me or anyone else from using it.
submit cs307 MyFile.php
For the last lab of the term, I want you to create a practical
application for your database, in which you modify it in some way, for instance, taking a new order.
Since the emphasis is on the database
connectivity, neither a GUI nor a web application is required.
In fact, a GUI would require x2go, which I don't have at home and you probably don't either.
A web interface would need to be on osiris,
I could figure out some way to try it out, I guess. There are also numerous security concerns.
The applications should be easy for
someone like a store employee or on-line customer to use. The most
common operations, such as adding a customer or renting a tool, should
be implemented. These Part 1 instructions
now include how to use postgresql in C as well as Java and
python.
The important issue of transactions and commit status
is addressed here.
Some previous notes
about
web applications, and the PHP language
For the curious, let me just say that the SQL standard requires the ability to embed SQL statements in one programming language out of a list of 5. The postgres developers, being C programmers, chose C, and implemented it as a preprocessor that would turn the embeded SQL into function calls, giveing a C program that could be compiled normally. After a few years of nobody wanting to use it, I have removed most of the instructions.
[ecpg is set up in the lab. You proably don't care. All you need to do, to arrive at an executable named first, is:gcc
You need path setting for the include file. I have compiled
it. Run it from the course directory by:
<jensen@linux:/home/COURSES/cs307 > ./first J102Char and varchar attributes are best cast into type TEXT in your SELECT statements, this results in proper C-strings. Note that the size of the char array must be one greater than the maximum string size, to accomodate the zero byte that ends the string. Output of first: (or run it, and see what's changed!)
[jensen@J118-10 Database]$ ./first J102]
==== LIST of COURSES for room: J102 =====
CSC116B01 Low Level Programming Lan J102 Jensen email: ljensen
CSC301B01 Simulation Techniques J102 Bentabet email: (null)
CSC314B01 Computer Networks J102 Bruda email: bruda@cs
CSC302B01 Computer Graphics J102 Allili email: (null)
CSC204B01 Data Structures J102 Khouzam email: nkhouzam
CSC407F01 Honours Dissertation J102 Staff email: (null)
CSC272B01 Electric Circuits&Electr. J102 Fournier email: sfournie
PHY212B01 Electric Circuits & Elect J102 Fournier email: sfournie
8 rows selected.
Alternatively, you can access postgreSQL functions from a C library,
as in other languages. In this package, all the functions start with PQ, all the types start with PG. (I supose PQ stands for Postgres Query?) This
is a different, more sophisticated libray than libecpg.
You need to include a header file:
#include <libpq-fe.h>
When linking, you will probably need -lpq
Functions include:
PQconnect //returns a connection
PQexec PQprepare
PQexecParams PQexecPrepared //use for safe user input
The library is used exactly the same in C or C++ See examples which can be fould on linux.ubishops.ca in the course directory, /home/COURSES/cs307 and you can execute the compiled versions:
sources executable libpq-example.c ./libpq-example-c libpq-example.cc, ./libpq-example-cc fruitstand.c ./fruitstand-c fruitstand.cc ./fruitstand-cc
try:
curs.execute(... some query ...)
except psycopg2.DatabaseError, message: #one error class, 'message' is the error string
print "Exception raised..."
print message #informative error message
else:
# what to do if no error, such as fetching results...
# continue with processing
Language | Autocommit
initially = |
comments |
errors |
SQL (psql) |
True (each statement is a separate atomic transaction) |
use begin; commit; for
multi-statement transactions |
messages appear |
ecpg |
False |
EXEC SQL COMMIT; |
EXEC SQL WHENEVER SQLERROR SQLPRINT; EXEC SQL WHENEVER SQLWARNING SQLPRINT; |
libpq (C, C++) |
True |
/* Start a transaction block */ |
PQerrorMessage(conn)Notices (info, notice, warn) are not errors, they are written by default to stderr |
Perl |
False, to turn it on: |
my $dbh =
DBI->connect('DBI:Oracle:payroll', {AutoCommit => 1}, ... ); |
$sth->errstr; |
Python |
False |
implicitly requires that a
transaction is automatically opened So -- use .commit() whenever a transaction need not be continued |
except Warning, Error |
PHP |
True, for trans's, use: |
pg_query ($conn, "begin"); /
pg_query ($conn, "commit"); |
pg_last_error(), pg_last_notice() |
Java |
True |
connection.setAutoCommit(boolean
autoCommit) |
catch SQLError, .getWarnings() |