Skip to content

Commit 9b0779e

Browse files
authored
Merge pull request #5 from ODM2/develop
Prep for Release V0.0.3
2 parents cbfa0fa + 30ed3fa commit 9b0779e

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/odm2datamodels/base.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pickle
99
from enum import Enum
10-
from typing import Dict, Union, Any, Type
10+
from typing import Dict, Union, Any, Type, List
1111
import warnings
1212

1313
import pandas as pd
@@ -105,17 +105,32 @@ def read_query(self,
105105
return df.to_dict()
106106
raise TypeError("Unknown output format")
107107

108-
def insert_query(self) -> None:
109-
"""Placeholder for bulk insert"""
110-
#accept dataframe & model
111-
#use pandas to_sql method to perform insert
112-
#if except return false or maybe raise error
113-
#else return true
114-
raise NotImplementedError
115-
116-
def create_object(self, obj:object) -> Union[int, str]:
117-
pkey_name = obj.get_pkey_name()
118-
setattr(obj, pkey_name, None)
108+
def insert_query(self, objs:List[object]) -> None:
109+
with self.session_maker() as session:
110+
session.add_all(objs)
111+
session.commit()
112+
113+
def create_object(self, obj:object, preserve_pkey:bool=False) -> Union[int, str]:
114+
""" Accepts an ORM mapped model and created a corresponding database record
115+
116+
Accepts on one of the ORM mapped models and creates the corresponding database
117+
record, returning the primary key of the newly created record.
118+
119+
Arguments:
120+
obj:object - The ORM mapped model
121+
preserve_pkey:bool - Default=False - flag indicating if the primary key for
122+
the object should be preserved. Avoid in general use cases where database has
123+
a serial that auto assigned primary key, however this can be set to True to
124+
specify you own the primary key value.
125+
126+
Returns:
127+
primary key: Union[int, str]
128+
129+
"""
130+
131+
if not perserve_pkey:
132+
pkey_name = obj.get_pkey_name()
133+
setattr(obj, pkey_name, None)
119134

120135
with self.session_maker() as session:
121136
session.add(obj)

src/odm2datamodels/models/core.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,5 @@ class SamplingFeatures():
4444
class TaxonomicClassifiers():
4545
"""http://odm2.github.io/ODM2/schemas/ODM2_Current/tables/ODM2Core_TaxonomicClassifiers.html"""
4646

47-
class CV_Units():
48-
"""http://odm2.github.io/ODM2/schemas/ODM2_Current/tables/ODM2Core_Units.html"""
49-
5047
class Variables():
5148
"""http://odm2.github.io/ODM2/schemas/ODM2_Current/tables/ODM2Core_Variables.html"""

0 commit comments

Comments
 (0)