Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "strconv"
- "github.com/jmoiron/sqlx"
- _ "github.com/lib/pq"
- )
- func main() {
- var db *sqlx.DB
- db, _ = sqlx.Open("postgres", "user=test password=test dbname=eavvsjsonb")
- // Create the EAV tables
- db.MustExec(`
- CREATE TABLE entity (
- id SERIAL PRIMARY KEY,
- name TEXT,
- description TEXT
- );`)
- db.MustExec(`
- CREATE TABLE entity_attribute (
- id SERIAL PRIMARY KEY,
- name TEXT,
- description TEXT
- );`)
- db.MustExec(`
- CREATE TABLE entity_attribute_value (
- id SERIAL PRIMARY KEY,
- entity_id INT references entity(id),
- entity_attribute_id INT references entity_attribute(id),
- value TEXT
- );`)
- // Create an 'all-in-one' table for JSONB
- db.MustExec(`
- CREATE TABLE entity_jsonb (
- id SERIAL PRIMARY KEY,
- name TEXT,
- description TEXT,
- properties JSONB
- );`)
- // Create some attributes for EAV
- var colorId int
- db.Get(&colorId, "INSERT INTO entity_attribute (name, description) VALUES ('color', 'The color of the entity') RETURNING id;")
- var lengthId int
- db.Get(&lengthId, "INSERT INTO entity_attribute (name, description) VALUES ('lenght', 'The lenght of the entity') RETURNING id;")
- var widthId int
- db.Get(&widthId, "INSERT INTO entity_attribute (name, description) VALUES ('width', 'The width of the entity') RETURNING id;")
- var hasSomethingId int
- db.Get(&hasSomethingId, "INSERT INTO entity_attribute (name, description) VALUES ('hassomething', 'A bool expressing if the entity has something') RETURNING id;")
- var countryId int
- db.Get(&countryId, "INSERT INTO entity_attribute (name, description) VALUES ('country', 'The home country of the entity') RETURNING id;")
- // InsertSomeEntities
- for i := 0; i < 1000000; i++ {
- e := getRandomEntity(i)
- // Insert for EAV
- var eId int
- db.Get(&eId, "INSERT INTO entity (name, description) VALUES ($1, $2) RETURNING id;", e.Name, e.Description)
- db.Exec("INSERT INTO entity_attribute_value (entity_id, entity_attribute_id, value) VALUES ($1, $2, $3);", eId, colorId, e.Properties["color"])
- db.Exec("INSERT INTO entity_attribute_value (entity_id, entity_attribute_id, value) VALUES ($1, $2, $3);", eId, lengthId, e.Properties["length"])
- db.Exec("INSERT INTO entity_attribute_value (entity_id, entity_attribute_id, value) VALUES ($1, $2, $3);", eId, widthId, e.Properties["width"])
- db.Exec("INSERT INTO entity_attribute_value (entity_id, entity_attribute_id, value) VALUES ($1, $2, $3);", eId, hasSomethingId, e.Properties["hassomething"])
- db.Exec("INSERT INTO entity_attribute_value (entity_id, entity_attribute_id, value) VALUES ($1, $2, $3);", eId, countryId, e.Properties["country"])
- // Insert for jsonb
- db.NamedExec(`INSERT INTO entity_jsonb (name, description, properties) VALUES (:name, :description, :properties);`, e)
- // Report progress...
- if i%1000 == 0 {
- fmt.Println("Progress:", float64(i)/10000.0, "%")
- }
- }
- // var test []Entity
- // db.Select(&test, `SELECT e.id, e.name, e.description FROM entity e
- // INNER JOIN entity_attribute_value eav ON eav.entity_id = e.id
- // INNER JOIN entity_attribute ea ON ea.id = eav.entity_attribute_id
- // WHERE ea.name = 'hassomething' and eav.value = 'true';`)
- // fmt.Println(test)
- // drop all tables
- //db.MustExec("DROP TABLE IF EXISTS entity_jsonb;")
- //db.MustExec("DROP TABLE IF EXISTS entity_attribute_value;")
- //db.MustExec("DROP TABLE IF EXISTS entity_attribute;")
- //db.MustExec("DROP TABLE IF EXISTS entity;")
- }
- func getRandomEntity(i int) (e Entity) {
- name := "Entity" + strconv.Itoa(i)
- description := "Entity number " + strconv.Itoa(i)
- length := i % 10
- width := 50.3285 / float64(length)
- hassomething := false
- if i%7 == 0 {
- hassomething = true
- }
- color := "red"
- if i%3 == 0 {
- color = "green"
- }
- country := "Belgium"
- if i%7 == 0 || i%86 == 0 {
- country = "The Netherlands"
- }
- e = Entity{
- 0,
- name,
- description,
- PropertyMap{
- "color": color,
- "length": length,
- "width": width,
- "hassomething": hassomething,
- "country": country,
- },
- }
- return
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement