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

91
LINES

< > BotCompany Repo | #1000650 // Find insert statements in MySQL dump

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

Libraryless. Click here for Pure Java version (3659L/25K/86K).

!7

sinterface StringFunc {
  S get(S s);
}

static O in;
static L<S> markedStrings;

p {
  if (in == null) // may have been set by hotwire client
    in = "create table `bla`(hello int);";
  
  if (args.length != 0) in = loadSnippet(args[0]);
  
  S s = [[
    mysqltok input
    find "insert" ... ";"
  ]];
  L<S> ll = toLinesFullTrim(s);
  //System.out.println(ll);
  
  // remove comments
  ll = map(ll, func(S s) -> S { s.replaceAll("//.*$", "").trim() });
  
  L<S> tok = null;
  
  for (S c : ll) {
    if (eq(c, "mysqltok input"))
      in = tok = mysqlTok((String) in);
    else if (c.startsWith("find")) {
      L<S> pat = javaTok(c);
      pat.remove(0); pat.remove(0);
      L<S> inp = cast in;
      //System.out.println("inp: " + inp);
      //System.out.println("pat: " + pat);
      in = matchTokensList(inp, pat);
      //System.out.println("Result: " + structure(in));
    }
  }
  
  if (in instanceof List) {
    List<int[]> list = cast in;
    markedStrings = getMarkedStrings(tok, list);
    markTokens(tok, list);
    in = join(tok);
  
    System.out.println("Markings found: " + size(list));
    for (int[] ij : list) {
      S bla = join(tok.subList(ij[0], ij[1]+1));
      System.out.println("Got: " + bla);
    }
  }
}

static List<String> getMarkedStrings(L<S> tok, L<int[]> markings) {
  new L<S> result;
  for (int i = 0; i < markings.size(); i++)
    result.add(join(tok.subList(markings.get(i)[0], markings.get(i)[1])));
  return result;
}

static int size(Object o) {
  return ((List) o).size();
}

static void markTokens(L<S> tok, L<int[]> marks) {
  System.out.println(tok.size());
  for (int i = 0; i < tok.size(); i++) {
    String t = tok.get(i);
    if (isStartMark(i, marks))
      t = "[[" + t;
    if (isEndMark(i, marks))
      t += "]]";
    tok.set(i, t);
  }
}

static boolean isStartMark(int i, L<int[]> marks) {
  for (int[] ij : marks)
    if (ij[0] == i)
      return true;
  return false;
}

static boolean isEndMark(int i, L<int[]> marks) {
  for (int[] ij : marks)
    if (ij[1] == i+1)
      return true;
  return false;
}

Author comment

Began life as a copy of #1000642

download  show line numbers  debug dex  old transpilations   

Relations

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: #1000650
Snippet name: Find insert statements in MySQL dump
Eternal ID of this version: #1000650/3
Text MD5: 574b4c0f573d4e849f23195702352cc2
Transpilation MD5: 17388beafe7eafadadfe59e77a4ea9a4
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-23 00:31:12
Source code size: 2166 bytes / 91 lines
Pitched / IR pitched: No / No
Views / Downloads: 843 / 1308
Version history: 2 change(s)
Referenced in: #1000660 - MySQL dump importer
#1000662 - Test "process" function
#3000189 - Answer for stefanreich(>> t bla)
#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?)