private Task UpsertAsyncInternal(string tableName, IEnumerable <JObject> items, bool ignoreMissingColumns)
        {
            TableDefinition table = GetTable(tableName);

            var first = items.FirstOrDefault();

            if (first == null)
            {
                return(Task.FromResult(0));
            }

            // Get the columns which we want to map into the database.
            var columns = new List <ColumnDefinition>();

            foreach (var prop in first.Properties())
            {
                ColumnDefinition column;

                // If the column is coming from the server we can just ignore it,
                // otherwise, throw to alert the caller that they have passed an invalid column
                if (!table.TryGetValue(prop.Name, out column) && !ignoreMissingColumns)
                {
                    throw new InvalidOperationException(string.Format("Column with name '{0}' is not defined on the local table '{1}'.", prop.Name, tableName));
                }

                if (column != null)
                {
                    columns.Add(column);
                }
            }

            if (columns.Count == 0)
            {
                // no query to execute if there are no columns in the table
                return(Task.FromResult(0));
            }

            return(this.operationSemaphore.WaitAsync()
                   .ContinueWith(t =>
            {
                try
                {
                    this.ExecuteNonQueryInternal("BEGIN TRANSACTION", null);

                    BatchInsert(tableName, items, columns.Where(c => c.Name.Equals(MobileServiceSystemColumns.Id)).Take(1).ToList());
                    BatchUpdate(tableName, items, columns);

                    this.ExecuteNonQueryInternal("COMMIT TRANSACTION", null);
                }
                finally
                {
                    this.operationSemaphore.Release();
                }
            }));
        }
Exemple #2
0
        private JObject ReadRow(TableDefinition table, SqliteDataReader reader)
        {
            JObject row = new JObject();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                if (!reader.IsDBNull(i))
                {
                    string name  = reader.GetName(i);
                    object value = new object();
                    if (reader.GetDataTypeName(i) == "DATETIME")
                    {
                        try
                        {
                            value = reader.GetDouble(reader.GetOrdinal(name));
                        }
                        catch (FormatException ex)
                        {
                            value = reader.GetInt64(reader.GetOrdinal(name));
                        }
                    }
                    else
                    {
                        value = reader.GetValue(i);
                    }

                    ColumnDefinition column;

                    if (table.TryGetValue(name, out column))
                    {
                        JToken jVal = SqlHelpers.DeserializeValue(value, column.StoreType, column.JsonType);
                        row[name] = jVal;
                    }
                    else
                    {
                        row[name] = value == null ? null : JToken.FromObject(value);
                    }
                }
            }
            return(row);
        }
        private JObject ReadRow(TableDefinition table, sqlite3_stmt statement)
        {
            var row = new JObject();

            for (int i = 0; i < raw.sqlite3_column_count(statement); i++)
            {
                string name  = raw.sqlite3_column_name(statement, i).utf8_to_string();
                object value = SQLitePCLRawHelpers.GetValue(statement, i);

                if (table.TryGetValue(name, out ColumnDefinition column))
                {
                    JToken jVal = SqlHelpers.DeserializeValue(value, column.StoreType, column.JsonType);
                    row[name] = jVal;
                }
                else
                {
                    row[name] = value == null ? null : JToken.FromObject(value);
                }
            }
            return(row);
        }
Exemple #4
0
        private JObject ReadRow(TableDefinition table, ISQLiteStatement statement)
        {
            var row = new JObject();

            for (int i = 0; i < statement.ColumnCount; i++)
            {
                string name  = statement.ColumnName(i);
                object value = statement[i];

                ColumnDefinition column;
                if (table.TryGetValue(name, out column))
                {
                    JToken jVal = SqlHelpers.DeserializeValue(value, column.StoreType, column.JsonType);
                    row[name] = jVal;
                }
                else
                {
                    row[name] = value == null ? null : JToken.FromObject(value);
                }
            }
            return(row);
        }