Makefile..
[goDBhammer.git] / src / dbconnector.go
index 668242d..fa86e41 100644 (file)
 // um yes... its a db connector
 package dbconnector
 
+import (
+       //"./dbibridge";
+       "dbibridge";
+       //"strings";
+       "time";
+       "rand";
+       "fmt";
+)
+
 type DBConnector struct {
        // err, yes
 }
 
-func DBSetup(dbtype string, host string, username string, password string, database string, createtables bool)
+
+func CreateRandomText(len int) (str string)
+{
+       buf := make([]byte, len);
+
+       rand.Seed(time.Nanoseconds());
+       
+       for i:=0; i<len; i++ {
+               switch(rand.Int()%2) {
+                case 0:
+                    buf[i] = uint8(rand.Int()%22+66);
+                case 1:
+                    buf[i] = uint8(rand.Int()%22+98);
+                case 2:
+                    buf[i] = uint8(rand.Int()%9+48);
+               }
+               //buf[i+1] = 0; // this bit pisses off the c.cstring conversion somehow
+        }
+       
+       
+       return string(buf);
+}
+
+func DBSetup(dbtype string, host string, username string, password string, database string, datamult int, comout chan int)
 {
 
        // the job of this function is to bring up the connection and create tables based on dbtype
+       var Dbconn *dbibridge.DBIConnection;
+       
+       Dbconn = dbibridge.DBICreate();
+       
+       dbibridge.DBIConnect(Dbconn, "mysql", host, username, password, database);
+       
+       if dbtype == "mysql" {
+               dbibridge.ExecSQL(Dbconn,"drop table WAREHOUSE");
+               dbibridge.ExecSQL(Dbconn,"drop table DISTRICT");
+               dbibridge.ExecSQL(Dbconn,"drop table ITEM");
+               dbibridge.ExecSQL(Dbconn,"create table WAREHOUSE ( \
+               W_ID integer NOT NULL, \
+               W_NAME CHARACTER(10), \
+               W_STREET_1 CHARACTER(20), \
+               W_STREET_2 CHARACTER(20), \
+               W_CITY CHARACTER(20), \
+               W_STATE CHARACTER(2), \
+               W_ZIP CHARACTER(9), \
+               W_TAX integer, \
+               W_YTD integer, \
+               PRIMARY KEY(W_ID))");
+
+               dbibridge.ExecSQL(Dbconn,"create table DISTRICT ( \
+               D_ID integer NOT NULL, \
+               D_W_ID integer NOT NULL, \
+               D_NAME CHARACTER(10), \
+               D_STREET_1 CHARACTER(20), \
+               D_STREET_2 CHARACTER(20), \
+               D_CITY CHARACTER(20), \
+               D_STATE CHARACTER(2), \
+               D_ZIP CHARACTER(9), \
+               D_TAX integer, \
+               D_YTD integer, \
+               D_NEXT_O_ID integer, \
+               PRIMARY KEY(D_W_ID, D_ID))");
+               
+               dbibridge.ExecSQL(Dbconn,"create table ITEM ( \
+               I_ID integer NOT NULL, \
+               I_IM_ID integer, \
+               I_NAME CHARACTER(24), \
+               I_PRICE integer, \
+               I_DATA CHARACTER(50), \
+               PRIMARY KEY(I_ID))");
+       }
+       
+       
+       // now we need to actually create data in the tables... this is not how it was meant to be originally, but it'll do
+       // there is dbmult * warehouse
+       // there are 20 districts for each warehosuse
+       // there are 50000 items in each district
+       for i := 0; i < datamult; i++ {
+               whst := fmt.Sprintf("insert into WAREHOUSE values ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)", i, CreateRandomText(9),
+                       CreateRandomText(19), CreateRandomText(19), CreateRandomText(19), CreateRandomText(2), CreateRandomText(8), rand.Int()%50, rand.Int()%100);
+               dbibridge.ExecSQL(Dbconn, whst); 
+               for j:=0; j < 20; j++ {
+                       dtst := fmt.Sprintf("insert into DISTRICT values ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d)", j, i, 
+                               CreateRandomText(9), CreateRandomText(19), CreateRandomText(19), CreateRandomText(19), CreateRandomText(2), CreateRandomText(8), rand.Int()%50, rand.Int()%100, j+1);
+                       dbibridge.ExecSQL(Dbconn, dtst);
+                       for k:=0; k<50000; k++ {
+                               itst := fmt.Sprintf("insert into ITEM values ( %d, %d, '%s', %d, '%s')", (i+1)*(j+1)*(k+1), rand.Int()%50000, 
+                                       CreateRandomText(23), rand.Int()%1000000, CreateRandomText(49));
+                               dbibridge.ExecSQL(Dbconn, itst);
+                       }
+               }
+       }
+       
 
+       dbibridge.DBIDisconnect(Dbconn);
 }