-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unification of writing. Also, the "mapping" version doesn't copy rows…
… out of the dataframe.
- Loading branch information
King-Ozymandias
authored and
King-Ozymandias
committed
Sep 4, 2024
1 parent
d1f88c1
commit 882775d
Showing
5 changed files
with
69 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
src/DataFrame-IO-Sqlite/DataFrameAbstractSqliteWriter.class.st
This file was deleted.
Oops, something went wrong.
57 changes: 0 additions & 57 deletions
57
src/DataFrame-IO-Sqlite/DataFrameSqliteColumnMappingWriter.class.st
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,105 @@ | ||
Class { | ||
#name : 'DataFrameSqliteWriter', | ||
#superclass : 'DataFrameAbstractSqliteWriter', | ||
#superclass : 'DataFrameWriter', | ||
#instVars : [ | ||
'columnNames' | ||
'tableName', | ||
'columnMappings' | ||
], | ||
#category : 'DataFrame-IO-Sqlite', | ||
#package : 'DataFrame-IO-Sqlite' | ||
} | ||
|
||
{ #category : 'instance creation' } | ||
{ #category : 'writing' } | ||
DataFrameSqliteWriter class >> writeToTable: aString [ | ||
|
||
^ self new | ||
tableName: aString; | ||
yourself | ||
] | ||
|
||
{ #category : 'instance creation' } | ||
DataFrameSqliteWriter class >> writeToTable: aString columnNames: aCollection [ | ||
{ #category : 'writing' } | ||
DataFrameSqliteWriter class >> writeToTable: aString columnMappings: aCollection [ | ||
|
||
^ self new | ||
tableName: aString; | ||
columnNames: aCollection; | ||
columnMappings: aCollection; | ||
yourself | ||
] | ||
|
||
{ #category : 'accessing' } | ||
DataFrameSqliteWriter >> columnNames [ | ||
DataFrameSqliteWriter >> columnMappings [ | ||
|
||
^ columnNames | ||
^ columnMappings | ||
] | ||
|
||
{ #category : 'accessing' } | ||
DataFrameSqliteWriter >> columnNames: anObject [ | ||
DataFrameSqliteWriter >> columnMappings: anObject [ | ||
|
||
columnMappings := anObject | ||
] | ||
|
||
{ #category : 'helpers' } | ||
DataFrameSqliteWriter >> fieldIndicesFor: aDataFrame [ | ||
"gather indices of columns in dataframe (to avoid lookup by field name later, in loop)" | ||
|
||
^ (self getColumnMappings: aDataFrame) collect: [ :m | | ||
| sourceName | | ||
sourceName := m isAssociation | ||
ifTrue: [ m key ] | ||
ifFalse: [ m ]. | ||
aDataFrame columnNames indexOf: sourceName ] | ||
] | ||
|
||
{ #category : 'helpers' } | ||
DataFrameSqliteWriter >> getColumnMappings: aDataFrame [ | ||
|
||
^ columnMappings ifNil: [ aDataFrame columnNames ] | ||
] | ||
|
||
{ #category : 'helpers' } | ||
DataFrameSqliteWriter >> getColumnNames: aDataFrame [ | ||
|
||
columnNames := anObject | ||
^ (self getColumnMappings: aDataFrame) collect: [ :m | m value ] | ||
] | ||
|
||
{ #category : 'helpers' } | ||
DataFrameSqliteWriter >> getColumnNamesFor: aDataFrame [ | ||
DataFrameSqliteWriter >> insertQueryForColumns: aSequence [ | ||
"" | ||
^ String streamContents: [ :strm | | ||
strm | ||
nextPutAll: 'INSERT INTO '; | ||
nextPutAll: tableName; | ||
nextPut: $(; | ||
nextPutAll: (',' join: aSequence); | ||
nextPutAll: ')VALUES('. | ||
aSequence do: [ :ignore | strm nextPut: $? ] separatedBy: [ strm nextPut: $, ]. | ||
strm nextPut: $) ] | ||
] | ||
|
||
{ #category : 'accessing' } | ||
DataFrameSqliteWriter >> tableName [ | ||
|
||
columnNames ifNil: [ ^ aDataFrame columnNames ]. | ||
columnNames size ~= aDataFrame columns size ifTrue: [ | ||
self error: | ||
'Column count mismatch (Writer columns <=> DataFrame columns)' ]. | ||
^ columnNames | ||
^ tableName | ||
] | ||
|
||
{ #category : 'accessing' } | ||
DataFrameSqliteWriter >> tableName: anObject [ | ||
|
||
tableName := anObject | ||
] | ||
|
||
{ #category : 'writing' } | ||
DataFrameSqliteWriter >> write: aDataFrame to: aSqliteConnection [ | ||
|
||
| stmt | | ||
| fieldIndices args stmt | | ||
fieldIndices := self fieldIndicesFor: aDataFrame. | ||
args := Array new: fieldIndices size. | ||
stmt := aSqliteConnection prepare: | ||
(self insertQueryForColumns: | ||
(self getColumnNamesFor: aDataFrame)). | ||
aDataFrame do: [ :row | stmt execute: row asArray ] | ||
(self getColumnNames: aDataFrame)). | ||
|
||
1 to: aDataFrame dimensions x do: [ :rowIndex | | ||
fieldIndices withIndexDo: [ :srcCol :dstCol | | ||
args at: dstCol put: (aDataFrame contents at: rowIndex at: srcCol) ]. | ||
stmt execute: args ] | ||
] |