Skip to content

Commit

Permalink
Refactor: Separate terminal-related files from package query into pac…
Browse files Browse the repository at this point in the history
…kage terminal.
  • Loading branch information
mithrandie committed Jun 28, 2022
1 parent 2634df8 commit b191fba
Show file tree
Hide file tree
Showing 32 changed files with 616 additions and 490 deletions.
5 changes: 3 additions & 2 deletions lib/action/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
csvqfile "github.com/mithrandie/csvq/lib/file"
"github.com/mithrandie/csvq/lib/parser"
"github.com/mithrandie/csvq/lib/query"
"github.com/mithrandie/csvq/lib/terminal"

"github.com/mithrandie/go-file/v2"
)
Expand Down Expand Up @@ -65,11 +66,11 @@ func LaunchInteractiveShell(ctx context.Context, proc *query.Processor) error {
return query.NewIncorrectCommandUsageError("input from pipe or redirection cannot be used in interactive shell")
}

if err := proc.Tx.Session.SetStdin(query.GetStdinForREPL()); err != nil {
if err := proc.Tx.Session.SetStdin(terminal.GetStdinForREPL()); err != nil {
return query.NewIOError(nil, err.Error())
}

term, err := query.NewTerminal(ctx, proc.ReferenceScope)
term, err := terminal.NewTerminal(ctx, proc.ReferenceScope)
if err != nil {
return query.ConvertLoadConfigurationError(err)
}
Expand Down
14 changes: 7 additions & 7 deletions lib/query/built_in_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,15 @@ func ShowObjects(scope *ReferenceScope, expr parser.ShowObjects) (string, error)

switch strings.ToUpper(expr.Type.Literal) {
case ShowTables:
keys := scope.Tx.cachedViews.SortedKeys()
keys := scope.Tx.CachedViews.SortedKeys()

if len(keys) < 1 {
s = scope.Tx.Warn("No table is loaded")
} else {
createdFiles, updatedFiles := scope.Tx.uncommittedViews.UncommittedFiles()
createdFiles, updatedFiles := scope.Tx.UncommittedViews.UncommittedFiles()

for _, key := range keys {
if view, ok := scope.Tx.cachedViews.Load(key); ok {
if view, ok := scope.Tx.CachedViews.Load(key); ok {
fields := view.Header.TableColumnNames()
info := view.FileInfo
ufpath := strings.ToUpper(info.Path)
Expand Down Expand Up @@ -516,7 +516,7 @@ func ShowObjects(scope *ReferenceScope, expr parser.ShowObjects) (string, error)
} else {
keys := views.SortedKeys()

updatedViews := scope.Tx.uncommittedViews.UncommittedTempViews()
updatedViews := scope.Tx.UncommittedViews.UncommittedTempViews()

for _, key := range keys {
if view, ok := views.Load(key); ok {
Expand Down Expand Up @@ -554,7 +554,7 @@ func ShowObjects(scope *ReferenceScope, expr parser.ShowObjects) (string, error)
if cur, ok := cursors.Load(key); ok {
isOpen := cur.IsOpen()

w.WriteColor(cur.name, cmd.ObjectEffect)
w.WriteColor(cur.Name, cmd.ObjectEffect)
w.BeginBlock()

w.NewLine()
Expand Down Expand Up @@ -874,14 +874,14 @@ func ShowFields(ctx context.Context, scope *ReferenceScope, expr parser.ShowFiel
}

if !view.FileInfo.IsFile() {
updatedViews := scope.Tx.uncommittedViews.UncommittedTempViews()
updatedViews := scope.Tx.UncommittedViews.UncommittedTempViews()
ufpath := strings.ToUpper(view.FileInfo.Path)

if _, ok := updatedViews[ufpath]; ok {
status = ObjectUpdated
}
} else {
createdViews, updatedView := scope.Tx.uncommittedViews.UncommittedFiles()
createdViews, updatedView := scope.Tx.UncommittedViews.UncommittedFiles()
ufpath := strings.ToUpper(view.FileInfo.Path)

if _, ok := createdViews[ufpath]; ok {
Expand Down
30 changes: 15 additions & 15 deletions lib/query/built_in_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2017,11 +2017,11 @@ var showObjectsTests = []struct {
{
scopeNameCursors: {
"CUR": &Cursor{
name: "cur",
Name: "cur",
query: selectQueryForCursorTest,
},
"CUR2": &Cursor{
name: "cur2",
Name: "cur2",
query: selectQueryForCursorTest,
view: &View{
RecordSet: RecordSet{
Expand All @@ -2039,7 +2039,7 @@ var showObjectsTests = []struct {
index: -1,
},
"CUR3": &Cursor{
name: "cur3",
Name: "cur3",
query: selectQueryForCursorTest,
view: &View{
RecordSet: RecordSet{
Expand All @@ -2057,7 +2057,7 @@ var showObjectsTests = []struct {
index: 1,
},
"CUR4": &Cursor{
name: "cur4",
Name: "cur4",
query: selectQueryForCursorTest,
view: &View{
RecordSet: RecordSet{
Expand All @@ -2075,7 +2075,7 @@ var showObjectsTests = []struct {
index: 2,
},
"CUR5": &Cursor{
name: "stmtcur",
Name: "stmtcur",
statement: parser.Identifier{Literal: "stmt"},
},
},
Expand Down Expand Up @@ -2305,7 +2305,7 @@ var showObjectsTests = []struct {
func TestShowObjects(t *testing.T) {
defer func() {
_ = TestTx.ReleaseResources()
TestTx.uncommittedViews.Clean()
TestTx.UncommittedViews.Clean()
TestTx.PreparedStatements = NewPreparedStatementMap()
initFlag(TestTx.Flags)
}()
Expand All @@ -2330,14 +2330,14 @@ func TestShowObjects(t *testing.T) {
TestTx.Flags.ExportOptions.DelimiterPositions = v.WriteDelimiterPositions
TestTx.Flags.ExportOptions.SingleLine = v.WriteAsSingleLine
TestTx.Flags.ExportOptions.Format = v.Format
_ = TestTx.cachedViews.Clean(TestTx.FileContainer)
_ = TestTx.CachedViews.Clean(TestTx.FileContainer)
if v.ViewCache.SyncMap != nil {
TestTx.cachedViews = v.ViewCache
TestTx.CachedViews = v.ViewCache
}
if v.UncommittedViews.mtx == nil {
TestTx.uncommittedViews = NewUncommittedViews()
TestTx.UncommittedViews = NewUncommittedViews()
} else {
TestTx.uncommittedViews = v.UncommittedViews
TestTx.UncommittedViews = v.UncommittedViews
}
TestTx.PreparedStatements = NewPreparedStatementMap()
if v.PreparedStatements.SyncMap != nil {
Expand Down Expand Up @@ -2644,7 +2644,7 @@ func calcShowRuninfoWidth(wd string) int {
func TestShowFields(t *testing.T) {
defer func() {
_ = TestTx.ReleaseResources()
TestTx.uncommittedViews.Clean()
TestTx.UncommittedViews.Clean()
initFlag(TestTx.Flags)
}()

Expand All @@ -2653,14 +2653,14 @@ func TestShowFields(t *testing.T) {
ctx := context.Background()

for _, v := range showFieldsTests {
_ = TestTx.cachedViews.Clean(TestTx.FileContainer)
_ = TestTx.CachedViews.Clean(TestTx.FileContainer)
if v.ViewCache.SyncMap != nil {
TestTx.cachedViews = v.ViewCache
TestTx.CachedViews = v.ViewCache
}
if v.UncommittedViews.mtx == nil {
TestTx.uncommittedViews = NewUncommittedViews()
TestTx.UncommittedViews = NewUncommittedViews()
} else {
TestTx.uncommittedViews = v.UncommittedViews
TestTx.UncommittedViews = v.UncommittedViews
}

if v.Scope == nil {
Expand Down
6 changes: 3 additions & 3 deletions lib/query/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (m CursorMap) Count(name parser.Identifier) (int, error) {
}

type Cursor struct {
name string
Name string
query parser.SelectQuery
statement parser.Identifier
view *View
Expand All @@ -145,7 +145,7 @@ type Cursor struct {

func NewCursor(e parser.CursorDeclaration) *Cursor {
return &Cursor{
name: e.Cursor.Literal,
Name: e.Cursor.Literal,
query: e.Query,
statement: e.Statement,
mtx: &sync.Mutex{},
Expand All @@ -164,7 +164,7 @@ func NewPseudoCursor(name string, values []value.Primary) *Cursor {
view.RecordSet = records

return &Cursor{
name: name,
Name: name,
view: view,
index: -1,
fetched: false,
Expand Down
Loading

0 comments on commit b191fba

Please sign in to comment.