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

114
LINES

< > BotCompany Repo | #1000660 // MySQL dump importer

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

Libraryless. Click here for Pure Java version (4181L/29K/76K).

// todo: hotwire programs only once

!636
!sf
!L<S>
!cast
!class _javax 19
!JavaTok

main {
  static new Map<S, List> tables;
  static new Map<S, List> tableFields;
  static long totalRows;
  static Object in;
  
  static Class x1000656;
  
  static Object releaseNote = new Object() { public void finalize() {
    System.err.println("Note: MySQL dump importer released.");
  }};
  
  psvm {
    if (in == null)
      in = loadSnippet("#3000013"); // mysql dump
      
    // load subprograms
    x1000656 = hotwire("#1000656");
      
    L<S> creates = find_create_statements((S) in);
    print(creates.size() + " creates");
    for (S create : creates) {
      String tableName = get_table_name_from_create_statement(create);
      String[] names = find_field_names(create).split(", ");
      tableFields.put(tableName, Arrays.asList(names));
    }
      
    L<S> inserts = find_insert_statements((S) in);
    print(inserts.size() + " inserts");
    for (S insert : inserts) {
      String table = get_table_name_from_insert_statement(insert);
      print("Getting values");
      List data = parse_mysql_values(insert);
      print("Got values (" + data.size() + " rows)");
      addData(table, data);
      totalRows += data.size();
    }
    
    print("Tables: " + structure(tables.keySet()));
    print("Fields: " + structure(tableFields));
    print("Total rows imported: " + totalRows);
  }
  
  static S get_table_name_from_create_statement(S create) {
    print(create);
    // Java code taken from ?
    O in = javaTok(create);
    in = ((List) in).get(7);
    return (S) in;
  }
  
  static S get_table_name_from_insert_statement(S insert) {
    // Java code taken from #1000665
    O in = javaTok(insert);
    in = ((List) in).get(7);
    return (S) in;
  }
  
  static S find_field_names(String create) {
    return (S) process("#1000648", create);
  }
  
  static L<S> find_create_statements(String mysqlDump) {
    //return (L<S>) process("#1000642", mysqlDump, "markedStrings");
    return find_statements(mysqlDump, "create");
  }
  
  static L<S> find_insert_statements(String mysqlDump) {
    //return (L<S>) process("#1000650", mysqlDump, "markedStrings");
    return find_statements(mysqlDump, "insert");
  }
  
  static L<S> find_statements(String mysqlDump, String cmd) {
    new L<S> l;
    L<S> tok = mysqlTok(mysqlDump);
    int c = 0;
    for (int i = 1; i < tok.size(); i += 2) {
      S t = tok.get(i);
      if (t.equalsIgnoreCase(cmd)) {
        print("create at " + i);
        c = i;
      }
      if (t.equals(";")) {
        //print("; at " + i);
        if (c != 0) {
          l.add(JavaTok.join(tok.subList(c, i+1)));
          c = 0;
        }
      }
    }
    return l;
  }
  
  static L parse_mysql_values(S insert) {
    // It prints all the data if we don't silence it...
    return (L) processSilent(x1000656, insert);
  }
  
  static void addData(S table, List data) {
    List t = tables.get(table);
    if (t == null)
      tables.put(table, t = new List);
    t.addAll(data);
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000660
Snippet name: MySQL dump importer
Eternal ID of this version: #1000660/1
Text MD5: e953e86e2dea571509c3639ccfebb4ef
Transpilation MD5: bc4ecea791522991e996e803048d7ddd
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-15 18:16:18
Source code size: 3147 bytes / 114 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 903 / 1782
Referenced in: [show references]