From 9e82811a2a963be962fc3ffc426c137e01d56e2d Mon Sep 17 00:00:00 2001 From: Priyansh Date: Thu, 28 Aug 2025 13:09:22 +0530 Subject: Restructure codebase with proper organization, fix file picker navigation, and add utils package --- utils/constants.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/database.go | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 utils/constants.go create mode 100644 utils/database.go (limited to 'utils') diff --git a/utils/constants.go b/utils/constants.go new file mode 100644 index 0000000..6ec594c --- /dev/null +++ b/utils/constants.go @@ -0,0 +1,75 @@ +package utils + +import "nectar/types" + +// Form field indices using iota for better readability +const ( + FieldConnectionType = iota + FieldHost + FieldPort + FieldSSL + FieldUser + FieldPassword + FieldConnectionName + FieldColor +) + +// SQLite-specific field indices (redefine to match the layout) +const ( + SQLiteFieldConnectionType = iota // 0: Connection Type + SQLiteFieldDatabaseFile // 1: Database File + SQLiteFieldConnectionName // 2: Connection Name + SQLiteFieldColor // 3: Color +) + +// Input field indices using iota +const ( + InputHost = iota + InputPort + InputUser + InputPassword + InputConnectionName +) + +// Database connection defaults +var ( + DefaultPorts = map[types.ConnectionType]string{ + types.PostgreSQL: "5432", + types.MySQL: "3306", + types.SQLite: "", + } + + ConnectionTypes = []types.ConnectionType{ + types.PostgreSQL, + types.MySQL, + types.SQLite, + } + + // Total field counts for each database type + FieldCounts = map[types.ConnectionType]int{ + types.SQLite: 4, // Connection Type, Database File, Connection Name, Color + types.PostgreSQL: 8, // Connection Type, Host, Port, SSL, User, Password, Connection Name, Color + types.MySQL: 8, // Same as PostgreSQL + } +) + +// Field mapping for SQLite (simplified structure) +var SQLiteFieldMapping = map[int]int{ + SQLiteFieldConnectionName: InputConnectionName, +} + +// Field mapping for PostgreSQL and MySQL (full structure) +var NonSQLiteFieldMapping = map[int]int{ + FieldHost: InputHost, + FieldPort: InputPort, + FieldUser: InputUser, + FieldPassword: InputPassword, + FieldConnectionName: InputConnectionName, +} + +// Complete field mappings for all database types +var FieldMappings = map[types.ConnectionType]map[int]int{ + types.SQLite: SQLiteFieldMapping, + types.PostgreSQL: NonSQLiteFieldMapping, + types.MySQL: NonSQLiteFieldMapping, +} diff --git a/utils/database.go b/utils/database.go new file mode 100644 index 0000000..877ec2d --- /dev/null +++ b/utils/database.go @@ -0,0 +1,50 @@ +package utils + +import "nectar/types" + +// GetDefaultPort returns the default port for a given connection type +func GetDefaultPort(connType types.ConnectionType) string { + if port, exists := DefaultPorts[connType]; exists { + return port + } + return "" +} + +// GetFieldCount returns the total number of fields for a connection type +func GetFieldCount(connType types.ConnectionType) int { + if count, exists := FieldCounts[connType]; exists { + return count + } + return 0 +} + +// GetInputIndex returns the input index for a given field based on connection type +func GetInputIndex(connType types.ConnectionType, fieldIndex int) int { + if mapping, exists := FieldMappings[connType]; exists { + if inputIndex, exists := mapping[fieldIndex]; exists { + return inputIndex + } + } + return -1 +} + +// NextConnectionType cycles to the next connection type +func NextConnectionType(current types.ConnectionType) types.ConnectionType { + for i, connType := range ConnectionTypes { + if connType == current { + return ConnectionTypes[(i+1)%len(ConnectionTypes)] + } + } + return current +} + +// PrevConnectionType cycles to the previous connection type +func PrevConnectionType(current types.ConnectionType) types.ConnectionType { + for i, connType := range ConnectionTypes { + if connType == current { + prevIndex := (i - 1 + len(ConnectionTypes)) % len(ConnectionTypes) + return ConnectionTypes[prevIndex] + } + } + return current +} -- cgit v1.2.3