DrewJensen Super User


Joined: 06 Jul 2005 Posts: 2616 Location: Cumberland, MD
|
Posted: Fri Dec 23, 2005 10:49 pm Post subject: Return Base Column Values as variant array |
|
|
The following is routine that takes a name of a registered datasource, a table name and a column name and a variant. Optionally you may pass in a username and password.
The function fills the variant passed in with strings for every entry in the column named and returns the number of entrys as an integer.
The connection metadata object is used to determine if table and column names should be quoted, so should work with most datasources Base, MySQL, PostgrSQL. (But I have only tested with embedded Base file so far)
| Code: |
'ColumnAsArray
' fills a variant array of strings
' with the contents of a database
' table column
' -1 is returned is there is no records
' in the table
'
' 12/25/05 - Bug fix, routine was not closing
' db connection when none passed in
'
' 12/27/05 - Changed the default setting for
' the optional parameter DISTINCT
' to be FALSE
'
' Parameteres
' REQUIRED
'
' RegDSName (String) - Registered Datasource Name
'
' TableName (String) - Table name in datasource
'
' ColName (STring) - Column name in table
'
' RetArray (Variant) - the variant array that will be
' filled with the columns values
' as stirings
'
' OPTIONAL
'
' UserName (string) Database username if required
' default = ""
'
' Password (string) Database password if required
' default = ""
'
' distinct (Boolean) : default = False
' If True return only distinct values
' this will also sort the returned values
' in ascending order
'
' noNull (Boolean) - If True do not include empty stirings
' default = True for null values
'
'
' aConn (XConnection) An open connection to the database
' if this is supplied then it will be
' used to generate the result set
' Otherwise a new connection is opened
' and closed with each call
' NO DEFAULT
'
' THE FUNCTION WILL NOT PROMPT FOR USERNAME / PASSWORD
' if the database requires these, they must be passed
' on the call to the function
'
' RETURN VALUE (Integer)
' -1 : No records found
'
' > -1 : Number of items in
' retArray
'
function ColumnAsArray( RegDSName as String, _
TableName as String, _
ColName as String, _
retArray as variant, _
optional UserName as String, _
optional Password as String, _
optional distinct as boolean, _
optional noNull as boolean, _
optional aConn as variant) _
as integer
dim qtStr as string ' Character used to quote identifiers
dim conn as variant ' database connection
dim strUserName as string ' username passed to connection function
dim strPassword as string ' password passed to connection function
dim strDistinct as string '
dim noBlanks as boolean '
dim tmpBool as boolean '
dim stmt as variant ' statement used for result set
dim rs as variant ' result set object from query
dim RecCnt as integer ' number of records that will be returned
' used to redimension result array
dim strSQL as string
ColumnAsArray = -1
on local error goto ColumnAsArrayError
if ismissing( UserName ) then
strUserName = ""
else
strUserName = UserName
endif
if ismissing( Password ) then
strPassword = ""
else
strPassword = Password
endif
if ismissing( distinct ) then
tmpBool = False
else
tmpBool = DISTINCT
endif
if tmpBool then
strDistinct = "DISTINCT"
else
strDistinct = ""
endif
if ismissing( noNull ) then
noBlanks = True
else
noBlanks = noNull
endif
if not ismissing( aConn ) then
conn = aConn
else
conn = CreateUnoService("com.sun.star.sdb.DatabaseContext")._
getByname( RegDSName )._
getConnection( strUserName, StrPassword )
endif
qtStr = conn.getMetadata.getIdentifierQuoteString
' first thing run a query to see how many
' records we can expect
stmt = conn.createStatement
strSQL = " SELECT " & strDistinct _
& " COUNT( " & qtStr & colName & qtStr _
& " ) from " & qtStr & tableName & qtStr
rs = stmt.executeQuery( strSQL )
rs.next
RecCnt = rs.getInt( 1 )
if RecCnt < 1 then
goto ColumnAsArrayNoRecords
endif
' we have records so dimension our array
' and fill it
RetArray = DimArray( RecCnt )
' reset the recCnt variable to use as counter
RecCnt = 0
' now get the actual records
strSQL = "SELECT " & strDistinct & " " _
& qtStr & colName & qtStr _
& " from " & qtStr & tableName & qtStr
rs = stmt.executeQuery( strSQL )
rs.next
do
if noBlanks then
if rs.getString( 1 ) <> "" then
RetArray( RecCnt ) = rs.getString( 1 )
else
' if we did not write the value to
' the array then do not increment
' the counter
goto ColumnAsArrayNoInc
endif
else
RetArray( RecCnt ) = rs.getString( 1 )
endif
RecCnt = RecCnt + 1
ColumnAsArrayNoInc:
rs.next
loop until rs.isAfterLast
redim Preserve RetArray( RecCnt -1 )
ColumnAsArrayNoRecords:
if ismissing( aConn ) then
conn.dispose
endif
ColumnAsArray = RecCnt - 1
exit function
ColumnAsArrayError:
print "Error in ColumnAsArray: " & error( err )
if ismissing( aConn ) then
conn.dispose
endif
end function
|
For an example of one use of this routine you can look at this thread.
http://www.oooforum.org/forum/viewtopic.phtml?p=115558#115558
Drew _________________ Blog - http://baseanswers.spaces.live.com/ |
|