mongo to psudo mysql
by - Thursday, January 1, 1970 at 12:00 AM
this is fucking cancer but heres a script I made to convert mongo json queries to mysql, any advice?

import sys
import json
import getopt;
import math;

file = "";
tablename = "";
ignore = [];
options, args = getopt.getopt(sys.argv[1:], "i:t:", ["ignore=", "tblname="])

if not sys.stdin.isatty():
    file = sys.stdin

else:
    file = open(str(args[1]), 'r');

for name, value in options:
    if name in ['-t', '--table']:   tablename = value;
    if name in ['-i', '--ignore']:  ignore = value.split(',');

print("#INFO T: " + tablename, file=sys.stderr);
print("#INFO I: " + str(ignore), file=sys.stderr);

line = file.readline()
jdata = json.loads(line);
attrbs = list(jdata.keys());
print("CREATE TABLE `" + tablename + "` (", end="");
for key in [x for x in list(jdata.keys()) if x not in ignore]:
    print("`" + key + "` ", end="");

    pend = "" if list(jdata.keys())[len(jdata)-1] is key else ", ";

    if      isinstance(jdata[key],int):    print("int",      end=pend);
    elif    isinstance(jdata[key],float):  print("float",    end=pend);
    elif    isinstance(jdata[key],bool):   print("boolean",  end=pend);
    else:                                  print("tinytext", end=pend);

print("); LOCK TABLE `" + tablename + "` WRITE;");

line = file.readline()
while line :
    try:
        jdata = json.loads(line);
        jkeys = [x for x in list(jdata.keys()) if x not in ignore];

        for diff in [x for x in jkeys if x not in attrbs]:
            diftype = "";

            if      isinstance(jdata[diff],int):    diftype = "int";
            elif    isinstance(jdata[diff],float):  diftype = "float";
            elif    isinstance(jdata[diff],bool):   diftype = "boolean";
            else:                                   diftype = "tinytext";

            print("ALTER TABLE `" + tablename + "` ADD `" + diff + "` " + diftype + ";");

            attrbs.append(diff);

        print("INSERT INTO `" + tablename + "` (", end="");
        for key in jkeys:
            value   = jdata[key];
            while isinstance(value, dict):
                jstr = str(value).replace('\'', '\"');
                jtmp = json.loads(jstr);
                jkey = list(jtmp)[0];
                value = jtmp[jkey];

            if value in ("", [], None, math.nan, "null", "NULL", "Null"):   continue;

            print("`" + key + "`", end="");
            pend = "" if jkeys[len(jkeys)-1] is key else ", ";
            print(pend, end="");

        print(") VALUES (", end='');
        for key in jkeys:
            value   = jdata[key];
            while isinstance(value, dict):
                jstr = str(value).replace('\'', '\"');
                jtmp = json.loads(jstr);
                jkey = list(jtmp)[0];
                value = jtmp[jkey];

            if value in ("", [], None, math.nan, "null", "NULL", "Null"):   continue;

            pend    = "" if jkeys[len(jkeys)-1] is key else ", ";

            if   isinstance(value,str):                                     print('"' + str(value).replace('"', '\\"') + '"', end=pend);
            elif isinstance(value,int):                                     print(str(value).replace('nan', 'null').replace('NaN', 'null').replace('NAN', 'null'), end=pend);
            else:                                                           print(str(value).replace('"', '\\"').replace('nan', 'null').replace('NaN', 'null').replace('NAN', 'null'), end=pend);

        print(");");

    except Exception as e:
        print("#ERROR on: " + line + " | " + str(e), file=sys.stderr);

    line = file.readline()

print("UNLOCK TABLES;");
file.close()

please no bully
Reply
How to do it /
Reply
cool,cool,cool
Reply


 Users viewing this thread: mongo to psudo mysql: No users currently viewing.