| package main | |
| /* | |
| This is just psuedo-code for an ORM that I am making. The goal is to make MySQL work quick and easy | |
| and minimize on the time spent on touching database stuff. | |
| */ | |
| import ( | |
| "fmt" | |
| "github.com/protosam/myorm" | |
| ) | |
| func main(){ | |
| // We register our JSON so that we don't have to push additional json config files later. | |
| // Some of the JSON is boilerplate text in the `id` column. Defaults are nil/false/""/0 | |
| myorm.EmbedTable("users", `{ | |
| "table_name": "users", | |
| "columns": { | |
| "id": { | |
| "type": "INT", | |
| "unique_key": false, | |
| "primary_key": true, | |
| "auto_increment": true, | |
| "join_frag": "" | |
| }, | |
| "name": { | |
| "type": "VARCHAR(25)" | |
| }, | |
| "email": { | |
| "type": "VARCHAR(255)" | |
| } | |
| }, | |
| "engine": "InnoDB" | |
| }`) | |
| // You can either call on pre-embedded table info, or use a path to a json config file | |
| // AutoMigrate will create new tables. It will only add new columns and setup indexes and autoincrement. | |
| myorm.AutoMigrate("users") | |
| // You can either call on pre-embedded table info, or use a path to a json config file | |
| user := myorm.Table("users") | |
| // Insert example | |
| user.Set("name", "John Smith") | |
| user.Set("email", "john.smith@anonymous.com") | |
| user.Create() // INSERT INTO users (`id`, `name`, `email`) VALUES (null, `John Smith`, `john.smith@anonymous.com`) | |
| // Select example. FindWhere just uses a SQL fragment after the WHERE clause. Your join fragment | |
| // will be inserted before WHERE. | |
| user.FindWhere("name = ? and email = ?", "John Smith", "john.smith@anonymous.com") | |
| fmt.Println("Count: ", user.Count()) | |
| for user.Next() { | |
| fmt.Println("Name: ", user.Get("name") ) | |
| // UPDATE examble, requires a primary key to work. | |
| user.Set("name", "Not John Smith") | |
| user.Save() // UPDATE users WHERE id = 1 SET name = "Not John Smith" | |
| // Delete him... | |
| user.Delete() // DELETE FROM users WHERE id = 1 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment