cat ~/bin/mysql2json.sh #!/bin/bash sed -e 's/\t/\",\"/g' \ -e 's/^/\[\"/' \ -e 's/$/\"\],/' \ -e '1s/\(.*\)/\{\"fields\":\1\ \"data\":[/g' -e '$s/.$/\]\}/' \ | tr -d "\n"
mysql -e "SELECT * FROM `mytable`" | ./mysql2json.sh {"fields":["field_1","field_2","field_3"],"data":[["1","2","3"],["4","5","6"],["7","8","9"]]}
#!/bin/bash :;while [ $? -eq 0 ] do. nc -vlp 8880 -c '( r=read; e=echo; $rabc; z=$r; while [ ${#z} -gt 2 ] do. $rz; done; f=`$e $b|sed 's/[^a-z0-9_.-]//gi'`; h="HTTP/1.0"; o="$h 200 OK\r\n"; c="Content"; m="mysql -ulol -ptrololo" m2j="~/bin/mysql2json.sh" $e "$o$c-Type: text/json"; $e; if [[ ( -n "$f" ) && ( "$f" != "favicon.ico" ) ]] then. $e "+ Connect to [$f]">&2; db=${f%.*}; tb=${f#*.}; if [ "$tb" = "$db" ] then $e `$m ${db:-test} -e "show tables" | $m2j`; else $e `$m ${db:-test} -e "select * from $tb" | $m2j`; fi; else $e `$m -e "show databases" | $m2j`; fi )'; done
[ bash: ./dbjs.sh listening on [any] 8880 ...
// http://127.0.0.1:8880 - { fields: [ "Database" ], data: [ [ "information_schema" ], [ "test" ] ] }
// http://127.0.0.1:8880/test - test { fields: [ "Tables_in_test" ], data: [ ["prods"], ["shops"], ["sp"], ["stat"] ] }
// http://127.0.0.1:8880/test.shops - test.shops { "fields": [ "id", "name", "adress" ], "data": [ ["1","aaaaa",""], ["2","bbbbbbbbb",""], ["3","cccccccccccccccc","ccc"] ] }
Source: https://habr.com/ru/post/154191/
All Articles