Re: [racket-users] Capturar valor de retorno da query Racket

2018-09-28 Thread dev . videira
Olá, Funcionou 100%, do jeito que esperava.

Muito obrigado.

Em quinta-feira, 27 de setembro de 2018 17:07:13 UTC-3, Vincent St-Amour 
escreveu:
>
> Olá, 
>
> Se eu entendi sua pergunta, a função `dict-ref` é o que você quer: 
>
> (dict-ref (query ..) 'insert-id) 
>
> Vincent 
>
>
>
> On Thu, 27 Sep 2018 14:24:39 -0500, 
> dev.v...@gmail.com  wrote: 
> > 
> > Olá, sou iniciante no racket. Gostaria de capturar o valor insert-id que 
> retorna em um uma struct chama simple-result da query que executo. O 
> retorno é o seguinte: 
> > 
> > (simple-result '((insert-id . 30) (affected-rows . 1))) 
> > 
> > Gostaria de obter o valor 30 por exemplo. 
> > 
> > Código da execução da query: 
> > 
> > (define save_pergunta 
> >   (lambda (tf_pergunta) 
> >   (define result_save_pergunta (query conn "INSERT INTO perguntas VALUES 
> (null, $pergunta)" tf_pergunta)) 
> >   (print result_save_pergunta) 
> >   (printf "\nPergunta Cadastrada!\n"))) 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups "Racket Users" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to racket-users...@googlegroups.com . 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Capturar valor de retorno da query Racket

2018-09-27 Thread David Storrs
Some other thoughts for you, in no particular order:

* I encourage you to always specify your field names in a SQL query, as it
is both more readable and it will future-proof you against changes in the
schema.  So:

Use this:  INSERT INTO questions (description, qtext, author) VALUES
(...data...);
Not this:   INSERT INTO questions VALUES (...data...);

* With Racket, look at query-rows and related functions.  They can be a lot
easier than query.

* If you happen to be using PostgreSQL there is a very convenient extension
to the SQL standard: RETURNING.
https://www.postgresql.org/docs/current/static/dml-returning.html

(query-value db "INSERT INTO questions (descriptions, qtext, author)
VALUES ($1, $2, $3) RETURNING id" descript question-text author)  ; This
will return the id of the row that it just inserted

* You might want to take a look at the
query-rows-as-dicts family of functions in handy/db  

;  This query is being sent against a PG database
(query-rows-as-dicts
   '(first-name last-name titles)
   db
  "SELECT first_name, last_name, ARRAY_AGG(title) FROM users u LEFT JOIN
titles t on u.id = t.user_id GROUP BY first_name, last_name"
  #:transform-data (make-transform-data-func sql-null? "unknown" pg-array?
pg-array->list string? string-trim)
  #:transform-dict (lambda (d) (hash-set d 'titles (map string-downcase
(hash-ref d 'titles)

This returns a list of hashes that might look like so:
(list
  (hash 'first-name "bob" 'last-name "smith" 'titles '("ceo" "founder")) ;
in the database it's CEO, but we lowercased it
  (hash 'first-name "alice" 'last-name "unknown" 'titles '("team leader"))
; in the database her last_name field is NULL
  (hash 'first-name "joe" 'last-name "smith" 'titles ()) ; in the database
his last name is "  smith" with leading whitespace
)

The regular db module would have returned the 'title' field as a pg-array
struct which you would then have had to convert via pg-array->list, but
here the query does it for you.


On Thu, Sep 27, 2018 at 4:14 PM George Neuner  wrote:

>
>
> On 9/27/2018 4:07 PM, Vincent St-Amour wrote:
> > Olá,
> >
> > Se eu entendi sua pergunta, a função `dict-ref` é o que você quer:
> >
> >  (dict-ref (query ..) 'insert-id)
> >
> > Vincent
>
> Yes, that will work on the alist.  But first you have to get the alist
> out of the simple-result struct.
>
> George
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Capturar valor de retorno da query Racket

2018-09-27 Thread George Neuner




On 9/27/2018 4:07 PM, Vincent St-Amour wrote:

Olá,

Se eu entendi sua pergunta, a função `dict-ref` é o que você quer:

 (dict-ref (query ..) 'insert-id)

Vincent


Yes, that will work on the alist.  But first you have to get the alist 
out of the simple-result struct.


George

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Capturar valor de retorno da query Racket

2018-09-27 Thread George Neuner

Hi,

On 9/27/2018 3:24 PM, dev.vide...@gmail.com wrote:
Olá, sou iniciante no racket. Gostaria de capturar o valor 
*insert-id* que retorna em um uma struct chama simple-result 
 da 
query 
 que 
executo. O retorno é o seguinte:


|(simple-result '((insert-id . 30) (affected-rows . 1))) |

Gostaria de obter o valor *30* por exemplo.
Código da execução da query:

|(define save_pergunta (lambda (tf_pergunta) (define 
result_save_pergunta (query conn "INSERT INTO perguntas VALUES (null, 
$pergunta)" tf_pergunta)) (print result_save_pergunta) (printf 
"\nPergunta Cadastrada!\n")))|


*simple-result* is a struct - you need to extract what you want from the 
association list in the info field.


The function to extract a field from a struct is [usually] the 
concatenation of the field name with the struct name: so the *info* 
field in *simple-result* would be  (*simple-result-info* ).


You find an entry in an association list with *assoc* [or *assv* or 
*assq* depending on the type of the key].  The entry itself is a cons 
pair, so you need to extract the 2nd value in a pair with *cdr*.


So to get the identifier you need to do something like the following:

  (require db)
   :
  (let (
        (result (query conn "insert into questions values 
(null,$question)" tf_question))

       )
    (cdr (assoc 'insert-id (simple-result-info result
   :

Note the above is not checking for errors.  You should always check that 
the result really is a simple-result before trying to extract the info 
because query can return other kinds of results (including no results 
... query can throw an exception if it fails).


Hope this helps,
George


--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Capturar valor de retorno da query Racket

2018-09-27 Thread Vincent St-Amour
Olá,

Se eu entendi sua pergunta, a função `dict-ref` é o que você quer:

(dict-ref (query ..) 'insert-id)

Vincent



On Thu, 27 Sep 2018 14:24:39 -0500,
dev.vide...@gmail.com wrote:
> 
> Olá, sou iniciante no racket. Gostaria de capturar o valor insert-id que 
> retorna em um uma struct chama simple-result da query que executo. O retorno 
> é o seguinte:
> 
> (simple-result '((insert-id . 30) (affected-rows . 1)))
> 
> Gostaria de obter o valor 30 por exemplo.
> 
> Código da execução da query:
> 
> (define save_pergunta 
>   (lambda (tf_pergunta)
>   (define result_save_pergunta (query conn "INSERT INTO perguntas VALUES 
> (null, $pergunta)" tf_pergunta))
>   (print result_save_pergunta)
>   (printf "\nPergunta Cadastrada!\n")))
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Capturar valor de retorno da query Racket

2018-09-27 Thread dev . videira


Olá, sou iniciante no racket. Gostaria de capturar o valor *insert-id* que 
retorna em um uma struct chama simple-result 

 da query 

 que 
executo. O retorno é o seguinte:


(simple-result '((insert-id . 30) (affected-rows . 1)))


Gostaria de obter o valor *30* por exemplo.

Código da execução da query:


(define save_pergunta 
  (lambda (tf_pergunta)
  (define result_save_pergunta (query conn "INSERT INTO perguntas VALUES (null, 
$pergunta)" tf_pergunta))
  (print result_save_pergunta)
  (printf "\nPergunta Cadastrada!\n")))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.