2009-07-18 17:50:51 +02:00
|
|
|
' Gambas class file
|
|
|
|
|
|
|
|
Export
|
|
|
|
|
|
|
|
Class Desktop
|
|
|
|
|
2015-03-07 11:26:01 +01:00
|
|
|
Static Property Read Count As Integer
|
2009-07-18 17:50:51 +02:00
|
|
|
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
|
|
|
|
|
2014-11-19 19:45:18 +01:00
|
|
|
Static Private Sub ReadConnectionFile(sName As String) As Collection
|
2009-07-18 17:50:51 +02:00
|
|
|
|
|
|
|
Dim sPath As String
|
2014-11-19 19:45:18 +01:00
|
|
|
Dim cData As New Collection
|
2009-07-18 17:50:51 +02:00
|
|
|
Dim hFile As File
|
|
|
|
Dim sLine As String
|
2014-11-19 19:45:18 +01:00
|
|
|
Dim bInConnection As Boolean
|
2009-07-18 17:50:51 +02:00
|
|
|
Dim aLine As String[]
|
|
|
|
|
2014-11-19 19:45:18 +01:00
|
|
|
sPath = "../.connection" &/ sName & ".connection"
|
2009-07-18 17:50:51 +02:00
|
|
|
If Not Exist(sPath) Then Return
|
|
|
|
|
|
|
|
hFile = Open sPath
|
|
|
|
|
|
|
|
While Not Eof(hFile)
|
|
|
|
|
|
|
|
Line Input #hFile, sLine
|
|
|
|
If Left(sLine) = "#" Then Continue
|
2014-11-19 19:45:18 +01:00
|
|
|
If Left(sLine) = "[" Then
|
|
|
|
bInConnection = sLine = "[Connection]"
|
|
|
|
Continue
|
|
|
|
Endif
|
2009-07-18 17:50:51 +02:00
|
|
|
|
2014-11-19 19:45:18 +01:00
|
|
|
If bInConnection Then
|
2009-07-18 17:50:51 +02:00
|
|
|
|
2014-11-19 19:45:18 +01:00
|
|
|
aLine = Scan(sLine, "*=*")
|
|
|
|
If aLine.Count < 2 Then Continue
|
|
|
|
|
|
|
|
Select Case LCase(aLine[0])
|
|
|
|
|
|
|
|
Case "type"
|
|
|
|
cData["type"] = UnQuote(aLine[1])
|
2009-07-18 17:50:51 +02:00
|
|
|
|
2014-11-19 19:45:18 +01:00
|
|
|
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
|
2009-07-18 17:50:51 +02:00
|
|
|
|
|
|
|
Wend
|
|
|
|
|
|
|
|
Close #hFile
|
|
|
|
|
2014-11-19 19:45:18 +01:00
|
|
|
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"]
|
|
|
|
|
2009-07-18 17:50:51 +02:00
|
|
|
If bPassword Then
|
2014-10-25 16:35:35 +02:00
|
|
|
If Component.IsLoaded("gb.desktop") Then
|
2014-11-19 19:45:18 +01:00
|
|
|
Try hConn.Password = Desktop.Passwords[Application.Name &/ sName]
|
2014-09-15 05:28:41 +02:00
|
|
|
If Error Then
|
2014-11-19 19:45:18 +01:00
|
|
|
Error "gb.db: warning: "; Application.Name &/ sName; ": unable to retrieve connection password: "; Error.Text
|
2014-09-15 05:28:41 +02:00
|
|
|
Endif
|
2011-11-24 18:49:53 +01:00
|
|
|
Endif
|
2009-07-18 17:50:51 +02:00
|
|
|
Endif
|
|
|
|
|
2014-11-19 19:45:18 +01:00
|
|
|
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
|
|
|
|
|
2014-11-23 03:22:57 +01:00
|
|
|
hConn = GetConnectionFrom(Name, cData)
|
2009-07-18 17:50:51 +02:00
|
|
|
$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
|
2014-11-19 19:45:18 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-11-19 21:02:43 +01:00
|
|
|
hConn = GetConnectionFrom(Name, cData)
|
2014-11-19 19:45:18 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-03-07 11:26:01 +01:00
|
|
|
|
|
|
|
Static Private Function Count_Read() As Integer
|
|
|
|
|
|
|
|
Init()
|
|
|
|
Return $aConn.Count
|
|
|
|
|
|
|
|
End
|