Skip to content

Commit

Permalink
fixes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Jan 27, 2021
1 parent 1c3311d commit 7f1e2b5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
14 changes: 10 additions & 4 deletions lodstorage/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def execute(self,ddlCmd):
'''
self.c.execute(ddlCmd)

def createTable(self,listOfRecords,entityName,primaryKey=None,withDrop=False,sampleRecordCount=1):
def createTable(self,listOfRecords,entityName,primaryKey=None,withDrop=False,sampleRecordCount=1,failIfToFew=True):
'''
derive Data Definition Language CREATE TABLE command from list of Records by examining first recorda
as defining sample record and execute DDL command
Expand All @@ -91,12 +91,18 @@ def createTable(self,listOfRecords,entityName,primaryKey=None,withDrop=False,sam
primaryKey(string): the key/column to use as a primary key
withDrop(boolean): true if the existing Table should be dropped
sampleRecords(int): number of sampleRecords expected and to be inspected
failIftoFew(boolean): raise an Exception if to few sampleRecords else warn only
Returns:
EntityInfo: meta data information for the created table
'''
if len(listOfRecords)<sampleRecordCount:
raise Exception("Need %d sample records to createTable" % (sampleRecordCount))
l= len(listOfRecords)
if l<sampleRecordCount:
msg="only %d/%d of needed sample records to createTable available" % (l,sampleRecordCount)
if failIfToFew:
raise Exception(msg)
else:
if self.debug:
print(msg,file=sys.stderr,flush=True)
sampleRecords=listOfRecords[:sampleRecordCount]
entityInfo=EntityInfo(sampleRecords,entityName,primaryKey,debug=self.debug)
if withDrop:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='pylodstorage',
version='0.0.19',
version='0.0.20',

packages=['lodstorage',],
author='Wolfgang Fahl',
Expand Down
20 changes: 20 additions & 0 deletions tests/testSqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,26 @@ def testCopy(self):
# https://stackoverflow.com/a/44707371/1497139
copyDB.execute("pragma user_version=0")

def testIssue16(self):
'''
https://github.com/WolfgangFahl/pyLoDStorage/issues/16
allow to only warn if samplerecordcount is higher than number of available records
'''
listOfRecords=Sample.getSample(1000)
sqlDB=SQLDB()
entityName="sample"
primaryKey='pKey'
withDrop=False
sampleRecordCount=10000
sqlDB.debug=True
sqlDB.createTable(listOfRecords, entityName, primaryKey, withDrop, sampleRecordCount,failIfToFew=False)
withDrop=True
try:
sqlDB.createTable(listOfRecords, entityName, primaryKey, withDrop, sampleRecordCount)
self.fail("There should be an exception that too few sample records where provided")
except Exception as ex:
self.assertTrue("only 1000/10000 of needed sample records to createTable available" in str(ex))


if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testSqllit3']
Expand Down

0 comments on commit 7f1e2b5

Please sign in to comment.