README ¶
GoHANA is an ORM library for HANA Databases. We aim to port it to the GORM library in the future and add new features.
This library is a port of https://github.com/shwetasrivastava/Ohana so Kudos to Shweta Srivastava!
Feel free to open issues and create pull requests to contribute to project!
Supported operations and features
- Find(table, columns, conditions)
- FindOne(table, columns, conditions)
- Insert(table, inteface)
- Delete(table, conditions)
- Update(table, conditions, interface)
- Avg(table, column, conditions)
- Count(table, column, conditions)
- Max(table, column, conditions)
- Min(table, column, conditions)
- Raw(query)
- Sum(table, column, conditions)
How to use it?
- Install from pkg.go.dev
go get github.com/YonchevSimeon/gohana/gohana
- Setup the connection
Please create an .env file and add your environment variables
package main
import (
"log"
"os"
"github.com/YonchevSimeon/gohana/gohana"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
}
func main() {
gohana := &gohana.Instance{}
host := os.Getenv("SAP_HDB_URL")
port := os.Getenv("SAP_HDB_PORT")
username := os.Getenv("SAP_HDB_UNAME")
password := os.Getenv("SAP_HDB_PWD")
gohana.Connect(host, port, username, password)
defer gohana.Disconnect()
}
How to use them?
- Find/FindOne
func main() {
...
table := "hotels"
columns := []string{"HOTEL_ID", "NAME"}
conditions := make(map[string]string)
conditions["HOTEL_ID"] = "1"
results, err := gohana.Find(table, columns, conditions)
if err != nil {
//handle error
fmt.Println(err)
}
fmt.Println(results)
}
- Insert
// First we have to create our structure of the relevant table, in this case will be "Hotel"
type Hotel struct {
NAME string
CITY string
}
func main() {
...
hotel := Hotel{NAME: "Hotel California", CITY: "Todos Santos"}
table := "hotels"
rowsAffected, err := gohana.Insert(table, hotel)
if err != nil {
// handle error
fmt.Println(err)
}
fmt.Println(rowsAffected)
}
- Delete
func main() {
...
// Keep in mind that if you don't specify any conditions
// it will delete EVERYTHING from the given table
table := "hotels"
conditions := make(map[string]string)
conditions["HOTEL_ID"] = "1"
rowsAffected, err := gohana.Delete(table, conditions)
if err != nil {
// handle error
fmt.Println(err)
}
fmt.Println(rowsAffected)
}
- Update
// First we have to create our structure of the relevant table, in this case will be "Hotel"
type Hotel struct {
NAME string
CITY string
}
func main() {
...
table := "hotels"
hotel := Hotel{NAME: "Hotel California Updated", CITY: "Todos Santos Updated"}
conditions := make(map[string]string)
conditions["HOTEL_ID"] = "1"
rowsAffected, err := gohana.Update(table, conditions, hotel)
if err != nil {
//handle error
fmt.Println(err)
}
fmt.Println(rowsAffected)
}
- Avg/Min/Max/Sum
func main() {
...
column := "HOTEL_ID"
results, err := gohana.Avg(table, column, conditions)
if err != nil {
//handle error
fmt.Println(err)
}
fmt.Println(results)
// For methods Min/Max/Sum is exactly the same
}
- Raw
// Here we will use raw query to create new column table named "demo"
func main() {
...
query := "create column table demo (demo_id int primary key GENERATED BY DEFAULT as identity, name nvarchar(255));"
sqlRows, err := gohana.Raw(query)
// handle sqlRows
}
Click to show internal directories.
Click to hide internal directories.