Skip to content

Commit d1f88c1

Browse files
King-OzymandiasKing-Ozymandias
King-Ozymandias
authored and
King-Ozymandias
committed
Nicer & faster SQLite #write:to:
1 parent 8d90242 commit d1f88c1

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/DataFrame-IO-Sqlite/DataFrameSqliteColumnMappingWriter.class.st

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,29 @@ DataFrameSqliteColumnMappingWriter >> columnMappings: anObject [
2929
columnMappings := anObject
3030
]
3131

32+
{ #category : 'helpers' }
33+
DataFrameSqliteColumnMappingWriter >> fieldIndicesFor: aDataFrame [
34+
"gather indices of columns in dataframe (to avoid lookup by field name later, in loop)"
35+
36+
^ columnMappings collect: [ :m |
37+
| sourceName |
38+
sourceName := m isAssociation
39+
ifTrue: [ m key ]
40+
ifFalse: [ m ].
41+
aDataFrame columnNames indexOf: sourceName ]
42+
]
43+
3244
{ #category : 'writing' }
3345
DataFrameSqliteColumnMappingWriter >> write: aDataFrame to: aSqliteConnection [
3446

35-
| fieldIndices args dfCols tblCols stmt nCols |
36-
nCols := columnMappings size.
37-
dfCols := aDataFrame columnNames.
38-
fieldIndices := columnMappings collect: [ :m |
39-
dfCols indexOf: (m isAssociation
40-
ifTrue: [ m key ]
41-
ifFalse: [ m ]) ].
42-
tblCols := columnMappings collect: [ :m | m value ].
47+
| fieldIndices args stmt |
48+
fieldIndices := self fieldIndicesFor: aDataFrame.
4349
args := Array new: fieldIndices size.
50+
stmt := aSqliteConnection prepare: (self insertQueryForColumns:
51+
(columnMappings collect: [ :m | m value ])).
4452

45-
stmt := aSqliteConnection prepare:
46-
(self insertQueryForColumns: tblCols).
47-
48-
aDataFrame do: [ :r |
49-
| row |
50-
row := r asArray.
51-
1 to: nCols do: [ :i |
52-
| rowVal |
53-
rowVal := row at: (fieldIndices at: i).
54-
args at: i put: rowVal ].
53+
1 to: aDataFrame dimensions x do: [ :rowIndex |
54+
fieldIndices withIndexDo: [ :srcCol :dstCol |
55+
args at: dstCol put: (aDataFrame contents at: rowIndex at: srcCol) ].
5556
stmt execute: args ]
5657
]

0 commit comments

Comments
 (0)