-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.C
78 lines (70 loc) · 2.16 KB
/
statement.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<vector>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio> //sprintf()
#include "statement.H"
int Statement::getRandomInt(int max=0) {
srand( (unsigned) time(NULL) ) ;
if(!max) {
std::cout << "rand() = " << rand() << std::endl;
return rand();
} else
return rand() % (max+1);
}
char * Statement::getRandomString(int length) {
std::cout << "start getrandomstring()" << std::endl;
char *string = new char[length]; //memory leak
for(int i=0; i<length; ++i) {
string[i] = static_cast<char>(getRandomInt(126)); //127 max in asci?
}
return string;
}
Statement::Statement(char * tablename, std::vector<char*> attributes, std::vector<char*> types, std::vector<int> lengths) {
this->table_name = tablename;
this->attributes = attributes;
this->types = types;
this->lengths = lengths;
}
bool Statement::createFile(char *filename, int count) {
std::string output("INSERT INTO ");
output += this->table_name;
output += " (";
int count_lengths = 0;
for(int i=0; i<this->attributes.size(); ++i) {
output += attributes[i];
output += ", ";
}
output.erase(output.end()-1); // cut ", " off last one
output.erase(output.end()-1);
output += ") VALUES (";
//begin calculating random data and inserting it
for(int i=0; i<this->types.size(); ++i) {
int s=0; //0=int
if(!(std::strcmp(this->types[i],"string"))) //strcmp = 0 if the same
s=1;
switch(s) {
case 0: //int
char buffer[33];
sprintf(buffer,"%d",getRandomInt());
output += buffer;
output += ",";
break;
case 1: //string
output += "\'";
std::cout << "before string" << std::endl;
std::cout << "length: " << this->lengths[count_lengths] << std::endl;
output += getRandomString(this->lengths[count_lengths++]); //increas count afterwards
std::cout << "after" << std::endl;
output += "\',";
break;
default:
break;
}
}
output.erase(output.end()-1); //cut off ","
output += ");";
std::cout << output << std::endl;
return true;
}