query

package
v0.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommonTemplates = map[string]*Template{

	"project_overview": {
		Name:        "Project Overview",
		Description: "Get basic statistics about a project",
		Category:    "overview",
		Query: `MATCH (n) WHERE n.project_id = $project_id
		RETURN 
		  labels(n)[0] as node_type,
		  count(n) as count
		ORDER BY count DESC`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},
	"project_files": {
		Name:        "List Project Files",
		Description: "List all files in a project with their package information",
		Category:    "overview",
		Query: `MATCH (f:File) WHERE f.project_id = $project_id
		RETURN f.path as file_path, f.name as file_name, f.package as package_name
		ORDER BY f.package, f.name`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},
	"project_packages": {
		Name:        "List Project Packages",
		Description: "List all packages in a project with file counts",
		Category:    "overview",
		Query: `MATCH (f:File) WHERE f.project_id = $project_id
		RETURN f.package as package_name, count(f) as file_count
		ORDER BY file_count DESC, package_name`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},

	"functions_by_package": {
		Name:        "Functions by Package",
		Description: "List all functions grouped by package",
		Category:    "functions",
		Query: `MATCH (f:Function) WHERE f.project_id = $project_id
		RETURN f.package as package_name, f.name as function_name, 
		       f.signature as signature, f.is_exported as is_exported
		ORDER BY f.package, f.name`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},
	"exported_functions": {
		Name:        "Exported Functions",
		Description: "List all exported functions in a project",
		Category:    "functions",
		Query: `MATCH (f:Function) 
		WHERE f.project_id = $project_id AND f.is_exported = true
		RETURN f.package as package_name, f.name as function_name, f.signature as signature
		ORDER BY f.package, f.name`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},
	"function_complexity": {
		Name:        "Function Complexity",
		Description: "List functions ordered by complexity (line count)",
		Category:    "functions",
		Query: `MATCH (f:Function) WHERE f.project_id = $project_id
		WITH f, (f.line_end - f.line_start) as complexity
		RETURN f.package as package_name, f.name as function_name, 
		       complexity, f.signature as signature
		ORDER BY complexity DESC
		LIMIT 20`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},

	"package_dependencies": {
		Name:        "Package Dependencies",
		Description: "Show dependencies between packages",
		Category:    "dependencies",
		Query: `MATCH (f1:File)-[:DEPENDS_ON]->(f2:File) 
		WHERE f1.project_id = $project_id
		RETURN DISTINCT f1.package as from_package, f2.package as to_package
		ORDER BY from_package, to_package`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},
	"external_dependencies": {
		Name:        "External Dependencies",
		Description: "List external package imports",
		Category:    "dependencies",
		Query: `MATCH (i:Import) WHERE i.project_id = $project_id
		AND NOT i.path STARTS WITH "."
		RETURN DISTINCT i.path as external_package, count(*) as usage_count
		ORDER BY usage_count DESC, external_package`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},
	"dependency_graph": {
		Name:        "Dependency Graph",
		Description: "Full dependency graph with relationships",
		Category:    "dependencies",
		Query: `MATCH (f1:File)-[r:DEPENDS_ON]->(f2:File) 
		WHERE f1.project_id = $project_id
		RETURN f1.path as from_file, f2.path as to_file, type(r) as relationship_type`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},

	"interface_implementations": {
		Name:        "Interface Implementations",
		Description: "List all interfaces and their implementations",
		Category:    "types",
		Query: `MATCH (s:Struct)-[:IMPLEMENTS]->(i:Interface) 
		WHERE s.project_id = $project_id
		RETURN i.package as interface_package, i.name as interface_name,
		       s.package as struct_package, s.name as struct_name
		ORDER BY i.name, s.name`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},
	"unimplemented_interfaces": {
		Name:        "Unimplemented Interfaces",
		Description: "Find interfaces with no implementations",
		Category:    "types",
		Query: `MATCH (i:Interface) WHERE i.project_id = $project_id
		AND NOT EXISTS {
		  MATCH (s:Struct)-[:IMPLEMENTS]->(i)
		}
		RETURN i.package as package_name, i.name as interface_name
		ORDER BY i.package, i.name`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},
	"struct_methods": {
		Name:        "Struct Methods",
		Description: "List all structs and their methods",
		Category:    "types",
		Query: `MATCH (s:Struct)-[:HAS_METHOD]->(m:Function) 
		WHERE s.project_id = $project_id
		RETURN s.package as struct_package, s.name as struct_name,
		       m.name as method_name, m.signature as method_signature
		ORDER BY s.name, m.name`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},

	"function_calls": {
		Name:        "Function Call Relationships",
		Description: "Show which functions call which other functions",
		Category:    "calls",
		Query: `MATCH (f1:Function)-[:CALLS]->(f2:Function) 
		WHERE f1.project_id = $project_id
		RETURN f1.package as caller_package, f1.name as caller_name,
		       f2.package as callee_package, f2.name as callee_name
		ORDER BY caller_package, caller_name`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},
	"most_called_functions": {
		Name:        "Most Called Functions",
		Description: "Functions ordered by how often they are called",
		Category:    "calls",
		Query: `MATCH (f:Function)<-[:CALLS]-() WHERE f.project_id = $project_id
		RETURN f.package as package_name, f.name as function_name, 
		       count(*) as call_count, f.signature as signature
		ORDER BY call_count DESC
		LIMIT 20`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},
	"unused_functions": {
		Name:        "Unused Functions",
		Description: "Functions that are never called (potential dead code)",
		Category:    "calls",
		Query: `MATCH (f:Function) WHERE f.project_id = $project_id
		AND NOT EXISTS {
		  MATCH ()-[:CALLS]->(f)
		}
		AND f.name <> "main" AND f.name <> "init"
		RETURN f.package as package_name, f.name as function_name, f.signature as signature
		ORDER BY f.package, f.name`,
		Parameters: map[string]string{
			"project_id": "string - The project identifier",
		},
	},

	"find_function": {
		Name:        "Find Function by Name",
		Description: "Search for functions by name (case-insensitive)",
		Category:    "search",
		Query: `MATCH (f:Function) WHERE f.project_id = $project_id
		AND toLower(f.name) CONTAINS toLower($function_name)
		RETURN f.package as package_name, f.name as function_name, 
		       f.signature as signature, f.is_exported as is_exported
		ORDER BY f.package, f.name`,
		Parameters: map[string]string{
			"project_id":    "string - The project identifier",
			"function_name": "string - Function name to search for",
		},
	},
	"find_struct": {
		Name:        "Find Struct by Name",
		Description: "Search for structs by name (case-insensitive)",
		Category:    "search",
		Query: `MATCH (s:Struct) WHERE s.project_id = $project_id
		AND toLower(s.name) CONTAINS toLower($struct_name)
		RETURN s.package as package_name, s.name as struct_name, s.is_exported as is_exported
		ORDER BY s.package, s.name`,
		Parameters: map[string]string{
			"project_id":  "string - The project identifier",
			"struct_name": "string - Struct name to search for",
		},
	},
	"find_interface": {
		Name:        "Find Interface by Name",
		Description: "Search for interfaces by name (case-insensitive)",
		Category:    "search",
		Query: `MATCH (i:Interface) WHERE i.project_id = $project_id
		AND toLower(i.name) CONTAINS toLower($interface_name)
		RETURN i.package as package_name, i.name as interface_name, i.is_exported as is_exported
		ORDER BY i.package, i.name`,
		Parameters: map[string]string{
			"project_id":     "string - The project identifier",
			"interface_name": "string - Interface name to search for",
		},
	},
	"search_code": {
		Name:        "Search in Code",
		Description: "Search for text patterns in function signatures",
		Category:    "search",
		Query: `MATCH (f:Function) WHERE f.project_id = $project_id
		AND toLower(f.signature) CONTAINS toLower($search_term)
		RETURN f.package as package_name, f.name as function_name, f.signature as signature
		ORDER BY f.package, f.name`,
		Parameters: map[string]string{
			"project_id":  "string - The project identifier",
			"search_term": "string - Text to search for in function signatures",
		},
	},
}

CommonTemplates contains frequently used query templates

Functions

func ListTemplates

func ListTemplates() map[string][]*Template

ListTemplates returns all available templates grouped by category

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder provides a fluent interface for building Cypher queries

func NewBuilder

func NewBuilder() *Builder

NewBuilder creates a new query builder

func (*Builder) And

func (b *Builder) And(condition string) *Builder

And adds an AND condition to the WHERE clause

func (*Builder) Build

func (b *Builder) Build() (string, map[string]any, error)

Build returns the final query and parameters

func (*Builder) Create

func (b *Builder) Create(pattern string) *Builder

Create adds a CREATE clause

func (*Builder) Delete

func (b *Builder) Delete(nodes string) *Builder

Delete adds a DELETE clause

func (*Builder) DetachDelete

func (b *Builder) DetachDelete(nodes string) *Builder

DetachDelete adds a DETACH DELETE clause

func (*Builder) Limit

func (b *Builder) Limit(count int) *Builder

Limit adds a LIMIT clause

func (*Builder) Match

func (b *Builder) Match(pattern string) *Builder

Match adds a MATCH clause to the query

func (*Builder) Merge

func (b *Builder) Merge(pattern string) *Builder

Merge adds a MERGE clause

func (*Builder) OptionalMatch

func (b *Builder) OptionalMatch(pattern string) *Builder

OptionalMatch adds an OPTIONAL MATCH clause

func (*Builder) Or

func (b *Builder) Or(condition string) *Builder

Or adds an OR condition to the WHERE clause

func (*Builder) OrderBy

func (b *Builder) OrderBy(fields string) *Builder

OrderBy adds an ORDER BY clause

func (*Builder) ProjectFilter

func (b *Builder) ProjectFilter(projectID core.ID) *Builder

ProjectFilter adds a project ID filter condition

func (*Builder) Return

func (b *Builder) Return(fields string) *Builder

Return adds a RETURN clause

func (*Builder) Set

func (b *Builder) Set(assignments string) *Builder

Set adds a SET clause

func (*Builder) SetParameter

func (b *Builder) SetParameter(name string, value any) *Builder

SetParameter adds a parameter to the query

func (*Builder) SetParameters

func (b *Builder) SetParameters(params map[string]any) *Builder

SetParameters adds multiple parameters to the query

func (*Builder) Skip

func (b *Builder) Skip(count int) *Builder

Skip adds a SKIP clause

func (*Builder) String

func (b *Builder) String() string

String returns the query string

func (*Builder) Where

func (b *Builder) Where(condition string) *Builder

Where adds a WHERE clause

func (*Builder) With

func (b *Builder) With(fields string) *Builder

With adds a WITH clause

type ExportFormat

type ExportFormat string

ExportFormat represents the export format

const (
	FormatJSON ExportFormat = "json"
	FormatCSV  ExportFormat = "csv"
	FormatTSV  ExportFormat = "tsv"
)

type ExportOptions

type ExportOptions struct {
	Format      ExportFormat `json:"format"`
	Pretty      bool         `json:"pretty"`       // For JSON: pretty formatting
	Headers     bool         `json:"headers"`      // For CSV/TSV: include headers
	Delimiter   string       `json:"delimiter"`    // For CSV/TSV: custom delimiter
	NullValue   string       `json:"null_value"`   // How to represent null values
	BoolFormat  string       `json:"bool_format"`  // "true/false" or "1/0"
	DateFormat  string       `json:"date_format"`  // Date formatting
	IncludeNull bool         `json:"include_null"` // Whether to include null fields in JSON
}

ExportOptions contains options for exporting query results

func DefaultExportOptions

func DefaultExportOptions(format ExportFormat) *ExportOptions

DefaultExportOptions returns default export options

type ExportResult

type ExportResult struct {
	Format      ExportFormat `json:"format"`
	RowCount    int          `json:"row_count"`
	ColumnCount int          `json:"column_count"`
	Size        int64        `json:"size"`
	Error       string       `json:"error,omitempty"`
}

ExportResult represents the result of an export operation

type Exporter

type Exporter struct {
	// contains filtered or unexported fields
}

Exporter handles exporting query results to different formats

func NewExporter

func NewExporter(options *ExportOptions) *Exporter

NewExporter creates a new exporter with the specified options

func (*Exporter) Export

func (e *Exporter) Export(writer io.Writer, results []map[string]any) error

Export exports the query results to the specified writer

func (*Exporter) ExportWithMetadata

func (e *Exporter) ExportWithMetadata(writer io.Writer, results []map[string]any) (*ExportResult, error)

ExportWithMetadata exports results and returns metadata about the export

type HighLevelBuilder

type HighLevelBuilder struct{}

HighLevelBuilder provides high-level query building methods for common patterns

func NewHighLevelBuilder

func NewHighLevelBuilder() *HighLevelBuilder

NewHighLevelBuilder creates a new high-level query builder

func (*HighLevelBuilder) CountNodesByType

func (hlb *HighLevelBuilder) CountNodesByType(projectID core.ID) *Builder

CountNodesByType creates a query to count nodes by type

func (*HighLevelBuilder) CountRelationshipsByType

func (hlb *HighLevelBuilder) CountRelationshipsByType(projectID core.ID) *Builder

CountRelationshipsByType creates a query to count relationships by type

func (*HighLevelBuilder) FindCircularDependencies

func (hlb *HighLevelBuilder) FindCircularDependencies(projectID core.ID) *Builder

FindCircularDependencies creates a query to detect circular dependencies

func (*HighLevelBuilder) FindComplexFunctions

func (hlb *HighLevelBuilder) FindComplexFunctions(projectID core.ID, minComplexity int) *Builder

FindComplexFunctions creates a query to find functions with high complexity

func (*HighLevelBuilder) FindDependencies

func (hlb *HighLevelBuilder) FindDependencies(nodeID core.ID, projectID core.ID) *Builder

FindDependencies creates a query to find dependencies for a specific node

func (*HighLevelBuilder) FindDependents

func (hlb *HighLevelBuilder) FindDependents(nodeID core.ID, projectID core.ID) *Builder

FindDependents creates a query to find what depends on a specific node

func (*HighLevelBuilder) FindInterfaceImplementations

func (hlb *HighLevelBuilder) FindInterfaceImplementations(projectID core.ID) *Builder

FindInterfaceImplementations creates a query to find interface implementations

func (*HighLevelBuilder) FindMostCalledFunctions

func (hlb *HighLevelBuilder) FindMostCalledFunctions(projectID core.ID, limit int) *Builder

FindMostCalledFunctions creates a query to find the most called functions

func (*HighLevelBuilder) FindNodesByName

func (hlb *HighLevelBuilder) FindNodesByName(namePattern string, projectID core.ID) *Builder

FindNodesByName creates a query to find nodes by name pattern

func (*HighLevelBuilder) FindNodesByType

func (hlb *HighLevelBuilder) FindNodesByType(nodeType core.NodeType, projectID core.ID) *Builder

FindNodesByType creates a query to find nodes by type and project

func (*HighLevelBuilder) FindPath

func (hlb *HighLevelBuilder) FindPath(fromID, toID core.ID, projectID core.ID) *Builder

FindPath creates a query to find the shortest path between two nodes

func (*HighLevelBuilder) FindRelationshipsByType

func (hlb *HighLevelBuilder) FindRelationshipsByType(relType core.RelationType, projectID core.ID) *Builder

FindRelationshipsByType creates a query to find relationships by type

func (*HighLevelBuilder) FindUnusedFunctions

func (hlb *HighLevelBuilder) FindUnusedFunctions(projectID core.ID) *Builder

FindUnusedFunctions creates a query to find potentially unused functions

type ResultProcessor

type ResultProcessor struct{}

ResultProcessor provides utilities for processing query results

func NewResultProcessor

func NewResultProcessor() *ResultProcessor

NewResultProcessor creates a new result processor

func (*ResultProcessor) Aggregate

func (rp *ResultProcessor) Aggregate(
	results []map[string]any,
	groupKey string,
	aggregator func([]map[string]any) map[string]any,
) []map[string]any

Aggregate aggregates results by grouping key

func (*ResultProcessor) Filter

func (rp *ResultProcessor) Filter(
	results []map[string]any,
	predicate func(map[string]any) bool,
) []map[string]any

Filter filters results based on a predicate function

func (*ResultProcessor) Flatten

func (rp *ResultProcessor) Flatten(results []map[string]any, separator string) []map[string]any

Flatten flattens nested maps in query results

func (*ResultProcessor) SortBy

func (rp *ResultProcessor) SortBy(results []map[string]any, field string, ascending bool) []map[string]any

SortBy sorts results by the specified field

func (*ResultProcessor) Transform

func (rp *ResultProcessor) Transform(
	results []map[string]any,
	transformer func(map[string]any) map[string]any,
) []map[string]any

Transform transforms results using a transformer function

type Template

type Template struct {
	Name        string            `json:"name"`
	Description string            `json:"description"`
	Query       string            `json:"query"`
	Parameters  map[string]string `json:"parameters"`
	Category    string            `json:"category"`
}

Template represents a query template with parameters

func GetTemplate

func GetTemplate(name string) (*Template, error)

GetTemplate retrieves a template by name

func GetTemplatesByCategory

func GetTemplatesByCategory(category string) []*Template

GetTemplatesByCategory returns templates for a specific category

func (*Template) BuildQuery

func (t *Template) BuildQuery(params map[string]any) (string, error)

BuildQuery builds the final query with parameter substitution validation

func (*Template) GetParameterHelp

func (t *Template) GetParameterHelp() string

GetParameterHelp returns help text for template parameters

func (*Template) ValidateParameters

func (t *Template) ValidateParameters(params map[string]any) error

ValidateParameters checks if all required parameters are provided

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL