Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_web_howto thread: Re: Clob datatypes


Message #1 by "William Meitzen" <wmeitzen@y...> on Wed, 31 Jul 2002 00:03:08
Sorry about the double-posting, but I ran into errors in the above code.  
This code has been hammered for about a month on a somewhat active website 
(>600 hits/day), so I'm pretty confident with it. However, though my code 
checks for errors in the where clause, I haven't aggressively tested it.

CLOBRead: Returns string from Oracle CLOB.
Limits: ASP's string size limit is 2M (I think).
strTableCR: name of Oracle table (ex: "search_index")
strCLOBColumnCR: name of column (ex: "content")
strWhereClauseCR: where clause (ex: "recordnumber=142 and title='help'")
strConnectionStringCR: DSN connection string 
(ex: "DSN=name;UID=user;PWD=password")
Usage: strContent=CLOBRead("search_index", "content", "recordnumber=142 
and title='help'", "DSN=name;UID=user;PWD=password")

function CLOBRead(strTableCR, strCLOBColumnCR, strWhereClauseCR, 
strConnectionStringCR)
	on error resume next
	dim conDBOracleGenericReadCR, strStringReturnedCR
	dim intCLOBLengthCR, intStartCR, strSQLCR
	const intSegmentLengthCR=4000
	strStringReturnedCR=""
	set conDBOracleGenericReadCR=server.CreateObject("ADODB.Recordset")
	strSQLCR="select dbms_lob.getlength(" & strCLOBColumnCR & ") as 
CLOBLength from " & strTableCR & " where " & strWhereClauseCR & ";"
	conDBOracleGenericReadCR.open strSQLCR, strConnectionStringCR
	if err.number<>0 then
		response.write "CLOBRead() error: Error in SQL call<br>"
		response.write "Table: <b>" & strTableCR & "</b><br>"
		response.write "Column: <b>" & strCLOBColumnCR & "</b><br>"
		response.write "Where clause: <b>" & strWhereClauseCR 
& "</b><br>"
		response.write "Connection string: <b>" & 
strConnectionStringCR & "</b><br>"
		response.write "SQL statement generated: <b>" & strSQLCR 
& "</b><br>"
		err.clear
	else
		intCLOBLengthCR=conDBOracleGenericReadCR("CLOBLength")
		conDBOracleGenericReadCR.close
		intStartCR=1
		if intCLOBLengthCR>0 then
			do
				conDBOracleGenericReadCR.open "select 
dbms_lob.substr(" & strCLOBColumnCR & ", " & intSegmentLengthCR & ", " & 
intStartCR & ") as CLOBSegment from " & strTableCR & " where " & 
strWhereClauseCR, strConnectionStringCR
				strStringReturnedCR=strStringReturnedCR & 
conDBOracleGenericReadCR("CLOBSegment")
				conDBOracleGenericReadCR.close
				intStartCR=intStartCR+intSegmentLengthCR
			loop until intStartCR=>intCLOBLengthCR
		end if
	end if
	set conDBOracleGenericReadCR=nothing
	CLOBRead=strStringReturnedCR
end function

CLOBUpdate: Writes to Oracle CLOB.
Limits: ASP's string size limit is 2M (I think).
strTableCU: name of Oracle table (ex: "search_index")
strCLOBColumnCU: name of column (ex: "content")
strContentCU: Content to write (ex: "Lee O'Leary" Note: do not convert 
single-quotes to double-quotes)
strWhereClauseCU: where clause (ex: "recordnumber=142 and title='help'")
strConnectionStringCU: DSN connection string 
(ex: "DSN=name;UID=user;PWD=password")
Usage: call CLOBUpdate("search_index", "content", "Lee 
O'Leary", "recordnumber=142 and 
title='help'", "DSN=name;UID=user;PWD=password")
Note: do not convert single-quotes to double-quotes in strContentCU. The 
subroutine does that automatically. Also, though my code checks for errors 
in the where clause, I haven't aggressively tested it.

sub CLOBUpdate(strTableCU, strCLOBColumnCU, strContentCU, 
strWhereClauseCU, strConnectionStringCU)
	dim conDBOracleGenericWriteCU, strSQLCU
	dim intRowsAffectedCU, intCountSQLStringsCU, strSubStringCU
	dim objRegexpCU, objSubStringsCU, intCountSubStringsCU
	dim objSubStringCU
	const intSegmentLengthCU=2000
	set conDBOracleGenericWriteCU=server.CreateObject
("ADODB.Connection")
	if strContentCU="" then
		' - if empty string, write empty string
		conDBOracleGenericWriteCU.open strConnectionStringCU
		conDBOracleGenericWriteCU.BeginTrans
		conDBOracleGenericWriteCU.Execute "update " & strTableCU 
& " set " & strCLOBColumnCU & "='' where " & strWhereClauseCU, 
intRowsAffectedCU
		if intRowsAffectedCU=1 then
			conDBOracleGenericWriteCU.CommitTrans
		else
			conDBOracleGenericWriteCU.RollbackTrans
			response.write "CLOBUpdate() error: <b>" & 
err.description & "</b><br>"
			response.write "Table: <b>" & strTableCU 
& "</b><br>"
			response.write "Column: <b>" & strCLOBColumnCU 
& "</b><br>"
			response.write "Where clause: <b>" & 
strWhereClauseCU & "</b><br>"
			response.write "Connection string: <b>" & 
strConnectionStringCU & "</b><br>"
			response.write "Content: <b>" & strContentCU 
& "</b><br>"
			err.clear
		end if
		conDBOracleGenericWriteCU.close
	else
		' - write string one segment at a time using Oracle's 
dbms_lob.write function
		set objRegexpCU=new regexp
		objRegexpCU.global=true
		objRegexpCU.pattern="((\n|.){1," & intSegmentLengthCU 
& "})"
		set objSubStringsCU=objRegexpCU.execute(strContentCU)
		for intCountSubStringsCU=0 to objSubStringsCU.count-1
			strSubStringCU=objSubStringsCU.item
(intCountSubStringsCU)
			if intCountSubStringsCU=0 then
				' - write 1st segment
				strSQLCU = "update " & strTableCU & " 
set " & strCLOBColumnCU & "='" & replace(strSubStringCU, "'", "''") & "' 
where " & strWhereClauseCU
			else
				' - write subsequent segments
				strSQLCU = "declare Destination clob; "
				strSQLCU = strSQLCU & "begin "
				strSQLCU = strSQLCU & "select " & 
strCLOBColumnCU & " into Destination from " & strTableCU
				strSQLCU = strSQLCU & " where " & 
strWhereClauseCU & " for update; "
				strSQLCU = strSQLCU & "dbms_lob.write
(Destination, " & len(strSubStringCU) & ", "
				strSQLCU = strSQLCU & "dbms_lob.getlength
(Destination)+1, "
				strSQLCU = strSQLCU & "'" & replace
(strSubStringCU, "'", "''") & "'); "
				strSQLCU = strSQLCU & "commit; "
				strSQLCU = strSQLCU & "end; "
			end if

			conDBOracleGenericWriteCU.open 
strConnectionStringCU
			conDBOracleGenericWriteCU.BeginTrans
			conDBOracleGenericWriteCU.Execute strSQLCU, 
intRowsAffectedCU
			conDBOracleGenericWriteCU.CommitTrans
			conDBOracleGenericWriteCU.close
		next
	end if
	set conDBOracleGenericWriteCU=nothing
end sub

  Return to Index