๐Ÿš€ Crafting an Old-School ORM in VB6

Jean AndreattaJean Andreatta
5 min read

Hey retro coders! I'm Jean, the quirky mind behind this wild ride into the VB6 era. Today, I'm spilling the deets on VB6_SimpleORM_Idea โ€“ my attempt to sprinkle some vintage magic into your coding life.

๐Ÿ•ฐ๏ธ A Stroll Down Nostalgia Lane

VB6 might be older than your grandma's secret cookie recipe, but hey, sometimes you gotta embrace the classics! I can practically hear you saying, "Isn't VB6 a bit outdated for modern development?" Well, my friend, it's not outdated; it's just vintage-chic. It's like programming with a typewriter, but cooler.

In a world where everyone's obsessed with the shiny and new, VB6 invites us to "think outside the box" - or in our case, maybe outside the floppy disk. It's not about following trends; it's about breaking the mold and finding new possibilities in the good ol' familiar. VB6 might be the underdog, but let's face it, who doesn't love an underdog story? ๐Ÿ˜‰

๐Ÿ’ก About the Project

VB6_SimpleORM_Idea is my attempt to sprinkle a bit of magic into VB6 development, offering an misterious thing like an ORM so easy even your cat could use it. Now, keep in mind, this project is a personal project, a playground of code without the shackles of best practices. It started for the sheer fun of it, free from the weight of deadlines or expectations.- it's a bit like trying to teach your grandma how to use Snapchat. But hey, we're getting there!

๐Ÿ› ๏ธLet's Get Our Fingers Dusty (and Maybe a Little Greasy)

To kickstart your VB6_SimpleORM_Idea adventure, follow these simple steps:

  1. Clone the Repository:

     git clone https://github.com/jecsatta/VB6_SimpleORM_Idea.git
    
  2. Integrate into Your VB6 Project:

    To unleash the mystical powers of VB6_SimpleORM_Idea, you need to perform a sacred ritual involving adding the classes and modules to your project. It's not quite as epic as pulling Excalibur from the stone, but it's close. Picture it: you, in a dark room, with your mouse as your wand, casting spells by clicking and dragging. It's practically wizardry, folks! So, summon the courage of a coding wizard, add those mystical files to your project!

    • Import the necessary classes:

      •           BaseClassFactory.cls
                  Classes_Constants.bas
                  CustomTypes.bas
                  DatabaseMethods.bas
                  IBaseClass.cls
                  NamedPropertiesClass.cls
                  ReflectionSimulator.bas
                  Validator_Constants.bas
                  clsAnnotation.cls
                  clsParam.cls
                  clsParams.cls
                  clsProperty.cls
                  clsValidator.cls
        
  3. Classes Setup
    In Class_Initialize all properties and annotations must be added [File:clsClient.bas](File:clsClient.bas)

     Implements IBaseClass
     Private namedClass As NamedPropertiesClass
    
     Public Sub Class_Initialize()
         Set namedClass = New NamedPropertiesClass
         namedClass.Add("id", PrimaryKey, True).AddAnnotation AutoIncrement, True
         namedClass.Add "Name"
         namedClass.Add "Age", Validator, V_AgeValidator
         namedClass.Add "Email"
     End Sub
    
     Private Function IBaseClass_GetTableName() As String
             IBaseClass_GetTableName = "client"
     End Function
    
     Private Function IBaseClass_Props() As NamedPropertiesClass
           Set IBaseClass_Props = namedClass
     End Function
     '-------------------------'
     'Properties implementation'
     Public Property Let ID(ByVal value As Variant)
         namedClass.PropertyByName("ID") = value
     End Property
    
     Public Property Get ID() As Variant
         ID = namedClass.PropertyByName("ID")
     End Property
    
  4. Usage Example (because coding in VB6 is like riding a unicycle - a bit quirky but surprisingly fun):

     Dim objArray As Variant
     Dim objClient As clsClient
     Dim i As Long
     Dim params As New clsParams
    
     params.Add "name", "%mary%", "ilike"
     params.Add "id", 6
     objArray = DataBaseSelect(C_clsClient, params)
     If HasValues(objArray) Then
         Debug.Print "Count:" & UBound(objArray)
         For i = 1 To UBound(objArray)
           Set objClient = objArray(i)
            Debug.Print "ID:" & objClient.ID
            Debug.Print "Name:" & objClient.Name
            Debug.Print "Age:" & objClient.Age
            Debug.Print "Email:" & objClient.Email
            Debug.Print "Errors:" &  objClient.AsIBaseClass.CheckErrors
            Debug.Print ""
            Debug.Print ""
         Next i
     End If
    
     'Output:'
     Count:1
     ID:6
     Name:Mary
     Age:31
     Email:mary@example.com
     Errors:Age value is invalid
    

๐Ÿ› ๏ธ How It Works

Curious about the inner workings of VB6 code? Let's take a peek behind the curtains and demystify the magic:

ORM classes must implement the IBaseClass interface, which has three main functions: GetTableName, CheckErrors, and Props.

Public Interface IBaseClass
    Function GetTableName() As String
    Function CheckErrors() As String
    Function Props() As Variant
End Interface

All ORM classes must have a constant in the Classes_Constants module as follows:

Public Const C_clsClient As String = "clsClient"

All ORM classes must have a "constructor" in the BaseClassFactory as follows:

Public Function clsClient_Create() As Object
    Set clsClient_Create = New clsClient
End Function

To handle properties, we create the NamedPropertiesClass containing an array of the property class:

Private Properties() As clsProperty
Private propNumber As Long

The clsProperty contains its name, value, and an array of annotations:

Private mName As String
Private mValue As Variant
Private mAnnotations() As clsAnnotation

Annotations store their type and value:

Private mDefinition As AnnotationDefinition 'PrimaryKey AutoIncrement MaxLength MinLength Required validator etc'
Private mValue As Variant

To create a new validator, add its constant in the Validator_Constants module:

Public Const V_AgeValidator As String = "Validator_Age"

Then create it in clsValidator:

Public Function Validator_Age(value As Variant) As Boolean
    Validator_Age = (value >= 40)
End Function

In ReflectionSimulator, there's a function GetCreate(ClassName As String) using CallByName to return the class object:

Public Function GetCreate(ClassName As String) As Object
    Dim factory As New BaseClassFactory
    If HasMethod(factory, ClassName & "_Create") Then
        Set GetCreate = CallByName(factory, ClassName & "_Create", VbMethod)
    End If
    Set factory = Nothing
End Function

Properties have a similar structure with the validator:

For i = 0 To annotationsCount - 1
    If mAnnotations(i).Definition = AnnotationDefinition.validator Then
        If Not CallByName(objValidator, "" & mAnnotations(i).value, VbMethod, mValue) Then
            result = result & mName & " value is invalid" & vbNewLine
        End If
    End If
Next i

As you can imagine now, DatabaseSelect executes a GetCreate by the class name, then uses GetTableName. After iterating through the parameters, it populates the class based on the property names. Similarly, the CheckErrors function of the ORM class executes the CheckErrors of ClsProperty, which contains the call to the validator.

Disclaimer: Keep in mind, this is a no-frills, down-to-earth approach. No rocket science here, just good ol' VB6 vibes and a sprinkle of coding charm.

Ready to embark on your own VB6 coding adventure? Dive into the GitHub repository and start exploring!

๐ŸŽ‰ Conclusion - VB6: Where Coding Meets Nostalgic Awesomeness

I hope VB6_SimpleORM_Idea brings a dash of nostalgia and a chuckle or two to your development journey. Share your retro coding stories and feedback in the comments - and remember, VB6 may be old-school, but it's a classic! ๐ŸŽฎโœจ

0
Subscribe to my newsletter

Read articles from Jean Andreatta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jean Andreatta
Jean Andreatta

๐ŸŒŽ Greetings from Brazil! ๐Ÿ‡ง๐Ÿ‡ท A proud father of four and a tech enthusiast living in the beautiful land of Brazil.