﻿B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=9.8
@EndOfDesignText@
#Event: _QueryResultsDone(Result As List)
#Event: _QueryNoResultsDone(Result As Boolean)
Sub Class_Globals
	Private Phpurl2,Hostname2,Dbusername2,Dbpassword2,DbName2 As String
	Private TargetModule As Object
	Private TargetEventName As String
End Sub

'Phpurl : the link of the php file
'Hostname : The link of the database
'DbName : Name of the database
'Dbusername : Username of the database
'Dbpassword : Password of the database
Public Sub Initialize(EventName As String,Target As Object,Phpurl As String, Hostname As String,DbName As String,Dbusername As String,Dbpassword As String)
	Phpurl2 = Phpurl
	Hostname2 = Hostname
	Dbusername2 = Dbusername
	Dbpassword2 = Dbpassword
	DbName2 = DbName
	TargetModule = Target
	TargetEventName = EventName
	CheckConn
End Sub

Private Sub CheckConn
	Dim j As HttpJob
	j.Initialize("j",Me)
	j.Download2(Phpurl2,Array As String("Type","Test","Hostname",Hostname2,"username",Dbusername2,"password",Dbpassword2,"DbName",DbName2))
	Wait For (j) JobDone(j As HttpJob)
	If j.Success Then
		If j.GetString = 1 Then
			Log("Connected to the database : "&DbName2)
		Else
			Log("Not connected to the database : "&DbName2&" Please check your password or hostname")
		End If
	Else
		Log("Error while connecting to the server")
		Log("Try to connect to the internet")
	End If
End Sub

'Execute query with values returning as a List
Public Sub QueryResults(Query As String)
	Dim j As HttpJob
	j.Initialize("j",Me)
	j.Download2(Phpurl2,Array As String("Type","QueryResults","Hostname",Hostname2,"username",Dbusername2,"password",Dbpassword2,"DbName",DbName2,"Query",Query))
	Wait For (j) JobDone(j As HttpJob)
	If j.Success Then
		CallSub2(TargetModule,TargetEventName & "_QueryResultsDone",MysqlResultSet(j.GetString))
	Else
		Log("Error while connecting to the server")
		Log("Try to connect to the internet")
	End If
End Sub

'Execute query without returning any values
Public Sub QueryNoResults(Query As String)
	Dim j As HttpJob
	j.Initialize("j",Me)
	j.Download2(Phpurl2,Array As String("Type","QueryNoResults","Hostname",Hostname2,"username",Dbusername2,"password",Dbpassword2,"DbName",DbName2,"Query",Query))
	Wait For (j) JobDone(j As HttpJob)
	If j.Success Then
		If j.GetString = 1 Then
			CallSub2(TargetModule,TargetEventName & "_QueryNoResultsDone",True)
		Else
			CallSub2(TargetModule,TargetEventName & "_QueryNoResultsDone",False)
		End If
	Else
		Log("Error while connecting to the server")
		Log("Try to connect to the internet")
	End If
End Sub

Public Sub MysqlResultSet(Result As String) As List
	Dim parser As JSONParser
	Dim Out As List
	Out.Initialize
	parser.Initialize(Result)
	Out = parser.NextArray
	Return Out
End Sub

Public Sub GetString(ResultSet As List,RowIndex As Int,colName As String) As String
	Dim Map As Map = ResultSet.Get(RowIndex)
	Return Map.Get(colName)
End Sub

Public Sub GetDouble(ResultSet As List,RowIndex As Int,colName As String) As Double
	Dim Map As Map = ResultSet.Get(RowIndex)
	Return Map.Get(colName)
End Sub

Public Sub GetInt(ResultSet As List,RowIndex As Int,colName As String) As Int
	Dim Map As Map = ResultSet.Get(RowIndex)
	Return Map.Get(colName)
End Sub