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