@@ -613,108 +613,91 @@ func (c *conn) IsPrimaryKey(ctx context.Context, tableName, columnName string) (
613613}
614614
615615func (c * conn ) normalizePath (folderOrTable string ) (absPath string ) {
616- return c .connector .PathNormalizer .NormalizePath (folderOrTable )
616+ return c .connector .pathNormalizer .NormalizePath (folderOrTable )
617617}
618618
619- func (c * conn ) GetTables (ctx context.Context , folder string ) (tables []string , err error ) {
620- absPath := c .normalizePath (folder )
621- var e scheme.Entry
619+ func isSysDir (databaseName , dirAbsPath string ) bool {
620+ for _ , sysDir := range [... ]string {
621+ path .Join (databaseName , ".sys" ),
622+ path .Join (databaseName , ".sys_health" ),
623+ } {
624+ if strings .HasPrefix (dirAbsPath , sysDir ) {
625+ return true
626+ }
627+ }
628+ return false
629+ }
630+
631+ func (c * conn ) getTables (ctx context.Context , absPath string , recursive , excludeSysDirs bool ) (
632+ tables []string , err error ,
633+ ) {
634+ if excludeSysDirs && isSysDir (c .connector .parent .Name (), absPath ) {
635+ return nil , nil
636+ }
637+
638+ var d scheme.Directory
622639 err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
623- e , err = c .connector .parent .Scheme ().DescribePath (ctx , absPath )
640+ d , err = c .connector .parent .Scheme ().ListDirectory (ctx , absPath )
624641 return err
625642 }, retry .WithIdempotent (true ))
626-
627643 if err != nil {
628644 return nil , xerrors .WithStackTrace (err )
629645 }
630646
631- switch {
632- case e .IsTable ():
633- _ , table := path .Split (absPath )
634- tables = append (tables , table )
635- return tables , nil
636- case e .IsDirectory ():
637- var d scheme.Directory
638- err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
639- d , err = c .connector .parent .Scheme ().ListDirectory (ctx , absPath )
640- return err
641- }, retry .WithIdempotent (true ))
642-
643- if err != nil {
644- return nil , xerrors .WithStackTrace (err )
645- }
647+ if ! d .IsDirectory () && ! d .IsDatabase () {
648+ return nil , xerrors .WithStackTrace (fmt .Errorf ("'%s' is not a folder" , absPath ))
649+ }
646650
647- for i := range d .Children {
648- if d .Children [i ].IsTable () {
649- tables = append (tables , d .Children [i ].Name )
651+ for i := range d .Children {
652+ switch d .Children [i ].Type {
653+ case scheme .EntryTable , scheme .EntryColumnTable :
654+ tables = append (tables , path .Join (absPath , d .Children [i ].Name ))
655+ case scheme .EntryDirectory , scheme .EntryDatabase :
656+ if recursive {
657+ childTables , err := c .getTables (ctx , path .Join (absPath , d .Children [i ].Name ), recursive , excludeSysDirs )
658+ if err != nil {
659+ return nil , xerrors .WithStackTrace (err )
660+ }
661+ tables = append (tables , childTables ... )
650662 }
651663 }
652- return tables , nil
653- default :
654- return nil , xerrors .WithStackTrace (fmt .Errorf ("path '%s' should be a table or directory" , folder ))
655664 }
665+
666+ return tables , nil
656667}
657668
658- func (c * conn ) GetAllTables (ctx context.Context , folder string ) (tables []string , err error ) {
659- folder = c .normalizePath (folder )
660- ignoreDirs := map [string ]bool {
661- ".sys" : true ,
662- ".sys_health" : true ,
663- }
669+ func (c * conn ) GetTables (ctx context.Context , folder string , recursive , excludeSysDirs bool ) (
670+ tables []string , err error ,
671+ ) {
672+ absPath := c .normalizePath (folder )
664673
665- canEnter := func (dir string ) bool {
666- if _ , ignore := ignoreDirs [dir ]; ignore {
667- return false
668- }
669- return true
674+ var e scheme.Entry
675+ err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
676+ e , err = c .connector .parent .Scheme ().DescribePath (ctx , absPath )
677+ return err
678+ }, retry .WithIdempotent (true ))
679+ if err != nil {
680+ return nil , xerrors .WithStackTrace (err )
670681 }
671682
672- queue := make ([]string , 0 )
673- queue = append (queue , folder )
674-
675- for st := 0 ; st < len (queue ); st ++ {
676- curPath := queue [st ]
677-
678- var e scheme.Entry
679- err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
680- e , err = c .connector .parent .Scheme ().DescribePath (ctx , curPath )
681- return err
682- }, retry .WithIdempotent (true ))
683-
684- if err != nil {
685- return nil , xerrors .WithStackTrace (err )
686- }
687-
688- if e .IsTable () {
689- switch curPath {
690- case folder :
691- _ , table := path .Split (curPath )
692- tables = append (tables , table )
693- default :
694- tables = append (tables , strings .TrimPrefix (curPath , folder + "/" ))
695- }
696- continue
697- }
698-
699- var d scheme.Directory
700- err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
701- d , err = c .connector .parent .Scheme ().ListDirectory (ctx , curPath )
702- return err
703- }, retry .WithIdempotent (true ))
704-
683+ switch e .Type {
684+ case scheme .EntryTable , scheme .EntryColumnTable :
685+ return []string {e .Name }, err
686+ case scheme .EntryDirectory , scheme .EntryDatabase :
687+ tables , err = c .getTables (ctx , absPath , recursive , excludeSysDirs )
705688 if err != nil {
706689 return nil , xerrors .WithStackTrace (err )
707690 }
708691
709- for i := range d .Children {
710- if d .Children [i ].IsDirectory () || d .Children [i ].IsTable () {
711- if canEnter (d .Children [i ].Name ) {
712- queue = append (queue , path .Join (curPath , d .Children [i ].Name ))
713- }
714- }
692+ for i := range tables {
693+ tables [i ] = strings .TrimPrefix (tables [i ], absPath + "/" )
715694 }
695+ return tables , nil
696+ default :
697+ return nil , xerrors .WithStackTrace (
698+ fmt .Errorf ("'%s' is not a table or directory (%s)" , folder , e .Type .String ()),
699+ )
716700 }
717- return tables , nil
718701}
719702
720703func (c * conn ) GetIndexes (ctx context.Context , tableName string ) (indexes []string , err error ) {
0 commit comments