File tree Expand file tree Collapse file tree
packages/react-native-nitro-sqlite/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -86,6 +86,29 @@ describe('Node SQLite mock', () => {
8686 } )
8787 } )
8888
89+ it . each ( [ 0 , 1 ] ) (
90+ 'refreshes raw prepared column names after a schema change, id=%s' ,
91+ async ( id ) => {
92+ const database = open ( { name : 'raw-schema-change' } )
93+ database . execute ( 'CREATE TABLE items (id INTEGER)' )
94+ database . execute ( 'INSERT INTO items VALUES (1)' )
95+ const statement = database . prepare ( 'SELECT * FROM items WHERE id = ?' )
96+ database . execute (
97+ "ALTER TABLE items ADD COLUMN label TEXT DEFAULT 'added'" ,
98+ )
99+
100+ const result = statement . executeRaw ( [ id ] )
101+ expect ( result . columnNames ) . toEqual ( [ 'id' , 'label' ] )
102+ expect ( result . rows ) . toEqual ( id === 1 ? [ [ 1 , 'added' ] ] : [ ] )
103+
104+ database . execute ( 'ALTER TABLE items RENAME COLUMN label TO name' )
105+ const asyncResult = await statement . executeRawAsync ( [ id ] )
106+ expect ( asyncResult . columnNames ) . toEqual ( [ 'id' , 'name' ] )
107+ expect ( asyncResult . rows ) . toEqual ( result . rows )
108+ statement . finalize ( )
109+ } ,
110+ )
111+
89112 it ( 'binds repeated named parameters in first-occurrence order' , ( ) => {
90113 const database = open ( { name : 'named' } )
91114 expect (
Original file line number Diff line number Diff line change @@ -49,15 +49,15 @@ export function executePreparedRaw(
4949 let columnNames : string [ ] = [ ]
5050 let rows : SQLiteValue [ ] [ ] = [ ]
5151 if ( statement . reader ) {
52- columnNames = statement . columns ( ) . map ( ( column ) => column . name )
5352 try {
5453 // better-sqlite3's types keep the object row type after raw(true).
5554 const rawStatement = statement . raw ( true ) as unknown as RawStatement
56- rows = rawStatement
57- . all ( ...bindings )
58- . map ( ( row ) =>
59- row . map ( ( value , index ) => normalizeValue ( value , columnNames [ index ] ! ) ) ,
60- )
55+ const rawRows = rawStatement . all ( ...bindings )
56+ // Execution may reprepare the statement after a schema change.
57+ columnNames = statement . columns ( ) . map ( ( column ) => column . name )
58+ rows = rawRows . map ( ( row ) =>
59+ row . map ( ( value , index ) => normalizeValue ( value , columnNames [ index ] ! ) ) ,
60+ )
6161 } finally {
6262 // Raw mode stays on the statement, and prepared statements also serve execute().
6363 statement . raw ( false )
You can’t perform that action at this time.
0 commit comments