Makefile..
[goDBhammer.git] / src / dbconnector.go
1 // um yes... its a db connector
2 package dbconnector
3
4 import (
5         //"./dbibridge";
6         "dbibridge";
7         //"strings";
8         "time";
9         "rand";
10         "fmt";
11 )
12
13 type DBConnector struct {
14         // err, yes
15 }
16
17
18 func CreateRandomText(len int) (str string)
19 {
20         buf := make([]byte, len);
21
22         rand.Seed(time.Nanoseconds());
23         
24         for i:=0; i<len; i++ {
25                 switch(rand.Int()%2) {
26                 case 0:
27                     buf[i] = uint8(rand.Int()%22+66);
28                 case 1:
29                     buf[i] = uint8(rand.Int()%22+98);
30                 case 2:
31                     buf[i] = uint8(rand.Int()%9+48);
32                 }
33                 //buf[i+1] = 0; // this bit pisses off the c.cstring conversion somehow
34         }
35         
36         
37         return string(buf);
38 }
39
40 func DBSetup(dbtype string, host string, username string, password string, database string, datamult int, comout chan int)
41 {
42
43         // the job of this function is to bring up the connection and create tables based on dbtype
44         var Dbconn *dbibridge.DBIConnection;
45         
46         Dbconn = dbibridge.DBICreate();
47         
48         dbibridge.DBIConnect(Dbconn, "mysql", host, username, password, database);
49         
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), \
61                 W_ZIP CHARACTER(9), \
62                 W_TAX integer, \
63                 W_YTD integer, \
64                 PRIMARY KEY(W_ID))");
65
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), \
74                 D_ZIP CHARACTER(9), \
75                 D_TAX integer, \
76                 D_YTD integer, \
77                 D_NEXT_O_ID integer, \
78                 PRIMARY KEY(D_W_ID, D_ID))");
79                 
80                 dbibridge.ExecSQL(Dbconn,"create table ITEM ( \
81                 I_ID integer NOT NULL, \
82                 I_IM_ID integer, \
83                 I_NAME CHARACTER(24), \
84                 I_PRICE integer, \
85                 I_DATA CHARACTER(50), \
86                 PRIMARY KEY(I_ID))");
87         }
88         
89         
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);
106                         }
107                 }
108         }
109         
110
111         dbibridge.DBIDisconnect(Dbconn);
112 }
113
114
115