Click here to Skip to main content
15,880,725 members
Articles / Programming Languages / Visual Basic

Object-Oriented database design with the DatabaseObjects library

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
31 Jan 20076 min read 109.3K   3.9K   64  
Demonstrates creating object-oriented database systems with the DatabaseObjects library.
' ___________________________________________________
'
'  � Hi-Integrity Systems 2007. All rights reserved.
'  www.hisystems.com.au - Toby Wicks
' ___________________________________________________
'

Option Strict On
Option Explicit On

Namespace SQL

    Public Class SQLSelectTables
        Implements IEnumerable

        Private pobjTables As ArrayList = New ArrayList
        Private pobjJoins As SQLSelectTableJoins = New SQLSelectTableJoins

        Public Sub New()

            MyBase.New()

        End Sub

        Public Function Add() As SQLSelectTable

            Return Add("")

        End Function

        Public Function Add(ByVal strTableName As String) As SQLSelectTable

            Dim objTable As SQLSelectTable = New SQLSelectTable

            objTable.Name = strTableName
            pobjTables.Add(objTable)

            Return objTable

        End Function

        Default Public ReadOnly Property Item(ByVal strTableName As String) As SQLSelectTable
            Get

                Return DirectCast(pobjTables.Item(TableNameIndex(strTableName)), SQLSelectTable)

            End Get
        End Property

        Default Public ReadOnly Property Item(ByVal intIndex As Integer) As SQLSelectTable
            Get

                Return DirectCast(pobjTables.Item(intIndex), SQLSelectTable)

            End Get
        End Property

        Public ReadOnly Property Count() As Integer
            Get

                Return pobjTables.Count()

            End Get
        End Property

        Public Property Joins() As SQLSelectTableJoins
            Get

                Return pobjJoins

            End Get

            Set(ByVal Value As SQLSelectTableJoins)

                pobjJoins = Value

            End Set
        End Property

        Public Function Exists(ByVal strTableName As String) As Boolean

            Return TableNameIndex(strTableName) >= 0

        End Function

        Public Sub Delete(ByRef objTable As SQLSelectTable)

            If pobjTables.Contains(objTable) Then
                Throw New ObjectDoesNotExistException
            End If

            pobjTables.Remove(objTable)
            objTable = Nothing

        End Sub

        Friend ReadOnly Property SQL(ByVal eConnectionType As Database.ConnectionType) As String
            Get

                Dim strSQL As String
                Dim bAddTable As Boolean

                For intIndex As Integer = 0 To Me.Count - 1
                    'Include the table if it's not being used in a join
                    If pobjJoins Is Nothing Then
                        bAddTable = True
                    ElseIf Not pobjJoins.Exists(Me.Item(intIndex)) Then
                        bAddTable = True
                    Else
                        bAddTable = False
                    End If

                    If bAddTable Then
                        strSQL &= Me.Item(intIndex).SQL(eConnectionType)
                        If intIndex <> Me.Count - 1 Then
                            strSQL &= ", "
                        End If
                    End If
                Next

                If Not pobjJoins Is Nothing Then
                    If pobjJoins.SQL(eConnectionType) <> Nothing And strSQL <> Nothing Then
                        strSQL &= " "
                    End If
                    strSQL &= pobjJoins.SQL(eConnectionType)
                End If

                Return strSQL

            End Get
        End Property

        Private Function TableNameIndex(ByVal strTableName As String) As Integer

            Dim objTable As SQLSelectTable

            strTableName = strTableName.Trim()

            For intIndex As Integer = 0 To Me.Count - 1
                objTable = DirectCast(pobjTables.Item(intIndex), SQLSelectTable)
                If String.Compare(strTableName, objTable.Name, ignoreCase:=True) = 0 Then
                    Return intIndex
                End If
            Next

            Return -1

        End Function

        Private Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator

            Return pobjTables.GetEnumerator

        End Function

    End Class

End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions