Monday, October 29, 2007

Object Oriented ASP: Using Classes in Classic ASP

The Class:

**************cBook.asp********************
< %
Class cBook

'Private, class member variable
Private m_ID

'Read the current ID value
Public Property Get ID()
ID = clng(m_ID)
End Property

'store a new ISBN value
Public Property Let ID(p_Data)
m_ID = p_Data
End Property

'############# Public Functions, accessible to the web pages ##############
'Loads this object's values by loading a record based on the given ID
Public Function LoadFromId(p_Id)
dim strSQL, MyID
MyID = clng(p_Id)
strSQL = "SELECT * FROM Book WHERE (lngBookID = " & MyID & ") "
LoadFromId = LoadData (strSQL)
End Function

Private Function LoadData(p_strSQL)
dim rs
set rs = LoadRSFromDB(p_strSQL)
LoadData = FillFromRS(rs)
rs. close
set rs = nothing
End Function

'Takes a recordset
'Fills the object's properties using the recordset
Private Function FillFromRS(p_RS)
select case p_RS.recordcount
case 1
Me.ID = p_RS.fields("lngBookID").Value
case -1, 0
'err.raise 2, "Item was not found"
case else
err.raise 3, "Item was not unique"
end select
End Function

public Function Save()
Dim strSQL
'If the book has an existing (Autogenerated!) ID, then run an insert
if Me.ID < 1 then
Me.ID = InsertRecord("Book", "lngBookID", ...)
'Otherwise run an update
else
strSQL = strSQL & " UPDATE Book SET ... where lngBookID = " & Me.ID
RunSQL strSQL
End if
Save= Me.ID
End Function
End Class
% >

**************cBooks.asp********************
Class cBooks
'Private, class member variable
Private m_Books

Sub Class_Initialize()
set m_Books = Server.CreateObject ("Scripting.Dictionary")
End Sub
Sub Class_Terminate()
set m_Books = Nothing
End Sub

'Loads all books into the library
Public Function GetAllBooks()
dim strSQL
strSQL = "SELECT * FROM Book "
GetAllBooks = LoadBookData (strSQL)
End Function

Private Function LoadBookData(p_strSQL)
dim rs
set rs = LoadRSFromDB(p_strSQL)
FillBooksFromRS(rs)
LoadBookData = rs.recordcount
rs. close
set rs = nothing
End Function

'Takes a recordset
'Fills the object's properties using the recordset
Private Function FillBooksFromRS(p_RS)
dim myBook
do while not p_RS.eof
set myBook = New cBook
myBook .ID = p_RS.fields("lngBookID").Value
myBook .Title = p_RS.fields("strTitle").Value
m_Books.Add myBook.ID, myBook
p_RS.movenext
loop
End Function

End Class

**************default.asp********************
Dim myBook
Set myBook = New cBook
myBook.ID = ...

**************edit_book.asp********************
Dim myBook
Set myBook = new cBook
myBook.loadById(request("bookid"))
myBook.Title = Request.Form("title")
myBook.Save

Friday, October 26, 2007

Oracle PL/SQL

More Oracle/PLSQL Topics on Tech on the net

Login to
SQL*Plus:
sqlplus username/password

Finding the objects in the database:
select owner, object_name , object_type from dba_objects where object_name like 'cust%'; 

Finding the structure of a table:
desc customers;

Creating Tables in Default tablespace:
create table customers
(
CUST_ID NUMBER(5) NOT NULL,
CUST_NAME VARCHAR2(20) NOT NULL,
CUST_DATE Date NULL
);

DUAL table: A standard Oracle database table named DUAL, which contains exactly one row.
select abs(-1) from dual;

Using Sequences:
create sequence student_id start with 1 increment by 1 cache 20 order;
select student_id.nextval from dual;

to see the contents of the database sequence
desc user_sequences;
select * from user_sequences;

To find out what default tablespace is assigned when our Oracle account was initially created:
select * from user_users;

Storage Characteristics of Tables
select * from user_tablespaces;

To_Date Function:converts a string to a date
to_date( string1, [ format_mask ], [ nls_language ] )
to_date('2003/07/09', 'yyyy/mm/dd')