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

1  
// todo: hotwire programs only once
2  
3  
!636
4  
!sf
5  
!L<S>
6  
!cast
7  
!class _javax 19
8  
!JavaTok
9  
10  
main {
11  
  static new Map<S, List> tables;
12  
  static new Map<S, List> tableFields;
13  
  static long totalRows;
14  
  static Object in;
15  
  
16  
  static Class x1000656;
17  
  
18  
  static Object releaseNote = new Object() { public void finalize() {
19  
    System.err.println("Note: MySQL dump importer released.");
20  
  }};
21  
  
22  
  psvm {
23  
    if (in == null)
24  
      in = loadSnippet("#3000013"); // mysql dump
25  
      
26  
    // load subprograms
27  
    x1000656 = hotwire("#1000656");
28  
      
29  
    L<S> creates = find_create_statements((S) in);
30  
    print(creates.size() + " creates");
31  
    for (S create : creates) {
32  
      String tableName = get_table_name_from_create_statement(create);
33  
      String[] names = find_field_names(create).split(", ");
34  
      tableFields.put(tableName, Arrays.asList(names));
35  
    }
36  
      
37  
    L<S> inserts = find_insert_statements((S) in);
38  
    print(inserts.size() + " inserts");
39  
    for (S insert : inserts) {
40  
      String table = get_table_name_from_insert_statement(insert);
41  
      print("Getting values");
42  
      List data = parse_mysql_values(insert);
43  
      print("Got values (" + data.size() + " rows)");
44  
      addData(table, data);
45  
      totalRows += data.size();
46  
    }
47  
    
48  
    print("Tables: " + structure(tables.keySet()));
49  
    print("Fields: " + structure(tableFields));
50  
    print("Total rows imported: " + totalRows);
51  
  }
52  
  
53  
  static S get_table_name_from_create_statement(S create) {
54  
    print(create);
55  
    // Java code taken from ?
56  
    O in = javaTok(create);
57  
    in = ((List) in).get(7);
58  
    return (S) in;
59  
  }
60  
  
61  
  static S get_table_name_from_insert_statement(S insert) {
62  
    // Java code taken from #1000665
63  
    O in = javaTok(insert);
64  
    in = ((List) in).get(7);
65  
    return (S) in;
66  
  }
67  
  
68  
  static S find_field_names(String create) {
69  
    return (S) process("#1000648", create);
70  
  }
71  
  
72  
  static L<S> find_create_statements(String mysqlDump) {
73  
    //return (L<S>) process("#1000642", mysqlDump, "markedStrings");
74  
    return find_statements(mysqlDump, "create");
75  
  }
76  
  
77  
  static L<S> find_insert_statements(String mysqlDump) {
78  
    //return (L<S>) process("#1000650", mysqlDump, "markedStrings");
79  
    return find_statements(mysqlDump, "insert");
80  
  }
81  
  
82  
  static L<S> find_statements(String mysqlDump, String cmd) {
83  
    new L<S> l;
84  
    L<S> tok = mysqlTok(mysqlDump);
85  
    int c = 0;
86  
    for (int i = 1; i < tok.size(); i += 2) {
87  
      S t = tok.get(i);
88  
      if (t.equalsIgnoreCase(cmd)) {
89  
        print("create at " + i);
90  
        c = i;
91  
      }
92  
      if (t.equals(";")) {
93  
        //print("; at " + i);
94  
        if (c != 0) {
95  
          l.add(JavaTok.join(tok.subList(c, i+1)));
96  
          c = 0;
97  
        }
98  
      }
99  
    }
100  
    return l;
101  
  }
102  
  
103  
  static L parse_mysql_values(S insert) {
104  
    // It prints all the data if we don't silence it...
105  
    return (L) processSilent(x1000656, insert);
106  
  }
107  
  
108  
  static void addData(S table, List data) {
109  
    List t = tables.get(table);
110  
    if (t == null)
111  
      tables.put(table, t = new List);
112  
    t.addAll(data);
113  
  }
114  
}

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