about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-02-13 13:09:54 +0100
committerEmile <hanemile@protonmail.com>2019-02-13 13:09:54 +0100
commitbbed0542a975bdb1a67aadabea7a54b580fcbd08 (patch)
tree2b845c617d069726433d540f97ae82a72dcfa594
parentee709e790d5866b3f5d5ef70b015729c522e63a6 (diff)
moved the db_actions to an own package and adjusted the functions so that the database gets reached into the function for usage
-rw-r--r--db_actions/db_actions.go (renamed from db_actions.go)50
-rw-r--r--http.go42
-rw-r--r--main.go3
3 files changed, 53 insertions, 42 deletions
diff --git a/db_actions.go b/db_actions/db_actions.go
index b09af68..561ce56 100644
--- a/db_actions.go
+++ b/db_actions/db_actions.go
@@ -14,7 +14,7 @@
 // You should have received a copy of the GNU General Public License
 // along with this program. If not, see <https://www.gnu.org/licenses/>.
 
-package main
+package db_actions
 
 import (
 	"database/sql"
@@ -36,8 +36,12 @@ const (
 	DBSSLMODE = "disable"
 )
 
+var (
+	db *sql.DB
+)
+
 // connectToDB returns a pointer to an sql database writing to the database
-func connectToDB() *sql.DB {
+func ConnectToDB() *sql.DB {
 	connStr := fmt.Sprintf("user=%s dbname=%s sslmode=%s", DBUSER, DBNAME, DBSSLMODE)
 	db := dbConnect(connStr)
 	return db
@@ -55,7 +59,8 @@ func dbConnect(connStr string) *sql.DB {
 }
 
 // newTree creates a new tree with the given width
-func newTree(width float64) {
+func NewTree(database *sql.DB, width float64) {
+	db = database
 	// get the current max root id
 	query := fmt.Sprintf("SELECT COALESCE(max(root_id), 0) FROM nodes")
 	var currentMaxRootID int64
@@ -76,7 +81,8 @@ func newTree(width float64) {
 }
 
 // insertStar inserts the given star into the stars table and the nodes table tree
-func insertStar(star structs.Star2D, index int64) {
+func InsertStar(database *sql.DB, star structs.Star2D, index int64) {
+	db = database
 	start := time.Now()
 	// insert the star into the stars table
 	starID := insertIntoStars(star)
@@ -348,7 +354,8 @@ func getStarID(nodeID int64) int64 {
 }
 
 // deleteAll Stars deletes all the rows in the stars table
-func deleteAllStars() {
+func DeleteAllStars(database *sql.DB) {
+	db = database
 	// build the query creating a new node
 	query := "DELETE FROM stars WHERE TRUE"
 
@@ -361,7 +368,8 @@ func deleteAllStars() {
 }
 
 // deleteAll Stars deletes all the rows in the nodes table
-func deleteAllNodes() {
+func DeleteAllNodes(database *sql.DB) {
+	db = database
 	// build the query creating a new node
 	query := "DELETE FROM nodes WHERE TRUE"
 
@@ -511,7 +519,8 @@ func removeStarFromNode(nodeID int64) {
 }
 
 // getListOfStarsGo returns the list of stars in go struct format
-func getListOfStarsGo() []structs.Star2D {
+func GetListOfStarsGo(database *sql.DB) []structs.Star2D {
+	db = database
 	// build the query
 	query := fmt.Sprintf("SELECT * FROM stars")
 
@@ -527,9 +536,9 @@ func getListOfStarsGo() []structs.Star2D {
 	// iterate over the returned rows
 	for rows.Next() {
 
-		var star_id int64
+		var starID int64
 		var x, y, vx, vy, m float64
-		scanErr := rows.Scan(&star_id, &x, &y, &vx, &vy, &m)
+		scanErr := rows.Scan(&starID, &x, &y, &vx, &vy, &m)
 		if scanErr != nil {
 			log.Fatalf("[ E ] scan error: %v", scanErr)
 		}
@@ -553,7 +562,8 @@ func getListOfStarsGo() []structs.Star2D {
 }
 
 // getListOfStarsCsv returns an array of strings containing the coordinates of all the stars in the stars table
-func getListOfStarsCsv() []string {
+func GetListOfStarsCsv(database *sql.DB) []string {
+	db = database
 	// build the query
 	query := fmt.Sprintf("SELECT * FROM stars")
 
@@ -584,7 +594,8 @@ func getListOfStarsCsv() []string {
 }
 
 // insertList inserts all the stars in the given .csv into the stars and nodes table
-func insertList(filename string) {
+func InsertList(database *sql.DB, filename string) {
+	db = database
 	// open the file
 	content, readErr := ioutil.ReadFile(filename)
 	if readErr != nil {
@@ -622,7 +633,7 @@ func insertList(filename string) {
 		}
 
 		fmt.Printf("Inserting (%f, %f)\n", star.C.X, star.C.Y)
-		insertStar(star, 1)
+		InsertStar(db, star, 1)
 	}
 }
 
@@ -640,7 +651,8 @@ func getRootNodeID(index int64) int64 {
 }
 
 // updateTotalMass gets a tree index and returns the nodeID of the trees root node
-func updateTotalMass(index int64) {
+func UpdateTotalMass(database *sql.DB, index int64) {
+	db = database
 	rootNodeID := getRootNodeID(index)
 	log.Printf("RootID: %d", rootNodeID)
 	updateTotalMassNode(rootNodeID)
@@ -695,7 +707,8 @@ func updateTotalMassNode(nodeID int64) float64 {
 
 // updateCenterOfMass recursively updates the center of mass of all the nodes starting at the node with the given
 // root index
-func updateCenterOfMass(index int64) {
+func UpdateCenterOfMass(database *sql.DB, index int64) {
+	db = database
 	rootNodeID := getRootNodeID(index)
 	log.Printf("RootID: %d", rootNodeID)
 	updateCenterOfMassNode(rootNodeID)
@@ -769,12 +782,6 @@ func updateCenterOfMassNode(nodeID int64) structs.Vec2 {
 				X: centerOfMassX,
 				Y: centerOfMassY,
 			}
-			//centerOfMassX := star.C.X * star.M
-			//centerOfMassY := star.C.Y * star.M
-			//centerOfMass = structs.Vec2{
-			//	X: centerOfMassX / star.M,
-			//	Y: centerOfMassY / star.M,
-			//}
 		}
 	}
 
@@ -794,7 +801,8 @@ func updateCenterOfMassNode(nodeID int64) structs.Vec2 {
 }
 
 // genForestTree generates a forest representation of the tree with the given index
-func genForestTree(index int64) string {
+func GenForestTree(database *sql.DB, index int64) string {
+	db = database
 	rootNodeID := getRootNodeID(index)
 	return genForestTreeNode(rootNodeID)
 }
diff --git a/http.go b/http.go
index d19e1a0..7df8ed0 100644
--- a/http.go
+++ b/http.go
@@ -17,6 +17,8 @@
 package main
 
 import (
+	"database/sql"
+	"git.darknebu.la/GalaxySimulator/db-container/db_actions"
 	"git.darknebu.la/GalaxySimulator/structs"
 )
 
@@ -48,48 +50,48 @@ API:
 }
 
 // newTree creates a new tree
-func newTreeEndpoint(width float64) {
-	newTree(width)
+func newTreeEndpoint(db *sql.DB, width float64) {
+	db_actions.NewTree(db, width)
 }
 
 // insertStarEndpoint inserts the star into the tree with the given index
-func insertStarEndpoint(star structs.Star2D, index int64) {
-	insertStar(star, index)
+func insertStarEndpoint(db *sql.DB, star structs.Star2D, index int64) {
+	db_actions.InsertStar(db, star, index)
 }
 
 // insertListEndpoint inserts the star into the tree with the given index
-func insertListEndpoint(filename string) {
-	insertList(filename)
+func insertListEndpoint(db *sql.DB, filename string) {
+	db_actions.InsertList(db, filename)
 }
 
 // deleteStarsEndpoint deletes all the rows from the stars table
-func deleteStarsEndpoint() {
-	deleteAllStars()
+func deleteStarsEndpoint(db *sql.DB) {
+	db_actions.DeleteAllStars(db)
 }
 
 // deleteNodesEndpoint deletes all the rows from the nodes table
-func deleteNodesEndpoint() {
-	deleteAllNodes()
+func deleteNodesEndpoint(db *sql.DB) {
+	db_actions.DeleteAllNodes(db)
 }
 
-func listOfStarsGoEndpoint() []structs.Star2D {
-	listOfStars := getListOfStarsGo()
+func listOfStarsGoEndpoint(db *sql.DB) []structs.Star2D {
+	listOfStars := db_actions.GetListOfStarsGo(db)
 	return listOfStars
 }
 
-func listOfStarsCsvEndpoint() []string {
-	listOfStars := getListOfStarsCsv()
+func listOfStarsCsvEndpoint(db *sql.DB) []string {
+	listOfStars := db_actions.GetListOfStarsCsv(db)
 	return listOfStars
 }
 
-func updateTotalMassEndpoint(index int64) {
-	updateTotalMass(index)
+func updateTotalMassEndpoint(db *sql.DB, index int64) {
+	db_actions.UpdateTotalMass(db, index)
 }
 
-func updateCenterOfMassEndpoint(index int64) {
-	updateCenterOfMass(index)
+func updateCenterOfMassEndpoint(db *sql.DB, index int64) {
+	db_actions.UpdateCenterOfMass(db, index)
 }
 
-func genForestTreeEndpoint(index int64) string {
-	return genForestTree(index)
+func genForestTreeEndpoint(db *sql.DB, index int64) string {
+	return db_actions.GenForestTree(db, index)
 }
diff --git a/main.go b/main.go
index 07fbc36..cca88a0 100644
--- a/main.go
+++ b/main.go
@@ -19,6 +19,7 @@ package main
 import (
 	"database/sql"
 	"fmt"
+	"git.darknebu.la/GalaxySimulator/db-container/db_actions"
 	"git.darknebu.la/GalaxySimulator/structs"
 	"log"
 	"net/http"
@@ -197,7 +198,7 @@ func main() {
 	//router.HandleFunc("/export/{treeindex}", exportHandler).Methods("POST")
 	//router.HandleFunc("/nrofgalaxies", nrofgalaxiesHandler).Methods("GET")
 
-	db = connectToDB()
+	db = db_actions.ConnectToDB()
 	db.SetMaxOpenConns(75)
 
 	fmt.Println("Database Container up on port 8081")