Skip to content

Commit

Permalink
persisted important entitiy into database💿
Browse files Browse the repository at this point in the history
  • Loading branch information
shivasurya committed Feb 24, 2025
1 parent 17d989f commit b7ac6f7
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 3 deletions.
4 changes: 4 additions & 0 deletions sourcecode-parser/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ func (s *StorageNode) AddMethodDecl(node *model.Method) {
s.MethodDecl = append(s.MethodDecl, node)

Check warning on line 186 in sourcecode-parser/db/db.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/db/db.go#L185-L186

Added lines #L185 - L186 were not covered by tests
}

func (s *StorageNode) AddMethodCall(node *model.MethodCall) {
s.MethodCall = append(s.MethodCall, node)

Check warning on line 190 in sourcecode-parser/db/db.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/db/db.go#L189-L190

Added lines #L189 - L190 were not covered by tests
}

func (s *StorageNode) AddFieldDecl(node *model.FieldDeclaration) {
s.Field = append(s.Field, node)

Check warning on line 194 in sourcecode-parser/db/db.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/db/db.go#L193-L194

Added lines #L193 - L194 were not covered by tests
}
Expand Down
22 changes: 22 additions & 0 deletions sourcecode-parser/model/expr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"database/sql"
"fmt"
"strings"

Expand Down Expand Up @@ -59,6 +60,12 @@ type BinaryExpr struct {
RightOperand *Expr
}

func (e *BinaryExpr) Insert(db *sql.DB) error {
query := "INSERT INTO binary_expr (binary_expr_name) VALUES (?)"
_, err := db.Exec(query, e.NodeString)
return err

Check warning on line 66 in sourcecode-parser/model/expr.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/expr.go#L63-L66

Added lines #L63 - L66 were not covered by tests
}

func (e *BinaryExpr) GetLeftOperand() *Expr {
return e.LeftOperand
}
Expand Down Expand Up @@ -498,6 +505,12 @@ type MethodCall struct {
IsOwnMethodCall bool // Whether this is a call to an instance method of 'this'
}

func (m *MethodCall) Insert(db *sql.DB) error {
query := `INSERT INTO method_call (method_name) VALUES (?)`
_, err := db.Exec(query, m.MethodName)
return err

Check warning on line 511 in sourcecode-parser/model/expr.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/expr.go#L508-L511

Added lines #L508 - L511 were not covered by tests
}

// NewMethodCall initializes a new MethodCall instance.
func NewMethodCall(primaryQlClass string, methodName string, qualifiedMethod string, arguments []string, typeArguments []string, qualifier string, receiverType string, enclosingCallable string, enclosingStmt string, hasQualifier bool, isEnclosingCall bool, isOwnMethodCall bool) *MethodCall {
return &MethodCall{
Expand Down Expand Up @@ -615,6 +628,15 @@ type FieldDeclaration struct {
SourceDeclaration string // Location of the field declaration
}

func (f *FieldDeclaration) Insert(db *sql.DB) error {
query := `
INSERT INTO field_decl (field_name)
VALUES (?)
`
_, err := db.Exec(query, f.FieldNames[0])
return err

Check warning on line 637 in sourcecode-parser/model/expr.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/expr.go#L631-L637

Added lines #L631 - L637 were not covered by tests
}

// NewFieldDeclaration initializes a new FieldDeclaration instance.
func NewFieldDeclaration(fieldType string, fieldNames []string, visibility string, isStatic, isFinal, isVolatile, isTransient bool, sourceDeclaration string) *FieldDeclaration {
return &FieldDeclaration{
Expand Down
26 changes: 25 additions & 1 deletion sourcecode-parser/model/import.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
package model

import "fmt"
import (
"database/sql"
"fmt"
)

// ImportType represents a single-type import declaration in Java.
type ImportType struct {
ImportedType string // The fully qualified name of the imported type
SourceDeclaration string // Location of the import statement
}

func (it *ImportType) Insert(db *sql.DB) error {
query := `
INSERT INTO import_decl (
import_type,
import_name,
file_path
) VALUES (?, ?, ?)
`
stmt, err := db.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()

_, err = stmt.Exec(it.ImportedType, it.ImportedType, it.SourceDeclaration)
if err != nil {
return err
}
return nil

Check warning on line 32 in sourcecode-parser/model/import.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/import.go#L14-L32

Added lines #L14 - L32 were not covered by tests
}

// NewImportType initializes a new ImportType instance.
func NewImportType(importedType, sourceDeclaration string) *ImportType {
return &ImportType{
Expand Down
11 changes: 11 additions & 0 deletions sourcecode-parser/model/package.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package model

import "database/sql"

// Package represents a Java package, grouping multiple types.
type Package struct {
QualifiedName string // Fully qualified package name (e.g., "com.example")
Expand All @@ -9,6 +11,15 @@ type Package struct {
URL string // Dummy URL for the package (for debugging or references)
}

func (p *Package) Insert(db *sql.DB) error {
query := `INSERT INTO packages (package_name) VALUES (?)`
_, err := db.Exec(query, p.QualifiedName)
if err != nil {
return err
}
return nil

Check warning on line 20 in sourcecode-parser/model/package.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/package.go#L14-L20

Added lines #L14 - L20 were not covered by tests
}

// NewPackage initializes a new Package instance.
func NewPackage(qualifiedName string, topLevelTypes []string, fromSource bool, metrics string, url string) *Package {
return &Package{
Expand Down
25 changes: 24 additions & 1 deletion sourcecode-parser/model/reftype.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package model

import "strings"
import (
"database/sql"
"strings"
)

// Modifiable represents a Java syntax element that may have modifiers.
type Modifiable struct {
Expand Down Expand Up @@ -338,6 +341,26 @@ type Class struct {
IsFileClass bool // Whether this is a Kotlin file class (e.g., FooKt for Foo.kt)
}

func (c *Class) Insert(db *sql.DB) error {
query := `
INSERT INTO class_decl (
class_name,
package_name)
VALUES (?, ?)
`

stmt, err := db.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(c.ClassOrInterface.QualifiedName, c.ClassOrInterface.Package)
if err != nil {
return err
}
return nil

Check warning on line 361 in sourcecode-parser/model/reftype.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/reftype.go#L344-L361

Added lines #L344 - L361 were not covered by tests
}

// NewClass initializes a new Class instance.
func NewClass(primaryQlClass string, annotations []string, isAnonymous bool, isFileClass bool, classOrInterface ClassOrInterface) *Class {
return &Class{
Expand Down
23 changes: 22 additions & 1 deletion sourcecode-parser/tree/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func buildQLTreeFromAST(node *sitter.Node, sourceCode []byte, file string, paren
case "binary_expression":
binaryExprNode := javalang.ParseExpr(node, sourceCode, file, parentNode)
parentNode.AddChild(&model.TreeNode{Node: &model.Node{BinaryExpr: binaryExprNode}, Parent: parentNode})
storageNode.AddBinaryExpr(binaryExprNode)
case "method_declaration":
methodDeclaration := javalang.ParseMethodDeclaration(node, sourceCode, file)
utilities.Log("Processing method:", methodDeclaration.Name, "in file:", file)
Expand All @@ -73,6 +74,7 @@ func buildQLTreeFromAST(node *sitter.Node, sourceCode []byte, file string, paren
methodInvokedNode := javalang.ParseMethodInvoker(node, sourceCode, file)
methodInvocationTreeNode := &model.TreeNode{Node: &model.Node{MethodCall: methodInvokedNode}, Parent: parentNode}
parentNode.AddChild(methodInvocationTreeNode)
storageNode.AddMethodCall(methodInvokedNode)
for i := 0; i < int(node.ChildCount()); i++ {
buildQLTreeFromAST(node.Child(i), sourceCode, file, methodInvocationTreeNode, storageNode)
}
Expand All @@ -81,6 +83,7 @@ func buildQLTreeFromAST(node *sitter.Node, sourceCode []byte, file string, paren
classNode := javalang.ParseClass(node, sourceCode, file)
classTreeNode := &model.TreeNode{Node: &model.Node{ClassDecl: classNode}, Children: nil, Parent: parentNode}
parentNode.AddChild(classTreeNode)
storageNode.AddClassDecl(classNode)
for i := 0; i < int(node.ChildCount()); i++ {
buildQLTreeFromAST(node.Child(i), sourceCode, file, classTreeNode, storageNode)
}
Expand Down Expand Up @@ -238,10 +241,28 @@ func Initialize(directory string) []*model.TreeNode {
close(progressChan)
close(treeChan)

// TODO:bulk insert into db
for _, packageDeclaration := range storageNode.Package {
packageDeclaration.Insert(storageNode.DB)
}
for _, importDeclaration := range storageNode.ImportDecl {
importDeclaration.Insert(storageNode.DB)
}
for _, classDeclaration := range storageNode.ClassDecl {
classDeclaration.Insert(storageNode.DB)
}
for _, fieldDeclaration := range storageNode.Field {
fieldDeclaration.Insert(storageNode.DB)
}
for _, methodDeclaration := range storageNode.MethodDecl {
methodDeclaration.Insert(storageNode.DB)
}
for _, methodCallDeclaration := range storageNode.MethodCall {
methodCallDeclaration.Insert(storageNode.DB)
}
for _, binaryExpression := range storageNode.BinaryExpr {
binaryExpression.Insert(storageNode.DB)
}

Check warning on line 264 in sourcecode-parser/tree/construct.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/tree/construct.go#L239-L264

Added lines #L239 - L264 were not covered by tests

storageNode.DB.Close()

end := time.Now()
Expand Down

0 comments on commit b7ac6f7

Please sign in to comment.