Advertisement
Badal_hs_shah

Untitled

Jan 25th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {ISPC_DB_NAME} from '@src/constants/Constants';
  2. import {RW3AlternativePriceReason} from '@src/services/models/RW3AlternativePriceReason';
  3. import React, {useContext, useEffect, useState} from 'react';
  4. import SQLite from 'react-native-sqlite-storage';
  5.  
  6. export const DatabaseContext = React.createContext<
  7.   SQLite.SQLiteDatabase | undefined
  8. >(undefined);
  9.  
  10. export const DatabaseProvider: React.FC = ({children}) => {
  11.   const [db, setDb] = useState<SQLite.SQLiteDatabase>();
  12.  
  13.   useEffect(() => {
  14.     const initDatabase = async () => {
  15.       try {
  16.         const database = await SQLite.openDatabase({
  17.           name: ISPC_DB_NAME,
  18.           location: 'default',
  19.           version: 1,
  20.         } as any);
  21.         console.log('Database opened successfully');
  22.  
  23.         const createAllTables = `
  24.           CREATE TABLE IF NOT EXISTS AlternativePriceReason (
  25.             id INTEGER PRIMARY KEY AUTOINCREMENT,
  26.             activeDefault INTEGER , name TEXT , tertiaryDefault INTEGER
  27.           );
  28.  
  29.           CREATE TABLE IF NOT EXISTS Brand (
  30.             id INTEGER PRIMARY KEY AUTOINCREMENT,
  31.              name TEXT
  32.           );
  33.  
  34.           CREATE TABLE IF NOT EXISTS Category (
  35.             id INTEGER PRIMARY KEY AUTOINCREMENT,
  36.             expectedDirectedItemsCount INTEGER , expectedUndirectedItemsCount INTEGER , fullCheck BOOLEAN , isAssigned BOOLEAN , localNotFoundDirectedItemsCount INTEGER , localScannedDirectedItemsCount INTEGER , localScannedUndirectedItemsCount INTEGER , name TEXT , priceCheckType INTEGER , serverNotFoundDirectedItemsCount INTEGER , serverScannedDirectedItemsCount INTEGER , serverScannedUndirectedItemsCount INTEGER
  37.           );
  38.  
  39.           CREATE TABLE IF NOT EXISTS DataCollectionField (
  40.             id INTEGER PRIMARY KEY AUTOINCREMENT,
  41.              name TEXT , tabOrder INTEGER
  42.           );
  43.  
  44.           CREATE TABLE IF NOT EXISTS FeatureUsage (
  45.             continuousScanMode BOOLEAN , linePriceMode BOOLEAN , shortcutKeyUsed BOOLEAN
  46.           );
  47.  
  48.           CREATE TABLE IF NOT EXISTS Item (
  49.             activeCount INTEGER , activePrice DOUBLE , shortcutKeyUsed BOOLEAN , anomalyIDs INTEGER , brand TEXT , brandID INTEGER , changed BOOLEAN , collected BOOLEAN , comment TEXT , isActivePastPrice BOOLEAN , isDirected BOOLEAN , isEditMode BOOLEAN , isNewItem BOOLEAN , isOriginal BOOLEAN , isPrivateLabel BOOLEAN , isRegularPastPrice BOOLEAN , isTertiaryPastPrice BOOLEAN , itemDescriptionChanged  BOOLEAN , name TEXT , notFound  BOOLEAN , pack INTEGER , pastActiveCount INTEGER , pastActivePrice DOUBLE , pastPricePerLb BOOLEAN , pastRegularCount INTEGER , pastRegularPrice DOUBLE , pastScanDate DATE , pastTertiaryCount INTEGER ,
  50.             pastTertiaryPrice DOUBLE , pricePerLb  BOOLEAN , regularCount INTEGER , regularPrice DOUBLE , scanDate DATE , scanned BOOLEAN , size DECIMAL , sortOrder INTEGER , symbologyID INTEGER , tertiaryCount INTEGER , tertiaryPrice DOUBLE , uomID INTEGER , upcDisplayed TEXT , upcPadded TEXT , upcUserInputted TEXT
  51.           );
  52.  
  53.           CREATE TABLE IF NOT EXISTS ItemDescription (
  54.             brand TEXT , brandID INTEGER , isPrivateLabel BOOLEAN , name TEXT , pack INTEGER , size DECIMAL , uomID INTEGER , upc TEXT
  55.           );
  56.  
  57.           CREATE TABLE IF NOT EXISTS ItemDescriptionsMeta (
  58.             timestamp DATE
  59.           );
  60.  
  61.           CREATE TABLE IF NOT EXISTS ItemScan (
  62.             peopleID INTEGER , priceCheckID INTEGER , scanDate DATE , storeID INTEGER , subcategoryID  INTEGER , symbologyID INTEGER , upcPadded TEXT , upcUserInputted TEXT
  63.           );
  64.  
  65.           CREATE TABLE IF NOT EXISTS Note (
  66.             changed BOOLEAN , text TEXT
  67.           );
  68.  
  69.           CREATE TABLE IF NOT EXISTS PastPriceItem (
  70.             activeCount INTEGER , activePrice DOUBLE , pricePerLb BOOLEAN , regularCount INTEGER , regularPrice DOUBLE , scanDate DATE , tertiaryCount  INTEGER , tertiaryPrice DOUBLE , upcPadded TEXT
  71.           );
  72.  
  73.           CREATE TABLE IF NOT EXISTS Photo (
  74.             changed BOOLEAN , timestamp DATE , image BLOB , thumbnail BLOB
  75.           );
  76.  
  77.           CREATE TABLE IF NOT EXISTS PriceCheck (
  78.             id INTEGER PRIMARY KEY AUTOINCREMENT ,  autoSetNewItem BOOLEAN , categoriesNeedsRefreshed BOOLEAN , disableActivePrice BOOLEAN , disableTertiaryPrice BOOLEAN , endDate DATE , hasScrapeData BOOLEAN , maxPriceInput DOUBLE , message TEXT , name TEXT , showPastPrice BOOLEAN , showPrivateLabelFlagInApp BOOLEAN , startDate DATE , subcategoriesNeedsRefreshed BOOLEAN , type INTEGER
  79.           );
  80.  
  81.           CREATE TABLE IF NOT EXISTS PriceCheckAttachment (
  82.             modifiedDate DATE , url TEXT
  83.           );
  84.  
  85.           CREATE TABLE IF NOT EXISTS Settings (
  86.             helpText TEXT , inactiveTimeout DOUBLE , linePricingDisabled BOOLEAN , logLevel INTEGER , maxPriceInput DOUBLE , regularActiveVariancePercentage DOUBLE , regularTertiaryVariancePercentage DOUBLE , storesCount INTEGER  
  87.           );
  88.  
  89.           CREATE TABLE IF NOT EXISTS SpentTimeRecord (
  90.             categoryID DOUBLE , enterDate DATE , leaveDate DATE , pageID TEXT , storeID DOUBLE , subcategoryID DOUBLE
  91.           );
  92.  
  93.           CREATE TABLE IF NOT EXISTS Store (
  94.             id INTEGER PRIMARY KEY AUTOINCREMENT, city TEXT , latitude DOUBLE , longitude DOUBLE , name TEXT , phone TEXT , postalCode TEXT , state TEXT , street TEXT , unrestrictedScanAccount  BOOLEAN
  95.           );
  96.  
  97.           CREATE TABLE IF NOT EXISTS Subcategory (
  98.             id INTEGER PRIMARY KEY AUTOINCREMENT, acceptableRangePriceMax DOUBLE , acceptableRangePriceMin DOUBLE , expectedDirectedItemsCount INTEGER , expectedUndirectedItemsCount INTEGER , fullCheck BOOLEAN ,
  99.             isAssigned BOOLEAN , isUndirectedPriceCheckCompleted BOOLEAN , isUploading BOOLEAN , localNotFoundDirectedItemsCount INTEGER , localScannedDirectedItemsCount INTEGER , localScannedUndirectedItemsCount INTEGER ,
  100.             name TEXT , newItemPhotoPromptExclusion BOOLEAN , pastPriceVariancePercent DOUBLE , priceCheckType INTEGER , serverNotFoundDirectedItemsCount INTEGER , serverScannedDirectedItemsCount INTEGER , serverScannedUndirectedItemsCount INTEGER , shortcutKey1Text TEXT , shortcutKey2Text TEXT , shortcutKey3Text TEXT
  101.           );
  102.  
  103.           CREATE TABLE IF NOT EXISTS UnitOfMeasure (
  104.             id INTEGER PRIMARY KEY AUTOINCREMENT, abbreviation TEXT , name TEXT
  105.           );
  106.  
  107.           CREATE TABLE IF NOT EXISTS ValidationPrompt (
  108.             userAction TEXT , validationText TEXT
  109.           );
  110.  
  111.  
  112.  
  113.  
  114.         `;
  115.         await database.transaction(tx => tx.executeSql(createAllTables));
  116.         console.log('Tables created successfully');
  117.  
  118.         setDb(database);
  119.       } catch (error) {
  120.         console.log(error);
  121.       }
  122.     };
  123.  
  124.     initDatabase();
  125.   }, []);
  126.  
  127.   const insertAlternativePriceReason = async (
  128.     alternativePriceReason: RW3AlternativePriceReason,
  129.   ) => {
  130.     try {
  131.       const result = await db.executeSql(
  132.         'INSERT INTO AlternativePriceReason (id, name, activeDefault, tertiaryDefault) VALUES (?, ?, ?, ?)',
  133.         [
  134.           alternativePriceReason.id,
  135.           alternativePriceReason.name,
  136.           alternativePriceReason.activeDefault,
  137.           alternativePriceReason.tertiaryDefault,
  138.         ],
  139.       );
  140.       console.log('Alternative Price Reason inserted successfully', result);
  141.     } catch (error) {
  142.       console.log('Error:', error);
  143.     }
  144.   };
  145.  
  146.   return (
  147.     <DatabaseContext.Provider value={db}>{children}</DatabaseContext.Provider>
  148.   );
  149. };
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement