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AO,CAAC,SAAS,EAAE,cAAc,CAAC;aAClC,OAAO,CAAC,SAAS,EAAE,cAAc,CAAC;aAClC,OAAO,CAAC,SAAS,EAAE,cAAc,CAAQ,CAAC;QAC7C,oCAAoC;KACrC;SAAM;QACL,OAAO,IAAI,CAAC;KACb;AACH,CAAC;AAED,MAAM,uBAAuB,GAAG,kBAAkB,CAAC,cAAc,CAAC,CAAC;AAEnE,2BAA2B;AAC3B;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,YAAY,CAC1B,IAAY,EACZ,UAAkB,KAAK,EACvB,cAAsB,IAAI,EAC1B,OAAe,CAAC,EAChB,QAAuC;IAEvC,IAAI,IAAI,KAAK,SAAS,EAAE;QACtB,MAAM,IAAI,SAAS,CAAC,yCAAyC,CAAC,CAAC;KAChE;IACD,MAAM,EAAE,GAAG,uBAAuB,CAAC,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC/E,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;IACnC,EAAE,CAAC,UAAU,GAAG,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;IAC1C,EAAE,CAAC,WAAW,GAAG,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;IACjD,OAAO,EAAE,CAAC;AACZ,CAAC","sourcesContent":["import './polyfillNextTick';\n\nimport customOpenDatabase from '@expo/websql/custom';\nimport { requireNativeModule } from 'expo-modules-core';\nimport { Platform } from 'react-native';\n\nimport { Query, ResultSet, ResultSetError, SQLiteCallback, WebSQLDatabase } from './SQLite.types';\n\nconst ExpoSQLite = requireNativeModule('ExpoSQLite');\n\nfunction zipObject(keys: string[], values: any[]) {\n  const result = {};\n  for (let i = 0; i < keys.length; i++) {\n    result[keys[i]] = values[i];\n  }\n  return result;\n}\n\nclass SQLiteDatabase {\n  _name: string;\n  _closed: boolean = false;\n\n  constructor(name: string) {\n    this._name = name;\n  }\n\n  exec(queries: Query[], readOnly: boolean, callback: SQLiteCallback): void {\n    if (this._closed) {\n      throw new Error(`The SQLite database is closed`);\n    }\n\n    ExpoSQLite.exec(this._name, queries.map(_serializeQuery), readOnly).then(\n      (nativeResultSets) => {\n        callback(null, nativeResultSets.map(_deserializeResultSet));\n      },\n      (error) => {\n        // TODO: make the native API consistently reject with an error, not a string or other type\n        callback(error instanceof Error ? error : new Error(error));\n      }\n    );\n  }\n\n  close() {\n    this._closed = true;\n    return ExpoSQLite.close(this._name);\n  }\n\n  deleteAsync(): Promise<void> {\n    if (!this._closed) {\n      throw new Error(\n        `Unable to delete '${this._name}' database that is currently open. Close it prior to deletion.`\n      );\n    }\n\n    return ExpoSQLite.deleteAsync(this._name);\n  }\n}\n\nfunction _serializeQuery(query: Query): [string, unknown[]] {\n  return [query.sql, Platform.OS === 'android' ? query.args.map(_escapeBlob) : query.args];\n}\n\nfunction _deserializeResultSet(nativeResult): ResultSet | ResultSetError {\n  const [errorMessage, insertId, rowsAffected, columns, rows] = nativeResult;\n  // TODO: send more structured error information from the native module so we can better construct\n  // a SQLException object\n  if (errorMessage !== null) {\n    return { error: new Error(errorMessage) } as ResultSetError;\n  }\n\n  return {\n    insertId,\n    rowsAffected,\n    rows: rows.map((row) => zipObject(columns, row)),\n  };\n}\n\nfunction _escapeBlob<T>(data: T): T {\n  if (typeof data === 'string') {\n    /* eslint-disable no-control-regex */\n    return data\n      .replace(/\\u0002/g, '\\u0002\\u0002')\n      .replace(/\\u0001/g, '\\u0001\\u0002')\n      .replace(/\\u0000/g, '\\u0001\\u0001') as any;\n    /* eslint-enable no-control-regex */\n  } else {\n    return data;\n  }\n}\n\nconst _openExpoSQLiteDatabase = customOpenDatabase(SQLiteDatabase);\n\n// @needsAudit @docsMissing\n/**\n * Open a database, creating it if it doesn't exist, and return a `Database` object. On disk,\n * the database will be created under the app's [documents directory](./filesystem), i.e.\n * `${FileSystem.documentDirectory}/SQLite/${name}`.\n * > The `version`, `description` and `size` arguments are ignored, but are accepted by the function\n * for compatibility with the WebSQL specification.\n * @param name Name of the database file to open.\n * @param version\n * @param description\n * @param size\n * @param callback\n * @return\n */\nexport function openDatabase(\n  name: string,\n  version: string = '1.0',\n  description: string = name,\n  size: number = 1,\n  callback?: (db: WebSQLDatabase) => void\n): WebSQLDatabase {\n  if (name === undefined) {\n    throw new TypeError(`The database name must not be undefined`);\n  }\n  const db = _openExpoSQLiteDatabase(name, version, description, size, callback);\n  db.exec = db._db.exec.bind(db._db);\n  db.closeAsync = db._db.close.bind(db._db);\n  db.deleteAsync = db._db.deleteAsync.bind(db._db);\n  return db;\n}\n"]}