diff --git a/DatabaseConnector.bas b/DatabaseConnector.bas index bb03d7f..e48974b 100644 --- a/DatabaseConnector.bas +++ b/DatabaseConnector.bas @@ -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 diff --git a/MiniORM.bas b/MiniORM.bas index e834475..a8e1ccf 100644 --- a/MiniORM.bas +++ b/MiniORM.bas @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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" @@ -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 @@ -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}"$ diff --git a/MiniORMUtils.b4j b/MiniORMUtils.b4j index ba448de..ca91f44 100644 --- a/MiniORMUtils.b4j +++ b/MiniORMUtils.b4j @@ -23,5 +23,5 @@ Sub Process_Globals End Sub Sub AppStart (Args() As String) - Log("Version: 1.13") + Log("Version: 1.14") End Sub \ No newline at end of file diff --git a/README.md b/README.md index 8f6d610..ee5d6f8 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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 @@ -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 ``` diff --git a/Snippets/00 Initialize MiniORM.txt b/Snippets/00 Initialize MiniORM.txt index 4219472..b9d49a1 100644 --- a/Snippets/00 Initialize MiniORM.txt +++ b/Snippets/00 Initialize MiniORM.txt @@ -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 \ No newline at end of file diff --git a/Snippets/00 Sub ReturnApiResponse.txt b/Snippets/00 Sub ReturnApiResponse.txt index 57cab70..80ebb28 100644 --- a/Snippets/00 Sub ReturnApiResponse.txt +++ b/Snippets/00 Sub ReturnApiResponse.txt @@ -1,4 +1,3 @@ Private Sub ReturnApiResponse - HRM.SimpleResponse = Main.SimpleResponse WebApiUtils.ReturnHttpResponse(HRM, Response) End Sub \ No newline at end of file diff --git a/Snippets/03 Post a new Resource.txt b/Snippets/03 Post a new Resource.txt index df58727..6b299a9 100644 --- a/Snippets/03 Post a new Resource.txt +++ b/Snippets/03 Post a new Resource.txt @@ -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" diff --git a/Snippets/04 Put Resource by Id.txt b/Snippets/04 Put Resource by Id.txt index 4ebde49..ff2d958 100644 --- a/Snippets/04 Put Resource by Id.txt +++ b/Snippets/04 Put Resource by Id.txt @@ -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" diff --git a/Snippets/06 Create Database.txt b/Snippets/06 Create Database.txt index 8da51a5..1d81aba 100644 --- a/Snippets/06 Create Database.txt +++ b/Snippets/06 Create Database.txt @@ -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))) @@ -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 diff --git a/Snippets/07 Find Resources.txt b/Snippets/07 Find Resources.txt index bfe976f..5cbacd0 100644 --- a/Snippets/07 Find Resources.txt +++ b/Snippets/07 Find Resources.txt @@ -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 diff --git a/manifest.txt b/manifest.txt index 7c24c52..01542da 100644 --- a/manifest.txt +++ b/manifest.txt @@ -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 \ No newline at end of file