Example #1
0
File: main.cpp Project: riiko/meta
int main(int argc, char *argv[])
{
    QString title;
    title = title + "****************************************************************** \n";
    title = title + " * MySQLtoFile 1.0                                                * \n";
    title = title + " * This tool exports a table in a MySQL schema to various file    * \n";
    title = title + " * formats like STATA, SPSS, CSV (tab delimited), JSON and XML    * \n";
    title = title + " * This tool is part of CSPro Tools (c) ILRI, 2013                * \n";
    title = title + " ****************************************************************** \n";

    TCLAP::CmdLine cmd(title.toAscii().data(), ' ', "1.0 (Beta 1)");
    //Required arguments
    TCLAP::ValueArg<std::string> hostArg("H","host","MySQL host. Default localhost",false,"localhost","string");
    TCLAP::ValueArg<std::string> portArg("P","port","MySQL port. Default 3306.",false,"3306","string");
    TCLAP::ValueArg<std::string> userArg("u","user","User to connect to MySQL",true,"","string");
    TCLAP::ValueArg<std::string> passArg("p","password","Password to connect to MySQL",true,"","string");
    TCLAP::ValueArg<std::string> schemaArg("s","schema","Schema in MySQL",true,"","string");
    TCLAP::ValueArg<std::string> auditArg("a","audit","Target directory for the audit files. Default ./audit (created if not exists)",false,"./audit","string");


    TCLAP::SwitchArg createSwitch("c","create","Create META dictionary tables", cmd, false);
    TCLAP::SwitchArg loadSwitch("l","load","Load META dictionary tables", cmd, false);
    TCLAP::SwitchArg deleteSwitch("d","delete","Delete previous data before load", cmd, false);
    TCLAP::SwitchArg viewSwitch("v","views","Include views in dictionary tables", cmd, false);


    cmd.add(hostArg);
    cmd.add(portArg);
    cmd.add(userArg);
    cmd.add(passArg);
    cmd.add(schemaArg);
    cmd.add(auditArg);

    cmd.parse( argc, argv );

    //Getting the variables from the command

    bool createMETA;
    bool loadMETA;
    bool deldata;
    bool includeViews;
    createMETA = createSwitch.getValue();
    loadMETA = loadSwitch.getValue();
    deldata = deleteSwitch.getValue();
    includeViews = viewSwitch.getValue();

    QString host = QString::fromUtf8(hostArg.getValue().c_str());
    QString port = QString::fromUtf8(portArg.getValue().c_str());
    QString user = QString::fromUtf8(userArg.getValue().c_str());
    QString pass = QString::fromUtf8(passArg.getValue().c_str());
    QString schema = QString::fromUtf8(schemaArg.getValue().c_str());
    QString auditPath = QString::fromUtf8(auditArg.getValue().c_str());


    //Dictionary tables
    ignoreTables << "dict_grpinfo";
    ignoreTables << "dict_tblinfo";
    ignoreTables << "dict_clminfo";
    ignoreTables << "dict_relinfo";
    ignoreTables << "dict_childinfo";
    ignoreTables << "audit_log";


    //Authorisation tables Not used since 2015-03-19. Backward compatibility
    ignoreTables << "auth_group";
    ignoreTables << "auth_group_permissions";
    ignoreTables << "auth_permission";
    ignoreTables << "auth_user";
    ignoreTables << "auth_user_groups";
    ignoreTables << "auth_user_user_permissions";
    ignoreTables << "user_log";


    //Django tables Not used since 2014. Backward compatibility
    ignoreTables << "auth_message";
    ignoreTables << "django_admin_log";
    ignoreTables << "django_content_type";
    ignoreTables << "django_session";
    ignoreTables << "django_site";

    //I18N tables
    ignoreTables << "dict_iso639";
    ignoreTables << "dict_lkpiso639";
    ignoreTables << "dict_dctiso639";



    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName(host);
        db.setPort(port.toInt());
        db.setDatabaseName(schema);
        db.setUserName(user);
        db.setPassword(pass);
        bool METACreated;
        METACreated = false;
        bool METALoaded;
        METALoaded = false;
        bool deletePrevious;
        deletePrevious = false;
        QString data;
        if (db.open())
        {
            getTableOrder(db);
            if (createMETA)
            {
                log("Are you sure that you want to create META's tables? This will delete any previous META information INCLUDING the audit. (Y/N): ",false);
                QTextStream createIn(stdin);
                data = createIn.readLine();
                if (data.toUpper().simplified() == "Y")
                {
                    if (createMETATables(db) == 0)
                        METACreated = true;
                    else
                        return 1;
                }
            }
            if (loadMETA)
            {
                if (deldata && !createMETA)
                {
                    log("Are you sure that you want to delete any previous META's data? This WILL NOT delete the audit. (Y/N): ",false);
                    QTextStream deleteIn(stdin);
                    data = deleteIn.readLine();
                    if (data.toUpper().simplified() == "Y")
                    {
                        deletePrevious = true;
                    }
                }
                if (loadMETATables(db,deletePrevious,includeViews) == 0)
                    METALoaded = true;
                else
                    return 1;
            }
            createMETAAudit(db,auditPath);
            if (METACreated && !METALoaded)
            {
                log("META was created but not information was loaded. Use initmeta -l to load METAS's information");
            }
        }
        else
        {
            log("Cannot connect to database");
            log(db.lastError().databaseText());
            return 1;
        }
    }


    return 0;
}
Example #2
0
int main(int argc, char *argv[]) {
    QString title;
    title = title + " ****************************************************************** \n";
    title = title + " * odkdatatomysql 1.0                                             * \n";
    title = title + " * This tool converts ODK data into MySQL.                        * \n";
    title = title + " * The tool uses manifest xml file and ODK xml data file          * \n";
    title = title + " * Outputs data into a MySQL database and generates a log file    * \n";
    title = title + " * This tool is part of ODK Tools (c) ILRI, 2014                  * \n";
    title = title + " ****************************************************************** \n";

    TCLAP::CmdLine cmd(title.toLatin1().constData(), ' ', "1.0 (Beta 1)");

    //Required arguments
    TCLAP::ValueArg<std::string> mysqlHostArg("H","mysqlHost","MySQL server host",true,"","string");
    TCLAP::ValueArg<std::string> userNameArg("u","userName","MySQL server user name",true,"","string");
    TCLAP::ValueArg<std::string> passwordArg("p","password","MySQL server user password",true,"","string");
    TCLAP::ValueArg<std::string> schemaArg("s","schema","Schema name in MySQL host",true,"","string");
    TCLAP::ValueArg<std::string> manifestMainTableArg("t","manifestMainTable","Manifest main table within the schema",true,"","string");
    TCLAP::ValueArg<std::string> manifestMainVariableArg("v","manifestMainVariable","Manifest main variable in main table. Identifies unique record",true,"","string");
    TCLAP::ValueArg<std::string> manifestXMLArg("f","manifestXML","ODK manifest XML file",true,"","string");
    TCLAP::ValueArg<std::string> datafileXMLArg("d","datafileXML","ODK data XML file",true,"","string");
    TCLAP::ValueArg<std::string> datafileBaseNodeArg("b","datafileBaseNode","ODK data file XML base node. Node just after the <?xml version=1.0?>",true,"","string");
    TCLAP::ValueArg<std::string> sqlInsertsFileArg("q","sqlInsertsFile","SQL output file. Default ./sqlInsertFile.sql",true,"./sqlInsertFile.sql","string");
    TCLAP::ValueArg<std::string> logFileArg("l","logFile","Log file for errors. Default ./logfile.csv",true,"./logFile.csv","string");
    TCLAP::ValueArg<std::string> prefixArg("r","prefix","Prefix for each table. _ is added to the prefix. Default no prefix",false,"","string");

    cmd.add(mysqlHostArg);
    cmd.add(userNameArg);
    cmd.add(passwordArg);
    cmd.add(schemaArg);
    cmd.add(manifestMainTableArg);
    cmd.add(manifestMainVariableArg);
    cmd.add(manifestXMLArg);
    cmd.add(datafileXMLArg);
    cmd.add(datafileBaseNodeArg);
    cmd.add(sqlInsertsFileArg);
    cmd.add(logFileArg);
    cmd.add(prefixArg);

    //Parsing the command lines
    cmd.parse( argc, argv );

    vMysqlHost = QString::fromUtf8(mysqlHostArg.getValue().c_str());
    vUserName = QString::fromUtf8(userNameArg.getValue().c_str());
    vPassword = QString::fromUtf8(passwordArg.getValue().c_str());
    vSchema = QString::fromUtf8(schemaArg.getValue().c_str());
    vManifestMainTable = QString::fromUtf8(manifestMainTableArg.getValue().c_str());
    vManifestMainVariable = QString::fromUtf8(manifestMainVariableArg.getValue().c_str());
    vManifestXML = QString::fromUtf8(manifestXMLArg.getValue().c_str());
    vDatafileXML = QString::fromUtf8(datafileXMLArg.getValue().c_str());
    vDatafileBaseNode = QString::fromUtf8(datafileBaseNodeArg.getValue().c_str());
    vSqlInsertsFile = QString::fromUtf8(sqlInsertsFileArg.getValue().c_str());
    vLogFile = QString::fromUtf8(logFileArg.getValue().c_str());
    vPrefix = QString::fromUtf8(prefixArg.getValue().c_str());

    //Check if any prefix specified and if not clear prefix
    vPrefix = vPrefix + "_";

    if (vPrefix == "_")
        vPrefix = "";

    count = 0;
    //Call the processing of the manifest file
    processManifestXML();

    return 0;

}