-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDataFrameSqliteWriter.class.st
More file actions
105 lines (83 loc) · 2.46 KB
/
DataFrameSqliteWriter.class.st
File metadata and controls
105 lines (83 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Class {
#name : 'DataFrameSqliteWriter',
#superclass : 'DataFrameWriter',
#instVars : [
'tableName',
'columnMappings'
],
#category : 'DataFrame-IO-Sqlite',
#package : 'DataFrame-IO-Sqlite'
}
{ #category : 'writing' }
DataFrameSqliteWriter class >> writeToTable: aString [
^ self new
tableName: aString;
yourself
]
{ #category : 'writing' }
DataFrameSqliteWriter class >> writeToTable: aString columnMappings: aCollection [
^ self new
tableName: aString;
columnMappings: aCollection;
yourself
]
{ #category : 'accessing' }
DataFrameSqliteWriter >> columnMappings [
^ columnMappings
]
{ #category : 'accessing' }
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 [
^ (self getColumnMappings: aDataFrame) collect: [ :m | m value ]
]
{ #category : 'helpers' }
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 [
^ tableName
]
{ #category : 'accessing' }
DataFrameSqliteWriter >> tableName: anObject [
tableName := anObject
]
{ #category : 'writing' }
DataFrameSqliteWriter >> write: aDataFrame to: aSqliteConnection [
| fieldIndices args stmt |
fieldIndices := self fieldIndicesFor: aDataFrame.
args := Array new: fieldIndices size.
stmt := aSqliteConnection prepare:
(self insertQueryForColumns:
(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 ]
]