Search code examples
asp.netasp-classicrecordset

Return processed DB data at once using class method


I have a class which is supposed to load an article with a given ID from a database. It returns a recordset with the requested database row. It's working well, but sometimes I would like to attach an additional column. However, I can only change the value of an already-existing column. Something like this:

article("imageID") = getImageURL(article("imageID"))

This way, I have rewritten image ID by it's URL.

But let's say I want to return both the ID and URL. From what I've found, it's not possible to append a column to recordset when it's already filled by data from the DB. One solution is to use a class variable, but I am looking for a way to return all the data at once (not dividing them into recordset and class variables), because there would be too many variables like this in my case and I don't think it's clean anyway. So what's the best other way to do it then?

Here is my current code:

public property get getRecordSet()
    set article = Server.CreateObject("ADODB.Recordset")        
    article.open "SELECT * FROM article WHERE id = '" & articleId & "'", db, 2, 3

    if not data.eof then
        // This formats some values, so I can directly use them when returned

        article("imageID") = getImageURL(article("imageID"))
        article("date") = getFormatedDate(article("date"))

        // imageID and date are rewritten now, so I can't access
        // their original values anymore

        // But instead of above, I would like to keep imageID and date
        //   and return their processed values as well.
        //     As follows (but it's not the correct working way):

        article("imageURL") = getImageURL(article("imageID"))
        article("formatedDate") = getFormatedDate(article("date"))
    end if

    // Return recordset
    set getRecordSet = article
end property

Solution

  • Is there any reason why you need to return a recordset? It looks like you're just returning values for a single row. If you need to add additional information, you're kind of breaking the recordset contract anyway. You could return an array or a dictionary just as easily.

    For example, if you had a getDictionary() property instead:

    Public Property Get getDictionary()
    
        set article = Server.CreateObject("ADODB.Recordset")        
        article.open "SELECT * FROM article WHERE id = '" & articleId & "'", db, 2, 3
    
        Set getDictionary = Server.CreateObject("Scripting.Dictionary")
    
        getDictionary.Add "imageID", article("imageID")
        getDictionary.Add "imageURL", getImageURL(article("imageID"))
        getDictionary.Add "date", article("date")
        getDictionary.Add "formatedDate", getFormatedDate(article("date"))
    
    End Property
    

    Then you could use it like so:

    Response.Write someObject.getDictionary.Item("imageURL")