@@ -29,28 +29,29 @@ DataFrameSqliteColumnMappingWriter >> columnMappings: anObject [
29
29
columnMappings := anObject
30
30
]
31
31
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
+
32
44
{ #category : ' writing' }
33
45
DataFrameSqliteColumnMappingWriter >> write: aDataFrame to: aSqliteConnection [
34
46
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.
43
49
args := Array new : fieldIndices size.
50
+ stmt := aSqliteConnection prepare: (self insertQueryForColumns:
51
+ (columnMappings collect: [ :m | m value ])).
44
52
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) ].
55
56
stmt execute: args ]
56
57
]
0 commit comments