Can this be made to execute faster?
Posted: Fri Mar 18, 2022 9:04 pm
Hi
I would like some suggestions for making this code faster. I need to create a JSON array of records from SQLite tables. However, I cannot use PB JSON functions because they do not handle doubles correctly (for example 2.00 becomes 2 in the JSON which then gets parsed out as an integer) and I need to strip out any commas at the end of text fields. So I created the following code which works but I wonder if it could be made much faster.
I would like some suggestions for making this code faster. I need to create a JSON array of records from SQLite tables. However, I cannot use PB JSON functions because they do not handle doubles correctly (for example 2.00 becomes 2 in the JSON which then gets parsed out as an integer) and I need to strip out any commas at the end of text fields. So I created the following code which works but I wonder if it could be made much faster.
Code: Select all
Procedure DoSelect(lcSQL.s)
Define lcComma1.s,lcComma2.s,lcJSON.s,ln.i,lcTxBuf.s,lnC.i,lnFnd.i,lnETX.i,lcVal.s,lnO.i,lndBHnd.i
lndBHnd = OpenDatabase(#PB_Any,#dCDataBaseName,"","",#PB_Database_SQLite)
If lndBHnd
If DatabaseQuery(lndBHnd,lcSQL,#PB_Database_StaticCursor)
lcTxBuf = ""
lcComma1=""
CreateJSON(0)
lnO = SetJSONObject(JSONValue(0))
While NextDatabaseRow(lndBHnd)
lcJSON = lcComma1 +"{"
lcComma2 = ""
For ln = 0 To DatabaseColumns(lndBHnd) - 1
Select DatabaseColumnType(lndBHnd,ln)
Case #PB_Database_String, 0 ; SQLite returns 0 for any column not based on a schema and treats it like text.
SetJSONString(AddJSONMember(lnO,DatabaseColumnName(lndBHnd,ln)),RTrim(GetDatabaseString(lndBHnd,ln),","))
Case #PB_Database_Long
lcJSON + lcComma2 + #DQUOTE$ + DatabaseColumnName(lndBHnd,ln) + #DQUOTE$+":"+GetDatabaseString(lndBHnd,ln)
lcComma2 = ","
Case #PB_Database_Double
lcJSON + lcComma2 + #DQUOTE$ + DatabaseColumnName(lndBHnd,ln) + #DQUOTE$+":"+GetDatabaseString(lndBHnd,ln)
lcComma2 = ","
Case #PB_Database_Quad
lcJSON + lcComma2 + #DQUOTE$ + DatabaseColumnName(lndBHnd,ln) + #DQUOTE$+":"+GetDatabaseString(lndBHnd,ln)
lcComma2 = ","
Case #PB_Database_Float
lcJSON + lcComma2 + #DQUOTE$ + DatabaseColumnName(lndBHnd,ln) + #DQUOTE$+":"+GetDatabaseString(lndBHnd,ln)
lcComma2 = ","
Default
lcJSON + lcComma2 + #DQUOTE$ + DatabaseColumnName(lndBHnd,ln) + #DQUOTE$+":"+#DQUOTE$+#DQUOTE$
lcComma2 = ","
EndSelect
Next
If Len(lcJSON) <= 2
lcTxBuf + lcJSON + LTrim(ComposeJSON(0),"{")
Else
lcTxBuf + lcJSON + RTrim(","+LTrim(ComposeJSON(0),"{"),",")
EndIf
lcComma1 = ","
Wend
FreeJSON(0)
FinishDatabaseQuery(lndBHnd)
ProcedureReturn lcTxBuf
EndIf
EndIf