Skip to content

A mini object–relational mapping (ORM) that can be use for creating db schema and SQL queries.

License

Notifications You must be signed in to change notification settings

pyhoon/MiniORMUtils-B4X

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

176 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MiniORMUtils-B4X

Version: 4.30

A mini object–relational mapping (ORM) that can be use for creating db schema and SQL queries.
It is suitable for Web API Template or any database system.
Currently it supports SQLite (for B4A, B4i and B4J), MariaDB and MySQL (B4J only).

Usage examples

Initialize object

Private DB As MiniORM
Private MS As ORMSettings

MS.Initialize
MS.DBType = "SQLite"
MS.DBFile = "data.db"
MS.DBDir = File.DirApp

DB.Initialize
DB.Settings = MS

If DB.Exist Then
	LogColor($"${MS.DBType} database found!"$, COLOR_BLUE)
	DB.Open
Else
	LogColor($"${MS.DBType} database not found!"$, COLOR_RED)
	CreateDatabase
End If

Note: Before calling DB.Create and DB.Insert, set DB.QueryAddToBatch = True

Initialize object (no execute)

DB.Initialize
DB.DbType = DB.SQLITE
DB.QueryExecute = False
DB.Table = "categories"
Log(DB.Statement)

Create database

#If MySQL Or MariaDB
Wait For (DB.CreateDatabaseAsync) Complete (Success As Boolean)
#Else
Dim Success As Boolean = DB.InitializeSQLite
#End If

Connect to database

DB.SQL = DB.Open

Create table

DB.Table = "tbl_categories"
DB.Columns.Add(DB.CreateColumn2(CreateMap("Name": "category_name")))
DB.Create

Insert rows

DB.Columns = Array("category_name")
DB.Insert2(Array("Hardwares"))
DB.Insert2(Array("Toys"))

Execute NonQuery batch

Wait For (DB.ExecuteBatchAsync) Complete (Success As Boolean)
If Success Then
    Log("Database is created successfully!")
Else
    Log("Database creation failed!")
End If
DB.Close

Select all rows

DB.Table = "tbl_categories"
DB.Query
Dim Items As List = DB.Results

Update row

DB.Table = "tbl_products"
DB.Columns = Array("category_id", "product_code", "product_name", "product_price")
DB.Id = 2
DB.Save2(Array(Category_Id, Product_Code, Product_Name, Product_Price))

Soft delete row

DB.Id = 3
DB.SoftDelete

Permanent delete row

DB.Id = 4
DB.Delete

Batch delete rows

DB.Destroy(Array(2, 3))

Return number of rows in query results

Dim Rows As Int = DB.RowCount

Return single row

Dim Data As Map = DB.Find(2)

Filter by conditions

DB.Table = "tbl_products"
DB.Conditions = Array("category_id = ?", "product_price > ?")
DB.Parameters = Array(2, 50)
DB.OrderBy = CreateMap("id": "DESC")
DB.Query
Dim Data As List = DB.Results

Join tables

DB.Table = "tbl_products p"
DB.Columns = Array("p.*", "c.category_name")
DB.Join("tbl_categories c", "p.category_id = c.id", "")
DB.WhereParam("c.id = ?", CategoryId)
DB.Query
Dim Data As List = DB.Results

About

A mini object–relational mapping (ORM) that can be use for creating db schema and SQL queries.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages