Share My Creation [BANanoDataSource] A CRUD Database Abstract Layer

Ola

It has been a busy week, one needs to finalize projects. Well, writing code can become tedious, so once again, an addition to the BVAD3 family, is a Database Abstract Layer.

So far everything we have tested with MySQL is working fine. We intend looking at other databases soon. Anyway, if MySQL is working, the rest can follow anywday, or perhaps any-year.



Well what does this do exactly?

We have taken away all the code that you will have to write inside a BVAD3 WebApp and provided you with an abstract designer based tool. You give it parameters like your database connection details, the table you will be accessing, the fields, the integers, doubles and blobs you will use. Anyway, you need to use 1 DataSource per table. You will notice in most of my custom views I have removed any functionality to update stuff via code, except for most exceptions like run-time stuff.

Well you still get your CRUD, however, you dont write any CRUD code. You just point your DataSource to the content you need. Same goes for data-tables. the BANanoDataSource fires a single event when done. It fires a done event.

This is the signature for the Done Event

B4X:
#Event: Done (Action As String, Success As Boolean, Response As String, Error As String, affectedRows As Int, Result As List)

1. The action we are performing
2. Success (true / false)
3. Response - can be Success / Error
4. Error - The returned error by the DB
5. affectedRows - rather obvious
6. Results - an array of objects / maps retrieved.

As an example, for our inforbox, we want to show the count of records for categories, customers, invoices etc. We have dropped infor boxes in columns inside a row. We drop a BANanoDataSource inside the form. We then give the data-source the connection parameters (for MySQL, you also need the mysqlconfig.php updated, for dynamic usage, mysqlconfig.php is not needed))

The table we will be accessing is categories, the primary key and auto-increment field is categoryid. The fields in the table are categoryid, categoryname and categorydescription. We have also specified some default values for the fields, also specified which ones are integers, doubles and blobs. Anything else is treated as a string.

BANanoDataSource.jpg


Anyway, we load our layout...

Our process globals

B4X:
'Static code module
Sub Process_Globals  
    Public vuetify As VuetifyApp
    Public home As VueComponent
    Public path As String
    Public name As String = "home"
    Private banano As BANano
    Private categoriesinforbox As VInfoBox
    Private customerinfo As VInfoBox
    Private invoicesinfo As VInfoBox
    Private CategeriesBDS As BananoDataSource
End Sub

Then our page is initialized i.e created in Vuetify memory. We load the layout and then bind the state variables. Ohh, by the way we have created a shortcut for BindVueElement, you can still use BindVueElement, nothing will be broken, BindState just sounds better and is shorted anywhere. So if you see code with ?.BindState(?), just understand that its the shortcut for ... BindVueElement.

B4X:
Sub Initialize
    'establish a reference to the app
    vuetify = pgIndex.vuetify
    'initialize the component
    home.Initialize(Me, name)
    home.vuetify = vuetify
    'make this the start page
    home.Path = "/"
    path = home.path
    '
    'build the page html here
    banano.LoadLayout(home.Here, "home")
    '
    categoriesinforbox.BindState(home)
    customerinfo.BindState(home)
    invoicesinfo.BindState(home)
    CategeriesBDS.BindState(home)
  
    'we need to run counters
    home.SetCreated(Me, "runcounters", Null)
  
    'add this route component to the app
    vuetify.AddRoute(home)
End Sub

Now when our app is running, a user opens the page, this "creates" the router, when that is happening, we want to execute "runcounters"

So runcounters, resets the counters to zero and then runs the BANanoDataSource.COUNT call based on the database, primary key specified in it. Remember we said, 1 BANanoDataSource per table from your DB.

B4X:
Sub runcounters                'ignoreDeadCode
    'set counters to zero
    categoriesinforbox.UpdateEndValue(home, 0)
    customerinfo.UpdateEndValue(home, 0)
    invoicesinfo.UpdateEndValue(home, 0)
    '
    'run the counters for categories
    CategeriesBDS.COUNT
End Sub

As the database is mysql, this in the back-end uses the BANanoMySQLE class, runs a promise and then fires the Done Event.

So when Done is done executing, we find the action that was being performed.

B4X:
Private Sub CategeriesBDS_Done (Action As String, Success As Boolean, Response As String, Error As String, affectedRows As Int, Result As List)
    Select Case Action
    Case CategeriesBDS.ACTION_COUNT
        'we have counted categories, get the affected records
        affectedRows = banano.parseInt(affectedRows)
        'update the dashboard counter
        categoriesinforbox.UpdateEndValue(home, affectedRows)
    End Select
End Sub

In the back-end a count of the record happens, this is then set as affectedRows in this case and from that you can updates the Categories InforBox Counters.

Current Supported functions:

Below is a list of currently supported actions..

B4X:
Public const ACTION_CREATE_TABLE As String = "CreateTable"
    Public const ACTION_CREATE As String = "Create"
    Public const ACTION_READ As String = "Read"
    Public const ACTION_UPDATE As String = "Update"
    Public const ACTION_DELETE As String = "Delete"
    Public const ACTION_SELECTALL As String = "SelectAll"
    Public const ACTION_SELECTWHERE As String = "SelectWhere"
    Public const ACTION_COUNT As String = "Count"
    Public const ACTION_GETMAX As String = "GetMax"
    Public const ACTION_GETMIN As String = "GetMin"
    Public const ACTION_SUMOF As String = "SumOf"
    Public const ACTION_CUSTOM As String = "Custom"

This should get anyone creating a CRUD based WebApp with BVAD3 in no time.

Here is the output

BANanoDataSource01.gif



Here is what is in out back-end.

1624290480090.png
 
Last edited:

Mashiane

Expert
Licensed User
Last edited:
Top