コード例 #1
0
        /**
         * Stores data as documents in the collection.
         */
        public int addData(JArray jsonArray, bool markDirty, JSONStoreAddOptions options)
        {
            // get the shared manager
            JSONStoreSQLLite store = JSONStoreSQLLite.sharedManager();

            if (store == null)
            {
                //JSONStoreLoggerError(@"Error: JSON_STORE_DATABASE_NOT_OPEN, code: %d", rc);
                throw new JSONStoreException(JSONStoreConstants.JSON_STORE_DATABASE_NOT_OPEN);
            }

            int numWorked = 0;

            lock (JSONStore.lockThis)
            {
                //Start transaction if no user transaction is already open
                if (!JSONStore.transactionInProgress)
                {
                    store.startTransaction();
                }

                foreach (JObject data in jsonArray)
                {
                    // loop through all the data in the array and attempt to store
                    if (store.storeObject(data, collectionName, markDirty, options.additionalSearchFields))
                    {
                        numWorked++;
                    }
                    else
                    {
                        //If we can't store all the data, we rollback and go
                        //to the error callback
                        if (!JSONStore.transactionInProgress)
                        {
                            store.rollbackTransaction();
                        }
                        throw new JSONStoreException(JSONStoreConstants.JSON_STORE_PERSISTENT_STORE_FAILURE);
                    }
                }

                // things worked, commit
                if (!JSONStore.transactionInProgress)
                {
                    store.commitTransaction();
                }
                return(numWorked);
            }
        }