Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

134
LINES

< > BotCompany Repo | #1000648 // Find field names in MySQL "create table"

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (3878L/26K/71K).

!636
!sf
!multi-line strings
!1000635 // stringfunc {
!723 // S, O etc.
!1000636 // compare strings with ==
!1000482 // quick cast
!JavaTok
!class _javax 19

interface StringFunc {
  S get(S s);
}

main {
  static O in;
  
  psvm {
    if (in == null) // may have been set by hotwire client
      in = [[CREATE TABLE `demotable2` (
  `dt2_a` int(11) NOT NULL,
  `dt2_b` date NOT NULL);]];
    
    if (args.length != 0) in = loadSnippet(args[0]);
    
    S s = [[
      example output: "a, b"
      mysqltok input
      find backquoted
      drop first
      fix duplicates
      fix output
    ]];
    L<S> ll = toLinesFullTrim(s);
    //System.out.println(ll);
    
    ll = map(ll, stringfunc { s.replaceAll("//.*$", "").trim() });
    //System.out.println(ll);
    O expectedOut = null;

    for (S c : ll) {
      if (c.startsWith("example output:"))
        expectedOut = unquote(c.substring("example output:".length()).trim());
      else if (c == "mysqltok input")
        in = mysqlTok((String) in);
      else if (c == "find backquoted") {
        L<S> inp = cast in;
        new L<S> l;
        for (int i = 1; i < inp.size(); i += 2)
          if (inp.get(i).startsWith("`"))
            l.add(mysqlUnquote(inp.get(i)));
        in = l;
        //System.out.println("Result: " + in);
      } else if (c == "fix output") {
        if (in instanceof List && expectedOut instanceof String)
          in = join(", ", in);
      } else if (c == "drop first") {
        if (in instanceof List)
          in = ((List) in).subList(1, ((List) in).size());
      } else if (c == "fix duplicates") {
        new L<S> list;
        new (Tree)Set<S> set;
        L<S> l = cast in;
        for (int i = 0; i < l.size(); i++)
          if (!set.contains(l.get(i))) {
            set.add(l.get(i));
            list.add(l.get(i));
          }
        in = list;
      }
    }
    
    if (expectedOut != null)
      System.out.println("Expected: " + expectedOut);
    System.out.println("Got: " + in);
    if (expectedOut != null && expectedOut.equals(in))
      print "OK!"
  }
  
  static S join(S glue, O o) {
    return join(glue, (L<S>) o);
  }
  
  !include #2000507 // original join function
  
  // returns a list of the matched tokens
  static L<S> matchTokensList(L<S> inp, L<S> pat) {
    new L<S> list;
    for (int i = 1; i+pat.size()-3 < inp.size(); i += 2) {
      S result = matchTokens_step(inp, pat, i);
      if (result != null)
        list.add(result);
    }
    return list;
  }
  
  // returns
  // -token matched for *
  // -"" on match without *
  // -null on non-match
  static String matchTokens(L<S> inp, L<S> pat) {
    for (int i = 1; i+pat.size()-3 < inp.size(); i += 2) {
      S result = matchTokens_step(inp, pat, i);
      if (result != null)
        return result;
    }
    return null;
  }
  
  static String matchTokens_step(L<S> inp, L<S> pat, int i) {
    S result = "";
    for (int j = 1; j < pat.size(); j += 2)
      if (pat.get(j).equals("*"))
        result = inp.get(i+j-1);
      else {
        String found = inp.get(i + j - 1);
        String expected = pat.get(j);
        if (!found.equals(expected))
          return null;
      }
    return result;
  }
  
  static L<S> map(L<S> l, StringFunc f) {
    new L<S> l2;
    for (S s : l)
      l2.add(f.get(s));
    return l2;
  }
  
  static S mysqlUnquote(S s) {
    return unquote("\"" + s.substring(1, s.length()-1) + "\"");
  }
}

Author comment

Began life as a copy of #1000634

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000648
Snippet name: Find field names in MySQL "create table"
Eternal ID of this version: #1000648/1
Text MD5: 6e854ff070dd1489e468a999bc2011af
Transpilation MD5: 51b2f0bd838eae270eff6b8aa2713620
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-21 01:05:19
Source code size: 3564 bytes / 134 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 789 / 1338
Referenced in: #1000660 - MySQL dump importer
#3000190 - Answer for stefanreich(>> t 20 questions)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)
#3000384 - Answer for 6uru0fsh1va (>> t-sql?)