Browse Source

выделенный класс connector

toomanysugar 3 years ago
parent
commit
8312705bc8
2 changed files with 170 additions and 0 deletions
  1. 85 0
      connector.go
  2. 85 0
      dbconnector.go

+ 85 - 0
connector.go

@@ -0,0 +1,85 @@
+
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"encoding/json"
+	"database/sql"
+	_ "github.com/go-sql-driver/mysql"
+)
+
+type ConnectionProperties struct {
+	User 	string
+	Pass 	string
+	Host	string
+	Db 		string
+}
+
+type connection struct {
+	dBOpenStr string
+}
+
+func (c *connection) Init(filepath string){
+	b, err := ioutil.ReadFile(filepath)
+    if err != nil {
+      fmt.Print(err)
+    }
+    propJson := string(b)
+	var connProperties ConnectionProperties
+
+	json.Unmarshal([]byte(propJson), &connProperties)
+	fmt.Printf("Connection data:\n%s\n%s\n%s\n%s\n", connProperties.User, connProperties.Pass,connProperties.Host,connProperties.Db)
+	c.dBOpenStr = connProperties.User + ":" + connProperties.Pass + "@tcp(" + connProperties.Host + ")/" + connProperties.Db
+	fmt.Printf("Connecting with:\n%s\n", c.dBOpenStr)
+	db, err := sql.Open("mysql", c.dBOpenStr)
+
+	if err != nil {
+		panic(err)
+	}
+
+	defer db.Close()
+}
+
+func (c connection) LogIn(username string, password string) bool{
+	fmt.Printf("Connecting with:\n%s\n", c.dBOpenStr)
+	db, err := sql.Open("mysql", c.dBOpenStr)
+
+	if err != nil {
+		panic(err)
+	}
+
+	defer db.Close()
+
+	var counter int
+
+	act_query := "SELECT count(*) FROM gosite.users WHERE username='" + username + "' AND password='" + password + "';"
+	db.QueryRow(act_query).Scan(&counter)
+	fmt.Println("we have", counter, "rows")
+    
+    if (counter == 0) {
+    	return false
+    }
+	return true
+
+}
+
+/*
+func main() {
+
+	var dBConnector connection
+
+	dBConnector.Init("config.json")
+	if (dBConnector.LogIn("Alex", "09Alex09")) {
+		fmt.Printf("Succesfull logIn\n")
+	} else {
+		fmt.Printf("logIn error\n")
+	}
+
+	if (dBConnector.LogIn("Alax", "09Alex09")) {
+		fmt.Printf("Succesfull logIn\n")
+	} else {
+		fmt.Printf("logIn error\n")
+	}
+}
+*/

+ 85 - 0
dbconnector.go

@@ -0,0 +1,85 @@
+
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"encoding/json"
+	"database/sql"
+	_ "github.com/go-sql-driver/mysql"
+)
+
+type ConnectionProperties struct {
+	User 	string
+	Pass 	string
+	Host	string
+	Db 		string
+}
+
+type connection struct {
+	dBOpenStr string
+}
+
+func (c *connection) Init(filepath string){
+	b, err := ioutil.ReadFile(filepath)
+    if err != nil {
+      fmt.Print(err)
+    }
+    propJson := string(b)
+	var connProperties ConnectionProperties
+
+	json.Unmarshal([]byte(propJson), &connProperties)
+	fmt.Printf("Connection data:\n%s\n%s\n%s\n%s\n", connProperties.User, connProperties.Pass,connProperties.Host,connProperties.Db)
+	c.dBOpenStr = connProperties.User + ":" + connProperties.Pass + "@tcp(" + connProperties.Host + ")/" + connProperties.Db
+	fmt.Printf("Connecting with:\n%s\n", c.dBOpenStr)
+	db, err := sql.Open("mysql", c.dBOpenStr)
+
+	if err != nil {
+		panic(err)
+	}
+
+	defer db.Close()
+}
+
+func (c connection) LogIn(username string, password string) bool{
+	fmt.Printf("Connecting with:\n%s\n", c.dBOpenStr)
+	db, err := sql.Open("mysql", c.dBOpenStr)
+
+	if err != nil {
+		panic(err)
+	}
+
+	defer db.Close()
+
+	var counter int
+
+	act_query := "SELECT count(*) FROM gosite.users WHERE username='" + username + "' AND password='" + password + "';"
+	db.QueryRow(act_query).Scan(&counter)
+	fmt.Println("we have", counter, "rows")
+    
+    if (counter == 0) {
+    	return false
+    }
+	return true
+
+}
+
+/*
+func main() {
+
+	var dBConnector connection
+
+	dBConnector.Init("config.json")
+	if (dBConnector.LogIn("Alex", "09Alex09")) {
+		fmt.Printf("Succesfull logIn\n")
+	} else {
+		fmt.Printf("logIn error\n")
+	}
+
+	if (dBConnector.LogIn("Alax", "09Alex09")) {
+		fmt.Printf("Succesfull logIn\n")
+	} else {
+		fmt.Printf("logIn error\n")
+	}
+}
+*/