Skip to content

Commit bdbde8f

Browse files
King-OzymandiasKing-Ozymandias
King-Ozymandias
authored and
King-Ozymandias
committed
Unification of writing. Also, the "mapping" version doesn't copy rows out of the dataframe.
1 parent d1f88c1 commit bdbde8f

4 files changed

+68
-113
lines changed

src/DataFrame-IO-Sqlite/DataFrame.extension.st

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString [
1818
DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString columnMappings: aCollection [
1919

2020
| writer |
21-
writer := DataFrameSqliteColumnMappingWriter
21+
writer := DataFrameSqliteWriter
2222
writeToTable: aString
2323
columnMappings: aCollection.
2424
self writeTo: aSqlite3Connection using: writer
@@ -30,6 +30,6 @@ DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString columnNames: a
3030
| writer |
3131
writer := DataFrameSqliteWriter
3232
writeToTable: aString
33-
columnNames: aCollection.
33+
columnMappings: aCollection.
3434
self writeTo: aSqlite3Connection using: writer
3535
]

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

-35
This file was deleted.

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

-57
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,105 @@
11
Class {
22
#name : 'DataFrameSqliteWriter',
3-
#superclass : 'DataFrameAbstractSqliteWriter',
3+
#superclass : 'DataFrameWriter',
44
#instVars : [
5-
'columnNames'
5+
'tableName',
6+
'columnMappings'
67
],
78
#category : 'DataFrame-IO-Sqlite',
89
#package : 'DataFrame-IO-Sqlite'
910
}
1011

11-
{ #category : 'instance creation' }
12+
{ #category : 'writing' }
1213
DataFrameSqliteWriter class >> writeToTable: aString [
1314

1415
^ self new
1516
tableName: aString;
1617
yourself
1718
]
1819

19-
{ #category : 'instance creation' }
20-
DataFrameSqliteWriter class >> writeToTable: aString columnNames: aCollection [
20+
{ #category : 'writing' }
21+
DataFrameSqliteWriter class >> writeToTable: aString columnMappings: aCollection [
2122

2223
^ self new
2324
tableName: aString;
24-
columnNames: aCollection;
25+
columnMappings: aCollection;
2526
yourself
2627
]
2728

2829
{ #category : 'accessing' }
29-
DataFrameSqliteWriter >> columnNames [
30+
DataFrameSqliteWriter >> columnMappings [
3031

31-
^ columnNames
32+
^ columnMappings
3233
]
3334

3435
{ #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 [
3661

37-
columnNames := anObject
62+
^ (self getColumnMappings: aDataFrame) collect: [ :m | m value ]
3863
]
3964

4065
{ #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 [
4281

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
4889
]
4990

5091
{ #category : 'writing' }
5192
DataFrameSqliteWriter >> write: aDataFrame to: aSqliteConnection [
5293

53-
| stmt |
94+
| fieldIndices args stmt |
95+
fieldIndices := self fieldIndicesFor: aDataFrame.
96+
args := Array new: fieldIndices size.
5497
stmt := aSqliteConnection prepare:
5598
(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 ]
58105
]

0 commit comments

Comments
 (0)