jueves, 1 de mayo de 2014

Singleton para MySQL

Singleton para MySQL
Esta es nuestra primer entrada, les comparto nuestro objeto Singleton para conexiones MySQL
cortesía del proyecto Norring.



Imports MySql.Data.MySqlClient

'Imports MySql.Data.MySqlClient

Namespace sepsol.databases

    Public Class SingletonMySql

        'Singleton implemented just for mysql commands

#Region "Attributes"

        Private Shared _instance As SingletonMySql = Nothing

        Private _dataobjects As IConnections.DataObject

        Private _stringconnection As String

        Private _driver As IConnections.Driver
        Private _host As String
        Private _port As String
        Private _user As String
        Private _database As String
        Private _password As String
        Private _others1 As String
        Private _allowvariables As String
        Private _characterset As String

        'Objects MySQLCommand
        Private _mysqlconnection As MySqlConnection

        'Status
        Private _statusconnection As Boolean

        'Load configuration
        Private _ifileconfig As IFileManager



#End Region

#Region "Builders"

        Private Sub New()

            'Init first vars
            _statusconnection = False
            _dataobjects = New IConnections.DataObject
            _dataobjects = IConnections.DataObject.MySQLCommand

            _stringconnection = Nothing


            'Load external config for norring apps
            _ifileconfig = New ReadXML

            _ifileconfig.FileName = "NorringCredentials.xml"
            _ifileconfig.FilePath = "c:\nor"

            _ifileconfig.LoadFile(IFileManager.FileType.Xml)

            'Collect data for connection
            _driver = New IConnections.Driver
            _host = _ifileconfig.XMLFileReturnNodeValue("host") 'Nothing
            _port = _ifileconfig.XMLFileReturnNodeValue("port") ''Nothing
            _user = _ifileconfig.XMLFileReturnNodeValue("user") 'Nothing
            _database = _ifileconfig.XMLFileReturnNodeValue("database") 'Nothing
            _password = _ifileconfig.XMLFileReturnNodeValue("password") 'Nothing
            _others1 = _ifileconfig.XMLFileReturnNodeValue("others") 'Nothing
            _allowvariables = _ifileconfig.XMLFileReturnNodeValue("allowvariables")
            _characterset = _ifileconfig.XMLFileReturnNodeValue("characterset")



            'Build connection string
            _string_formysql51()

            'Instance of mysql connecion
            _mysqlconnection = New MySqlConnection

            'Initialize connection
            StartConnection()


        End Sub

#End Region

#Region "Properties"
        Public ReadOnly Property IsConnected() As Boolean
            Get
                Return _statusconnection
            End Get
        End Property

        Public ReadOnly Property MySQLConnectionSingleton As MySqlConnection
            Get
                Return _mysqlconnection
            End Get
        End Property
#End Region

#Region "Methods"

        Private Sub _string_formysql51()

            Select Case _dataobjects
                'data source=localhost;database=temp_db;Uid=temp;password=pass;character set=utf8;
                Case IConnections.DataObject.MySQLCommand
                    _stringconnection = "Server=" & _host & ";" & _
                                      "Database=" & _database & ";" & _
                                      "UID=" & _user & ";" & _
                                      "Pwd=" & _password & ";" & _
                                      "Port=" & _port & ";" & _
                                      "Connect Timeout=5;" & _
                                      "Allow User Variables=" & _allowvariables & ";" & _
                                      "Pooling=True;" &
                                      "character set=" & _characterset & ";"

                Case Else
                    'NOT IMPLEMENTED
            End Select
        End Sub


        Private Sub StartConnection()
            If Not _statusconnection Then
                Try

                    _mysqlconnection.ConnectionString = _stringconnection
                    _mysqlconnection.Open()

                    'flag
                    _statusconnection = True
                Catch

                    'flag
                    _statusconnection = False
                End Try

            End If
        End Sub

        Private Sub StopConnection()
            If _statusconnection Then

                _mysqlconnection.Close()

                'flag
                _statusconnection = False
            End If
        End Sub


        Public Shared Function GetInstance() As SingletonMySql
            If _instance Is Nothing Then
                _instance = New SingletonMySql()

            End If
            Return _instance
        End Function

#End Region

#Region "Properties"

        Public Property DriverWork As IConnections.Driver
            Get
                Return _driver
            End Get
            Set(ByVal value As IConnections.Driver)
                _driver = value
            End Set
        End Property

        Public Property Host As String
            Get
                Return _host
            End Get
            Set(ByVal value As String)
                _host = value
            End Set
        End Property

        Public Property Password As String
            Get
                Return _password
            End Get
            Set(ByVal value As String)
                _password = value
            End Set
        End Property

        Public Property Port As String
            Get
                Return _port
            End Get
            Set(ByVal value As String)
                _port = value
            End Set
        End Property


        Public Property User As String
            Get
                Return _user
            End Get
            Set(ByVal value As String)
                _user = value
            End Set
        End Property

#End Region






    End Class

End Namespace

No hay comentarios.:

Publicar un comentario