'Handler class
Sub Class_Globals
#if VERSION1
Private const T_NULL = 0, T_STRING = 1, T_SHORT = 2, T_INT = 3, T_LONG = 4, T_FLOAT = 5 _
,T_DOUBLE = 6, T_BOOLEAN = 7, T_BLOB = 8 As Byte
Private bc As ByteConverter
Private cs As CompressedStreams
#end if
Private DateTimeMethods As Map
End Sub
Public Sub Initialize
DateTimeMethods = CreateMap(91: "getDate", 92: "getTime", 93: "getTimestamp")
End Sub
Sub Handle(req As ServletRequest, resp As ServletResponse)
Dim start As Long = DateTime.Now
Dim COMANDO As String
Dim in As InputStream = req.InputStream
Dim method As String = req.GetParameter("method")
Dim PARAMETROS As String = req.GetParameter("Parameters")
Dim ERRO As String
Dim con As SQL
Try
con = Main.rdcConnector1.GetConnection
If method = "query2" Then
COMANDO = ExecuteQuery2(con, in, resp)
Else if method = "batch2" Then
COMANDO = ExecuteBatch2(con, in, resp)
Else
Log("Unknown method: " & method)
resp.SendError(500, "unknown method")
End If
Catch
ERRO=LastException
resp.SendError(500, LastException.Message)
ESCREVE_NO_LOG(LastException.Message)
End Try
If con <> Null And con.IsInitialized Then con.Close
LogDebug($"${CRLF}********************************************************************${CRLF}METODO: ${method} ${CRLF}COMANDO: ${COMANDO} ${CRLF} TEMPO: ${DateTime.Now - start}ms${CRLF} TELEFONE ORIGEM=${req.RemoteAddress}${CRLF}PARAMETROS: ${PARAMETROS}${CRLF}ERRO:${ERRO}${CRLF}********************************************************************"$)
End Sub
Private Sub ExecuteQuery2 (con As SQL, in As InputStream, resp As ServletResponse) As String
Dim ser As B4XSerializator
Dim m As Map = ser.ConvertBytesToObject(Bit.InputStreamToBytes(in))
Dim cmd As RDC2_DBCommand = m.Get("COMANDO")
Dim limit As Int = m.Get("LIMITE")
Dim rs As ResultSet = con.ExecQuery2(Main.rdcConnector1.GetCommand(cmd.Name), cmd.Parameters)
If limit <= 0 Then limit = 0x7fffffff 'max int
Dim jrs As JavaObject = rs
Dim rsmd As JavaObject = jrs.RunMethod("getMetaData", Null)
Dim cols As Int = rs.ColumnCount
Dim res As RDC2_DBResult
res.Initialize
res.columns.Initialize
res.Tag = Null 'without this the Tag properly will not be serializable.
For i = 0 To cols - 1
res.columns.Put(rs.GetColumnName(i), i)
Next
res.Rows.Initialize
Do While rs.NextRow And limit > 0
Dim row(cols) As Object
For i = 0 To cols - 1
Dim ct As Int = rsmd.RunMethod("getColumnType", Array(i + 1))
'check whether it is a blob field
If ct = -2 Or ct = 2004 Or ct = -3 Or ct = -4 Then
row(i) = rs.GetBlob2(i)
Else if ct = 2 Or ct = 3 Then
row(i) = rs.GetDouble2(i)
Else If DateTimeMethods.ContainsKey(ct) Then
Dim SQLTime As JavaObject = jrs.RunMethodJO(DateTimeMethods.Get(ct), Array(i + 1))
If SQLTime.IsInitialized Then
row(i) = SQLTime.RunMethod("getTime", Null)
Else
row(i) = Null
End If
Else
row(i) = jrs.RunMethod("getObject", Array(i + 1))
End If
Next
res.Rows.Add(row)
Loop
rs.Close
Dim data() As Byte = ser.ConvertObjectToBytes(res)
resp.OutputStream.WriteBytes(data, 0, data.Length)
Return "query: " & cmd.Name
End Sub
Private Sub ExecuteBatch2(con As SQL, in As InputStream, resp As ServletResponse) As String
Dim ser As B4XSerializator
Dim m As Map = ser.ConvertBytesToObject(Bit.InputStreamToBytes(in))
Dim commands As List = m.Get("commands")
Dim res As RDC2_DBResult
res.Initialize
res.columns = CreateMap("AffectedRows (N/A)": 0)
res.Rows.Initialize
res.Tag = Null
Try
con.BeginTransaction
For Each cmd As RDC2_DBCommand In commands
con.ExecNonQuery2(Main.rdcConnector1.GetCommand(cmd.Name), _
cmd.Parameters)
Next
res.Rows.Add(Array As Object(0))
con.TransactionSuccessful
Catch
con.Rollback
Log(LastException)
resp.SendError(500, LastException.Message)
End Try
Dim data() As Byte = ser.ConvertObjectToBytes(res)
resp.OutputStream.WriteBytes(data, 0, data.Length)
Return $"batch (size=${commands.Size})"$
End Sub
PRIVATE Sub ESCREVE_NO_LOG (TEXTO As String)
Dim TextWriter1 As TextWriter
TextWriter1.Initialize(File.OpenOutput(File.DirApp, "Text.txt", True))
TextWriter1.WriteLine($"$DateTime{DateTime.Now} - ${TEXTO}"$)
TextWriter1.Close
End Sub