B4J Code Snippet Sharing the goodness: Useful methods

Discussion in 'B4J Code Snippets' started by Mashiane, Oct 6, 2015.

  1. Mashiane

    Mashiane Expert Licensed User

    'Description: Save Base64 string to an image file
    'Tag: base64, image, stringUtils
    Code:
    'convert the base64 string to an image
    private Sub Base64StringToImage(s As StringAs Image
        
    Dim su As StringUtils
        
    Dim bytes() As Byte = su.DecodeBase64(s)
        
    Dim inp As InputStream
        inp.InitializeFromBytesArray(bytes, 
    0, bytes.Length)
        
    Dim bmp As Image
        bmp.Initialize2(inp)
        inp.Close
        
    Return bmp
    End Sub

    'save the image
    public Sub Save(dataURL As String,imgDir As String, imgFile As String)
        
    Try
            
    'clean up the image first
            dataURL = dataURL.Replace("data:image/jpeg;base64,","")
            
    Dim img As Image = Base64StringToImage(dataURL)
            
    Dim outp As OutputStream = File.OpenOutput(imgDir, imgFile,False)
            img.WriteToStream(outp)
            outp.Close
        
    Catch
        
    End Try
    End Sub
     
  2. Mashiane

    Mashiane Expert Licensed User

    Automatic Type object creation from a delimited string.

    Why: I wanted to create a type from a delimited string, kinda lazy to type

    Code:
    'Description: Create a Type Object from a delimited string
    'Tags: delimited string, type object
    Sub MvStringToType(Delim As String, TypeName As String, EnumName As String, MvStr As StringAs String
        MvStr = MvStr.Replace(
    "'","")
        MvStr = MvStr.Replace(
    QUOTE,"")
        
    Dim sb As StringBuilder
        sb.initialize
        sb.Append(
    "Type ").Append(TypeName).Append("(")
        
    Dim items() As String = StrParse(Delim,MvStr)
        
    For Each strItem As String In items
            strItem = strItem.Trim
            sb.Append(strItem).Append(
    " As String,")
        
    Next
        sb = RemDelimSB(
    ",",sb)
        sb.Append(
    ")").Append(CRLF)
        sb.Append(
    "Public ").Append(EnumName).Append(" As ").Append(TypeName).Append(CRLF)
        sb.Append(EnumName).Append(
    ".Initialize").Append(CRLF)
        
    For Each strItem As String In items
            strItem = strItem.Trim
            sb.Append(EnumName).Append(
    ".").Append(strItem).Append(" = ")
            sb.Append(InQuotes(strItem)).append(
    CRLF)
        
    Next
        
    Return sb.tostring
    End Sub
    Usage..

    Code:
    Dim sString As String = "moveToLeft_moveFromRight', 'moveToRight_moveFromLeft', 'moveToTop_moveFromBottom', 'moveToBottom_moveFromTop', 'fade_moveFromRight', 'fade_moveFromLeft', 'fade_moveFromBottom', 'fade_moveFromTop', 'moveToLeftFade_moveFromRightFade', 'moveToRightFade_moveFromLeftFade', 'moveToTopFade_moveFromBottomFade', 'moveToBottomFade_moveFromTopFade', 'moveToLeftEasing_moveFromRight', 'moveToRightEasing_moveFromLeft', 'moveToTopEasing_moveFromBottom', 'moveToBottomEasing_moveFromTop', 'scaleDown_moveFromRight', 'scaleDown_moveFromLeft', 'scaleDown_moveFromBottom', 'scaleDown_moveFromTop', 'scaleDown_scaleUpDown', 'scaleDownUp_scaleUp', 'moveToLeft_scaleUp', 'moveToRight_scaleUp', 'moveToTop_scaleUp', 'moveToBottom_scaleUp', 'scaleDownCenter_scaleUpCenter', 'rotateRightSideFirst_moveFromRight', 'rotateLeftSideFirst_moveFromLeft', 'rotateTopSideFirst_moveFromTop', 'rotateBottomSideFirst_moveFromBottom', 'flipOutRight_flipInLeft', 'flipOutLeft_flipInRight', 'flipOutTop_flipInBottom', 'flipOutBottom_flipInTop', 'rotateFall_scaleUp', 'rotateOutNewspaper_rotateInNewspaper', 'rotatePushLeft_moveFromRight', 'rotatePushRight_moveFromLeft', 'rotatePushTop_moveFromBottom', 'rotatePushBottom_moveFromTop', 'rotatePushLeft_rotatePullRight', 'rotatePushRight_rotatePullLeft', 'rotatePushTop_rotatePullBottom', 'rotatePushBottom_page', 'rotateFoldLeft_moveFromRightFade', 'rotateFoldRight_moveFromLeftFade', 'rotateFoldTop_moveFromBottomFade', 'rotateFoldBottom_moveFromTopFade', 'moveToRightFade_rotateUnfoldLeft', 'moveToLeftFade_rotateUnfoldRight', 'moveToBottomFade_rotateUnfoldTop', 'moveToTopFade_rotateUnfoldBottom', 'rotateRoomLeftOut_rotateRoomLeftIn', 'rotateRoomRightOut_rotateRoomRightIn', 'rotateRoomTopOut_rotateRoomTopIn', 'rotateRoomBottomOut_rotateRoomBottomIn', 'rotateCubeLeftOut_rotateCubeLeftIn', 'rotateCubeRightOut_rotateCubeRightIn', 'rotateCubeTopOut_rotateCubeTopIn', 'rotateCubeBottomOut_rotateCubeBottomIn', 'rotateCarouselLeftOut_rotateCarouselLeftIn', 'rotateCarouselRightOut_rotateCarouselRightIn', 'rotateCarouselTopOut_rotateCarouselTopIn', 'rotateCarouselBottomOut_rotateCarouselBottomIn', 'rotateSidesOut_rotateSidesInDelay', 'rotateSlideOut_rotateSlideIn', 'random'"
        
    Log(jMash.MvStringToType(",","TransitionObject","EnumTransition",sString))
    Output (better to save to a file for very long types)

    Code:
    Type TransitionObject(moveToLeft_moveFromRight As String,moveToRight_moveFromLeft As String,moveToTop_moveFromBottom As String,moveToBottom_moveFromTop As String,fade_moveFromRight As String,fade_moveFromLeft As String,fade_moveFromBottom As String,fade_moveFromTop As String,moveToLeftFade_moveFromRightFade As String,moveToRightFade_moveFromLeftFade As String,moveToTopFade_moveFromBottomFade As String,moveToBottomFade_moveFromTopFade As String,moveToLeftEasing_moveFromRight As String,moveToRightEasing_moveFromLeft As String,moveToTopEasing_moveFromBottom As String,moveToBottomEasing_moveFromTop As String,scaleDown_moveFromRight As String,scaleDown_moveFromLeft As String,scaleDown_moveFromBottom As String,scaleDown_moveFromTop As String,scaleDown_scaleUpDown As String,scaleDownUp_scaleUp As String,moveToLeft_scaleUp As String,moveToRight_scaleUp As String,moveToTop_scaleUp As String,moveToBottom_scaleUp As String,scaleDownCenter_scaleUpCenter As String,rotateRightSideFirst_moveFromRight...
    Public EnumTransition As TransitionObject
    EnumTransition.Initialize
    EnumTransition.moveToLeft_moveFromRight = 
    "moveToLeft_moveFromRight"
    EnumTransition.moveToRight_moveFromLeft = 
    "moveToRight_moveFromLeft"
    EnumTransition.moveToTop_moveFromBottom = 
    "moveToTop_moveFromBottom"
    EnumTransition.moveToBottom_moveFromTop = 
    "moveToBottom_moveFromTop"
    EnumTransition.fade_moveFromRight = 
    "fade_moveFromRight"
    EnumTransition.fade_moveFromLeft = 
    "fade_moveFromLeft"
    EnumTransition.fade_moveFromBottom = 
    "fade_moveFromBottom"
    EnumTransition.fade_moveFromTop = 
    "fade_moveFromTop"
    EnumTransition.moveToLeftFade_moveFromRightFade = 
    "moveToLeftFade_moveFromRightFade"
    EnumTransition.moveToRightFade_moveFromLeftFade = 
    "moveToRightFade_moveFromLeftFade"
    EnumTransition.moveToTopFade_moveFromBottomFade = 
    "moveToTopFade_moveFromBottomFade"
    EnumTransition.moveToBottomFade_moveFromTopFade = 
    "moveToBottomFade_moveFromTopFade"
    EnumTransition.moveToLeftEasing_moveFromRight = 
    "moveToLeftEasing_moveFromRight"
    EnumTransition.moveToRightEasing_moveFromLeft = 
    "moveToRightEasing_moveFromLeft"
    EnumTransition.moveToTopEasing_moveFromBottom = 
    "moveToTopEasing_moveFromBottom"
    EnumTransition.moveToBottomEasing_moveFromTop = 
    "moveToBottomEasing_moveFromTop"
    EnumTransition.scaleDown_moveFromRight = 
    "scaleDown_moveFromRight"
    EnumTransition.scaleDown_moveFromLeft = 
    "scaleDown_moveFromLeft"
    EnumTransition.scaleDown_moveFromBottom = 
    "scaleDown_moveFromBottom"
    EnumTransition.scaleDown_moveFromTop = 
    "scaleDown_moveFromTop"
    EnumTransition.scaleDown_scaleUpDown = 
    "scaleDown_scaleUpDown"
     
  3. Mashiane

    Mashiane Expert Licensed User

    'Description: Get the column header position in a tableview TV is tableview
    'Tag:Tableview, column header position
    Code:
    'get the column position
    Sub ColumnPos(colName As StringAs Int
        
    Dim colCnt As Int
        colTot = TV.ColumnsCount - 
    1
        
    For colCnt = 0 To colTot
            
    Dim colName1 As String = TV.GetColumnHeader(colCnt)
            
    If colName.EqualsIgnoreCase(colName1) Then
                
    Return colCnt
            
    End If
        
    Next
        
    Return -1   
    End Sub
     
  4. Mashiane

    Mashiane Expert Licensed User

    'Description: Set the tableview header to an icon
    'Tags: TableView, header icon

    icons.png

    Usage

    Code:
    etvCRUD.TableViewSetColumnHeaderIcon("Update",0xF0C7)
            etvCRUD.TableViewSetColumnHeaderIcon(
    "Search",0xF002)
    Code:
    Sub TableViewSetColumnHeaderIcon(colName As String, colIcon As String)
        
    'find the column position in the tableview
        Dim idxPos As Int = ColumnPos(colName)
        
    If idxPos = -1 Then Return
        
    Dim btnAction As Button
        btnAction.Initialize(
    "")
        btnAction.PrefWidth = 
    "14"
        btnAction.PrefHeight = 
    "14"
        btnAction.Enabled = 
    True
        
    'btnAction.MouseCursor = fx.Cursors.HAND
        btnAction.Font = awesome
        btnAction.Text = 
    Chr(colIcon)
        CSSUtils.SetBackgroundColor(btnAction, fx.Colors.Transparent)
        CSSUtils.SetBorder(btnAction,
    0,fx.colors.transparent,0)
        TableViewSetColumnGraphic(idxPos,btnAction)
    End Sub

    private Sub TableViewSetColumnGraphic(Index As Int, xNode As Node)
        
    Dim jo As JavaObject = TV
        
    Dim column As JavaObject = jo.RunMethodJO("getColumns"Null).RunMethod("get"Array(Index))
        column.RunMethod(
    "setGraphic"Array(xNode))
    End Sub
     
    Last edited: Feb 28, 2018
    amaxco and joulongleu like this.
  5. Mashiane

    Mashiane Expert Licensed User

    Code:
    'Description:get the table structure from the pragma statement
    tag: table structure for CreateTable
    Sub GetTableStructure(jSQL As SQL, tblName As StringAs Map
        
    Dim fld As Map
        fld.Initialize
        
    Dim fields As List = DBUtils.ExecuteMaps(jSQL,"PRAGMA table_info ('" & tblName & "')",Null)
        
    For Each fldm As Map In fields
            
    Dim fldname As String = fldm.GetDefault("name","")
            
    Dim fldtype As String = fldm.GetDefault("type","")
            fld.put(fldname,fldtype)
        
    Next
        
    Return fld
    End Sub

    'Description: get the table primary key field name from the pragma statement
    'tag: table, primary key
    Sub GetTablePrimaryKey(jSQL As SQL, tblName As StringAs String
        
    Dim fields As List = DBUtils.ExecuteMaps(jSQL,"PRAGMA table_info ('" & tblName & "')",Null)
        
    For Each fldm As Map In fields
            
    Dim fldname As String = fldm.GetDefault("name","")
            
    Dim fldpk As String = fldm.GetDefault("pk","")
            
    If fldpk = "1" Then Return fldname
        
    Next
        
    Return ""
    End Sub
    pragma.png
     
  6. Mashiane

    Mashiane Expert Licensed User

    Code:
    'Description: Quickly clone a table with its data
    'Tag: clone table
    Sub CloneTable(jSQL As SQL, prevTable As String, newTable As StringAs Boolean
        
    If jSQL.IsInitialized = False Then Return False
        jSQL.ExecNonQuery(
    "PRAGMA foreign_keys=off;")
        
    Try
            jSQL.BeginTransaction
            jSQL.ExecNonQuery(
    $"CREATE TABLE [${newTable}] AS SELECT * FROM [${prevTable}];"$)      
            jSQL.TransactionSuccessful
        
    Catch
            jSQL.Rollback
            
    Log("CloneTable: " & LastException)
        
    End Try
        jSQL.ExecNonQuery(
    "PRAGMA foreign_keys=on;")
        
    'check if table exists
        Return TableExists(jSQL,newTable)
    End Sub
    NB: Whilst this does the job, it DOES NOT keep the primary key definition on the new table
     
  7. Mashiane

    Mashiane Expert Licensed User

    Code:
    'Description: get indexes of the table using pragma for AddIndexes
    'Tag: indexes
    Sub GetTableIndexes(jSQL As SQL, tblName As StringAs Map
        
    Dim fld As Map
        fld.initialize
        
    Dim fields As List = ExecuteMaps(jSQL, $"PRAGMA INDEX_LIST ('${tblName}')"$,Null)
        
    For Each fldm As Map In fields
            
    Dim fldname As String = fldm.GetDefault("name","")
            
    Dim unique As String = fldm.GetDefault("unique","")
            
    Dim bunique As Boolean = False
            
    If unique = "1" Then bunique = True
            
    'clean the name
            fldname = fldname.replace(tblName,"")
            fldname = fldname.replace(tblName.tolowercase,
    "")
            fldname = fldname.tolowercase
            fld.put(fldname,bunique)
        
    Next
        
    Return fld
    End Sub
    Code:
    'Description: Clone a table maintaining its primary key and field types [STRUCTURE ONLY]
    'Tag: clone a table, primary key
    Sub CloneTable(jSQL As SQL, prevTable As String, newTable As StringAs Boolean
        
    'get the structure of the table
        Dim flds As Map = GetTableStructure(jSQL,prevTable)
        
    'get the primary key
        Dim pkey As String = GetTablePrimaryKey(jSQL,prevTable)
        
    'get the indexes of the table
        Dim idxs As Map = GetTableIndexes(jSQL,prevTable)
       
        CreateTable(jSQL,newTable,flds,pkey,pkey)
        
    'add the indexes
        AddIndexes(jSQL,newTable,idxs)
        
    Return TableExists(jSQL,newTable)
    End Sub

    Sub AddIndexes(jSQL As SQL,TableName As String, FieldsAndTypes As Map)
        IsDone = 
    False
        jSQL.ExecNonQuery(
    "PRAGMA foreign_keys=off;")
        
    Try
            jSQL.begintransaction
            FieldsAndTypes = DeDuplicateMap(FieldsAndTypes)
            
    Dim i As Int
            
    For i = 0 To FieldsAndTypes.Size - 1
                
    Dim sb As StringBuilder
                
    Dim field As String
                
    Dim unique As Boolean
                field = FieldsAndTypes.GetKeyAt(i)
                unique = FieldsAndTypes.GetValueAt(i)
                
    If field.Length > 0 Then
                    sb.Initialize
                    sb.Append(
    "CREATE ")
                    
    If unique = True Then sb.Append("UNIQUE ")
                    sb.Append(
    "INDEX IF NOT EXISTS [").Append(TableName).Append(field).Append("]")
                    sb.Append(
    " ON [").Append(TableName).Append("] ([").Append(field).Append("])")
                    jSQL.ExecNonQuery(sb.tostring)
                
    End If
            
    Next
            jSQL.TransactionSuccessful
            IsDone = 
    True       
        
    Catch
            jSQL.Rollback
            
    Log("AddIndexes: " & LastException)
            IsDone = 
    False
        
    End Try
        jSQL.ExecNonQuery(
    "PRAGMA foreign_keys=on;")
    End Sub
     
  8. Mashiane

    Mashiane Expert Licensed User

    Based on the methods in post $47 and #46, the sub to remove a column can be..

    Code:
    'Description: Remove a column from sqlite table keeping the primary key and table structure
    'Tag: remove a column from a table
    Sub RemoveColumn(jsql As SQL, TableName As String, FldName As StringAs Boolean
        
    'get the structure of the table
        Dim flds As Map = GetTableStructure(jsql,TableName)
        
    'get the primary key
        Dim pkey As String = GetTablePrimaryKey(jsql,TableName)
        
    'get the indexes of the table
        Dim idxs As Map = GetTableIndexes(jsql,TableName)
          
        
    'remove the fldname to be deleted
        flds.Remove(FldName)
        flds.Remove(FldName.ToLowerCase)
        
    'remove the indexes
        idxs.Remove(FldName)
        idxs.Remove(FldName.ToLowerCase)
        
    'create a new table with the updated fields
        CreateTable(jsql,TableName & "_old",flds,pkey,pkey)
        
    'add the indexes
        AddIndexes(jsql,TableName & "_old",idxs)
        
    'get the new field names created
        Dim newFlds As StringBuilder
        newFlds.Initialize
        
    For Each strFld As String In flds.Keys
            newFlds.Append(
    "[").Append(strFld).Append("],")
        
    Next
        newFlds = jMash.RemDelimSB(
    ",",newFlds)
        
    'copy the data from original to old table
        jsql.ExecNonQuery("PRAGMA foreign_keys=off;")
        jsql.BeginTransaction
        
    Try
            
    'copy records to new table from original table
            jsql.ExecNonQuery("INSERT INTO [" & TableName & "_old] (" & newFlds & ") SELECT " & newFlds & " FROM [" & TableName & "]")
            
    ' drop the original table
            jsql.ExecNonQuery("DROP TABLE [" & TableName & "]")
            
    'rename the old to new table
            jsql.ExecNonQuery("ALTER TABLE [" & TableName & "_old] RENAME TO [" & TableName & "]")
            jsql.TransactionSuccessful
        
    Catch
            jsql.Rollback
            
    Log("RemoveColumn: " & LastException)
        
    End Try   
        jsql.ExecNonQuery(
    "PRAGMA foreign_keys=on;")
        
    'does the column exists
        Return Not(ColumnExists(jsql,TableName,FldName))
    End Sub
     
  9. Mashiane

    Mashiane Expert Licensed User

    'Description: Get a tables indexes and column names using PRAGMA statements
    'Tag: sqlite, table index, columns
    Code:
    'get indexes of the table
    Sub GetTableIndexesWithColumns(jSQL As SQL, tblName As StringAs Map
        
    Dim fld As Map
        fld.initialize
        
    Dim fields As List = ExecuteMaps(jSQL, $"PRAGMA INDEX_LIST ('${tblName}')"$,Null)
        
    For Each fldm As Map In fields
            
    Dim fldname As String = fldm.GetDefault("name","")
            
    Dim unique As String = fldm.GetDefault("unique","")
            
    Dim bunique As Boolean = False
            
    If unique = "1" Then bunique = True
            
    'go deeper and get the column names for this index
            Dim idxcols As List = ExecuteMaps(jSQL,$"PRAGMA index_info('${fldname}');"$,Null)
            
    Dim idxcolNames As List
            idxcolNames.Initialize
            
    For Each idxm As Map In idxcols
                
    Dim idxcolName As String = idxm.GetDefault("name","")
                idxcolNames.Add(idxcolName)
            
    Next
            
    Dim nidx As Map = CreateMap("unique":bunique,"columns":idxcolNames)
            fld.Put(fldname,nidx)
        
    Next
        
    Return fld
    End Sub
     
  10. icefairy333

    icefairy333 Active Member Licensed User

    Code:
    Public Sub genTypeByMap(m As Map,typName As String)
        
    If m=Null Or m.IsInitialized=False Then Return
        typName=typName&
    "Model"
        
    Dim sbType As StringBuilder
        sbType.Initialize
        sbType.Append(
    $"Type ${typName}("$)
        
    For Each k As String In m.Keys
            
    Dim obj As Object=m.Get(k)
            
    If obj<>Null Then
                
    Dim objType As String=GetType(obj)
                objType=objType.SubString(objType.LastIndexOf(
    ".")+1).ToLowerCase
                
    Select objType
                    
    Case "integer"
                        sbType.Append(k).Append(
    " As Int,")
                    
    Case "double"
                        sbType.Append(k).Append(
    " As Double,")
                    
    Case "long"
                        sbType.Append(k).Append(
    " As Long,")
                    
    Case "boolean"
                        sbType.Append(k).Append(
    " As Boolean,")
                    
    Case "string"
                        sbType.Append(k).Append(
    " As String,")
                    
    Case Else
                        sbType.Append(k).Append(
    " As Object,")
                
    End Select
            
    Else
                sbType.Append(k).Append(
    " As Object,")
            
    End If
        
    Next
        sbType.Remove(sbType.Length-
    1,sbType.Length)
        sbType.Append(
    ")")
    End Sub
     
    Mashiane likes this.
  11. Mashiane

    Mashiane Expert Licensed User

    'Description: UpdateMaps is like InsertMaps in DButils but for running updates
    'you pass it a list of maps, each map has "qry" and an "args" where qry is the query to execute and args is a list of values
    'Tags: SQLite, UpdateMaps
    Code:
    Public Sub UpdateMaps(jSQL As SQL, TableName As String, ListOfMaps As ListAs Boolean
        
    'Small check for a common error where the same map is used in a loop
        If ListOfMaps.Size > 1 And ListOfMaps.Get(0) = ListOfMaps.Get(1Then
            
    Log("Same Map found twice in list. Each item in the list should include a different map object.")
            
    Return False
        
    End If
        jSQL.BeginTransaction
        
    Try
            
    For i1 = 0 To ListOfMaps.Size - 1
                
    Dim qrymap As Map = ListOfMaps.Get(i1)
                
    If qrymap.IsInitialized Then
                    
    Dim qry As String = qrymap.Get("qry")
                    
    Dim args As List = qrymap.Get("args")
                    jSQL.ExecNonQuery2(qry,  args)
                
    End If
            
    Next
            jSQL.TransactionSuccessful
            
    Return True
        
    Catch
            
    Log("UpdateMaps: " & LastException)
            jSQL.Rollback
            
    Return False
        
    End Try
    End Sub
     
  12. Mashiane

    Mashiane Expert Licensed User

    'Description: Useful with UpdateMaps to create a Map for each record to be updated.
    'Tags: SQLite, UpdateMaps, you can then add each map to the list to use with UpdateMaps
    'Usage: Dim upmap As Map = DBUtils.UpdateRecordMap("Fields",CreateMap("underscore":iunderscore),CreateMap("id":fid))

    Code:
    Public Sub UpdateRecordMap(TableName As String, Fields As Map, WhereFieldEquals As MapAs Map
        
    Dim out As Map
        
    If WhereFieldEquals.Size = 0 Then
            
    Log("WhereFieldEquals map empty!")
            
    Return out
        
    End If
        
    If Fields.Size = 0 Then
            
    Log("Fields empty")
            
    Return out
        
    End If
        Fields = DeDuplicateMap(Fields)
        WhereFieldEquals = DeDuplicateMap(WhereFieldEquals)
        
    Dim sb As StringBuilder
        
    Try
            sb.Initialize
            sb.Append(
    "UPDATE ").Append(EscapeField(TableName)).Append(" SET ")
            
    Dim args As List
            args.Initialize
            
    For i=0 To Fields.Size-1
                
    If i<>Fields.Size-1 Then
                    sb.Append(EscapeField(Fields.GetKeyAt(i))).Append(
    "=?,")
                
    Else
                    sb.Append(EscapeField(Fields.GetKeyAt(i))).Append(
    "=?")
                
    End If
                args.Add(Fields.GetValueAt(i))
            
    Next
        
            sb.Append(
    " WHERE ")
            
    For i = 0 To WhereFieldEquals.Size - 1
                
    If i > 0 Then
                    sb.Append(
    " AND ")
                
    End If
                
    Dim skey As String = WhereFieldEquals.GetKeyAt(i)
                skey = 
    $"lower(${skey})"$
                sb.Append(skey).Append(
    " = ?")
                
    Dim svalue As String = WhereFieldEquals.GetValueAt(i)
                svalue = svalue.ToLowerCase
                args.Add(svalue)
            
    Next
            out = CreateMap(
    "qry":sb.tostring,"args":args)
            
    Return out
        
    Catch
            
    Return out
        
    End Try
    End Sub
     
  13. Mashiane

    Mashiane Expert Licensed User

    'Description: Get time lapsed from a DateTime String
    'tags: TimeAgo
    'usage: dim ta as string = TimeAgo("2018-04-25 14:00:00")
    Code:
    Sub TimeAgo(dt As StringAs String
        
    If dt.IndexOf(" ") = -1 Then Return ""
        
    Dim diff As Period
        diff.Initialize
        
    DateTime.DateFormat="yyyy-MM-dd"
        
    DateTime.TimeFormat="HH:mm:ss"
        
    Dim mtime() As String = Regex.split(" ", dt)
        
    Dim dd As String = mtime(0)
        
    Dim tt As String = mtime(1)
        
    Dim pd As Long = DateTime.DateTimeParse(dd,tt)
        diff = DateUtils.PeriodBetween(pd,
    DateTime.now)
        
    Dim ta As String
        
    If diff.Years <> 0 Then
            ta = 
    $"${diff.years} year(s) ago"$
            
    Return ta
        
    End If
        
    If diff.Months <> 0 Then
            ta = 
    $"${diff.months} month(s) ago"$
            
    Return ta
        
    End If
        
    If diff.Days <> 0 Then
            ta = 
    $"${diff.days} day(s) ago"$
            
    Return ta
        
    End If
        
    If diff.Hours <> 0 Then
            ta = 
    $"${diff.hours} hour(s) ago"$
            
    Return ta
        
    End If
        
    If diff.Minutes <> 0 Then
            ta = 
    $"${diff.minutes} minute(s) ago"$
            
    Return ta
        
    End If
        
    If diff.Seconds <> 0 Then
            ta = 
    $"${diff.seconds} second(s) ago"$
            
    Return ta
        
    End If
    End Sub
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice