Coverage for gws-app/gws/plugin/postgres/finder.py: 0%

29 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""PostgreSQL finder.""" 

2 

3from typing import Optional, cast 

4 

5import gws 

6import gws.base.database 

7import gws.base.model 

8import gws.base.search 

9import gws.config.util 

10 

11 

12@gws.ext.config.finder('postgres') 

13class Config(gws.base.search.finder.Config): 

14 """Search in a PostgreSQL table.""" 

15 

16 dbUid: Optional[str] 

17 """UID of the database provider.""" 

18 tableName: str 

19 """Table to search, optionally schema-qualified.""" 

20 sqlFilter: Optional[str] 

21 """SQL condition added to all search queries.""" 

22 

23 

24@gws.ext.object.finder('postgres') 

25class Object(gws.base.search.finder.Object): 

26 """Finder that searches a PostgreSQL table through its ``postgres`` models.""" 

27 

28 db: gws.DatabaseProvider 

29 """Database provider.""" 

30 tableName: str 

31 """Table to search.""" 

32 

33 def configure(self): 

34 self.tableName = self.cfg('tableName') or self.cfg('_defaultTableName') 

35 self.configure_provider() 

36 self.configure_models() 

37 self.configure_templates() 

38 

39 # it's difficult to decide if we support keyword/geometry search, 

40 # because different models can have different rules 

41 

42 self.supportsKeywordSearch = True 

43 self.supportsGeometrySearch = True 

44 self.supportsFilterSearch = True 

45 

46 def configure_provider(self): 

47 """Set the database provider from ``dbUid``, or the first ``postgres`` provider. 

48 

49 Returns: 

50 ``True`` if a provider was set. 

51 

52 Raises: 

53 ``gws.Error``: If no provider is found. 

54 """ 

55 return gws.config.util.configure_database_provider_for(self) 

56 

57 def configure_models(self): 

58 return gws.config.util.configure_models_for(self, with_default=True) 

59 

60 def create_model(self, cfg): 

61 """Create a ``postgres`` model for the table of the finder. 

62 

63 The model gets the database provider, the table and the ``sqlFilter`` of the finder. 

64 

65 Args: 

66 cfg: Model configuration, or ``None`` for the default model. 

67 

68 Returns: 

69 The model object. 

70 """ 

71 return self.create_child( 

72 gws.ext.object.model, 

73 cfg, 

74 type=self.extType, 

75 sqlFilter=self.cfg('sqlFilter'), 

76 _defaultDb=self.db, 

77 _defaultTableName=self.tableName 

78 )