AI (claude.ai in my case) seems pretty good in generating SQL statements. Copy/paste into the Xojo IDE is helped with a way to convert the pasted SQL into a string. An example query generated by AI and pasted into the IDE looks like this:
Select
p.category,
COUNT(s.sale_id) As total_sales,
Round(AVG(s.amount), 2) As avg_sale_amount,
SUM(s.amount) As revenue,
SUM(s.amount) / COUNT(DISTINCT s.customer_id) As revenue_per_customer
FROM
sales s
Join products p ON s.product_id = p.product_id
WHERE
s.sale_date >= date('now', '-30 days')
GROUP BY
p.category
HAVING
COUNT(s.sale_id) > 10
ORDER BY
revenue DESC
LIMIT 5;
After selecting the pasted sql and running the “stringify” script, it looks like this:
"Select " + _
"p.category, " + _
"COUNT(s.sale_id) As total_sales, " + _
"Round(AVG(s.amount), 2) As avg_sale_amount, " + _
"SUM(s.amount) As revenue, " + _
"SUM(s.amount) / COUNT(DISTINCT s.customer_id) As revenue_per_customer " + _
"FROM " + _
"sales s " + _
"Join products p ON s.product_id = p.product_id " + _
"WHERE " + _
"s.sale_date >= date('now', '-30 days') " + _
"GROUP BY " + _
"p.category " + _
"HAVING " + _
"COUNT(s.sale_id) > 10 " + _
"ORDER BY " + _
"revenue DESC " + _
"LIMIT 5;"
Then you only need to define a string var to hold the string:
Var sql As String
sql = "Select " + _
"p.category, " + _
"COUNT(s.sale_id) As total_sales, " + _
"Round(AVG(s.amount), 2) As avg_sale_amount, " + _
"SUM(s.amount) As revenue, " + _
"SUM(s.amount) / COUNT(DISTINCT s.customer_id) As revenue_per_customer " + _
"FROM " + _
"sales s " + _
"Join products p ON s.product_id = p.product_id " + _
"WHERE " + _
"s.sale_date >= date('now', '-30 days') " + _
"GROUP BY " + _
"p.category " + _
"HAVING " + _
"COUNT(s.sale_id) > 10 " + _
"ORDER BY " + _
"revenue DESC " + _
"LIMIT 5;"
I thought I had seen a script to do this before but I could not find it with search, so I wrote my own (stringigy.xojo_script):
Var Text2Stringify As String
Text2Stringify = SelText
Var lines() As String = Text2Stringify.Split(EndOfLine)
Var modifiedText As String = ""
For i As Integer = 0 To lines.LastIndex
Var line As String = lines(i)
Var modifiedLine As String
' Add double quote at the beginning
modifiedLine = """" + line.Trim
If i < lines.LastIndex Then
' For all lines except the last one
If Right(line.Trim, 1) = " " Then
modifiedLine = modifiedLine + """ + _"
Else
modifiedLine = modifiedLine + " "" + _"
End If
modifiedLine = modifiedLine + EndOfLine
Else
' For the last line
modifiedLine = modifiedLine + """"
End If
' Add the modified line to the result
modifiedText = modifiedText + modifiedLine
Next
' Replace the selected text with the modified text
SelText = modifiedText
4 posts - 3 participants