|
1 | 1 | Class {
|
2 | 2 | #name : 'DataFrameSqliteWriter',
|
3 |
| - #superclass : 'DataFrameAbstractSqliteWriter', |
| 3 | + #superclass : 'DataFrameWriter', |
4 | 4 | #instVars : [
|
5 |
| - 'columnNames' |
| 5 | + 'tableName', |
| 6 | + 'columnMappings' |
6 | 7 | ],
|
7 | 8 | #category : 'DataFrame-IO-Sqlite',
|
8 | 9 | #package : 'DataFrame-IO-Sqlite'
|
9 | 10 | }
|
10 | 11 |
|
11 |
| -{ #category : 'instance creation' } |
| 12 | +{ #category : 'writing' } |
12 | 13 | DataFrameSqliteWriter class >> writeToTable: aString [
|
13 | 14 |
|
14 | 15 | ^ self new
|
15 | 16 | tableName: aString;
|
16 | 17 | yourself
|
17 | 18 | ]
|
18 | 19 |
|
19 |
| -{ #category : 'instance creation' } |
20 |
| -DataFrameSqliteWriter class >> writeToTable: aString columnNames: aCollection [ |
| 20 | +{ #category : 'writing' } |
| 21 | +DataFrameSqliteWriter class >> writeToTable: aString columnMappings: aCollection [ |
21 | 22 |
|
22 | 23 | ^ self new
|
23 | 24 | tableName: aString;
|
24 |
| - columnNames: aCollection; |
| 25 | + columnMappings: aCollection; |
25 | 26 | yourself
|
26 | 27 | ]
|
27 | 28 |
|
28 | 29 | { #category : 'accessing' }
|
29 |
| -DataFrameSqliteWriter >> columnNames [ |
| 30 | +DataFrameSqliteWriter >> columnMappings [ |
30 | 31 |
|
31 |
| - ^ columnNames |
| 32 | + ^ columnMappings |
32 | 33 | ]
|
33 | 34 |
|
34 | 35 | { #category : 'accessing' }
|
35 |
| -DataFrameSqliteWriter >> columnNames: anObject [ |
| 36 | +DataFrameSqliteWriter >> columnMappings: anObject [ |
| 37 | + |
| 38 | + columnMappings := anObject |
| 39 | +] |
| 40 | + |
| 41 | +{ #category : 'helpers' } |
| 42 | +DataFrameSqliteWriter >> fieldIndicesFor: aDataFrame [ |
| 43 | + "gather indices of columns in dataframe (to avoid lookup by field name later, in loop)" |
| 44 | + |
| 45 | + ^ (self getColumnMappings: aDataFrame) collect: [ :m | |
| 46 | + | sourceName | |
| 47 | + sourceName := m isAssociation |
| 48 | + ifTrue: [ m key ] |
| 49 | + ifFalse: [ m ]. |
| 50 | + aDataFrame columnNames indexOf: sourceName ] |
| 51 | +] |
| 52 | + |
| 53 | +{ #category : 'helpers' } |
| 54 | +DataFrameSqliteWriter >> getColumnMappings: aDataFrame [ |
| 55 | + |
| 56 | + ^ columnMappings ifNil: [ aDataFrame columnNames ] |
| 57 | +] |
| 58 | + |
| 59 | +{ #category : 'helpers' } |
| 60 | +DataFrameSqliteWriter >> getColumnNames: aDataFrame [ |
36 | 61 |
|
37 |
| - columnNames := anObject |
| 62 | + ^ (self getColumnMappings: aDataFrame) collect: [ :m | m value ] |
38 | 63 | ]
|
39 | 64 |
|
40 | 65 | { #category : 'helpers' }
|
41 |
| -DataFrameSqliteWriter >> getColumnNamesFor: aDataFrame [ |
| 66 | +DataFrameSqliteWriter >> insertQueryForColumns: aSequence [ |
| 67 | + "" |
| 68 | + ^ String streamContents: [ :strm | |
| 69 | + strm |
| 70 | + nextPutAll: 'INSERT INTO '; |
| 71 | + nextPutAll: tableName; |
| 72 | + nextPut: $(; |
| 73 | + nextPutAll: (',' join: aSequence); |
| 74 | + nextPutAll: ')VALUES('. |
| 75 | + aSequence do: [ :ignore | strm nextPut: $? ] separatedBy: [ strm nextPut: $, ]. |
| 76 | + strm nextPut: $) ] |
| 77 | +] |
| 78 | + |
| 79 | +{ #category : 'accessing' } |
| 80 | +DataFrameSqliteWriter >> tableName [ |
42 | 81 |
|
43 |
| - columnNames ifNil: [ ^ aDataFrame columnNames ]. |
44 |
| - columnNames size ~= aDataFrame columns size ifTrue: [ |
45 |
| - self error: |
46 |
| - 'Column count mismatch (Writer columns <=> DataFrame columns)' ]. |
47 |
| - ^ columnNames |
| 82 | + ^ tableName |
| 83 | +] |
| 84 | + |
| 85 | +{ #category : 'accessing' } |
| 86 | +DataFrameSqliteWriter >> tableName: anObject [ |
| 87 | + |
| 88 | + tableName := anObject |
48 | 89 | ]
|
49 | 90 |
|
50 | 91 | { #category : 'writing' }
|
51 | 92 | DataFrameSqliteWriter >> write: aDataFrame to: aSqliteConnection [
|
52 | 93 |
|
53 |
| - | stmt | |
| 94 | + | fieldIndices args stmt | |
| 95 | + fieldIndices := self fieldIndicesFor: aDataFrame. |
| 96 | + args := Array new: fieldIndices size. |
54 | 97 | stmt := aSqliteConnection prepare:
|
55 | 98 | (self insertQueryForColumns:
|
56 |
| - (self getColumnNamesFor: aDataFrame)). |
57 |
| - aDataFrame do: [ :row | stmt execute: row asArray ] |
| 99 | + (self getColumnNames: aDataFrame)). |
| 100 | + |
| 101 | + 1 to: aDataFrame dimensions x do: [ :rowIndex | |
| 102 | + fieldIndices withIndexDo: [ :srcCol :dstCol | |
| 103 | + args at: dstCol put: (aDataFrame contents at: rowIndex at: srcCol) ]. |
| 104 | + stmt execute: args ] |
58 | 105 | ]
|
0 commit comments