1 // um yes... its a db connector
12 type DBConnector struct {
17 func CreateRandomText(len int) (str string)
19 buf := make([]byte, len);
21 rand.Seed(time.Nanoseconds());
23 for i:=0; i<len; i++ {
24 switch(rand.Int()%2) {
26 buf[i] = uint8(rand.Int()%22+66);
28 buf[i] = uint8(rand.Int()%22+98);
30 buf[i] = uint8(rand.Int()%9+48);
32 //buf[i+1] = 0; // this bit pisses off the c.cstring conversion somehow
39 func DBSetup(dbtype string, host string, username string, password string, database string, datamult int, comout chan int)
42 // the job of this function is to bring up the connection and create tables based on dbtype
43 var Dbconn *dbibridge.DBIConnection;
45 Dbconn = dbibridge.DBICreate();
47 dbibridge.DBIConnect(Dbconn, "mysql", host, username, password, database);
49 if dbtype == "mysql" {
50 dbibridge.ExecSQL(Dbconn,"drop table WAREHOUSE");
51 dbibridge.ExecSQL(Dbconn,"drop table DISTRICT");
52 dbibridge.ExecSQL(Dbconn,"drop table ITEM");
53 dbibridge.ExecSQL(Dbconn,"create table WAREHOUSE ( \
54 W_ID integer NOT NULL, \
55 W_NAME CHARACTER(10), \
56 W_STREET_1 CHARACTER(20), \
57 W_STREET_2 CHARACTER(20), \
58 W_CITY CHARACTER(20), \
59 W_STATE CHARACTER(2), \
65 dbibridge.ExecSQL(Dbconn,"create table DISTRICT ( \
66 D_ID integer NOT NULL, \
67 D_W_ID integer NOT NULL, \
68 D_NAME CHARACTER(10), \
69 D_STREET_1 CHARACTER(20), \
70 D_STREET_2 CHARACTER(20), \
71 D_CITY CHARACTER(20), \
72 D_STATE CHARACTER(2), \
76 D_NEXT_O_ID integer, \
77 PRIMARY KEY(D_W_ID, D_ID))");
79 dbibridge.ExecSQL(Dbconn,"create table ITEM ( \
80 I_ID integer NOT NULL, \
82 I_NAME CHARACTER(24), \
84 I_DATA CHARACTER(50), \
89 // now we need to actually create data in the tables... this is not how it was meant to be originally, but it'll do
90 // there is dbmult * warehouse
91 // there are 20 districts for each warehosuse
92 // there are 50000 items in each district
93 for i := 0; i < datamult; i++ {
94 whst := fmt.Sprintf("insert into WAREHOUSE values ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)", i, CreateRandomText(9),
95 CreateRandomText(19), CreateRandomText(19), CreateRandomText(19), CreateRandomText(2), CreateRandomText(8), rand.Int()%50, rand.Int()%100);
96 dbibridge.ExecSQL(Dbconn, whst);
97 for j:=0; j < 20; j++ {
98 dtst := fmt.Sprintf("insert into DISTRICT values ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d)", j, i,
99 CreateRandomText(9), CreateRandomText(19), CreateRandomText(19), CreateRandomText(19), CreateRandomText(2), CreateRandomText(8), rand.Int()%50, rand.Int()%100, j+1);
100 dbibridge.ExecSQL(Dbconn, dtst);
101 for k:=0; k<50000; k++ {
102 itst := fmt.Sprintf("insert into ITEM values ( %d, %d, '%s', %d, '%s')", (i+1)*(j+1)*(k+1), rand.Int()%50000,
103 CreateRandomText(23), rand.Int()%1000000, CreateRandomText(49));
104 dbibridge.ExecSQL(Dbconn, itst);
110 dbibridge.DBIDisconnect(Dbconn);