sqlalchemy-query-helpers 1.0.6__tar.gz → 1.0.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sqlalchemy-query-helpers might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlalchemy-query-helpers
3
- Version: 1.0.6
3
+ Version: 1.0.7
4
4
  Summary: Some helpers for SQLAlchemy
5
5
  Author-email: vladiscripts <blagopoluchie12@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/vladiscripts/sqlalchemy-query-helpers
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sqlalchemy-query-helpers"
7
- version = "1.0.6"
7
+ version = "1.0.7"
8
8
  authors = [
9
9
  { name = "vladiscripts", email = "blagopoluchie12@gmail.com" },
10
10
  ]
@@ -21,7 +21,7 @@ class DB:
21
21
  engine_str = self.make_engine_str(use_os_env)
22
22
  self.engine = create_engine(f'{engine_str}/{db_name}', echo=echo)
23
23
  self.Session = sessionmaker(bind=self.engine)
24
- self.session = self.Session()
24
+ # self.session = self.Session()
25
25
 
26
26
  base.metadata.create_all(self.engine) # create tables and index if not exists
27
27
  self.name = self.engine.url.database
@@ -48,8 +48,8 @@ class DB:
48
48
  engine_str = f'mysql+pymysql://{user}:{password}@{host}'
49
49
  return engine_str
50
50
 
51
- def get_predifined_table(self, table_name: str, base_metadata):
52
- table = Table(table_name, base_metadata, autoload_with=self.engine)
51
+ def get_predefined_table(self, table_name: str, base_metadata=None):
52
+ table = Table(table_name, base_metadata or declarative_base().metadata, autoload_with=self.engine)
53
53
  return table
54
54
 
55
55
  @staticmethod
@@ -144,16 +144,14 @@ class DB:
144
144
  self.insert_ignore_many_core(t, [row], mfields)
145
145
 
146
146
  def insert_ignore_many_core(self, t, rows: List[Union[dict, list, tuple]], mfields: Union[list, tuple] = None) -> None:
147
- """Core instead ORM. IGNORE can ignore don't only doubles but other errors. Many warnings."""
148
- # for row in rows:
149
- # row = self.to_dict(row, mfields)
150
- # # self.session.execute(insert(t, values=row, prefixes=['IGNORE']))
151
- # s = insert(t).values(row).prefix_with('IGNORE', dialect='mysql')
152
- # # self.session.execute(s)
147
+ """If can better use upsert, or insert after select with filtering exists rows. Problems of IGNORE:
148
+ * This make very large skips of row ids in table.
149
+ * Can ignore don't only doubles but other errors. Many warnings."""
153
150
  rows_to_insert = [self.__to_dict(row, mfields) for row in rows]
154
151
  q = insert(t).values(rows_to_insert).prefix_with('IGNORE', dialect='mysql')
155
- self.session.execute(q)
156
- self.session.commit()
152
+ with self.Session() as session:
153
+ session.execute(q)
154
+ session.commit()
157
155
 
158
156
  def insert_ignore_instanses(self, instances):
159
157
  if not isinstance(instances, Iterable): instances = (instances,)
@@ -226,12 +224,15 @@ class DB:
226
224
  self.session.commit()
227
225
  return is_updated, is_inserted
228
226
 
229
- def upsert(self, t, rows: Union[list[dict], tuple[dict]], mfields=None):
227
+ def upsert(self, t, rows: Union[list[dict], tuple[dict]], mfields=None, do_commit=False):
230
228
  rows_to_insert = [self.__to_dict(row, mfields) for row in rows]
231
229
  stmt = insert(t).values(rows_to_insert)
232
230
  update_dict = {x.name: x for x in stmt.inserted}
233
231
  upsert_query = stmt.on_duplicate_key_update(update_dict)
234
- self.session.execute(upsert_query)
232
+ with self.Session() as session:
233
+ session.execute(upsert_query)
234
+ if do_commit:
235
+ session.commit()
235
236
 
236
237
  # def upsert(self, t, row, mfields=None):
237
238
  # row = self.to_dict(row, mfields)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlalchemy-query-helpers
3
- Version: 1.0.6
3
+ Version: 1.0.7
4
4
  Summary: Some helpers for SQLAlchemy
5
5
  Author-email: vladiscripts <blagopoluchie12@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/vladiscripts/sqlalchemy-query-helpers