Best Syzkaller code snippet using db.Create
setup.go
Source:setup.go
...34 )35 db = database36 password, err := bcrypt.GenerateFromPassword([]byte("123456"), 14)37 //employee38 db.Model(&Employee{}).Create(&Employee{39 Name: "à¸à¸²à¸à¸ªà¸²à¸§à¸à¸£ มà¸à¸µà¸§à¸£à¸£à¸",40 Email: "porn@gmail.com",41 Tel: "0883322456",42 Password: string(password),43 })44 db.Model(&Employee{}).Create(&Employee{45 Name: "à¸à¸²à¸¢à¸ªà¸¡ à¸à¸±à¸à¸à¸£à¹à¹à¸à¹à¸",46 Email: "som@gmail.com",47 Tel: "0885548900",48 Password: string(password),49 })50 db.Model(&Employee{}).Create(&Employee{51 Name: "à¸à¸²à¸à¸ªà¸²à¸§à¸à¸¥à¹à¸§à¸¢ à¹à¸à¸¢à¸§à¸²à¸à¸µ",52 Email: "naruemon@gmail.com",53 Tel: "0610091572",54 Password: string(password),55 })56 db.Model(&Employee{}).Create(&Employee{57 Name: "Phupha Bbbb",58 Email: "bbb@gmail.com",59 Tel: "0945333333",60 Password: string(password),61 })62 var porn Employee63 var som Employee64 var kluy Employee65 var Phupha Employee66 db.Raw("SELECT * FROM employees WHERE email = ?", "porn@gmail.com").Scan(&porn)67 db.Raw("SELECT * FROM employees WHERE email = ?", "som@gmail.com").Scan(&som)68 db.Raw("SELECT * FROM employees WHERE email = ?", "naruemon@gmail.com").Scan(&kluy)69 db.Raw("SELECT * FROM employees WHERE email = ?", "bbb@gmail.com").Scan(&Phupha)70 // Customer Data71 C1 := Customer{72 Name: "à¸à¸²à¸¢à¹à¸à¸¡ à¸à¸¹à¸¥à¸ªà¸§à¸±à¸ªà¸à¸´à¹",73 Email: "b6100124@g.sut.ac.th",74 Tel: "0983322403",75 Password: string(password),76 }77 db.Model(&Customer{}).Create(&C1)78 C2 := Customer{79 Name: "à¸à¸²à¸¢à¹à¸à¸à¸à¹ à¸à¸±à¸à¹à¸à¸à¸²à¸à¸±à¸",80 Email: "Tanapol@gmail.com",81 Tel: "0673322403",82 Password: string(password),83 }84 db.Model(&Customer{}).Create(&C2)85 C3 := Customer{86 Name: "à¸à¸²à¸¢à¸«à¸¢à¸à¸à¸²à¸¢ à¸à¸à¸¨à¸à¸£",87 Email: "name@example.com",88 Tel: "0953322883",89 Password: string(password),90 }91 db.Model(&Customer{}).Create(&C3)92 var sam Customer93 var benz Customer94 var yok Customer95 db.Raw("SELECT * FROM customers WHERE email = ?", "b6100124@g.sut.ac.th").Scan(&sam)96 db.Raw("SELECT * FROM customers WHERE email = ?", "Tanapol@gmail.com").Scan(&benz)97 db.Raw("SELECT * FROM customers WHERE email = ?", "name@example.com").Scan(&yok)98 //roomtype99 Standard := RoomType{100 Name: "standard",101 Detail: "à¹à¸à¸µà¸¢à¸à¸à¸¹à¹ 1 à¹à¸à¸µà¸¢à¸ à¹à¸à¹à¸²à¸à¸±à¸à¹à¸à¹ 2 à¸à¸ มีสิà¹à¸à¸à¸³à¸à¸§à¸¢à¸à¸§à¸²à¸¡à¸ªà¸°à¸à¸§à¸ (à¹à¸à¸£à¸·à¹à¸à¸à¸à¸£à¸±à¸à¸à¸²à¸à¸²à¸¨, à¸à¹à¸³à¸à¸¸à¹à¸)",102 Price: 500,103 }104 db.Model(&RoomType{}).Create(&Standard)105 Deluxe := RoomType{106 Name: "Deluxe",107 Detail: "à¹à¸à¸µà¸¢à¸à¸à¸à¸²à¸à¸à¸¥à¸²à¸ 1 à¹à¸à¸µà¸¢à¸ à¹à¸à¹à¸²à¸à¸±à¸à¹à¸à¹ 2 à¸à¸ มà¸à¸à¹à¸«à¹à¸à¸§à¸´à¸§à¸à¸´à¸§à¸à¸±à¸¨à¸à¸µà¸¢à¸ าà¸à¸à¸µà¹à¸ªà¸§à¸¢à¸à¸²à¸¡ หà¹à¸à¸à¸à¸±à¸à¸à¸à¸²à¸à¸à¸¥à¸²à¸",108 Price: 1000,109 }110 db.Model(&RoomType{}).Create(&Deluxe)111 Suite := RoomType{112 Name: "suite",113 Detail: "à¹à¸à¸µà¸¢à¸à¸à¸à¸²à¸à¹à¸«à¸à¹ 1 à¹à¸à¸µà¸¢à¸ à¹à¸à¹à¸²à¸à¸±à¸à¹à¸à¹ 3 à¸à¸ มีหà¹à¸à¸à¸à¸±à¹à¸à¹à¸¥à¹à¸ หà¹à¸à¸à¸à¸à¸ หà¹à¸à¸à¸à¸±à¸à¸à¸à¸²à¸à¹à¸«à¸à¹",114 Price: 1500,115 }116 db.Model(&RoomType{}).Create(&Suite)117 //status118 available := Status{119 Detail: "วà¹à¸²à¸",120 }121 db.Model(&Status{}).Create(&available)122 noavailable := Status{123 Detail: "à¹à¸¡à¹à¸§à¹à¸²à¸",124 }125 db.Model(&Status{}).Create(&noavailable)126 // --- Room data127 r1 := Room{128 Roomnumber: "A101",129 Location: "ZoneA à¸à¸±à¹à¸ 1",130 Type: Standard,131 Recorder: kluy,132 Status: available,133 }134 db.Model(&Room{}).Create(&r1)135 r2 := Room{136 Roomnumber: "B201",137 Location: "ZoneB à¸à¸±à¹à¸ 2",138 Type: Deluxe,139 Recorder: kluy,140 Status: available,141 }142 db.Model(&Room{}).Create(&r2)143 r3 := Room{144 Roomnumber: "A301",145 Location: "ZoneA à¸à¸±à¹à¸ 3",146 Type: Suite,147 Recorder: kluy,148 Status: available,149 }150 db.Model(&Room{}).Create(&r3)151 // Payment152 p1 := Payment{153 Method: "KTB",154 }155 db.Model(&Payment{}).Create(&p1)156 p2 := Payment{157 Method: "SCB",158 }159 db.Model(&Payment{}).Create(&p2)160 p3 := Payment{161 Method: "TMB",162 }163 db.Model(&Payment{}).Create(&p3)164 //reserve165 //reserve 1166 RS1 := Reservation{167 DateAndTime: time.Now(),168 People: 1,169 Customer: sam,170 Room: r1,171 Payment: p1,172 }173 //reserve 2174 RS2 := Reservation{175 DateAndTime: time.Now(),176 People: 2,177 Customer: yok,178 Room: r2,179 Payment: p3,180 }181 //reserve 3182 RS3 := Reservation{183 DateAndTime: time.Now(),184 People: 1,185 Customer: benz,186 Room: r2,187 Payment: p1,188 }189 RS4 := Reservation{190 DateAndTime: time.Now(),191 People: 1,192 Customer: sam,193 Room: r1,194 Payment: p1,195 }196 db.Model(&Reservation{}).Create(&RS1)197 db.Model(&Reservation{}).Create(&RS2)198 db.Model(&Reservation{}).Create(&RS3)199 db.Model(&Reservation{}).Create(&RS4)200 //balance201 B1 := Balance{202 Type: "Full",203 }204 db.Model(&Balance{}).Create(&B1)205 B2 := Balance{206 Type: "Half",207 }208 db.Model(&Balance{}).Create(&B2)209 // --- RoomPayment Data210 RP1 := RoomPayment{211 PaymentDate: time.Now(),212 Amount: 250,213 Recorder: som,214 Reservation: RS1,215 Balance: B2,216 }217 db.Model(&RoomPayment{}).Create(&RP1)218 RP2 := RoomPayment{219 PaymentDate: time.Now(),220 Amount: 500,221 Recorder: porn,222 Reservation: RS2,223 Balance: B2,224 }225 db.Model(&RoomPayment{}).Create(&RP2)226 RP3 := RoomPayment{227 PaymentDate: time.Now(),228 Amount: 1500,229 Recorder: kluy,230 Reservation: RS3,231 Balance: B1,232 }233 db.Model(&RoomPayment{}).Create(&RP3)234 RP4 := RoomPayment{235 PaymentDate: time.Now(),236 Amount: 1500,237 Recorder: kluy,238 Reservation: RS4,239 Balance: B1,240 }241 db.Model(&RoomPayment{}).Create(&RP4)242 RP5 := RoomPayment{243 PaymentDate: time.Now(),244 Amount: 500,245 Recorder: kluy,246 Reservation: RS1,247 Balance: B1,248 }249 db.Model(&RoomPayment{}).Create(&RP5)250 // --- CheckIn data251 CheckIntana1 := CheckIn{252 DateTime: time.Now(),253 Customer: benz,254 Room: r1,255 RoomPayment: RP2,256 Employee: kluy,257 }258 db.Model(&CheckIn{}).Create(&CheckIntana1)259 CheckIntana2 := CheckIn{260 DateTime: time.Now(),261 Customer: sam,262 Room: r2,263 RoomPayment: RP1,264 Employee: som,265 }266 db.Model(&CheckIn{}).Create(&CheckIntana2)267 CheckIntana3 := CheckIn{268 DateTime: time.Now(),269 Customer: yok,270 Room: r3,271 RoomPayment: RP3,272 Employee: porn,273 }274 db.Model(&CheckIn{}).Create(&CheckIntana3)275 CheckIntana4 := CheckIn{276 DateTime: time.Now(),277 Customer: sam,278 Room: r1,279 RoomPayment: RP4,280 Employee: porn,281 }282 db.Model(&CheckIn{}).Create(&CheckIntana4)283 // checkout data284 CheckOuttana3 := CheckOut{285 CheckIn: CheckIntana3,286 Employee: som,287 Customer: yok,288 CheckOutTime: time.Time{},289 Condition: "No damage",290 }291 db.Model(&CheckOut{}).Create(&CheckOuttana3)292 CheckOutemp := CheckOut{293 CheckIn: CheckIntana1,294 Employee: som,295 Customer: benz,296 CheckOutTime: time.Time{},297 Condition: "No damage",298 }299 db.Model(&CheckOut{}).Create(&CheckOutemp)300 // equipment data301 equipdm := Equipment{302 Name: "à¸à¸£à¸°à¸à¸à¸ªà¸³à¸«à¸£à¸±à¸à¹à¸à¹à¸à¸à¸±à¸§ (Dressing mirror)",303 }304 db.Model(&Equipment{}).Create(&equipdm)305 equipchair := Equipment{306 Name: "à¹à¸à¹à¸²à¸à¸µà¹ (Chair)",307 }308 db.Model(&Equipment{}).Create(&equipchair)309 equipwaterheater := Equipment{310 Name: "à¹à¸à¸£à¸·à¹à¸à¸à¸à¸³à¸à¹à¸³à¸à¸¸à¹à¸ (Water heater)",311 }312 db.Model(&Equipment{}).Create(&equipwaterheater)313 equipac := Equipment{314 Name: "à¹à¸à¸£à¸·à¹à¸à¸à¸à¸£à¸±à¸à¸à¸²à¸à¸²à¸¨ (Air conditioner)",315 }316 db.Model(&Equipment{}).Create(&equipac)317 equiplamp := Equipment{318 Name: "à¹à¸à¸¡à¹à¸ (Lamp)",319 }320 db.Model(&Equipment{}).Create(&equiplamp)321 equipflushtoilet := Equipment{322 Name: "à¸à¸±à¸à¹à¸à¸£à¸ (flush toilet)",323 }324 db.Model(&Equipment{}).Create(&equipflushtoilet)325 equipbed := Equipment{326 Name: "à¹à¸à¸µà¸¢à¸à¸à¸à¸ (Bed)",327 }328 db.Model(&Equipment{}).Create(&equipbed)329 equipfridge := Equipment{330 Name: "à¸à¸¹à¹à¹à¸¢à¹à¸ (Fridge)",331 }332 db.Model(&Equipment{}).Create(&equipfridge)333 equiptable := Equipment{334 Name: "à¹à¸à¹à¸° (Table)",335 }336 db.Model(&Equipment{}).Create(&equiptable)337 equipwardrobe := Equipment{338 Name: "à¸à¸¹à¹à¹à¸ªà¸·à¹à¸à¸à¹à¸² (Wardrobe)",339 }340 db.Model(&Equipment{}).Create(&equipwardrobe)341 equiptv := Equipment{342 Name: "à¹à¸à¸£à¸à¸±à¸¨à¸à¹ (television)",343 }344 db.Model(&Equipment{}).Create(&equiptv)345 equipdoor := Equipment{346 Name: "à¸à¸£à¸°à¸à¸¹ (Door)",347 }348 db.Model(&Equipment{}).Create(&equipdoor)349 equipshower := Equipment{350 Name: "à¸à¸±à¸à¸à¸±à¸§ (Shower)",351 }352 db.Model(&Equipment{}).Create(&equipshower)353 equipfan := Equipment{354 Name: "à¸à¸±à¸à¸¥à¸¡ (Fan)",355 }356 db.Model(&Equipment{}).Create(&equipfan)357 equipFluorescentlamp := Equipment{358 Name: "หลà¸à¸à¸à¸¥à¸¹à¸à¸à¹à¸£à¸ªà¹à¸à¸à¸à¹ (Fluorescent lamp)",359 }360 db.Model(&Equipment{}).Create(&equipFluorescentlamp)361 // problem data362 prodefective := Problem{363 Value: "à¸à¸³à¸£à¸¸à¸ (Defective)",364 }365 db.Model(&Problem{}).Create(&prodefective)366 pronotwork := Problem{367 Value: "à¹à¸à¹à¸à¸²à¸à¹à¸¡à¹à¹à¸à¹ (Not working)",368 }369 db.Model(&Problem{}).Create(&pronotwork)370 // urgency data371 urgent := Urgency{372 Value: "à¹à¸£à¹à¸à¸à¹à¸§à¸ (Urgent)",373 }374 db.Model(&Urgency{}).Create(&urgent)375 urfast := Urgency{376 Value: "à¹à¸£à¹à¸§ (Fast)",377 }378 db.Model(&Urgency{}).Create(&urfast)379 urmedium := Urgency{380 Value: "à¸à¸²à¸à¸à¸¥à¸²à¸ (Medium)",381 }382 db.Model(&Urgency{}).Create(&urmedium)383 urslow := Urgency{384 Value: "à¸à¹à¸² (Slow)",385 }386 db.Model(&Urgency{}).Create(&urslow)387 urvslow := Urgency{388 Value: "à¸à¹à¸²à¸¡à¸²à¸à¹ (Vary slow)",389 }390 db.Model(&Urgency{}).Create(&urvslow)391 // 1 repair392 db.Model(&RepairInformation{}).Create(&RepairInformation{393 CheckIn: CheckIntana1,394 Equipment: equipfridge,395 Problem: pronotwork,396 Urgency: urfast,397 Datetime: time.Now(),398 })399 // 2 repair400 db.Model(&RepairInformation{}).Create(&RepairInformation{401 CheckIn: CheckIntana2,402 Equipment: equiptable,403 Problem: prodefective,404 Urgency: urmedium,405 Datetime: time.Now(),406 })407 // 3 repair408 db.Model(&RepairInformation{}).Create(&RepairInformation{409 CheckIn: CheckIntana3,410 Equipment: equipwardrobe,411 Problem: prodefective,412 Urgency: urslow,413 Datetime: time.Now(),414 })415}...
mysqldb.go
Source:mysqldb.go
1package mysqldb2import (3 "database/sql"4 "errors"5 "regexp"6 "github.com/sirupsen/logrus"7 "github.com/go-sql-driver/mysql"8)9var log = logrus.WithField("prefix", "MySQL")10var dsnRegex = regexp.MustCompile(`\:(.+?)\@`)11type Database struct {12 DB *sql.DB13 ConnectionString string14}15// Close closes the underlying database.16func (db *Database) Close() error {17 return db.DB.Close()18}19func ValidateConnectionString(dsn string) (string, error) {20 if dsn == "" {21 return "", errors.New("Connection string not provided")22 }23 cfg, err := mysql.ParseDSN(dsn)24 if err != nil {25 log.Warnf("Connection string could not be parsed: %s", err.Error())26 return "", err27 }28 log.Debugf("DB_ADDR := %s", cfg.Addr)29 log.Debugf("DB_NET := %s", cfg.Net)30 log.Debugf("DB_DBNAME := %s", cfg.DBName)31 log.Debugf("DB_USER := %s", cfg.User)32 log.Debugf("PARAMS := %v", cfg.Params)33 if !cfg.ParseTime {34 cfg.ParseTime = true35 }36 charset, ok := cfg.Params["charset"]37 if !(ok && charset == "utf8") {38 if cfg.Params == nil {39 cfg.Params = make(map[string]string)40 }41 cfg.Params["charset"] = "utf8"42 }43 log.Debugf("ParseTime := %v", cfg.ParseTime)44 log.Debugf("Charset := %s", cfg.Params["charset"])45 dsnString := cfg.FormatDSN()46 log.Debugf("DSN := %s", redactPassword(dsnString))47 return dsnString, nil48}49func redactPassword(rawDSNString string) string {50 cleanedDSNString := dsnRegex.ReplaceAll([]byte(rawDSNString), []byte(":**REDACTED**@"))51 return string(cleanedDSNString)52}53// NewDB initializes a new DB.54func NewDB(connection_string string) (*Database, error) {55 validatedString, err := ValidateConnectionString(connection_string)56 if err != nil {57 log.Fatal(err.Error())58 return nil, err59 }60 MySQLDB, err := sql.Open("mysql", validatedString)61 if err != nil {62 log.Fatal(err.Error())63 return nil, err64 }65 db := &Database{DB: MySQLDB, ConnectionString: connection_string}66 return db, nil67}68func (db *Database) InitDB() error {69 log.Info("Initialising DB Table")70 //USERS71 createDB := `72 CREATE TABLE IF NOT EXISTS users (73 user_id VARCHAR(255) NOT NULL,74 username VARCHAR(255) NOT NULL,75 PRIMARY KEY(user_id)76 );`77 log.Debug("Query: " + createDB)78 _, err := db.DB.Exec(createDB)79 if err != nil {80 log.Fatalf("Creating users table failed: %s", err)81 }82 //ACCOUNTS83 createDB = `84 CREATE TABLE IF NOT EXISTS accounts (85 account_id VARCHAR(255) NOT NULL,86 name VARCHAR(255) NOT NULL,87 PRIMARY KEY(account_id)88 );`89 log.Debug("Query: " + createDB)90 _, err = db.DB.Exec(createDB)91 if err != nil {92 log.Fatalf("Creating accounts table failed: %s", err)93 }94 //TAGS95 createDB = `96 CREATE TABLE IF NOT EXISTS tags (97 tag_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,98 tag_name VARCHAR(100) NOT NULL UNIQUE99 );`100 log.Debug("Query: " + createDB)101 _, err = db.DB.Exec(createDB)102 if err != nil {103 log.Fatalf("Creating tags table failed: %s", err)104 }105 //TAGS FOR ACCOUNTS106 createDB = `107 CREATE TABLE IF NOT EXISTS account_tag (108 account_id VARCHAR(255) NOT NULL,109 tag_id INTEGER NOT NULL,110 FOREIGN KEY (account_id) REFERENCES accounts (account_id) ON DELETE CASCADE ON UPDATE CASCADE,111 FOREIGN KEY (tag_id) REFERENCES tags (tag_id) ON DELETE CASCADE ON UPDATE CASCADE,112 PRIMARY KEY (account_id, tag_id)113 );`114 log.Debug("Query: " + createDB)115 _, err = db.DB.Exec(createDB)116 if err != nil {117 log.Fatalf("Creating Account_Tag table failed: %s", err)118 }119 //CURRENCIES120 createDB = `121 CREATE TABLE IF NOT EXISTS currencies (122 name VARCHAR(255) NOT NULL,123 decimals INT NOT NULL,124 PRIMARY KEY(name)125 );`126 log.Debug("Query: " + createDB)127 _, err = db.DB.Exec(createDB)128 if err != nil {129 log.Fatal(err)130 }131 //TRANSACTIONS132 createDB = `133 CREATE TABLE IF NOT EXISTS transactions (134 transaction_id VARCHAR(255) NOT NULL,135 postdate DATETIME NOT NULL,136 description VARCHAR(255),137 poster_user_id VARCHAR(255),138 PRIMARY KEY(transaction_id),139 FOREIGN KEY (poster_user_id) REFERENCES users (user_id) ON DELETE RESTRICT ON UPDATE CASCADE140 );`141 log.Debug("Query: " + createDB)142 _, err = db.DB.Exec(createDB)143 if err != nil {144 log.Fatal(err)145 }146 //TRANSACTIONS BODY147 createDB = `148 CREATE TABLE IF NOT EXISTS transactions_body (149 transaction_id VARCHAR(255) NOT NULL,150 body TEXT,151 FOREIGN KEY(transaction_id) REFERENCES transactions(transaction_id) ON DELETE CASCADE ON UPDATE CASCADE152 );`153 log.Debug("Query: " + createDB)154 _, err = db.DB.Exec(createDB)155 if err != nil {156 log.Fatal(err)157 }158 //TAGS FOR Transactions159 createDB = `160 CREATE TABLE IF NOT EXISTS transaction_tag (161 transaction_id VARCHAR(255) NOT NULL,162 tag_id INTEGER NOT NULL,163 FOREIGN KEY (transaction_id) REFERENCES transactions (transaction_id) ON DELETE CASCADE ON UPDATE CASCADE,164 FOREIGN KEY (tag_id) REFERENCES tags (tag_id) ON DELETE CASCADE ON UPDATE CASCADE,165 PRIMARY KEY (transaction_id, tag_id)166 );`167 log.Debug("Query: " + createDB)168 _, err = db.DB.Exec(createDB)169 if err != nil {170 log.Fatalf("Creating Transaction_Tag table failed: %s", err)171 }172 //LINE ITEMS FOR TRANSACTIONS (SPLITS)173 createDB = `174 CREATE TABLE IF NOT EXISTS splits (175 split_id VARCHAR(255) NOT NULL,176 split_date DATETIME,177 description VARCHAR(255),178 currency VARCHAR(255),179 amount BIGINT,180 transaction_id VARCHAR(255),181 FOREIGN KEY(transaction_id) REFERENCES transactions(transaction_id) ON DELETE CASCADE ON UPDATE CASCADE,182 PRIMARY KEY(split_id)183 );`184 log.Debug("Query: " + createDB)185 _, err = db.DB.Exec(createDB)186 if err != nil {187 log.Fatal(err)188 }189 //ACCOUNTS FOR SPLITS190 createDB = `191 CREATE TABLE IF NOT EXISTS split_accounts (192 id INT AUTO_INCREMENT PRIMARY KEY,193 split_id VARCHAR(255),194 account_id VARCHAR(255),195 FOREIGN KEY(split_id) REFERENCES splits(split_id) ON DELETE CASCADE ON UPDATE CASCADE,196 FOREIGN KEY(account_id) REFERENCES accounts(account_id) ON DELETE RESTRICT ON UPDATE CASCADE197 );`198 log.Debug("Query: " + createDB)199 _, err = db.DB.Exec(createDB)200 if err != nil {201 log.Fatal(err)202 }203 //RECONCILIATIONS204 createDB = `205 CREATE TABLE IF NOT EXISTS reconciliations (206 reconciliation_id VARCHAR(255) NOT NULL,207 split_id VARCHAR(255) NOT NULL,208 FOREIGN KEY (split_id) REFERENCES splits (split_id) ON DELETE CASCADE ON UPDATE CASCADE,209 PRIMARY KEY (reconciliation_id, split_id)210 );`211 log.Debug("Query: " + createDB)212 _, err = db.DB.Exec(createDB)213 if err != nil {214 log.Fatal(err)215 }216 //ENTITIES217 createDB = `218 CREATE TABLE IF NOT EXISTS entities (219 entity_id VARCHAR(255) NOT NULL,220 name VARCHAR(255) NOT NULL,221 tag VARCHAR(255),222 type VARCHAR(255),223 description VARCHAR(255),224 PRIMARY KEY(entity_id)225 );`226 log.Debug("Query: " + createDB)227 _, err = db.DB.Exec(createDB)228 if err != nil {229 log.Fatal(err)230 }231 //Default Currencies232 insertCurrency := `233 INSERT INTO currencies(name,decimals)234 VALUES("USD",2),235 ("AUD",2),236 ("GBP",2),237 ("BTC",8),238 ("ETH",18),239 ("OXEN",9);240 `241 log.Debug("Query: " + insertCurrency)242 _, _ = db.DB.Exec(insertCurrency)243 return err244}245// ClearDB drops all tables246func (db *Database) ClearDB() error {247 //DROP TABLES248 dropDB := `249 DROP DATABASE ledger;250 `251 log.Debug("Query: " + dropDB)252 _, err := db.DB.Exec(dropDB)253 if err != nil {254 log.Fatalf("Dropping table failed with: %s", err)255 return err256 }257 //CREATE NEW DATABASE258 newDB := `259 CREATE DATABASE ledger;260 `261 log.Debug("Query: " + newDB)262 _, err = db.DB.Exec(newDB)263 if err != nil {264 log.Fatalf("Creating table failed with: %s", err)265 return err266 }267 //USE NEW DATABASE268 newDB = `269 USE ledger;270 `271 log.Debug("Query: " + newDB)272 _, err = db.DB.Exec(newDB)273 if err != nil {274 log.Fatalf("Creating table failed with: %s", err)275 return err276 }277 return nil278}...
sqlite3db.go
Source:sqlite3db.go
1package sqlite3db2import (3 "database/sql"4 "fmt"5 "os"6 "path"7 "github.com/sirupsen/logrus"8 _ "github.com/mattn/go-sqlite3"9)10const ledgerDBName = "ledger.db"11var log = logrus.WithField("prefix", "SQLLite")12type Database struct {13 DB *sql.DB14 DatabasePath string15 Mode string16}17// Close closes the underlying database.18func (db *Database) Close() error {19 return db.DB.Close()20}21// NewDB initializes a new DB.22func NewDB(dirPath, mode string) (*Database, error) {23 log.Debug("Creating DB")24 if err := os.MkdirAll(dirPath, 0770); err != nil {25 return nil, err26 }27 datafile := path.Join(dirPath, fmt.Sprintf("%s?_foreign_keys=true&parseTime=true&mode=%s", ledgerDBName, mode))28 if mode == "memory" {29 datafile = fmt.Sprintf("%s?_foreign_keys=true&parseTime=true&mode=%s", ":memory:", mode)30 }31 log.WithField("datafile", datafile).Debug("Opening SQLite3 Datafile")32 SqliteDB, err := sql.Open("sqlite3", datafile)33 if err != nil {34 return nil, err35 }36 db := &Database{DB: SqliteDB, DatabasePath: dirPath, Mode: mode}37 return db, err38}39func (db *Database) InitDB() error {40 log.Debug("Initialising DB Table")41 //USERS42 createDB := `43 CREATE TABLE IF NOT EXISTS users (44 user_id INT NOT NULL,45 username VARCHAR(255) NOT NULL,46 PRIMARY KEY(user_id)47 );`48 log.Debug("Query: " + createDB)49 _, err := db.DB.Exec(createDB)50 if err != nil {51 log.Fatal(err)52 }53 //ACCOUNTS54 createDB = `55 CREATE TABLE IF NOT EXISTS accounts (56 account_id VARCHAR(255) NOT NULL,57 name VARCHAR(255) NOT NULL,58 PRIMARY KEY(account_id)59 );`60 log.Debug("Query: " + createDB)61 _, err = db.DB.Exec(createDB)62 if err != nil {63 log.Fatal(err)64 }65 //TAGS66 createDB = `67 CREATE TABLE IF NOT EXISTS tags (68 tag_id INTEGER PRIMARY KEY,69 tag_name VARCHAR(100) NOT NULL UNIQUE70 );`71 log.Debug("Query: " + createDB)72 _, err = db.DB.Exec(createDB)73 if err != nil {74 log.Fatal(err)75 }76 //TAGS FOR ACCOUNTS77 createDB = `78 CREATE TABLE IF NOT EXISTS account_tag (79 account_id VARCHAR(255) NOT NULL,80 tag_id INTEGER NOT NULL,81 FOREIGN KEY (account_id) REFERENCES accounts (account_id) ON DELETE CASCADE ON UPDATE CASCADE,82 FOREIGN KEY (tag_id) REFERENCES tags (tag_id) ON DELETE CASCADE ON UPDATE CASCADE,83 PRIMARY KEY (account_id, tag_id)84 );`85 log.Debug("Query: " + createDB)86 _, err = db.DB.Exec(createDB)87 if err != nil {88 log.Fatal(err)89 }90 //CURRENCIES91 createDB = `92 CREATE TABLE IF NOT EXISTS currencies (93 name VARCHAR(255) NOT NULL,94 decimals INT NOT NULL,95 PRIMARY KEY(name)96 );`97 log.Debug("Query: " + createDB)98 _, err = db.DB.Exec(createDB)99 if err != nil {100 log.Fatal(err)101 }102 //TRANSACTIONS103 createDB = `104 CREATE TABLE IF NOT EXISTS transactions (105 transaction_id VARCHAR(255) NOT NULL,106 postdate DATETIME NOT NULL,107 description VARCHAR(255),108 poster_user_id VARCHAR(255),109 PRIMARY KEY(transaction_id),110 FOREIGN KEY (poster_user_id) REFERENCES users (user_id) ON DELETE RESTRICT ON UPDATE CASCADE111 );`112 log.Debug("Query: " + createDB)113 _, err = db.DB.Exec(createDB)114 if err != nil {115 log.Fatal(err)116 }117 //TRANSACTIONS BODY118 createDB = `119 CREATE TABLE IF NOT EXISTS transactions_body (120 transaction_id VARCHAR(255) NOT NULL,121 body TEXT,122 FOREIGN KEY(transaction_id) REFERENCES transactions(transaction_id) ON DELETE CASCADE ON UPDATE CASCADE123 );`124 log.Debug("Query: " + createDB)125 _, err = db.DB.Exec(createDB)126 if err != nil {127 log.Fatal(err)128 }129 //TAGS FOR Transactions130 createDB = `131 CREATE TABLE IF NOT EXISTS transaction_tag (132 transaction_id VARCHAR(255) NOT NULL,133 tag_id INTEGER NOT NULL,134 FOREIGN KEY (transaction_id) REFERENCES transactions (transaction_id) ON DELETE RESTRICT ON UPDATE CASCADE,135 FOREIGN KEY (tag_id) REFERENCES tags (tag_id) ON DELETE CASCADE ON UPDATE CASCADE,136 PRIMARY KEY (transaction_id, tag_id)137 );`138 log.Debug("Query: " + createDB)139 _, err = db.DB.Exec(createDB)140 if err != nil {141 log.Fatal(err)142 }143 //LINE ITEMS FOR TRANSACTIONS (SPLITS)144 createDB = `145 CREATE TABLE IF NOT EXISTS splits (146 split_id VARCHAR(255) NOT NULL,147 split_date DATETIME,148 description VARCHAR(255),149 currency VARCHAR(255),150 amount BIGINT,151 transaction_id VARCHAR(255),152 FOREIGN KEY(transaction_id) REFERENCES transactions(transaction_id) ON DELETE CASCADE ON UPDATE CASCADE,153 PRIMARY KEY(split_id)154 );`155 log.Debug("Query: " + createDB)156 _, err = db.DB.Exec(createDB)157 if err != nil {158 log.Fatal(err)159 }160 //ACCOUNTS FOR SPLITS161 createDB = `162 CREATE TABLE IF NOT EXISTS split_accounts (163 id INT AUTO_INCREMENT PRIMARY KEY,164 split_id VARCHAR(255),165 account_id VARCHAR(255),166 FOREIGN KEY(split_id) REFERENCES splits(split_id) ON DELETE CASCADE ON UPDATE CASCADE,167 FOREIGN KEY(account_id) REFERENCES accounts(account_id) ON DELETE RESTRICT ON UPDATE CASCADE168 );`169 log.Debug("Query: " + createDB)170 _, err = db.DB.Exec(createDB)171 if err != nil {172 log.Fatal(err)173 }174 //RECONCILIATIONS175 createDB = `176 CREATE TABLE IF NOT EXISTS reconciliations (177 reconciliation_id VARCHAR(255) NOT NULL,178 split_id VARCHAR(255) NOT NULL,179 FOREIGN KEY (split_id) REFERENCES splits (split_id) ON DELETE CASCADE ON UPDATE CASCADE,180 PRIMARY KEY (reconciliation_id, split_id)181 );`182 log.Debug("Query: " + createDB)183 _, err = db.DB.Exec(createDB)184 if err != nil {185 log.Fatal(err)186 }187 //ENTITIES188 createDB = `189 CREATE TABLE IF NOT EXISTS entities (190 entity_id VARCHAR(255) NOT NULL,191 name VARCHAR(255) NOT NULL,192 tag VARCHAR(255),193 type VARCHAR(255),194 description VARCHAR(255),195 PRIMARY KEY(entity_id)196 );`197 log.Debug("Query: " + createDB)198 _, err = db.DB.Exec(createDB)199 if err != nil {200 log.Fatal(err)201 }202 //Default Currencies203 insertCurrency := `204 INSERT INTO currencies(name,decimals)205 VALUES("USD",2),206 ("AUD",2),207 ("GBP",2),208 ("BTC",8),209 ("ETH",18),210 ("OXEN",9);211 `212 log.Debug("Query: " + insertCurrency)213 _, _ = db.DB.Exec(insertCurrency)214 return err215}216// ClearDB removes the previously stored directory at the data directory.217func ClearDB(dirPath string) error {218 if _, err := os.Stat(dirPath); os.IsNotExist(err) {219 return nil220 }221 return os.RemoveAll(dirPath)222}...
Create
Using AI Code Generation
1import (2type User struct {3}4func main() {5 db, err := beedb.NewSqlite3Db("test.db")6 if err != nil {7 panic(err)8 }9 defer db.Close()10 user := new(User)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!