Advertisement
rgimenes

Untitled

Oct 21st, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.34 KB | None | 0 0
  1. #!/bin/sh
  2. #apaga arquivos antigos.
  3. rm -rf *.elb*;
  4. mysqldump  --compatible=ansi --skip-comments --skip-extended-insert --compact  --hex-blob "$@" | sed -e "s/,0x\([0-9A-Z]*\),/,X'\L\1',/g" | sed -e "s/,0x\([0-9A-Z]*\),/,X'\L\1',/g" | \
  5.  
  6. awk '
  7. #inicio
  8. BEGIN {
  9.     FS=",$"
  10.     print "PRAGMA synchronous = OFF;"
  11.     print "PRAGMA journal_mode = MEMORY;"
  12.     print "BEGIN TRANSACTION;"
  13. }
  14.  
  15. # tratando trigger
  16. /^\/\*.*CREATE.*TRIGGER/ {
  17.     gsub( /^.*TRIGGER/, "CREATE TRIGGER" )
  18.     print
  19.     inTrigger = 1
  20.     next
  21. }
  22.  
  23. # The end of CREATE TRIGGER has a stray comment terminator
  24. /END \*\/;;/ { gsub( /\*\//, "" ); print; inTrigger = 0; next }
  25.  
  26. # matando trigger
  27. inTrigger != 0 { print; next }
  28.  
  29. # Skip other comments
  30. /^\/\*/ { next }
  31.  
  32. # arrumando os quotes
  33. /INSERT/ {
  34.     gsub( /\\\047/, "\047\047" )
  35.     gsub(/\\n/, "\n")
  36.     gsub(/\\r/, "\r")
  37.     gsub(/\\"/, "\"")
  38.     gsub(/\\\\/, "\\")
  39.     gsub(/\\\032/, "\032")
  40.     print
  41.     next
  42. }
  43.  
  44. # colocando o create table
  45. /^CREATE/ {
  46.     print
  47.     if ( match( $0, /\"[^\"]+/ ) ) tableName = substr( $0, RSTART+1, RLENGTH-1 )
  48. }
  49.  
  50. # Replace `FULLTEXT KEY` or any other `XXXXX KEY` except PRIMARY by `KEY`
  51. /^  [^"]+KEY/ && !/^  PRIMARY KEY/ { gsub( /.+KEY/, "  KEY" ) }
  52.  
  53. # pegando tamanho dos campos
  54. / KEY/ { gsub(/\([0-9]+\)/, "") }
  55.  
  56. # subistituindo as palavras e tirando os lixos
  57. /^  / && !/^(  KEY|\);)/ {
  58.     gsub( /AUTO_INCREMENT|auto_increment/, "" )
  59.     gsub( /NOT NULL DEFAULT \047\047/, "NOT NULL DEFAULT NULL" )
  60.     gsub( /(COMMENT|comment)/,"")
  61.     gsub( /\047\T ou E\047/, "" )
  62.     gsub( /(CHARACTER SET|character set) [^ ]+ /, "" )
  63.     gsub( /DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP|default current_timestamp on update current_timestamp/, "" )
  64.     gsub( /(COLLATE|collate) [^ ]+ /, "" )
  65.     gsub(/(ENUM|enum)[^)]+\)/, "text ")
  66.     gsub(/(SET|set)\([^)]+\)/, "text ")
  67.     gsub(/UNSIGNED|unsigned/, "")
  68.     if (prev) print prev ","
  69.     prev = $1
  70. }
  71.  
  72. # evitando keys duplicada
  73. /^(  KEY|\);)/ {
  74.     if (prev) print prev
  75.     prev=""
  76.     if ($0 == ");"){
  77.         print
  78.     } else {
  79.         if ( match( $0, /\"[^"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
  80.         if ( match( $0, /\([^()]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
  81.         key[tableName]=key[tableName] "CREATE INDEX \"" tableName "_" indexName "\" ON \"" tableName "\" (" indexKey ");\n"
  82.     }
  83. }
  84.  
  85. # imprimindo
  86. END {
  87.     for (table in key) printf key[table]
  88.     print "END TRANSACTION;"
  89. }
  90. '
  91.  
  92.  
  93. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement