#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
Public CantidadBases As Int
End Sub
Sub Globals
Private txtSQL As EditText
Private TabHost1 As TabHost
Private LblBasedeDatos As Label
Private ComboBasesdeDatos As Spinner
Private ComboTablas As Spinner
Private ComboCampos As Spinner
Private BotonDel As Button
Private BotonSelectFrom As Button
Private Telefono As Phone
Private f As Funciones
Private BotonInsertarTable As Button
Private Texto As String
Private TextoEncriptado As String
Private Resultado As String
End Sub
#if java
public void _onCreate() {
setTheme(16973931);
}
#end if
Sub Activity_Create(FirstTime As Boolean)
f.Initialize
Telefono.SetScreenOrientation(1)
Activity.LoadLayout("Query")
Activity.Title=" QUERY Editor"
If FirstTime Then
StateManager.ResetState("QUERY")
End If
txtSQL.Text=""
TabHost1.AddTab("DATA","DATA")
TabHost1.AddTab("SQL","SQLButtons")
TabHost1.AddTab("SYMBOLS","SYMBOLS")
If StateManager.RestoreState(Activity, "QUERY", 60) = False Then
ProgressDialogShow("Opening Query Manager")
CargoBases
End If
Dim Eleccion As String
Dim Valido As Boolean
If MySQL.BaseSeleccionada.Length>0 Then
If File.Exists(MySQL.Carpeta,"reg5.smql")=True Then
TextoEncriptado=File.ReadString(MySQL.Carpeta,"sdata.smql")
Texto=f.Decrypt(TextoEncriptado)
Eleccion=Msgbox2("Last SQL Statement. " & CRLF & Texto,"SQL","Run","Ignore","Edit", Null)
If Eleccion=DialogResponse.CANCEL Then
txtSQL.Text=""
Return
End If
If Eleccion=DialogResponse.POSITIVE Then
If MySQL.DbTableConectado=False Then
If CallSub2(MySQL,"ConectaTables",MySQL.BaseSeleccionada)=True Then
Valido=True
Else
Valido=False
End If
End If
MySQL.TextoSQL=Texto
If Valido=True Then
StartActivity("ResultadoSELECT")
Return
End If
End If
If Eleccion=DialogResponse.NEGATIVE Then
txtSQL.Text=Texto
End If
End If
End If
End Sub
Sub Activity_Resume
MySQL.Dbtable.check_connection
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
StateManager.ResetState("QUERY")
Telefono.SetScreenOrientation(-1)
Activity.Finish
Else
StateManager.SaveState(Activity, "QUERY")
End If
StateManager.SaveSettings
End Sub
#Region "Cargo Bases"
Sub CargoBases
Dim t As String
t="SELECT DISTINCT(TABLE_SCHEMA) AS BASE, Count(DISTINCT(TABLE_SCHEMA)) as Registros FROM TABLES WHERE TABLE_SCHEMA<>'information_schema'"
CallSub2(MySQL,"LeoMySQLSchema",CreateMap("TextoSQL":t,"Activity":"QUERY","Modulo":"Schema_LeoBases"))
End Sub
Public Sub SchemaLeoBases_Result(data As List, meta As Map)
Dim m As Map, i As Int
If meta.Get("TaskID")="Schema_LeoBases" Then
CantidadBases=0
ComboBasesdeDatos.Clear
If data.Size=1 Then
m=data.Get(0)
MySQL.BaseSeleccionada=m.Get("BASE")
LblBasedeDatos.Visible=True
LblBasedeDatos.Text=m.Get("BASE")
ComboBasesdeDatos.Visible=False
ComboTablas.Visible=True
CargoTablas
Else
LblBasedeDatos.Visible=False
For i = 0 To data.Size-1
m=data.Get(i)
MySQL.BaseSeleccionada=""
ComboBasesdeDatos.Add(m.Get("BASE"))
Next
ComboBasesdeDatos.Visible=True
ComboTablas.Visible=False
End If
If data.Size=0 Then
Msgbox("No Databases to Query","No Data")
End If
End If
ProgressDialogHide
End Sub
Public Sub SchemaLeoBases_SqlError(meta As Map)
Msgbox("An error Occurs retrieving the Structure Info","Error")
Log(meta)
End Sub
#end region
Sub ComboBasesdeDatos_ItemClick (Position As Int, Value As Object)
MySQL.BaseSeleccionada=ComboBasesdeDatos.GetItem(Position)
CargoTablas
ComboCampos.Clear
ComboCampos.Visible=False
End Sub
#region "Cargo Tablas"
Sub CargoTablas
Dim t As String
If MySQL.BaseSeleccionada.Length>0 Then
ComboTablas.Clear
ProgressDialogShow("Reading Tables")
t="SELECT TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA='" & MySQL.BaseSeleccionada & "'"
CallSub2(MySQL,"LeoMySQLSchema",CreateMap("TextoSQL":t,"Activity":"QUERY","Modulo":"Schema_LeoTablas"))
End If
End Sub
Sub SchemaLeoTablas_Result(data As List, meta As Map)
ComboTablas.Clear
Dim m As Map
For i = 0 To data.Size-1
m=data.Get(i)
ComboTablas.Add(m.Get("TABLE_NAME"))
Next
ComboTablas.Visible=True
ProgressDialogHide
End Sub
Sub SchemaLeoTablas_SqlError(data As Map)
Msgbox("Unable to read Tables in this Database. Error : " & data.Get("Exception"),"Error")
End Sub
#end region
Sub ComboTablas_ItemClick (Position As Int, Value As Object)
Dim t As String
ProgressDialogShow("Reading Fields")
ComboCampos.Clear
ComboCampos.Visible=True
MySQL.Tabla=ComboTablas.GetItem(Position)
ComboCampos.Add(MySQL.Tabla)
t="SELECT COLUMN_NAME FROM COLUMNS WHERE TABLE_SCHEMA='" & MySQL.BaseSeleccionada & "' AND TABLE_NAME='" & MySQL.Tabla & "'"
CallSub2(MySQL,"LeoMySQLSchema",CreateMap("TextoSQL":t,"Activity":"QUERY","Modulo":"Schema_LeoCampos"))
End Sub
Sub BotonEjecutarSQL_Click
Dim Valido As Boolean, t As String
ProgressDialogShow("Running")
Valido=True
If txtSQL.Text.Trim.Length=0 Then
Valido=False
ProgressDialogHide
Msgbox("You must Enter an SQL Statement","Data Required")
Return
End If
If CantidadBases>1 And ComboBasesdeDatos.SelectedIndex=-1 Then
Valido=False
ProgressDialogHide
Msgbox("You must Select a DataBase before","Database Required")
Return
End If
MejoroSintaxis
If ValidoComando=False Then
Valido=False
ProgressDialogHide
Msgbox("Sintax Error, Check your SQL Stattement","Incorrect SQL Sintax")
Return
End If
If Valido=True Then
If MySQL.DbTableConectado=False Then
If CallSub2(MySQL,"ConectaTables",MySQL.BaseSeleccionada)=True Then
Valido=True
Else
Valido=False
End If
End If
If Valido=True Then
t=txtSQL.Text.ToUpperCase
If t.StartsWith("SELECT ") Then
MySQL.TextoSQL=txtSQL.Text
If Msgbox2("Note that an SQL query to read several thousand records, you could hang your mobile. Confirm ?","Confirmation Required","YES","NO","CANCEL",Null)=DialogResponse.POSITIVE Then
StartActivity("ResultadoSELECT")
End If
Else
CallSub2(MySQL,"EjecutoMySQLTables",CreateMap("TextoSQL":txtSQL.Text,"Activity":"QUERY","Modulo":"Tables_EjecutoSQL"))
End If
End If
End If
ProgressDialogHide
End Sub
#region "Tables_EjecutoSQL"
Sub Tables_EjecutoSQL(meta As Map)
ProgressDialogHide
Msgbox("Process OK","Done")
End Sub
Sub Tables_EjecutoSQL_SqlError(meta As Map)
ProgressDialogHide
Dim ErrDescription As String
ErrDescription=meta.Get("Exception")
If ErrDescription.Contains("java.sql.Timestamp")=True Then
Msgbox("You have an error : To avoid Null DateTime Error , you must use 'NULLIF(DTime," & Chr(34) & "0000-00-00 00:00:00" & Chr(34) & ") AS DTime " & ErrDescription,"Error")
Else
Msgbox("Your SQL Stattement has thrown the following error " & ErrDescription,"Error")
End If
Log(meta)
End Sub
#end region
Sub ValidoComando As Boolean
Dim valido As Boolean, t As String
valido=False
t=txtSQL.Text.Trim.ToUpperCase
If t.StartsWith("SELECT ")=True Then valido=True
If t.StartsWith("DELETE ")=True Then valido=True
If t.StartsWith("UPDATE ")=True Then valido=True
If t.StartsWith("INSERT ")=True Then valido=True
If t.StartsWith("CREATE ")=True Then valido=True
If t.StartsWith("ALTER ")=True Then valido=True
Return valido
End Sub
Sub MejoroSintaxis
txtSQL.Text=txtSQL.Text.Replace("select ","SELECT")
txtSQL.Text=txtSQL.Text.Replace("Select ","SELECT")
txtSQL.Text=txtSQL.Text.Replace("update ","UPDATE")
txtSQL.Text=txtSQL.Text.Replace("Update ","UPDATE")
txtSQL.Text=txtSQL.Text.Replace("insert ","INSERT")
txtSQL.Text=txtSQL.Text.Replace("Insert ","INSERT")
txtSQL.Text=txtSQL.Text.Replace("delete ","DELETE")
txtSQL.Text=txtSQL.Text.Replace("Delete ","DELETE")
End Sub
#region "LeoCampos"
Sub Schema_LeoCamposResult(data As List, meta As Map)
Dim m As Map, i As Int
For i = 0 To data.Size-1
m=data.Get(i)
ComboCampos.Add(m.Get("COLUMN_NAME"))
Next
ProgressDialogHide
End Sub
Sub Schema_LeoCamposError(meta As Map)
Msgbox("Unable to Read Field for the Table " & MySQL.Tabla & " Error : " & meta.Get("Exception"),"Error")
End Sub
#end region
Sub ComboCampos_ItemClick (Position As Int, Value As Object)
txtSQL.Text=txtSQL.Text & ComboCampos.GetItem(Position) & " "
End Sub
#region "Botones Pantalla"
Sub BotonClear_Click
txtSQL.Text=""
End Sub
Sub BotonDel_Click
txtSQL.Text=txtSQL.Text.SubString2(0,txtSQL.Text.Length-1)
End Sub
#end region
#region "Botones Symbols"
Sub BotonSimbolSpace_Click
txtSQL.Text=txtSQL.Text & " "
End Sub
Sub BotonSimboloPunto_Click
txtSQL.Text=txtSQL.Text & "."
End Sub
Sub BotonSimboloPorcentaje_Click
txtSQL.Text=txtSQL.Text & "%"
End Sub
Sub BotonSimboloParentesisCierra_Click
txtSQL.Text=txtSQL.Text & ") "
End Sub
Sub BotonSimboloParentesisAbre_Click
txtSQL.Text=txtSQL.Text & "( "
End Sub
Sub BotonSimboloMenor_Click
txtSQL.Text=txtSQL.Text & "< "
End Sub
Sub BotonSimboloMayor_Click
txtSQL.Text=txtSQL.Text & "> "
End Sub
Sub BotonSimboloMas_Click
txtSQL.Text=txtSQL.Text & "+ "
End Sub
Sub BotonSimboloIgual_Click
txtSQL.Text=txtSQL.Text & "= "
End Sub
Sub BotonSimboloGuionMedio_Click
txtSQL.Text=txtSQL.Text & "-"
End Sub
Sub BotonSimboloDividir_Click
txtSQL.Text=txtSQL.Text & "/ "
End Sub
Sub BotonSimboloComillaSimple_Click
txtSQL.Text=txtSQL.Text & "'"
End Sub
Sub BotonSimboloComillaDoble_Click
txtSQL.Text=txtSQL.Text & Chr(34)
End Sub
Sub BotonSimboloComa_Click
txtSQL.Text=txtSQL.Text & ","
End Sub
Sub BotonSimboloAsterisco_Click
txtSQL.Text=txtSQL.Text & "* "
End Sub
Sub BotonSimbolo9_Click
txtSQL.Text=txtSQL.Text & "9"
End Sub
Sub BotonSimbolo8_Click
txtSQL.Text=txtSQL.Text & "8"
End Sub
Sub BotonSimbolo7_Click
txtSQL.Text=txtSQL.Text & "7"
End Sub
Sub BotonSimbolo6_Click
txtSQL.Text=txtSQL.Text & "6"
End Sub
Sub BotonSimbolo5_Click
txtSQL.Text=txtSQL.Text & "5"
End Sub
Sub BotonSimbolo4_Click
txtSQL.Text=txtSQL.Text & "4"
End Sub
Sub BotonSimbolo3_Click
txtSQL.Text=txtSQL.Text & "3"
End Sub
Sub BotonSimbolo2_Click
txtSQL.Text=txtSQL.Text & "2"
End Sub
Sub BotonSimbolo1_Click
txtSQL.Text=txtSQL.Text & "1"
End Sub
Sub BotonSimbolo0_Click
txtSQL.Text=txtSQL.Text & "0"
End Sub
Sub BotonGuionBajo_Click
txtSQL.Text=txtSQL.Text & "_"
End Sub
#end region
#region "Botones SQL "
Sub BotonWhere_Click
txtSQL.Text=txtSQL.Text & "WHERE "
End Sub
Sub BotonVALUES_Click
txtSQL.Text=txtSQL.Text & "VALUES "
End Sub
Sub BotonUpdate_Click
txtSQL.Text=txtSQL.Text & "UPDATE "
End Sub
Sub BotonSET_Click
txtSQL.Text=txtSQL.Text & "SET "
End Sub
Sub BotonSelect_Click
txtSQL.Text=txtSQL.Text & "SELECT "
End Sub
Sub BotonOrder_Click
txtSQL.Text=txtSQL.Text & "ORDER BY "
End Sub
Sub BotonOR_Click
txtSQL.Text=txtSQL.Text & "OR "
End Sub
Sub BotonLike_Click
txtSQL.Text=txtSQL.Text & "LIKE "
End Sub
Sub BotonJoin_Click
txtSQL.Text=txtSQL.Text & "JOIN "
End Sub
Sub BotonINTO_Click
txtSQL.Text=txtSQL.Text & "INTO "
End Sub
Sub BotonInsert_Click
txtSQL.Text=txtSQL.Text & "INSERT INTO "
End Sub
Sub BotonIN_Click
txtSQL.Text=txtSQL.Text & "IN "
End Sub
Sub BotonGroupBy_Click
txtSQL.Text=txtSQL.Text & "GROUP BY "
End Sub
Sub BotonFROM_Click
txtSQL.Text=txtSQL.Text & "FROM "
End Sub
Sub BotonDelete_Click
txtSQL.Text=txtSQL.Text & "DELETE FROM "
End Sub
Sub BotonBetween_Click
txtSQL.Text=txtSQL.Text & "BETWEEN "
End Sub
Sub BotonAND_Click
txtSQL.Text=txtSQL.Text & "AND "
End Sub
Sub BotonSelectFrom_Click
txtSQL.Text=txtSQL.Text & "SELECT * FROM "
End Sub
#end region
Sub BotonInsertarTable_Click
If ComboTablas.SelectedIndex=-1 Then
If txtSQL.Text.Length=0 Then
txtSQL.Text="SELECT * FROM " & ComboTablas.GetItem(0)
Else
txtSQL.Text=txtSQL.Text & ComboTablas.GetItem(0)
End If
Else
If txtSQL.Text.Length=0 Then
txtSQL.Text="SELECT * FROM " & ComboTablas.SelectedItem
Else
txtSQL.Text=txtSQL.Text & ComboTablas.SelectedItem
End If
End If
End Sub