indexeddb: Add index schemas (#38891)

Creates schemas to hold index information. These tables are created when
the database is initialized. These tables are not updated however.

Testing: WPT and unit
Fixes: Partially #38100

---------

Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
This commit is contained in:
Ashwin Naren 2025-08-29 12:33:30 -07:00 committed by GitHub
parent 95adb6f673
commit aab9beb3de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,47 +2,43 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// Adapted from:
// https://github.com/mozilla-firefox/firefox/blob/ee102e926521b3e460293b0aea6b54b1a03f6f74/dom/indexedDB/DBSchema.cpp#L78
pub(crate) fn create_tables(conn: &rusqlite::Connection) -> Result<(), rusqlite::Error> { pub(crate) fn create_tables(conn: &rusqlite::Connection) -> Result<(), rusqlite::Error> {
conn.execute( const DATABASE: &str = r#"
r#"create table database create table database (
(
name varchar not null name varchar not null
primary key, primary key,
origin varchar not null, origin varchar not null,
version bigint default 0 not null version bigint default 0 not null
);"#, ) WITHOUT ROWID;"#;
[], conn.execute(DATABASE, [])?;
)?;
conn.execute( const OBJECT_STORE: &str = r#"
r#"create table object_store create table object_store (
(
id integer not null id integer not null
primary key autoincrement, primary key autoincrement,
name varchar not null name varchar not null
unique, unique,
key_path varbinary_blob, key_path varbinary_blob,
auto_increment boolean default FALSE not null auto_increment boolean default FALSE not null
);"#, );"#;
[], conn.execute(OBJECT_STORE, [])?;
)?;
conn.execute( const OBJECT_DATA: &str = r#"
r#"create table object_data create table object_data (
(
object_store_id integer not null object_store_id integer not null
references object_store, references object_store,
key blob not null, key blob not null,
data blob not null, data blob not null,
constraint "pk-object_data" constraint "pk-object_data"
primary key (object_store_id, key) primary key (object_store_id, key)
);"#, ) WITHOUT ROWID;"#;
[], conn.execute(OBJECT_DATA, [])?;
)?;
conn.execute( const OBJECT_STORE_INDEX: &str = r#"
r#"create table object_store_index create table object_store_index (
(
id integer not null id integer not null
primary key autoincrement, primary key autoincrement,
object_store_id integer not null object_store_id integer not null
@ -52,8 +48,35 @@ pub(crate) fn create_tables(conn: &rusqlite::Connection) -> Result<(), rusqlite:
key_path varbinary_blob not null, key_path varbinary_blob not null,
unique_index boolean not null, unique_index boolean not null,
multi_entry_index boolean not null multi_entry_index boolean not null
);"#, );"#;
[], conn.execute(OBJECT_STORE_INDEX, [])?;
)?;
const INDEX_DATA: &str = r#"
CREATE TABLE index_data (
index_id INTEGER NOT NULL,
value BLOB NOT NULL,
object_data_key BLOB NOT NULL,
object_store_id INTEGER NOT NULL,
value_locale BLOB,
PRIMARY KEY (index_id, value, object_data_key)
FOREIGN KEY (index_id) REFERENCES object_store_index(id),
FOREIGN KEY (object_store_id, object_data_key)
REFERENCES object_data(object_store_id, key)
) WITHOUT ROWID;"#;
conn.execute(INDEX_DATA, [])?;
const UNIQUE_INDEX_DATA: &str = r#"
CREATE TABLE unique_index_data (
index_id INTEGER NOT NULL,
value BLOB NOT NULL,
object_store_id INTEGER NOT NULL,
object_data_key BLOB NOT NULL,
value_locale BLOB,
PRIMARY KEY (index_id, value),
FOREIGN KEY (index_id) REFERENCES object_store_index(id),
FOREIGN KEY (object_store_id, object_data_key)
REFERENCES object_data(object_store_id, key)
) WITHOUT ROWID;"#;
conn.execute(UNIQUE_INDEX_DATA, [])?;
Ok(()) Ok(())
} }