Skip to content

Commit

Permalink
Nicer & faster SQLite #write:to:
Browse files Browse the repository at this point in the history
  • Loading branch information
King-Ozymandias authored and King-Ozymandias committed Sep 3, 2024
1 parent 8d90242 commit d1f88c1
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/DataFrame-IO-Sqlite/DataFrameSqliteColumnMappingWriter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,29 @@ DataFrameSqliteColumnMappingWriter >> columnMappings: anObject [
columnMappings := anObject
]

{ #category : 'helpers' }
DataFrameSqliteColumnMappingWriter >> fieldIndicesFor: aDataFrame [
"gather indices of columns in dataframe (to avoid lookup by field name later, in loop)"

^ columnMappings collect: [ :m |
| sourceName |
sourceName := m isAssociation
ifTrue: [ m key ]
ifFalse: [ m ].
aDataFrame columnNames indexOf: sourceName ]
]

{ #category : 'writing' }
DataFrameSqliteColumnMappingWriter >> write: aDataFrame to: aSqliteConnection [

| fieldIndices args dfCols tblCols stmt nCols |
nCols := columnMappings size.
dfCols := aDataFrame columnNames.
fieldIndices := columnMappings collect: [ :m |
dfCols indexOf: (m isAssociation
ifTrue: [ m key ]
ifFalse: [ m ]) ].
tblCols := columnMappings collect: [ :m | m value ].
| fieldIndices args stmt |
fieldIndices := self fieldIndicesFor: aDataFrame.
args := Array new: fieldIndices size.
stmt := aSqliteConnection prepare: (self insertQueryForColumns:
(columnMappings collect: [ :m | m value ])).

stmt := aSqliteConnection prepare:
(self insertQueryForColumns: tblCols).

aDataFrame do: [ :r |
| row |
row := r asArray.
1 to: nCols do: [ :i |
| rowVal |
rowVal := row at: (fieldIndices at: i).
args at: i put: rowVal ].
1 to: aDataFrame dimensions x do: [ :rowIndex |
fieldIndices withIndexDo: [ :srcCol :dstCol |
args at: dstCol put: (aDataFrame contents at: rowIndex at: srcCol) ].
stmt execute: args ]
]

0 comments on commit d1f88c1

Please sign in to comment.