parasti šo izmantojam psycopg2
asyncpg https://www.perplexity.ai/search/asyncpg-named-sql-query-mdyjT5JGQP2b48svzS6xfg
Piemērs kā jāizskatās ControllerDatabase funkcijām
xxxxxxxxxx
21def get_tasks_by_feature(self, feature: DbFeature = None, language_code: LanguageCodes = None) -> List[DbFeaturesInTask]:
2 results = []
Startup uztaisa pool un tad ControllerDatabase izmanto https://www.perplexity.ai/search/give-example-fastapi-psycopg2-C.04CzVHSYadcs8xowkCZQ
Select ar zvaigznīti
xxxxxxxxxx
131with self.db_context() as cursor:
2cursor.execute("SELECT * FROM features_workers")
3
4if cursor.rowcount > 0:
5for row in cursor.fetchall():
6columns = [it[0] for it in cursor.description]
7feature_worker_dictionary = dict(zip(columns, row))
8feature_worker: DbFeatureWorker = from_dict(
9data_class=DbFeatureWorker,
10data=feature_worker_dictionary,
11config=Config(check_types=False)
12)
13feature_workers.append(feature_worker)
x1def convert_enum_to_str(self, data_dict):
2for key, value in data_dict.items():
3if isinstance(value, Enum) or isinstance(value, datetime.datetime):
4data_dict[key] = str(value)
5elif isinstance(value, dict):
6data_dict[key] = self.convert_enum_to_str(data_dict[key])
7elif isinstance(value, list):
8data_dict[key] = self.convert_list_enum_to_str(data_dict[key])
9return data_dict
10
11def convert_list_enum_to_str(self, data_list):
12# concatenate LanguageCodes
13if all([type(i) is LanguageCodes for i in data_list]):
14return ','.join([str(i) for i in data_list])
15for idx in range(len(data_list)):
16value = data_list[idx]
17if isinstance(value, Enum) or isinstance(value, datetime.datetime):
18data_list[idx] = str(value)
19elif isinstance(value, dict):
20data_list[idx] = self.convert_enum_to_str(data_list[idx])
21elif isinstance(value, list):
22data_list[idx] = self.convert_list_enum_to_str(data_list[idx])
23return data_list
24
25def convert_numpy_to_bytes(self, data_dict):
26for key, value in data_dict.items():
27if isinstance(value, np.ndarray):
28data_dict[key] = value.astype(np.float32).tobytes()
29return data_dict
30