gambas-source-code/main/lib/db/gb.db/.src/Connections.class

211 lines
4.1 KiB
Text
Raw Normal View History

' Gambas class file
Export
Class Desktop
Static Property Read Count As Integer
Static Property Read Key As String
Static Private $cConn As New Collection
Static Private $aConn As String[]
Static Private $sKey As String
Static Public Sub Exist(Name As String) As Boolean
If $cConn.Exist(Name) Or If Exist("../.connection" &/ Name & ".connection") Then Return True
End
Static Private Sub Init()
Dim sFile As String
If $aConn Then Return
$aConn = New String[]
For Each sFile In Dir("../.connection", "*.connection")
$aConn.Add(File.BaseName(sFile))
Next
End
Static Private Sub ReadConnectionFile(sName As String) As Collection
Dim sPath As String
Dim cData As New Collection
Dim hFile As File
Dim sLine As String
Dim bInConnection As Boolean
Dim aLine As String[]
sPath = "../.connection" &/ sName & ".connection"
If Not Exist(sPath) Then Return
hFile = Open sPath
While Not Eof(hFile)
Line Input #hFile, sLine
If Left(sLine) = "#" Then Continue
If Left(sLine) = "[" Then
bInConnection = sLine = "[Connection]"
Continue
Endif
If bInConnection Then
aLine = Scan(sLine, "*=*")
If aLine.Count < 2 Then Continue
Select Case LCase(aLine[0])
Case "type"
cData["type"] = UnQuote(aLine[1])
Case "path", "host"
cData["host"] = UnQuote(aLine[1])
Case "port"
cData["port"] = UnQuote(aLine[1])
Case "database"
cData["name"] = UnQuote(aLine[1])
Case "ignorecharset"
cData["ignorecharset"] = LCase(aLine[1]) = "true"
Case "user"
cData["user"] = UnQuote(aLine[1])
Case "rememberpassword"
cData["rememberpassword"] = LCase(aLine[1]) = "true"
End Select
Endif
Wend
Close #hFile
Return cData
End
Static Private Sub GetConnectionFrom(sName As String, cData As Collection) As Connection
Dim hConn As Connection
Dim bPassword As Boolean
hConn = New Connection
hConn.Type = cData["type"]
hConn.Host = cData["host"]
hConn.Port = cData["port"]
hConn.Name = cData["name"]
hConn.IgnoreCharset = cData["ignorecharset"]
hConn.User = cData["user"]
bPassword = cData["rememberpassword"]
If bPassword Then
If Component.IsLoaded("gb.desktop") Then
Try hConn.Password = Desktop.Passwords[Application.Name &/ sName]
If Error Then
Error "gb.db: warning: "; Application.Name &/ sName; ": unable to retrieve connection password: "; Error.Text
Endif
Endif
Endif
Return hConn
End
Static Public Sub _get(Name As String) As Connection
Dim hConn As Connection
Dim cData As Collection
Init()
If $cConn.Exist(Name) Then Return $cConn[Name]
cData = ReadConnectionFile(Name)
If Not cData Then Return
hConn = GetConnectionFrom(Name, cData)
$cConn[Name] = hConn
Return hConn
End
Static Public Sub _next() As Connection
Init()
If Not Enum.Index Then Enum.Index = 0
If Enum.Index >= $aConn.Count Then
$sKey = ""
Enum.Stop
Else
Inc Enum.Index
$sKey = $aConn[Enum.Index - 1]
Return _get($sKey)
Endif
End
Static Private Function Key_Read() As String
Return $sKey
End
Static Public Sub Create(Name As String) As Connection
Dim cData As Collection
Dim hConn As Connection
Dim hConnDB As Connection
Dim sPath As String
Init()
cData = ReadConnectionFile(Name)
If Not cData Then Return
hConn = GetConnectionFrom(Name, cData)
sPath = "../.connection" &/ Name & ".template"
If Exist(sPath) Then
hConnDB = New Connection
hConnDB.Type = hConn.Type
hConnDB.Host = hConn.Host
hConnDB.Port = hConn.Port
hConnDB.Open
If Not hConnDB.Databases.Exist(hConn.Name) Then
hConnDB.Databases.Add(hConn.Name)
Endif
hConnDB.Close
hConn.Open
hConn.ApplyTemplate(File.Load(sPath))
hConn.Close
Endif
Return hConn
End
Static Private Function Count_Read() As Integer
Init()
Return $aConn.Count
End