Skip to content

added support for RemoveVertex, UpdateEdge, and EdgeCount #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jonbrandenburg
Copy link

This is an attempt to update the library to support the latest changes / graph.Store interface. I recognize this PR might be more palatable if it were broken down into smaller chunks and/or reworked however I thought I'd post it now to get some feedback before doing any more work on it.

@dominikbraun dominikbraun self-requested a review July 18, 2023 05:33
@ngfgrant
Copy link

ngfgrant commented Sep 7, 2023

This looks ok to me @dominikbraun is it worth merging in?

@dominikbraun
Copy link
Owner

@ngfgrant Absolutely, I haven't had the time yet but I'm going to merge @jonbrandenburg's PRs in this and the main repo soon.

@ngfgrant
Copy link

Nice one - let me know if I can help in any way 👍

@nicocesar
Copy link

nicocesar commented Nov 20, 2024

Update on this? I also bumped into the same issue.

quick hack on your go.mod, use jonbrandenburg patched version:

module nicograph

go 1.23.3

require (
	github.com/dominikbraun/graph v0.23.0
	github.com/dominikbraun/graph-sql v0.0.0-20241120000000-d8bb785eba28
	github.com/mattn/go-sqlite3 v1.14.24
)

require (
	github.com/Masterminds/squirrel v1.5.3 // indirect
	github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
	github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
)

replace github.com/dominikbraun/graph-sql => github.com/jonbrandenburg/graph-sql v0.0.0-20230718002458-d8bb785eba28

I'll leave here a snippet of code that won't compile yet. When this gets merged it will be easy to check:

package main

import (
	"database/sql"
	"log"

	"github.com/dominikbraun/graph"
	graphsql "github.com/dominikbraun/graph-sql"
	_ "github.com/mattn/go-sqlite3" // SQLite driver
)

// Room represents a room in the house
type Room struct {
	Name        string
	Description string
}

func main() {
	// Open the SQLite database file
	db, err := sql.Open("sqlite3", "database.sqlite")
	if err != nil {
		panic(err)
	}
	defer db.Close() // Ensure the database connection is closed when done

	// Set up the graph-sql store with SQLite
	store := graphsql.New[string, Room](db, graphsql.DefaultConfig)

	// Create tables in the SQLite database
	if err = store.SetupTables(); err != nil {
		log.Fatal(err)
	}

	// Initialize a graph with the SQLite store
	roomHash := func(r Room) string {
		return r.Name
	}

	// Check if the store implements the graph.Store interface. If not, the
	// program will not compile, and will have a better error message than the
	// below.
	var _ graph.Store[string, Room] = (*graphsql.Store[string, Room])(nil)

	g := graph.NewWithStore(roomHash, store)

	// Define the rooms in the house
	kitchen := Room{Name: "Kitchen", Description: "Where meals are prepared"}
	livingRoom := Room{Name: "Living Room", Description: "Where family gathers"}
	bedroom := Room{Name: "Bedroom", Description: "Where you sleep"}
	bathroom := Room{Name: "Bathroom", Description: "Where personal hygiene is maintained"}
	hallway := Room{Name: "Hallway", Description: "Connects the rooms"}

	// Add the rooms (vertices) to the graph
	_ = g.AddVertex(kitchen)
	_ = g.AddVertex(livingRoom)
	_ = g.AddVertex(bedroom)
	_ = g.AddVertex(bathroom)
	_ = g.AddVertex(hallway)

	// Define the connections (edges) between rooms
	_ = g.AddEdge(kitchen, hallway)
	_ = g.AddEdge(livingRoom, hallway)
	_ = g.AddEdge(bedroom, hallway)
	_ = g.AddEdge(bathroom, hallway)
	_ = g.AddEdge(kitchen, livingRoom)

	log.Println("House graph created with rooms and connections!")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants