Click here to Skip to main content
15,886,806 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.6K   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 SQLSelectFields
        Implements IEnumerable

        Private pobjFieldNames As ArrayList

        Public Sub New()

            pobjFieldNames = New ArrayList

        End Sub

        Public Function Add() As SQLSelectField

            Return Add("", "", 0, Nothing)

        End Function

        Public Function Add( _
            ByVal strFieldName As String) As SQLSelectField

            Return Add(strFieldName, "", 0, Nothing)

        End Function

        Public Function Add( _
            ByVal strFieldName As String, _
            ByVal eAggregateFunction As SQL.AggregateFunction) As SQLSelectField

            Return Add(strFieldName, "", eAggregateFunction, Nothing)

        End Function

        Public Function Add( _
            ByVal strFieldName As String, _
            ByVal strAlias As String, _
            ByVal eAggregateFunction As SQL.AggregateFunction) As SQLSelectField

            Return Add(strFieldName, strAlias, eAggregateFunction, Nothing)

        End Function

        Public Function Add( _
            ByVal strFieldName As String, _
            ByVal objTable As SQLSelectTable) As SQLSelectField

            Return Add(strFieldName, "", 0, objTable)

        End Function

        Public Function Add( _
            ByVal strFieldName As String, _
            ByVal strAlias As String, _
            ByVal eAggregateFunction As SQL.AggregateFunction, _
            ByVal objTable As SQLSelectTable) As SQLSelectField

            Dim objSQLField As SQLSelectField

            objSQLField = New SQLSelectField

            With objSQLField
                .Table = objTable
                .Name = strFieldName
                .Alias = strAlias
                .AggregateFunction = eAggregateFunction
            End With

            pobjFieldNames.Add(objSQLField)

            Return objSQLField

        End Function

        Public Sub Add(ByVal objFieldNames() As String)

            For intIndex As Integer = 0 To objFieldNames.Length - 1
                Me.Add(objFieldNames(intIndex))
            Next

        End Sub

        Default Public ReadOnly Property Item(ByVal strFieldName As String) As SQLSelectField
            Get

                Return DirectCast(pobjFieldNames.Item(FieldNameIndex(strFieldName)), SQLSelectField)

            End Get
        End Property

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

                Return DirectCast(pobjFieldNames.Item(intIndex), SQLSelectField)

            End Get
        End Property

        Public ReadOnly Property Count() As Integer
            Get

                Return pobjFieldNames.Count()

            End Get
        End Property

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

                Dim strSQL As String

                If Me.Count = 0 Then
                    strSQL = "*"
                Else
                    For intIndex As Integer = 0 To Me.Count - 1
                        strSQL &= Me.Item(intIndex).SQL(eConnectionType)
                        If intIndex <> Me.Count - 1 Then
                            strSQL &= ", "
                        End If
                    Next
                End If

                Return strSQL

            End Get
        End Property

        Public Function Exists(ByVal strFieldName As String) As Boolean

            Return FieldNameIndex(strFieldName) >= 0

        End Function

        Public Sub Delete(ByRef objSelectField As SQLSelectField)

            If Not pobjFieldNames.Contains(objSelectField) Then
                Throw New ObjectDoesNotExistException
            End If

            pobjFieldNames.Remove(objSelectField)
            objSelectField = Nothing

        End Sub

        Private Function FieldNameIndex(ByVal strFieldName As String) As Integer

            strFieldName = strFieldName.Trim()

            For intIndex As Integer = 0 To Me.Count - 1
                If String.Compare(strFieldName, Me.Item(intIndex).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 pobjFieldNames.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