Files
relspecgo/examples/pgsql_reader_example.go
Hein db6cd21511
Some checks are pending
CI / Build (push) Waiting to run
CI / Test (1.23) (push) Waiting to run
CI / Test (1.24) (push) Waiting to run
CI / Test (1.25) (push) Waiting to run
CI / Lint (push) Waiting to run
Added more examples and pgsql reader
2025-12-17 10:08:50 +02:00

97 lines
2.6 KiB
Go

package main
import (
"fmt"
"log"
"git.warky.dev/wdevs/relspecgo/pkg/readers"
"git.warky.dev/wdevs/relspecgo/pkg/readers/pgsql"
)
func main() {
// Example PostgreSQL connection string
// Format: postgres://username:password@localhost:5432/database_name
connectionString := "postgres://user:password@localhost:5432/mydb"
// Create reader options
options := &readers.ReaderOptions{
ConnectionString: connectionString,
}
// Create PostgreSQL reader
reader := pgsql.NewReader(options)
// Read the entire database
db, err := reader.ReadDatabase()
if err != nil {
log.Fatalf("Failed to read database: %v", err)
}
// Display database information
fmt.Printf("Database: %s\n", db.Name)
fmt.Printf("Type: %s\n", db.DatabaseType)
fmt.Printf("Version: %s\n", db.DatabaseVersion)
fmt.Printf("Schemas: %d\n\n", len(db.Schemas))
// Iterate through schemas
for _, schema := range db.Schemas {
fmt.Printf("Schema: %s\n", schema.Name)
fmt.Printf(" Tables: %d\n", len(schema.Tables))
fmt.Printf(" Views: %d\n", len(schema.Views))
fmt.Printf(" Sequences: %d\n", len(schema.Sequences))
// Display table details
for _, table := range schema.Tables {
fmt.Printf(" Table: %s.%s\n", schema.Name, table.Name)
fmt.Printf(" Columns: %d\n", len(table.Columns))
fmt.Printf(" Constraints: %d\n", len(table.Constraints))
fmt.Printf(" Indexes: %d\n", len(table.Indexes))
fmt.Printf(" Relationships: %d\n", len(table.Relationships))
// Display columns
for _, col := range table.Columns {
fmt.Printf(" - %s: %s", col.Name, col.Type)
if col.IsPrimaryKey {
fmt.Printf(" [PK]")
}
if col.NotNull {
fmt.Printf(" NOT NULL")
}
fmt.Println()
}
}
// Display view details
for _, view := range schema.Views {
fmt.Printf(" View: %s.%s\n", schema.Name, view.Name)
fmt.Printf(" Columns: %d\n", len(view.Columns))
fmt.Printf(" Definition: %s\n", view.Definition)
}
// Display sequence details
for _, seq := range schema.Sequences {
fmt.Printf(" Sequence: %s.%s\n", schema.Name, seq.Name)
fmt.Printf(" Start: %d, Increment: %d\n", seq.StartValue, seq.IncrementBy)
if seq.OwnedByTable != "" {
fmt.Printf(" Owned by: %s.%s\n", seq.OwnedByTable, seq.OwnedByColumn)
}
}
fmt.Println()
}
// Example: Read just the first schema
schema, err := reader.ReadSchema()
if err != nil {
log.Fatalf("Failed to read schema: %v", err)
}
fmt.Printf("First schema: %s\n", schema.Name)
// Example: Read just the first table
table, err := reader.ReadTable()
if err != nil {
log.Fatalf("Failed to read table: %v", err)
}
fmt.Printf("First table: %s.%s\n", table.Schema, table.Name)
}