Skip to content

Commit

Permalink
Version 1.14
Browse files Browse the repository at this point in the history
*What's New*
- Add Create2
- Add Query2
- Add getScalar2
- Add Insert2
- Add Save2
- Depreacated setWhereValue, replaced by WhereValue
- Updated Snippets to use in Web API Server template
  • Loading branch information
pyhoon committed Oct 29, 2024
1 parent 74c354c commit 2a71b87
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 38 deletions.
2 changes: 1 addition & 1 deletion DatabaseConnector.bas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Type=Class
Version=9.1
@EndOfDesignText@
' Database Connector class
' Version 1.13
' Version 1.14
Sub Class_Globals
Private DB As SQL
Private Conn As Conn
Expand Down
62 changes: 51 additions & 11 deletions MiniORM.bas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Type=Class
Version=9.71
@EndOfDesignText@
' Mini Object-Relational Mapper (ORM) class
' Version 1.13
' Version 1.14
Sub Class_Globals
Public SQL As SQL
Public INTEGER As String
Expand Down Expand Up @@ -441,6 +441,23 @@ Public Sub Create
End If
End Sub

Public Sub Create2 (CreateStatement As String)
DBStatement = CreateStatement
If BlnShowExtraLogs Then
Log(DBStatement)
End If

If BlnExecuteAfterCreate Then
Try
SQL.ExecNonQuery(DBStatement)
Catch
Log(LastException)
End Try
Else If BlnAddAfterCreate Then
SQL.AddNonQueryToBatch(DBStatement, Null)
End If
End Sub

' Replace default primary key
Public Sub Primary (mKeys() As String)
If mKeys.Length = 0 Then Return
Expand Down Expand Up @@ -678,6 +695,11 @@ Public Sub Query
#End If
End Sub

Public Sub Query2 (Params As List)
setParameters(Params)
Query
End Sub

' Return an object without query
' Note: ORMTable and ORMResults are not affected
Public Sub getScalar As Object
Expand All @@ -689,6 +711,11 @@ Public Sub getScalar As Object
End If
End Sub

Public Sub getScalar2 (Params As List) As Object
setParameters(Params)
Return getScalar
End Sub

Public Sub Insert
Dim cd As Boolean ' contains created_date
Dim sb As StringBuilder
Expand Down Expand Up @@ -738,6 +765,11 @@ Public Sub Insert
End If
End Sub

Public Sub Insert2 (Params As List)
setParameters(Params)
Insert
End Sub

' Update must have at least 1 condition
Public Sub Save
Dim BlnNew As Boolean
Expand Down Expand Up @@ -845,6 +877,11 @@ Public Sub Save
DBStatement = DBSaveStatement
End Sub

Public Sub Save2 (Params As List)
setParameters(Params)
Save
End Sub

Public Sub getLastInsertID As Object
Select DBEngine.ToUpperCase
Case "MYSQL"
Expand All @@ -866,18 +903,22 @@ Public Sub setWhere (mStatements As List)
End Sub

#If B4A or B4i
'Deprecated
Public Sub setWhereValue (mStatements As List, mParams() As String)
Dim sb As StringBuilder
sb.Initialize
For Each statement In mStatements
If sb.Length > 0 Then sb.Append(" AND ") Else sb.Append(" WHERE ")
sb.Append(statement)
Next
Condition = Condition & sb.ToString
setParameters(mParams)
End Sub
#Else
'Deprecated
Public Sub setWhereValue (mStatements As List, mParams As List)
#End If
WhereValue (mStatements, mParams)
End Sub

#If B4A or B4i
' formerly named as setWhereValue
Public Sub WhereValue (mStatements As List, mParams() As String)
#Else
' formerly named as setWhereValue
Public Sub WhereValue (mStatements As List, mParams As List)
#End If
Dim sb As StringBuilder
sb.Initialize
For Each statement In mStatements
Expand All @@ -887,7 +928,6 @@ Public Sub setWhereValue (mStatements As List, mParams As List)
Condition = Condition & sb.ToString
setParameters(mParams)
End Sub
#End If

Public Sub Delete
Dim qry As String = $"DELETE FROM ${DBTable}"$
Expand Down
2 changes: 1 addition & 1 deletion MiniORMUtils.b4j
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ Sub Process_Globals
End Sub

Sub AppStart (Args() As String)
Log("Version: 1.13")
Log("Version: 1.14")
End Sub
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# MiniORMUtils-B4X
Version: 1.13
Version: 1.14

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.
Expand Down Expand Up @@ -27,10 +27,8 @@ MDB.Create
## Insert rows
```
MDB.Columns = Array("category_name")
MDB.Parameters = Array As String("Hardwares")
MDB.Insert
MDB.Parameters = Array As String("Toys")
MDB.Insert
MDB.Insert2(Array("Hardwares"))
MDB.Insert2(Array("Toys"))
```

## Execute NonQuery Batch
Expand All @@ -55,8 +53,8 @@ Dim Items As List = MDB.Results
```
MDB.Table = "tbl_products"
MDB.Columns = Array("category_id", "product_code", "product_name", "product_price")
MDB.Parameters = Array As String(Category_Id, Product_Code, Product_Name, Product_Price)
MDB.Save
MDB.Id = 2
MDB.Save2(Array(Category_Id, Product_Code, Product_Name, Product_Price))
```

## Soft delete row
Expand Down Expand Up @@ -101,7 +99,7 @@ Dim Data As List = MDB.Results
MDB.Table = "tbl_products p"
MDB.Select = Array("p.*", "c.category_name")
MDB.Join = MDB.CreateORMJoin("tbl_category c", "p.category_id = c.id", "")
MDB.setWhereValue(Array("c.id = ?"), Array(CategoryId))
MDB.WhereValue(Array("c.id = ?"), Array(CategoryId))
MDB.Query
Dim Data As List = MDB.Results
```
1 change: 1 addition & 0 deletions Snippets/00 Initialize MiniORM.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ Public Sub Initialize (req As ServletRequest, resp As ServletResponse)
Request = req
Response = resp
HRM.Initialize
HRM.SimpleResponse = Main.SimpleResponse
DB.Initialize(Main.DBOpen, Main.DBEngine)
End Sub
1 change: 0 additions & 1 deletion Snippets/00 Sub ReturnApiResponse.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Private Sub ReturnApiResponse
HRM.SimpleResponse = Main.SimpleResponse
WebApiUtils.ReturnHttpResponse(HRM, Response)
End Sub
3 changes: 1 addition & 2 deletions Snippets/03 Post a new Resource.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ Private Sub Post$Controller$
' Check conflict product code
DB.Table = "$table$"
DB.Where = Array("$column$ = ?")
DB.Parameters = Array As String(data.Get("$column$"))
DB.Query
DB.Query2(Array(data.Get("$column$")))
If DB.Found Then
HRM.ResponseCode = 409
HRM.ResponseError = "$Controller$ already exist"
Expand Down
3 changes: 1 addition & 2 deletions Snippets/04 Put Resource by Id.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ Private Sub Put$Controller$ (id As Long)
' Check conflict $column$
DB.Table = "$table$"
DB.Where = Array("$column$ = ?", "id <> ?")
DB.Parameters = Array As String(data.Get("$column$"), id)
DB.Query
DB.Query2(Array(data.Get("$column$"), id))
If DB.Found Then
HRM.ResponseCode = 409
HRM.ResponseError = "$Controller$ already exist"
Expand Down
15 changes: 5 additions & 10 deletions Snippets/06 Create Database.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ Private Sub CreateDatabase
MDB.Create

MDB.Columns = Array("category_name")
MDB.Parameters = Array As String("Hardwares")
MDB.Insert
MDB.Parameters = Array As String("Toys")
MDB.Insert
MDB.Insert2(Array("Hardwares"))
MDB.Insert2(Array("Toys"))

MDB.Table = "tbl_products"
MDB.Columns.Add(MDB.CreateORMColumn2(CreateMap("Name": "category_id", "Type": MDB.INTEGER)))
Expand All @@ -37,12 +35,9 @@ Private Sub CreateDatabase
MDB.Create

MDB.Columns = Array("category_id", "product_code", "product_name", "product_price")
MDB.Parameters = Array As String(2, "T001", "Teddy Bear", 99.9)
MDB.Insert
MDB.Parameters = Array As String(1, "H001", "Hammer", 15.75)
MDB.Insert
MDB.Parameters = Array As String(2, "T002", "Optimus Prime", 1000.00)
MDB.Insert
MDB.Insert2(Array(2, "T001", "Teddy Bear", 99.9))
MDB.Insert2(Array(1, "H001", "Hammer", 15.75))
MDB.Insert2(Array(2, "T002", "Optimus Prime", 1000.00))

Wait For (MDB.ExecuteBatch) Complete (Success As Boolean)
If Success Then
Expand Down
2 changes: 1 addition & 1 deletion Snippets/07 Find Resources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Private Sub Query$Controller$ByKeyword (Condition As List, Value As List)
DB.Table = "tbl_$controller$ p"
DB.Select = Array("p.*", "c.$category$_name")
DB.Join = DB.CreateORMJoin("tbl_$category$ c", "p.$category$_id = c.id", "")
DB.setWhereValue(Condition, Value)
DB.WhereValue(Condition, Value)
DB.Query
HRM.ResponseCode = 200
HRM.ResponseData = DB.Results
Expand Down
2 changes: 1 addition & 1 deletion manifest.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Author=Aeric
Version=1.13
Version=1.14
B4J.DependsOn=javaobject, jSQL, jServer
B4i.DependsOn=DBUtils, iSQL
B4A.DependsOn=DBUtils, SQL

0 comments on commit 2a71b87

Please sign in to comment.