copy.go (858B)
1 package pqsql 2 3 // StartsWithCopy reports if the SQL strings start with "copy", ignoring 4 // whitespace, comments, and casing. 5 func StartsWithCopy(query string) bool { 6 if len(query) < 4 { 7 return false 8 } 9 var linecmt, blockcmt bool 10 for i := 0; i < len(query); i++ { 11 c := query[i] 12 if linecmt { 13 linecmt = c != '\n' 14 continue 15 } 16 if blockcmt { 17 blockcmt = !(c == '/' && query[i-1] == '*') 18 continue 19 } 20 if c == '-' && len(query) > i+1 && query[i+1] == '-' { 21 linecmt = true 22 continue 23 } 24 if c == '/' && len(query) > i+1 && query[i+1] == '*' { 25 blockcmt = true 26 continue 27 } 28 if c == ' ' || c == '\t' || c == '\r' || c == '\n' { 29 continue 30 } 31 32 // First non-comment and non-whitespace. 33 return len(query) > i+3 && c|0x20 == 'c' && query[i+1]|0x20 == 'o' && 34 query[i+2]|0x20 == 'p' && query[i+3]|0x20 == 'y' 35 } 36 return false 37 }