Re: Re: Re: bashSQL: SQL API for BaSH

From NAT, 6 Years ago, written in Bash, viewed 699 times. This paste is a reply to Re: Re: bashSQL: SQL API for BaSH from NAT - view diff
URL https://code.nat.moe/view/1fc0786e Embed
Download Paste or View Raw
  1. #!/bin/bash
  2. #
  3. # bashSQL: A BaSH SQL lib
  4. # usage:
  5. #
  6. # sql.conn -h <host> -P <port> -u <user> -p<pass>
  7. # create a new sql session. Return session ID.
  8. #
  9. # sql.disconn <session>
  10. # disconnect a sql session
  11. #
  12. # sql.query <session>
  13. # do SQL query. Read SQL from stdin.
  14. #
  15. # sample usage:
  16. #
  17. # % sql.conn localhost 3306 root toor
  18. # 1003
  19. # % echo 'show databases;' | sql.query 1003
  20. # information_schema
  21. # mysql
  22. # ...
  23. # % sql.disconn 1003
  24.  
  25. function sql.conn {
  26.         local id=$RANDOM
  27.         mkfifo /tmp/bashsql.$id
  28.         cat > /tmp/bashsql.$id &; echo $! > /tmp/bashsql.$id.pid
  29.         cat /tmp/bashsql.$id | mysql $* &
  30.         echo $id
  31. }
  32.  
  33. function sql.disconn {
  34.         [[ ! -e /tmp/bashsql.$1 ]] && return 1
  35.         echo 'quit' | sql.query $1
  36.         for tar in $(cat /tmp/bashsql.$1.pid)
  37.         do
  38.                 kill $tar 2> /dev/null > /dev/null
  39.         done
  40.         rm /tmp/bashsql.$1 /tmp/bashsql.$1.*
  41.         echo "killed $1"
  42. }
  43.  
  44. function sql.query {
  45.         [[ ! -e /tmp/bashsql.$1 ]] && return 1
  46.         cat > /tmp/bashsql.$1
  47. }

Reply to "Re: Re: Re: bashSQL: SQL API for BaSH"

Here you can reply to the paste above

captcha