about summary refs log tree commit diff
path: root/backend/new.go
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-03-07 16:14:12 +0100
committerEmile <hanemile@protonmail.com>2019-03-07 16:14:12 +0100
commitc8ca77ad65e9c0b31c6bd5289de0c1d332c06e63 (patch)
treeead406696f0c02a6b51e49a7dbcbbd1afce848c4 /backend/new.go
parent4d7880421ddc732d2f9fd3a2eaf1ca2c22a485c6 (diff)
subdivided the project into multiple logical compartments
Diffstat (limited to 'backend/new.go')
-rw-r--r--backend/new.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/backend/new.go b/backend/new.go
new file mode 100644
index 0000000..615a2e7
--- /dev/null
+++ b/backend/new.go
@@ -0,0 +1,48 @@
+package backend
+
+import (
+	"database/sql"
+	"fmt"
+	"log"
+)
+
+// newTree creates a new tree with the given width
+func NewTree(database *sql.DB, width float64) {
+	db = database
+
+	log.Printf("Creating a new tree with a width of %f", width)
+
+	// get the current max root id
+	query := fmt.Sprintf("SELECT COALESCE(max(root_id), 0) FROM nodes")
+	var currentMaxRootID int64
+	err := db.QueryRow(query).Scan(&currentMaxRootID)
+	if err != nil {
+		log.Fatalf("[ E ] max root id query: %v\n\t\t\t query: %s\n", err, query)
+	}
+
+	// build the query creating a new node
+	query = fmt.Sprintf("INSERT INTO nodes (box_width, root_id, box_center, depth, isleaf, timestep) VALUES (%f, %d, '{0, 0}', 0, TRUE, %d)", width, currentMaxRootID+1, currentMaxRootID+1)
+
+	// execute the query
+	rows, err := db.Query(query)
+	defer rows.Close()
+	if err != nil {
+		log.Fatalf("[ E ] insert new node query: %v\n\t\t\t query: %s\n", err, query)
+	}
+}
+
+// newNode Inserts a new node into the database with the given parameters
+func newNode(x float64, y float64, width float64, depth int64, timestep int64) int64 {
+	// build the query creating a new node
+	query := fmt.Sprintf("INSERT INTO nodes (box_center, box_width, depth, isleaf, timestep) VALUES ('{%f, %f}', %f, %d, TRUE, %d) RETURNING node_id", x, y, width, depth, timestep)
+
+	var nodeID int64
+
+	// execute the query
+	err := db.QueryRow(query).Scan(&nodeID)
+	if err != nil {
+		log.Fatalf("[ E ] newNode query: %v\n\t\t\t query: %s\n", err, query)
+	}
+
+	return nodeID
+}