Skip to content

Commit

Permalink
Started to restructure and add more logic for caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnbdz committed Nov 20, 2024
1 parent 1ec0e97 commit dff8213
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 138 deletions.
35 changes: 35 additions & 0 deletions cache/database/types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
package database

// Errors
const (
ErrorClosingDatabase = "closing database"
ErrorDatabaseNotInitialized = "database not initialized"
)

// Data types
/*
SQLite 3 data type:
Declared Type Type Affinity Storage Class
INTEGER INTEGER INTEGER
TINYINT, SMALLINT INTEGER INTEGER
MEDIUMINT, BIGINT INTEGER INTEGER
INT INTEGER INTEGER
REAL, DOUBLE, FLOAT REAL REAL
NUMERIC, DECIMAL NUMERIC REAL or INTEGER (if possible)
TEXT TEXT TEXT
CHARACTER, VARCHAR TEXT TEXT
CLOB TEXT TEXT
BLOB BLOB BLOB
BOOLEAN NUMERIC INTEGER (1 for true, 0 for false)
DATE, DATETIME NUMERIC TEXT, REAL, or INTEGER depending on the format
*/
const (
DataTypeInteger = "INTEGER"
DataTypeTinyint = "TINYINT"
DataTypeBigInteger = "BIGINT"
DataTypeReal = "REAL"
DataTypeNumeric = "NUMERIC"
DataTypeDecimal = "DECIMAL"
DataTypeBoolean = "BOOLEAN"
DataTypeText = "TEXT"
DataTypeCharacter = "CHARACTER"
DataTypeVarchar = "VARCHAR"
DataTypeClob = "CLOB"
DataTypeBlob = "BLOB"
DataTypeDate = "DATE"
DataTypeDateTime = "DATETIME"
)

// Table is a basic representation in a struct of a table in a SQL DB
type Table struct {
Name string
Expand Down
31 changes: 31 additions & 0 deletions cache/parser/column/column.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package column

import "github.com/AmadlaOrg/hery/cache/database"

var (
// Id returns simple database column structure for `Id`
Id = database.Column{
ColumnName: "Id",
DataType: "string",
Default: `(
lower(hex(randomblob(4))
|| '-'
|| hex(randomblob(2))
|| '-4'
|| substr(hex(randomblob(2)), 2)
|| '-'
|| substr('89ab', abs(random() % 4) + 1, 1)
|| substr(hex(randomblob(2)), 2)
|| '-'
|| hex(randomblob(6)))
)`,
}
InsertDateTime = database.Column{
ColumnName: "InsertDateTime",
DataType: "DATETIME",
}
UpdateDateTime = database.Column{
ColumnName: "UpdateDateTime",
DataType: "DATETIME",
}
)
188 changes: 50 additions & 138 deletions cache/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
jsonMarshal = json.Marshal
)

// Parse
// Entity parses the entity to SQLite 3 struct that can be used in the query builder in the database package
func (s *SParser) Entity(entity entity.Entity) ([]database.Table, error) {
// TODO: Use schema to determine the data type for the SQL
// string == TEXT
Expand Down Expand Up @@ -63,34 +63,11 @@ func (s *SParser) Entity(entity entity.Entity) ([]database.Table, error) {
);
*/

/*
Convert JSON-Schema Types to SQLite 3 data types:
string.
number.
integer.
object.
array.
boolean.
null.
SQLite 3 data type:
Declared Type Type Affinity Storage Class
INTEGER INTEGER INTEGER
TINYINT, SMALLINT INTEGER INTEGER
MEDIUMINT, BIGINT INTEGER INTEGER
INT INTEGER INTEGER
REAL, DOUBLE, FLOAT REAL REAL
NUMERIC, DECIMAL NUMERIC REAL or INTEGER (if possible)
TEXT TEXT TEXT
CHARACTER, VARCHAR TEXT TEXT
CLOB TEXT TEXT
BLOB BLOB BLOB
BOOLEAN NUMERIC INTEGER (1 for true, 0 for false)
DATE, DATETIME NUMERIC TEXT, REAL, or INTEGER depending on the format
*/

schema := entity.Schema.Schema

// TODO:
//entity.Schema.CompiledSchema.Types

var (
dynamicColumns []database.Column
dynamicRelationships []database.Relationships
Expand All @@ -110,41 +87,13 @@ func (s *SParser) Entity(entity entity.Entity) ([]database.Table, error) {
continue // Skip if schemaPropertyValue is not a map
}

// Basic data type
var dataType string
if typeValue, ok := propertyDetails["type"].(string); ok {
switch typeValue {
case "string":
dataType = "TEXT"
case "number":
dataType = "BIGINT"
case "integer":
dataType = "BIGINT"
case "object":
dataType = "TEXT"
case "array":
dataType = "TEXT"
case "boolean":
dataType = "BOOLEAN"
case "null":
dataType = "TEXT" // SQLite has no direct equivalent; treat as NULL
default:
dataType = "TEXT"
}
}

// Change the data type if the "format" property is present
if formatValue, ok := propertyDetails["format"].(string); ok {
switch formatValue {
case "date-time":
dataType = "DATETIME"
case "time":
dataType = "TEXT"
case "date":
dataType = "DATE"
case "duration":
dataType = "TEXT"
}
dataType = parseJsonSchemaFormatToSQLiteType(formatValue)
} else if typeValue, ok := propertyDetails["type"].(string); ok {
dataType = parseJsonSchemaToSQLiteType(typeValue)
}

// Append the column definition
Expand Down Expand Up @@ -173,75 +122,51 @@ func (s *SParser) Entity(entity entity.Entity) ([]database.Table, error) {
dynamicRelationships = append(dynamicRelationships, database.Relationships{})
}*/

return []database.Table{
{},
{},
{
Name: s.EntityToTableName(entity.Uri),
Columns: []database.Column{
{},
},
},
}, nil
}

// EntityToTableName
func (s *SParser) EntityToTableName(entity string) string {
re := regexp.MustCompile(`[^a-zA-Z0-9]+`)
tableName := re.ReplaceAllString(entity, "_")
return strings.Trim(tableName, "_")
}

// ParseTable
func (s *SParser) DatabaseTable(data []byte) (entity.Entity, error) {
return entity.Entity{}, nil
}

// ParseRow
func (s *SParser) DatabaseRow(data []byte) (entity.Entity, error) {
return entity.Entity{}, nil
}

//
// Private methods
//

func (s *SParser) databaseInsertTableEntities(entity entity.Entity) (*[]database.Table, error) {
schema := entity.Schema.Schema
// Convert schema map[string]any into a JSON string for cache storage
schemaJsonBytes, err := jsonMarshal(schema)
if err != nil {
return nil, err
}
schemaJsonString := string(schemaJsonBytes)

return []database.Table{
return &[]database.Table{
{
Name: "Entities",
Columns: []database.Column{
{
ColumnName: "Id",
DataType: "string",
Default: "(lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)), 2) || '-' || substr('89ab', abs(random() % 4) + 1, 1) || substr(hex(randomblob(2)), 2) || '-' || hex(randomblob(6))))",
},
{
ColumnName: "Entity",
DataType: "string",
},
{
ColumnName: "Name",
DataType: "string",
},
{
ColumnName: "RepoUrl",
DataType: "string",
},
{
ColumnName: "Origin",
DataType: "string",
},
{
ColumnName: "Version",
DataType: "string",
},
{
ColumnName: "IsLatestVersion",
DataType: "bool",
},
{
ColumnName: "IsPseudoVersion",
DataType: "bool",
},
{
ColumnName: "AbsPath",
DataType: "string",
},
{
ColumnName: "Have",
DataType: "bool",
},
{
ColumnName: "Hash",
DataType: "string",
},
{
ColumnName: "Exist",
DataType: "bool",
},
{
ColumnName: "Schema",
DataType: "string",
},
{
ColumnName: "Content",
DataType: "string",
},
},
Rows: []map[string]any{
{
"Id": entity.Id.String(),
Expand All @@ -262,27 +187,14 @@ func (s *SParser) Entity(entity entity.Entity) ([]database.Table, error) {
},
},
{
Name: s.EntityToTableName(entity.Uri),
Columns: []database.Column{
{},
Name: "Ids",
Rows: []map[string]any{
{
"Id": entity.Id.String(),
"Uri": entity.Uri,
"Name": entity.Name,
},
},
},
}, nil
}

// EntityToTableName
func (s *SParser) EntityToTableName(entity string) string {
re := regexp.MustCompile(`[^a-zA-Z0-9]+`)
tableName := re.ReplaceAllString(entity, "_")
return strings.Trim(tableName, "_")
}

// ParseTable
func (s *SParser) DatabaseTable(data []byte) (entity.Entity, error) {
return entity.Entity{}, nil
}

// ParseRow
func (s *SParser) DatabaseRow(data []byte) (entity.Entity, error) {
return entity.Entity{}, nil
}
Loading

0 comments on commit dff8213

Please sign in to comment.