0
\$\begingroup\$

I would really welcome some advice, and notes about my code. I am a long time Python user, and some days ago i decided i should try C++. So a couple of days passed, and i wrote my first application which has a practical use. I still find C++ very complicated (my current perspective is over complicated) but i can clearly say it is faster then my same python implementation.

The code:

/* 
 * File:   main.cpp
 * Author: gerdos
 *
 * Created on October 10, 2016, 4:23 PM
 */

#include <string>
#include <iostream>
#include <unistd.h>
#include <list>
#include <vector>
#include <algorithm>

#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/resultset_metadata.h>

using namespace sql;
using namespace std;

int main(int argc, char* argv[]){
    int opt;
    vector<string> column_name_list;

    // Shut GetOpt error messages down (return '?'): 
    opterr = 0;

    // Retrieve the options:
    while ( (opt = getopt(argc, argv, "F:")) != -1 ) {  // for each option...
        if (opt == 'F'){
            column_name_list.insert(column_name_list.end(), string(optarg));       
        }
    }
    string queries;
    for (unsigned i = 1; i < argc; i++){
        if (string(argv[i]) != "-F" && find(column_name_list.begin(), column_name_list.end(), argv[i]) == column_name_list.end()){
            queries += "'" + string(argv[i]) + "',";
        }
    }

    string columns;
    for (unsigned i = 0; i < column_name_list.size(); i++){
        columns += column_name_list[i] + ",";
    }
    columns.erase(columns.size()-1);
    queries.erase(queries.size()-1);

    try{
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;

        driver = get_driver_instance();

        con = driver->connect("sql_adress", "user", "nope"); // Join a server
        con->setSchema("database_name"); // Use a database

        stmt = con->createStatement();
        res = stmt->executeQuery(string("select " + columns + " from table_name where accession in (" + queries + ")"));

        sql::ResultSetMetaData *res_meta = res -> getMetaData();
        int cols = res_meta -> getColumnCount();
        if (res->rowsCount() == 0){
            cout << "Query not found!" << endl;
            return 0;
        }
        while (res->next()) {
            for (unsigned i = 1; i <= cols; i++){
                cout << res->getString(i) << endl;
            }  
        }
        delete res;
        delete stmt;
        delete con;
    }  
    // No idea about this part yet, copied it from a tutorial
    catch (sql::SQLException &e){
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }
    return 0;
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I found this code very difficult to understand, and I feel that I won't be alone. I can't refactor it (for the aforementioned reason) nevertheless some advice which you might find instructive:

  • Please use names which people will make sense of: column_name_list, opt, "F", opterr etc etc - this makes reading code very difficult.
  • formatting - please use good formatting it all makes a difference.
  • break things up into methods so that each method is doing only one thing.
  • Add better comments if things are not self-explanatory.

My two cents.

\$\endgroup\$
2
  • \$\begingroup\$ Just a side question: What should i do to improve the formatting? \$\endgroup\$ Commented Oct 12, 2016 at 16:42
  • \$\begingroup\$ hi it's just something i noticed - try and aim for something like the code example in #15 - especially if other people are going to read the code. \$\endgroup\$
    – BenKoshy
    Commented Oct 12, 2016 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.