[REBOL] Re: [COMMAND] Database null values
From: g:santilli:tiscalinet:it at: 26-Jan-2003 19:32
Hi Davide,
On Sunday, January 26, 2003, 4:59:02 AM, you wrote:
DG> Could be the following a good approach ?
This is what I use with MySQL:
form-sql: func [
"Forms a value into SQL syntax"
value
/local result chars normal-chars
] [
if none? :value [return "NULL"]
if :value = '__last-id [return "LAST_INSERT_ID()"]
if date? :value [
return rejoin compose [
"'" (value/year) "-" (value/month) "-" (value/day)
(either value/time [reduce [" " value/time/hour ":" value/time/minute ":" value/time/second]]
[])
"'"
]
]
if time? :value [
return rejoin ["'" value/hour ":" value/minute ":" value/second "'"]
]
if number? :value [return form value]
if block? :value [
if empty? value [return "(NULL)"] ; SQL's quite silly... this is a workaround
result: make string! 256
insert tail result "("
foreach element value [
insert insert tail result form-sql :element ","
]
return head change back tail result ")"
]
if word? :value [return form value]
if not any-string? :value [value: mold :value]
result: make string! 256
insert tail result "'"
normal-chars: complement charset "^(00)^/^-^M^(08)'\"
parse/all value [
any [
#"^(00)" (insert tail result "\0")
| #"^/" (insert tail result "\n")
| #"^-" (insert tail result "\t")
| #"^M" (insert tail result "\r")
| #"^(08)" (insert tail result "\b")
| #"'" (insert tail result "\'")
| #"\" (insert tail result "\\")
| copy chars some normal-chars (insert tail result chars)
]
]
head insert tail result "'"
]
I think Nenad has an improved version in his mysql-protocol,
called to-sql.
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r