Share My Creation MiniORM Models Code Generator - Convert Type to Code for generating database table

modgen-b4j​

version: 0.60

GitHub: https://github.com/pyhoon/modgen-b4j

Usage:
  1. Put the jar in B4J additional libraries folder
  2. Add the macro to Main
  3. Add a code or class module e.g UsersModel
  4. Inside Sub Process_Globals or Sub Class_Globals, add a type e.g Type Users (user_name As String, admin As Int)
  5. Place the cursor or click inside Sub Process_Globals or Sub Class_Globals
  6. Invoke macro: ModelGenerator. Wait until logs Completed. Exit code: 0
B4X:
#Macro: Title, ModelGenerator, ide://run?file=%ADDITIONAL%\modgen.jar&Args=%STATE1%&codesync=True
UsersModel.bas:
Sub Process_Globals
    Type Users ( _
    user_name As String, _
    user_email As String, _
    user_mobile As String, _
    password_hash As String, _
    password_salt As String, _
    user_image() As Byte, _
    admin As Int, _
    active As Int)
End Sub

The generated code will be appended at the bottom of code module or class immediately.
B4X:
Public Sub CreateUsersTable
    Dim DB As MiniORM
    DB = Main.DB
    DB.ShowExtraLogs = True
    DB.UseTimestamps = True
    DB.QueryExecute = False
    DB.QueryAddToBatch = True
    DB.IfNotExist = True
 
    Log("Creating Users table...")
    DB.Open
    DB.Table = "tbl_users"
    DB.Columns.Add(CreateMap("Name": "user_name", "Null": False))
    DB.Columns.Add(CreateMap("Name": "user_email", "Null": False))
    DB.Columns.Add(CreateMap("Name": "user_mobile", "Null": False))
    DB.Columns.Add(CreateMap("Name": "password_hash", "Null": False))
    DB.Columns.Add(CreateMap("Name": "password_salt", "Null": False))
    DB.Columns.Add(CreateMap("Name": "user_image", "Type": DB.BLOB))
    DB.Columns.Add(CreateMap("Name": "admin", "Type": DB.INTEGER, "Default": "0"))
    DB.Columns.Add(CreateMap("Name": "active", "Type": DB.INTEGER, "Default": "0"))
    DB.Create
 
    Wait For (DB.ExecuteBatchAsync) Complete (Success As Boolean)
    If Success Then
        Log("Table Users created successfully!")
    Else
        Log("Table Users creation failed!")
    End If
    DB.Close
    DB.QueryExecute = True
End Sub

You can then call the sub using the following code:
B4X:
UsersModel.CreateUsersTable
 

Attachments

  • modgen.jar
    128.2 KB · Views: 11
Last edited:

aeric

Expert
Licensed User
Longtime User
version: 0.50
Some improvement and support both code and class module.
 

aeric

Expert
Licensed User
Longtime User
Worth to check this tutorial if you are working with type.
 

aeric

Expert
Licensed User
Longtime User
version: 0.60
Fix bug for double execution of query directly and by batch.
Set QueryExecute back to True after finished executing batch command.

B4X:
DB.QueryExecute = False
DB.QueryAddToBatch = True
'
' Code for batch query ...
'
DB.QueryExecute = True
 

aeric

Expert
Licensed User
Longtime User
Update the Macro:

#Macro: Title, ModelGenerator, ide://run?file=%ADDITIONAL%\modgen.jar&Args=ide_state.json&codesync=True
B4X:
#Macro: Title, ModelGenerator, ide://run?file=%ADDITIONAL%\modgen.jar&Args=%STATE1%&codesync=True
Use %STATE1% for the arg instead of passing the ide_state.json file
Just press Ctrl+1 if it is the first macro
Export CodeBundle is not required.

Usage:
  1. Put the jar in B4J additional libraries folder
  2. Add the macro to Main
  3. Add a code or class module e.g UsersModel
  4. Inside Sub Process_Globals or Sub Class_Globals, add a type e.g Type Users (user_name As String, admin As Int)
  5. Place the cursor or click inside Sub Process_Globals or Sub Class_Globals
  6. Click Export state (or press Ctrl+R). Wait until logs Completed. Exit code: 0
  7. Invoke macro: ModelGenerator. Wait until logs Completed. Exit code: 0
 

aeric

Expert
Licensed User
Longtime User
Tips:
Please save the code module or class before calling the macro to avoid duplicate code injected.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Thanks for alerting me to the magic key that unlocks the treasure chest that is B4X...the %STATE1% parameter!

For those out there who haven't tried it:
If you want to process your own code to get info, generate new code, or perform some helper functions...
1. Write the code to have the desired effect in a B4J console App - use the Args() parameter in Main to receive info from a #Macro
2. After compilation, place the release .jar in the Additional folder.
3. In the macro linked to this .jar, pass the context and state of the code currently in the IDE with %STATE1% - it is a link to a .json file containing the info.

And there you have it - 1, 2, 3
 

aeric

Expert
Licensed User
Longtime User
Thanks for alerting me to the magic key that unlocks the treasure chest that is B4X...the %STATE1% parameter!

For those out there who haven't tried it:
If you want to process your own code to get info, generate new code, or perform some helper functions...
1. Write the code to have the desired effect in a B4J console App - use the Args() parameter in Main to receive info from a #Macro
2. After compilation, place the release .jar in the Additional folder.
3. In the macro linked to this .jar, pass the context and state of the code currently in the IDE with %STATE1% - it is a link to a .json file containing the info.

And there you have it - 1, 2, 3

Correct.
My understanding is when we execute a Macro by passing the %STATE1% argument, the IDE will generate an ide_state.json file in the Objects folder of the calling project.

We can then read the json file to obtain some useful info.

1776524104207.png

(Note some keys are removed for simple illustration)

This tool is making use of the key "caret_context" to read the value as code and "parse" it to get the "fields".
Maybe someone will find other useful values for their tools.

If anyone is interested, please check my code on GitHub.
 
Top