@@ -40,7 +40,7 @@ using v8::Uint32;
4040using v8::Value;
4141
4242#define THROW_SQLITE_ERROR (env, r ) \
43- node:: THROW_ERR_INVALID_STATE ((env), sqlite3_errstr((r)))
43+ THROW_ERR_INVALID_STATE ((env), sqlite3_errstr((r)))
4444
4545#define CHECK_ERROR_OR_THROW (env, expr, expected, ret ) \
4646 do { \
@@ -80,7 +80,7 @@ static void ThrowQuotaExceededException(Local<Context> context) {
8080Storage::Storage (Environment* env, Local<Object> object, Local<String> location)
8181 : BaseObject(env, object) {
8282 MakeWeak ();
83- node:: Utf8Value utf8_location (env->isolate (), location);
83+ Utf8Value utf8_location (env->isolate (), location);
8484 symbols_.Reset (env->isolate (), Map::New (env->isolate ()));
8585 db_ = nullptr ;
8686 location_ = utf8_location.ToString ();
@@ -97,9 +97,9 @@ void Storage::MemoryInfo(MemoryTracker* tracker) const {
9797
9898bool Storage::Open () {
9999 static const int kCurrentSchemaVersion = 1 ;
100- static const char get_schema_version_sql[] =
100+ static constexpr std::string_view get_schema_version_sql =
101101 " SELECT schema_version FROM nodejs_webstorage_state" ;
102- static const char init_sql_v0[] =
102+ static constexpr std::string_view init_sql_v0 =
103103 " PRAGMA encoding = 'UTF-16le';"
104104 " PRAGMA busy_timeout = 3000;"
105105 " PRAGMA journal_mode = WAL;"
@@ -165,13 +165,14 @@ bool Storage::Open() {
165165
166166 int r = sqlite3_open (location_.c_str (), &db);
167167 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, false );
168- r = sqlite3_exec (db, init_sql_v0, 0 , 0 , nullptr );
168+ r = sqlite3_exec (db, init_sql_v0. data () , 0 , 0 , nullptr );
169169 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, false );
170170
171171 // Get the current schema version, used to determine schema migrations.
172172 sqlite3_stmt* s = nullptr ;
173- r = sqlite3_prepare_v2 (db, get_schema_version_sql, -1 , &s, 0 );
174- r = sqlite3_exec (db, init_sql_v0, 0 , 0 , nullptr );
173+ r = sqlite3_prepare_v2 (
174+ db, get_schema_version_sql.data (), get_schema_version_sql.size (), &s, 0 );
175+ r = sqlite3_exec (db, init_sql_v0.data (), 0 , 0 , nullptr );
175176 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, false );
176177 auto stmt = stmt_unique_ptr (s);
177178 CHECK_ERROR_OR_THROW (env (), sqlite3_step (stmt.get ()), SQLITE_ROW, false );
@@ -180,7 +181,7 @@ bool Storage::Open() {
180181 stmt = nullptr ; // Force finalization.
181182
182183 if (schema_version > kCurrentSchemaVersion ) {
183- node:: THROW_ERR_INVALID_STATE (
184+ THROW_ERR_INVALID_STATE (
184185 env (), " localStorage was created with a newer version of Node.js" );
185186 return false ;
186187 }
@@ -217,10 +218,13 @@ void Storage::Clear() {
217218 return ;
218219 }
219220
220- static const char sql[] = " DELETE FROM nodejs_webstorage" ;
221+ static constexpr std::string_view sql = " DELETE FROM nodejs_webstorage" ;
221222 sqlite3_stmt* s = nullptr ;
222223 CHECK_ERROR_OR_THROW (
223- env (), sqlite3_prepare_v2 (db_.get (), sql, -1 , &s, 0 ), SQLITE_OK, void ());
224+ env (),
225+ sqlite3_prepare_v2 (db_.get (), sql.data (), sql.size (), &s, 0 ),
226+ SQLITE_OK,
227+ void ());
224228 auto stmt = stmt_unique_ptr (s);
225229 CHECK_ERROR_OR_THROW (env (), sqlite3_step (stmt.get ()), SQLITE_DONE, void ());
226230}
@@ -230,9 +234,9 @@ Local<Array> Storage::Enumerate() {
230234 return Local<Array>();
231235 }
232236
233- static const char sql[] = " SELECT key FROM nodejs_webstorage" ;
237+ static constexpr std::string_view sql = " SELECT key FROM nodejs_webstorage" ;
234238 sqlite3_stmt* s = nullptr ;
235- int r = sqlite3_prepare_v2 (db_.get (), sql, - 1 , &s, 0 );
239+ int r = sqlite3_prepare_v2 (db_.get (), sql. data (), sql. size () , &s, 0 );
236240 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, Local<Array>());
237241 auto stmt = stmt_unique_ptr (s);
238242 std::vector<Local<Value>> values;
@@ -253,12 +257,13 @@ Local<Array> Storage::Enumerate() {
253257
254258Local<Value> Storage::Length () {
255259 if (!Open ()) {
256- return Local<Value>() ;
260+ return {} ;
257261 }
258262
259- static const char sql[] = " SELECT count(*) FROM nodejs_webstorage" ;
263+ static constexpr std::string_view sql =
264+ " SELECT count(*) FROM nodejs_webstorage" ;
260265 sqlite3_stmt* s = nullptr ;
261- int r = sqlite3_prepare_v2 (db_.get (), sql, - 1 , &s, 0 );
266+ int r = sqlite3_prepare_v2 (db_.get (), sql. data (), sql. size () , &s, 0 );
262267 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, Local<Value>());
263268 auto stmt = stmt_unique_ptr (s);
264269 CHECK_ERROR_OR_THROW (
@@ -276,16 +281,16 @@ Local<Value> Storage::Load(Local<Name> key) {
276281 }
277282
278283 if (!Open ()) {
279- return Local<Value>() ;
284+ return {} ;
280285 }
281286
282- static const char sql[] =
287+ static constexpr std::string_view sql =
283288 " SELECT value FROM nodejs_webstorage WHERE key = ? LIMIT 1" ;
284289 sqlite3_stmt* s = nullptr ;
285- int r = sqlite3_prepare_v2 (db_.get (), sql, - 1 , &s, 0 );
290+ int r = sqlite3_prepare_v2 (db_.get (), sql. data (), sql. size () , &s, 0 );
286291 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, Local<Value>());
287292 auto stmt = stmt_unique_ptr (s);
288- node:: TwoByteValue utf16key (env ()->isolate (), key);
293+ TwoByteValue utf16key (env ()->isolate (), key);
289294 auto key_size = utf16key.length () * sizeof (uint16_t );
290295 r = sqlite3_bind_blob (stmt.get (), 1 , utf16key.out (), key_size, SQLITE_STATIC);
291296 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, Local<Value>());
@@ -312,10 +317,10 @@ Local<Value> Storage::LoadKey(const int index) {
312317 return Local<Value>();
313318 }
314319
315- static const char sql[] =
320+ static constexpr std::string_view sql =
316321 " SELECT key FROM nodejs_webstorage LIMIT 1 OFFSET ?" ;
317322 sqlite3_stmt* s = nullptr ;
318- int r = sqlite3_prepare_v2 (db_.get (), sql, - 1 , &s, 0 );
323+ int r = sqlite3_prepare_v2 (db_.get (), sql. data (), sql. size () , &s, 0 );
319324 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, Local<Value>());
320325 auto stmt = stmt_unique_ptr (s);
321326 r = sqlite3_bind_int (stmt.get (), 1 , index);
@@ -350,12 +355,13 @@ bool Storage::Remove(Local<Name> key) {
350355 return false ;
351356 }
352357
353- static const char sql[] = " DELETE FROM nodejs_webstorage WHERE key = ?" ;
358+ static constexpr std::string_view sql =
359+ " DELETE FROM nodejs_webstorage WHERE key = ?" ;
354360 sqlite3_stmt* s = nullptr ;
355- int r = sqlite3_prepare_v2 (db_.get (), sql, - 1 , &s, 0 );
361+ int r = sqlite3_prepare_v2 (db_.get (), sql. data (), sql. size () , &s, 0 );
356362 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, false );
357363 auto stmt = stmt_unique_ptr (s);
358- node:: TwoByteValue utf16key (env ()->isolate (), key);
364+ TwoByteValue utf16key (env ()->isolate (), key);
359365 auto key_size = utf16key.length () * sizeof (uint16_t );
360366 r = sqlite3_bind_blob (stmt.get (), 1 , utf16key.out (), key_size, SQLITE_STATIC);
361367 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, false );
@@ -379,14 +385,14 @@ bool Storage::Store(Local<Name> key, Local<Value> value) {
379385 return false ;
380386 }
381387
382- static const char sql[] =
388+ static constexpr std::string_view sql =
383389 " INSERT INTO nodejs_webstorage (key, value) VALUES (?, ?)"
384390 " ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value"
385391 " WHERE EXCLUDED.key = key" ;
386392 sqlite3_stmt* s = nullptr ;
387- node:: TwoByteValue utf16key (env ()->isolate (), key);
388- node:: TwoByteValue utf16val (env ()->isolate (), val);
389- int r = sqlite3_prepare_v2 (db_.get (), sql, - 1 , &s, 0 );
393+ TwoByteValue utf16key (env ()->isolate (), key);
394+ TwoByteValue utf16val (env ()->isolate (), val);
395+ int r = sqlite3_prepare_v2 (db_.get (), sql. data (), sql. size () , &s, 0 );
390396 CHECK_ERROR_OR_THROW (env (), r, SQLITE_OK, false );
391397 auto stmt = stmt_unique_ptr (s);
392398 auto key_size = utf16key.length () * sizeof (uint16_t );
@@ -428,14 +434,13 @@ static void GetItem(const FunctionCallbackInfo<Value>& info) {
428434 env, " Failed to execute 'getItem' on 'Storage': 1 argument required" );
429435 }
430436
431- Local<String> prop;
432- if (!info[0 ]->ToString (env->context ()).ToLocal (&prop)) {
437+ if (!info[0 ]->IsString ()) {
433438 return ;
434439 }
435440
436- Local<Value> result = storage->Load (prop );
441+ Local<Value> result = storage->Load (info[ 0 ]. As <String>() );
437442 if (result.IsEmpty ()) {
438- info.GetReturnValue ().Set ( Null (env-> isolate ()) );
443+ info.GetReturnValue ().SetNull ( );
439444 } else {
440445 info.GetReturnValue ().Set (result);
441446 }
@@ -457,13 +462,13 @@ static void Key(const FunctionCallbackInfo<Value>& info) {
457462 }
458463
459464 if (index < 0 ) {
460- info.GetReturnValue ().Set ( Null (env-> isolate ()) );
465+ info.GetReturnValue ().SetNull ( );
461466 return ;
462467 }
463468
464469 Local<Value> result = storage->LoadKey (index);
465470 if (result.IsEmpty ()) {
466- info.GetReturnValue ().Set ( Null (env-> isolate ()) );
471+ info.GetReturnValue ().SetNull ( );
467472 } else {
468473 info.GetReturnValue ().Set (result);
469474 }
@@ -473,19 +478,18 @@ static void RemoveItem(const FunctionCallbackInfo<Value>& info) {
473478 Storage* storage;
474479 ASSIGN_OR_RETURN_UNWRAP (&storage, info.This ());
475480 Environment* env = Environment::GetCurrent (info);
476- Local<String> prop;
477481
478482 if (info.Length () < 1 ) {
479483 return THROW_ERR_MISSING_ARGS (
480484 env,
481485 " Failed to execute 'removeItem' on 'Storage': 1 argument required" );
482486 }
483487
484- if (!info[0 ]->ToString (env-> context ()). ToLocal (&prop )) {
488+ if (!info[0 ]->IsString ( )) {
485489 return ;
486490 }
487491
488- storage->Remove (prop );
492+ storage->Remove (info[ 0 ]. As <String>() );
489493}
490494
491495static void SetItem (const FunctionCallbackInfo<Value>& info) {
@@ -498,12 +502,11 @@ static void SetItem(const FunctionCallbackInfo<Value>& info) {
498502 env, " Failed to execute 'setItem' on 'Storage': 2 arguments required" );
499503 }
500504
501- Local<String> prop;
502- if (!info[0 ]->ToString (env->context ()).ToLocal (&prop)) {
505+ if (!info[0 ]->IsString ()) {
503506 return ;
504507 }
505508
506- storage->Store (prop , info[1 ]);
509+ storage->Store (info[ 0 ]. As <String>() , info[1 ]);
507510}
508511
509512template <typename T>
0 commit comments