Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Building Better SQL Queries


Building Better SQL Queries

Listing 3 A rudimentary version of CSQL


Option Compare Database
Option Explicit

' =====================================================
' Collections of the Query Elements
' =====================================================
Private Sels As New Collection
Private Whrs As New Collection
Private Tbls As New Collection

' =====================================================
' Clears out all collections
' =====================================================
Sub Clear()
    clear_collection Sels
    clear_collection Whrs
    clear_collection Tbls
End Sub

' =====================================================
' Clears a single passed collection
' =====================================================
Private Sub clear_collection(coll As Collection)
    Dim ix As Long
    For ix = coll.Count To 1 Step -1
        coll.Remove ix
    Next ix
End Sub

' =====================================================
' When a user specifies s.Selct = "SOME_FIELD" 
' this appends the table to the selections collection
' =====================================================
Property Let Selct(sl$)
    Sels.Add sl
End Property

' =====================================================
' When a user specifies s.Table = "SOME_TABLE" 
' this appends the table to the collection
' =====================================================
Property Let Table(tb$)
    Tbls.Add tb$
End Property

' =====================================================
' When a user specifies s.Where = "SOMETHING = SOMETHING_ELSE" 
' this appends the table to the Wheres collection
' =====================================================
Property Let Where(whr$)
    Whrs.Add whr
End Property

' =====================================================
' When the user does a get as in SQL$ = s.SelectQuery (Select Query)
' The Selects, Tables and Wheres are retrieved and assembled
' and then concatenated
' =====================================================
Property Get SelectQuery() As String
    SelectQuery = Selct & Table & Where
End Property

' =====================================================
' The get Logic for each specific property actually
' pulls the list together using this function.
' The function starts by building an output
' string with the passed StartString$
' it appends the collection items one by one
' and in between each inserts the Delimiter$ string
' And a new line which makes the result easier to read
' Finally if the string has any content, and optional
' CloseString$ is added. Note that the function
' is structured in such a way that an empty collection
' returns an empty string
' =====================================================
Private Function get_item_list(coll As Collection, _
                       StartString$, Delimiter$, Optional CloseString$ = "")As String
    Dim tx$
    Dim ix As Long
    For ix = 1 To coll.Count
        If ix = 1 Then
            tx$ = StartString$ & vbCrLf
        End If
        If ix > 1 Then
            tx = tx$ & Delimiter$ & " "
        End If
        tx = tx$ & coll(ix) & vbCrLf
    Next ix
    If tx$ <> "" Then
        tx$ = tx$ & CloseString$
    End If
    get_item_list = tx$
End Function

' =====================================================
' When the Get SelectQuery() property requests the Selct
' attribute This is assembled using the get_item_list
' helper function.
' =====================================================
Property Get Selct() As String
    Selct = get_item_list(Sels, "SELECT", ",")
End Property

' =====================================================
' When the Get SelectQuery() property requests the Table
' attribute This is assembled using the get_item_list
' helper function.
' =====================================================
Property Get Table() As String
    Table = get_item_list(Tbls, "FROM", ",")
End Property

' =====================================================
' When the Get SelectQuery() property requests the Table
' attribute This is assembled using the get_item_list
' helper function.
' =====================================================
Property Get Where() As String
    Where = get_item_list(Whrs, "WHERE", "AND")
End Property


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.