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).

1  
!636
2  
!sf
3  
!multi-line strings
4  
!1000635 // stringfunc {
5  
!723 // S, O etc.
6  
!1000636 // compare strings with ==
7  
!1000482 // quick cast
8  
!JavaTok
9  
!class _javax 19
10  
11  
interface StringFunc {
12  
  S get(S s);
13  
}
14  
15  
main {
16  
  static O in;
17  
  
18  
  psvm {
19  
    if (in == null) // may have been set by hotwire client
20  
      in = [[CREATE TABLE `demotable2` (
21  
  `dt2_a` int(11) NOT NULL,
22  
  `dt2_b` date NOT NULL);]];
23  
    
24  
    if (args.length != 0) in = loadSnippet(args[0]);
25  
    
26  
    S s = [[
27  
      example output: "a, b"
28  
      mysqltok input
29  
      find backquoted
30  
      drop first
31  
      fix duplicates
32  
      fix output
33  
    ]];
34  
    L<S> ll = toLinesFullTrim(s);
35  
    //System.out.println(ll);
36  
    
37  
    ll = map(ll, stringfunc { s.replaceAll("//.*$", "").trim() });
38  
    //System.out.println(ll);
39  
    O expectedOut = null;
40  
41  
    for (S c : ll) {
42  
      if (c.startsWith("example output:"))
43  
        expectedOut = unquote(c.substring("example output:".length()).trim());
44  
      else if (c == "mysqltok input")
45  
        in = mysqlTok((String) in);
46  
      else if (c == "find backquoted") {
47  
        L<S> inp = cast in;
48  
        new L<S> l;
49  
        for (int i = 1; i < inp.size(); i += 2)
50  
          if (inp.get(i).startsWith("`"))
51  
            l.add(mysqlUnquote(inp.get(i)));
52  
        in = l;
53  
        //System.out.println("Result: " + in);
54  
      } else if (c == "fix output") {
55  
        if (in instanceof List && expectedOut instanceof String)
56  
          in = join(", ", in);
57  
      } else if (c == "drop first") {
58  
        if (in instanceof List)
59  
          in = ((List) in).subList(1, ((List) in).size());
60  
      } else if (c == "fix duplicates") {
61  
        new L<S> list;
62  
        new (Tree)Set<S> set;
63  
        L<S> l = cast in;
64  
        for (int i = 0; i < l.size(); i++)
65  
          if (!set.contains(l.get(i))) {
66  
            set.add(l.get(i));
67  
            list.add(l.get(i));
68  
          }
69  
        in = list;
70  
      }
71  
    }
72  
    
73  
    if (expectedOut != null)
74  
      System.out.println("Expected: " + expectedOut);
75  
    System.out.println("Got: " + in);
76  
    if (expectedOut != null && expectedOut.equals(in))
77  
      print "OK!"
78  
  }
79  
  
80  
  static S join(S glue, O o) {
81  
    return join(glue, (L<S>) o);
82  
  }
83  
  
84  
  !include #2000507 // original join function
85  
  
86  
  // returns a list of the matched tokens
87  
  static L<S> matchTokensList(L<S> inp, L<S> pat) {
88  
    new L<S> list;
89  
    for (int i = 1; i+pat.size()-3 < inp.size(); i += 2) {
90  
      S result = matchTokens_step(inp, pat, i);
91  
      if (result != null)
92  
        list.add(result);
93  
    }
94  
    return list;
95  
  }
96  
  
97  
  // returns
98  
  // -token matched for *
99  
  // -"" on match without *
100  
  // -null on non-match
101  
  static String matchTokens(L<S> inp, L<S> pat) {
102  
    for (int i = 1; i+pat.size()-3 < inp.size(); i += 2) {
103  
      S result = matchTokens_step(inp, pat, i);
104  
      if (result != null)
105  
        return result;
106  
    }
107  
    return null;
108  
  }
109  
  
110  
  static String matchTokens_step(L<S> inp, L<S> pat, int i) {
111  
    S result = "";
112  
    for (int j = 1; j < pat.size(); j += 2)
113  
      if (pat.get(j).equals("*"))
114  
        result = inp.get(i+j-1);
115  
      else {
116  
        String found = inp.get(i + j - 1);
117  
        String expected = pat.get(j);
118  
        if (!found.equals(expected))
119  
          return null;
120  
      }
121  
    return result;
122  
  }
123  
  
124  
  static L<S> map(L<S> l, StringFunc f) {
125  
    new L<S> l2;
126  
    for (S s : l)
127  
      l2.add(f.get(s));
128  
    return l2;
129  
  }
130  
  
131  
  static S mysqlUnquote(S s) {
132  
    return unquote("\"" + s.substring(1, s.length()-1) + "\"");
133  
  }
134  
}

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: 788 / 1338
Referenced in: [show references]