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