Index: openacs-4/packages/acs-core-docs/www/db-api.html =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-core-docs/www/db-api.html,v diff -u -r1.9 -r1.10 --- openacs-4/packages/acs-core-docs/www/db-api.html 28 Feb 2003 05:36:04 -0000 1.9 +++ openacs-4/packages/acs-core-docs/www/db-api.html 24 Jun 2003 03:58:11 -0000 1.10 @@ -1,73 +1,21 @@ -The OpenACS Database Access API

The OpenACS Database Access API

+The OpenACS Database Access API

The OpenACS Database Access API

By Pete Su and Jon Salz. Modified by Roberto Mello. -

Overview

+

Overview

One of OpenACS's great strengths is that code written for it is very close to the database. It is very easy to interact with the database from anywhere within OpenACS. Our goal is to develop a coherent API for database access which makes this even easier.

More detailed information about the DB api is available at Database Access API. -

The Old Way

- Here's a typical block of code from an OpenACS 3.x dynamic page: -

-set tcl_var "foo"
-
-set db [ns_db gethandle]
-
-ns_db dml $db "begin transaction"
-
-set sql "select 
-             foo, 
-             bar, 
-             baz 
-	 from some_table, 
-	     some_other_table
-	 where some_table.id = some_other_table.id  
-	     and some_table.condition_p = '$tcl_var'
-	 "
-
-set selection [ns_db select $db $sql] 
-set count 0 
-
-while { [ns_db getrow $db $selection] } { 
-      	set_variables_after_query 
- 
-        ...
-        call_some_proc $foo $bar $baz
-        incr count 
-}
-
-ns_db releasehandle $db
-    

- Writing code like this had the following annoyances: - -

  • - It was repetitive, tedious and error prone to write the same type of - loops over and over again. -

  • - Using Tcl variable interpolation in a literal string, to pass values - from the page to the database, is error prone, relatively inefficient, - and a good way to compromise the security of a web site. -

  • - Magic like set_variables_after_query made code confusing. -

  • - The scope of transactions is not clear from reading the code. -

  • - Passing handles around explicitly made it easy to use them in bad - ways, like holding a handle for too long while returning data to a - user's browser. -

- -

+

The New Way

Introduced in ACS 3.4, the new Database API is meant to save developers from the above tedium and provide a more structured syntax for specifying database operations, including transactions. -

The New Way

- Here is how you would code up the example above using the new API. + Here's an example of the API.

 set count 0 
 
@@ -113,7 +61,7 @@
 	    from a Tcl variable to the database, which we'll cover next.
 	  

-

Bind Variables

+

Bind Variables

Bind variables are placeholders for literal values in an SQL query being sent to the server. Take the example query above: in the old way, data was generally passed to Oracle directly, via Tcl string @@ -235,7 +183,7 @@ Finally, the DB API has several different styles for passing bind variable values to queries. In general, use the style presented here because it is the most convenient. -

Usage

Every db_* command accepting a SQL command as an argument +

Usage

Every db_* command accepting a SQL command as an argument supports bind variables. You can either

  • Specify the -bind switch to provide a set with bind variable values, or @@ -302,7 +250,7 @@ # of "administrator" } -

Nulls and Bind Variables

+

Nulls and Bind Variables

When processing a DML statement, Oracle coerces empty strings into null. (This coercion does not occur in the WHERE clause of a query, i.e. @@ -350,7 +298,7 @@ # # sets the values for both the "bar" and "baz" columns to null -

Sequence Pooling

+

Sequence Pooling

The database library can transparently maintain pools of sequence values, so that each request for a new sequence value (using db_nextval) does not incur a roundtrip to the server. For instance, this functionality is @@ -381,7 +329,7 @@ yourservername /acs/database] configuration section.) -

Basic API

+

Basic API

The Database API has several functions that wrap familiar parts of the AOLserver database API.

@@ -400,8 +348,8 @@ db_dml "abort" "abort transaction". -

db_multirow
-db_multirow [ -local ] [ -append ] [ -extend column_list ] \
+	  

db_multirow
+db_multirow [ -local ] [ -append ] [ -extend column_list ] \
     var-name statement-name sql \
     [ -bind bind_set_id | -bind bind_value_list ] \
     code_block [ if_no_rows if_no_rows_block ]
@@ -737,4 +685,4 @@
 
 	  

($Id$)

-

View comments on this page at openacs.org
+

View comments on this page at openacs.org