Advertisement
Guest User

Source code 4d-extract

a guest
Jun 27th, 2017
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. string createSelect(string table)
  2. {
  3.     std::string str("");
  4.     int array_size = 8192;
  5.     char * array = new char[array_size];
  6.     int position = 0;
  7.     ifstream fin("tables\\"+table+".tables");
  8.     if(fin.is_open())
  9.     {
  10.         str = "SELECT ";
  11.         while(!fin.eof() && position < array_size)
  12.         {
  13.             fin.get(array[position]); //reading one character from file to array
  14.             position++;
  15.         }
  16.         array[position-1] = '\0'; //placing character array terminating character
  17.         //this loop display all the charaters in array till \0
  18.         for(int i = 0; array[i] != '\0'; i++)
  19.         {
  20.             str += array[i];
  21.         }
  22.         str += " FROM " + table;
  23.     }
  24.     else
  25.     {
  26.         str = "";
  27.     }
  28.     return str;
  29. }
  30.  
  31. string createInsert(string table)
  32. {
  33.     std::string str("");
  34.     int array_size = 8192;
  35.     char * array = new char[array_size];
  36.     int position = 0;
  37.     ifstream fin("tables\\"+table+".tables");
  38.  
  39.     if(fin.is_open())
  40.     {
  41.         str += "INSERT INTO "+table+" (";
  42.         while(!fin.eof() && position < array_size)
  43.         {
  44.             fin.get(array[position]);
  45.             position++;
  46.         }
  47.         array[position-1] = '\0';
  48.         for(int i = 0; array[i] != '\0'; i++)
  49.         {
  50.             str += array[i];
  51.         }
  52.         str += ") VALUES (";
  53.     }
  54.     else
  55.     {
  56.         str = "";
  57.     }
  58.     return str;
  59. }
  60.  
  61. bool createSQL(string table)
  62. {
  63.     ofstream newFile;
  64.     ofstream newFile2;
  65.     newFile.open ("imports\\VUL_"+table+".sql");
  66.     newFile2.open ("output.txt");
  67.     std::string select("");
  68.     std::string insert("");
  69.  
  70.   SQLHANDLE hEnv ;
  71.   SQLRETURN retCode ;
  72.   SQLRETURN retCode2 ;
  73.  
  74.   retCode = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv ) ;
  75.  
  76.   CHECK( retCode, "allocate environment handle" ) ;
  77.  
  78.   retCode = SQLSetEnvAttr( hEnv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0 ) ;
  79.  
  80.   CHECK( retCode, "setting the environment attribute setting to ODBC version 3" ) ;
  81.  
  82.   SQLHANDLE hConn ;
  83.  
  84.   CHECK( SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hConn ), "allocate handle" ) ;
  85.  
  86.  
  87.  
  88.  
  89.  
  90.   SQLCHAR* dsnName = (SQLCHAR*)"REMOVED" ;
  91.   SQLCHAR* userid = (SQLCHAR*)"REMOVED";
  92.   SQLCHAR* password = (SQLCHAR*)"REMOVED";
  93.  
  94.  
  95.   retCode = SQLConnectA(
  96.  
  97.     hConn,
  98.  
  99.     dsnName,  // name of data source we are connecting to,
  100.  
  101.     SQL_NTS,  // the DSN name is a NULL TERMINATED STRING, so "count it yourself"
  102.  
  103.     userid,
  104.     SQL_NTS,  // userid is a null-terminated string
  105.  
  106.     password,
  107.     SQL_NTS   // password is a null terminated string
  108.  
  109.   ) ;
  110.   if( !CHECK( retCode, "SqlConnectA", false ) )
  111.   {
  112.     // if this fails, I want that extra status
  113.     // information about WHY the failure happened.
  114.     // status function is defined above.
  115.  
  116.     status( SQL_HANDLE_DBC, hConn, __LINE__ ) ;
  117.   }
  118.  
  119.  
  120.   // 6.  Create and allocate a statement
  121.   SQLHANDLE hStmt ;
  122.   CHECK( SQLAllocHandle( SQL_HANDLE_STMT, hConn, &hStmt ), "allocate handle for statement" ) ;
  123.  
  124.  
  125.   // 7.  Form a query to run and attach it to the hStmt
  126.   // this basically connects the hStmt up with
  127.   // some results.
  128.  
  129.   select = createSelect(table);
  130.   const char *cstr = select.c_str();
  131.   newFile2 << cstr;
  132.   SQLCHAR* query = (SQLCHAR*)cstr;
  133.  
  134.   CHECK( SQLExecDirectA( hStmt, query, SQL_NTS ), "execute query" ) ;
  135.  
  136.  
  137.   // 8.  Read data results that are now in the hStmt.
  138.   retCode = SQLFetch( hStmt ) ;
  139.  
  140.   CHECK( retCode, "first sqlFetch" ) ;
  141.  
  142.   // How many rows got returned?
  143.   SQLLEN numRows ;
  144.   retCode = SQLRowCount( hStmt, &numRows ) ;
  145.   printf( "%d regels zijn verwerkt.\n", numRows ) ;
  146.  
  147.   // With a query like the one we wrote (SELECT *),
  148.   // we don't know how many columsn should be in
  149.   // the result set at this point.
  150.   // So we ask ODBC to tell us!
  151.   SQLSMALLINT numCols ;
  152.   retCode = SQLNumResultCols( hStmt, &numCols ); // SqlNumResultCols
  153.  
  154.   // Now print the column names.
  155.   // SQLDescribeCol function
  156.   SQLCHAR colName[ 256 ] ;
  157.  
  158.   SQLSMALLINT colNameLen, dataType, numDecimalDigits, allowsNullValues ;
  159.   SQLUINTEGER columnSize ;
  160.  
  161.   for( int i = 1 ; i <= numCols ; i++ )
  162.   {
  163.     retCode = SQLDescribeColA( hStmt, i, colName, 255, &colNameLen, &dataType, &columnSize, &numDecimalDigits, &allowsNullValues ) ;
  164.     if( CHECK( retCode, "SQLDescribeCol" ) )
  165.     {
  166.       printf( "Column #%d: '%s', datatype=%d size=%d decimaldigits=%d nullable=%d\n",
  167.                        i,colName,   dataType, columnSize,  numDecimalDigits, allowsNullValues ) ;
  168.     }
  169.   }
  170.   newFile << "TRUNCATE TABLE "+table+";";
  171.   for( int i = 1 ; i <= numRows ; i++ )
  172.   {
  173.     // Datatypes
  174.     // SQLGetData
  175.  
  176.     char buf[256];
  177.     SQLINTEGER numBytes ;
  178.     newFile << createInsert(table);
  179.     for( int j = 1 ;
  180.       j <= numCols ;
  181.       j++ )
  182.     {
  183.  
  184.       retCode = SQLGetData(
  185.  
  186.         hStmt,
  187.         j,           // COLUMN NUMBER of the data to get
  188.         SQL_C_CHAR,  // the data type that you expect to receive
  189.         buf,         // the place to put the data that you expect to receive
  190.         255,         // the size in bytes of buf (-1 for null terminator)
  191.         &numBytes    // size in bytes of data returned
  192.  
  193.       ) ;
  194.  
  195.         if( CHECK( retCode, "SqlGetData", false ) )
  196.         {
  197.             retCode2 = SQLDescribeColA( hStmt, j, colName, 255, &colNameLen, &dataType, &columnSize, &numDecimalDigits, &allowsNullValues ) ;
  198.             if( CHECK( retCode2, "SQLDescribeCol" ) )
  199.             {
  200.                 //cout << dataType << endl;
  201.                 if(dataType != 91) {
  202.                     newFile << "'" << removeSlashes(removeSpecials(buf)) << "'";
  203.                 }
  204.                 else if (dataType == 91) {
  205.                     newFile << "date_format(str_to_date('" << fixDate(buf) << "', '%d-%m-%Y'),'%Y-%m-%d')";
  206.                 }
  207.             }
  208.         //Sleep(50);
  209.         }
  210.         if(j != numCols) {
  211.             newFile << ",";
  212.         }
  213.  
  214.     }
  215.     newFile << ");\n";
  216.     cout << "Regel #" << i <<  " van tabel " << table << " is verwerkt." << endl;
  217.  
  218.     retCode = SQLFetch( hStmt ) ;
  219.     if( !SQL_SUCCEEDED( retCode ) )
  220.     {
  221.       cout << "Tabel "+table+" is verwerkt." << endl;
  222.       printf( "Regel %d is de laatste regel.\n", i ) ;
  223.     }
  224.   }
  225.   newFile.close();
  226.   SQLFreeHandle( SQL_HANDLE_STMT, hStmt ) ;
  227.   SQLFreeHandle( SQL_HANDLE_DBC, hConn ) ;
  228.   SQLFreeHandle( SQL_HANDLE_ENV, hEnv ) ;
  229.   return true;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement