This is a Go-based ORM (Object Relational Mapping) project that simplifies database operations in Go applications. It offers a range of features such as session management, schema parsing, transaction support, and migrations, making it easier to interact with databases.
- Session Management: The
Sessionstruct is responsible for managing database operations, including SQL construction and execution. - Schema Parsing: The
schemapackage can convert Go structs into database table schemas, handling field types and tags. - Transaction Support: The
Enginestruct supports transaction operations to ensure data consistency. - Migrations: The
Engineprovides a migration function to handle changes in table structures.
To establish a database connection, you can use the NewEngine function. Here is an example of connecting to a MySQL database:
package main
import (
"log"
"go-orm"
)
func main() {
// Replace "your_database_source" with your actual database connection string
engine, err := go-orm.NewEngine("mysql", "your_database_source")
if err != nil {
log.Fatalf("Failed to connect to the database: %v", err)
}
defer engine.Close()
// Now you can start performing database operations
}To create a table in the database, you need to define a Go struct that represents the table structure and then use the Session to create the table.
package main
import (
"log"
"go-orm"
"go-orm/session"
)
type User struct {
Name string `go-orm:"PRIMARY KEY"`
Age int
}
func main() {
engine, err := go-orm.NewEngine("mysql", "your_database_source")
if err != nil {
log.Fatalf("Failed to connect to the database: %v", err)
}
defer engine.Close()
s := engine.NewSession()
// Drop the table if it exists
err = s.Model(&User{}).DropTable()
if err != nil {
log.Printf("Failed to drop table: %v", err)
}
// Create the table
err = s.Model(&User{}).CreateTable()
if err != nil {
log.Fatalf("Failed to create table: %v", err)
}
log.Println("Table created successfully")
}To insert data into the table, you can create an instance of the struct and use the Insert method of the Session.
package main
import (
"log"
"go-orm"
"go-orm/session"
)
type User struct {
Name string `go-orm:"PRIMARY KEY"`
Age int
}
func main() {
engine, err := go-orm.NewEngine("mysql", "your_database_source")
if err != nil {
log.Fatalf("Failed to connect to the database: %v", err)
}
defer engine.Close()
s := engine.NewSession()
// Insert a new user
_, err = s.Insert(&User{Name: "Alice", Age: 25})
if err != nil {
log.Fatalf("Failed to insert data: %v", err)
}
log.Println("Data inserted successfully")
}Transactions are used to ensure data consistency when performing multiple database operations. You can use the Transaction method of the Engine to execute a set of operations within a transaction.
package main
import (
"log"
"go-orm"
"go-orm/session"
)
type User struct {
Name string `go-orm:"PRIMARY KEY"`
Age int
}
func main() {
engine, err := go-orm.NewEngine("mysql", "your_database_source")
if err != nil {
log.Fatalf("Failed to connect to the database: %v", err)
}
defer engine.Close()
_, err = engine.Transaction(func(s *session.Session) (interface{}, error) {
// Create the table
err := s.Model(&User{}).CreateTable()
if err != nil {
return nil, err
}
// Insert a new user
_, err = s.Insert(&User{Name: "Bob", Age: 30})
if err != nil {
return nil, err
}
return nil, nil
})
if err != nil {
log.Fatalf("Transaction failed: %v", err)
}
log.Println("Transaction completed successfully")
}Migrations are used to handle changes in the table structure. You can use the Migrate method of the Engine to update the table structure.
package main
import (
"log"
"go-orm"
)
type User struct {
Name string `go-orm:"PRIMARY KEY"`
Age int
}
func main() {
engine, err := go-orm.NewEngine("mysql", "your_database_source")
if err != nil {
log.Fatalf("Failed to connect to the database: %v", err)
}
defer engine.Close()
err = engine.Migrate(&User{})
if err != nil {
log.Fatalf("Migration failed: %v", err)
}
log.Println("Migration completed successfully")
}The project includes a set of test cases to ensure the correctness of various functions. You can run the tests using the following command:
go test ./...go-orm/session: Contains code related to session management, including SQL construction and execution.go-orm/schema: Handles schema parsing and field value extraction.go-orm/clause: Manages the construction of SQL clauses.go-orm/engine: Manages database connections, transactions, and migrations.
If you wish to contribute to this project, please fork the repository, create a new branch, and submit a pull request.
This project is licensed under the MIT license. Please refer to the LICENSE file for details.