Skip to content

Commit

Permalink
Add ability to determine if a database view exists. Closes #5.
Browse files Browse the repository at this point in the history
Add support for SQL Server, MySQL and HyperSQL via the SQLViewExists class.
  • Loading branch information
hisystems committed Mar 5, 2012
1 parent 29c2f76 commit de3d660
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions DatabaseObjects.vbproj
Expand Up @@ -327,6 +327,7 @@
<Compile Include="Contraints\IConstraint.vb" />
<Compile Include="SQL\Views\SQLCreateView.vb" />
<Compile Include="SQL\Views\SQLDropView.vb" />
<Compile Include="SQL\Views\SQLViewExists.vb" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
Expand Down
79 changes: 79 additions & 0 deletions SQL/Views/SQLViewExists.vb
@@ -0,0 +1,79 @@
' ___________________________________________________
'
' © Hi-Integrity Systems 2012. All rights reserved.
' www.hisystems.com.au - Toby Wicks
' ___________________________________________________
'

Option Strict On
Option Explicit On

Namespace SQL

''' --------------------------------------------------------------------------------
''' <summary>
''' This class can be used to determine whether a view exists. This class can be
''' used will all databases. If after running the SQL statement the data set is not
''' empty then the view exists.
''' </summary>
''' --------------------------------------------------------------------------------
Public Class SQLViewExists
Inherits SQLStatement

Private pstrViewName As String

Public Sub New(ByVal strViewName As String)

If String.IsNullOrEmpty(strViewName) Then
Throw New ArgumentNullException
End If

pstrViewName = strViewName

End Sub

Public Overrides ReadOnly Property SQL() As String
Get

Dim strSQL As String = String.Empty
Dim objSelect As SQLSelect

Select Case Me.ConnectionType
Case Database.ConnectionType.MicrosoftAccess
Throw New NotSupportedException
Case Database.ConnectionType.MySQL
strSQL = "SHOW VIEWS LIKE " & SQLConvertValue(pstrViewName, Me.ConnectionType)
Case Database.ConnectionType.SQLServer
objSelect = New SQLSelect
With objSelect
.ConnectionType = Me.ConnectionType
.Tables.Add("sysobjects")
.Where.Add("Name", ComparisonOperator.EqualTo, pstrViewName)
.Where.Add("XType", ComparisonOperator.EqualTo, "V") 'V = User defined view
strSQL = .SQL
End With
Case Database.ConnectionType.Pervasive
Throw New NotSupportedException
Case Database.ConnectionType.HyperSQL
objSelect = New SQLSelect
With objSelect
.ConnectionType = Me.ConnectionType
.Tables.Add("VIEWS").SchemaName = "INFORMATION_SCHEMA"
.Where.Add("TABLE_SCHEMA", ComparisonOperator.EqualTo, "PUBLIC")
.Where.Add("TABLE_NAME", ComparisonOperator.EqualTo, pstrViewName)
strSQL = .SQL
End With
Case Database.ConnectionType.SQLServerCompactEdition
Throw New NotSupportedException
Case Else
Throw New NotImplementedException(Me.ConnectionType.ToString)
End Select

Return strSQL

End Get
End Property

End Class

End Namespace

0 comments on commit de3d660

Please sign in to comment.