<commit_before>class Backbone.SyncableModel extends Backbone.Model
  initialize: (options) ->
    @db = window.BlueCarbon.SQLiteDb
    super

  save: (key, value, options) ->
    this.sync = this.sqliteSync
    super

  fetch: (options) ->
    this.sync = this.sqliteSync
    super

  pushToServer: (options) ->
    super.save()

  sqliteSync: (method, model, options) ->
    attrs = model.toJSON()

    sql = ""
    switch method
      when "create"
        values = []
        # Retrieve all values as an array to be inserted in DB

        sql = []
        sql.push(
          """
            INSERT INTO #{model.constructor.name}
            VALUES (
          """
        )

        for attr, val of attrs
          if _.isArray(val) or _.isObject(val)
            val = JSON.stringify(val)

          sql.push("\"#{val}\", ")

        sql.push(")")

        sql = sql.join(" ")
      when "update"
        sql = []
        for attr, val of attrs
          sql.push(
            """
              UPDATE #{model.constructor.name}
              SET #{attr}=#{val}
              WHERE id="#{attrs['id']}"<commit_msg>Improved "create" Backbone.sync code for SyncableModel (now matches values to attribute name for INSERT)<commit_after>class Backbone.SyncableModel extends Backbone.Model
  initialize: (options) ->
    @db = window.BlueCarbon.SQLiteDb
    super

  save: (key, value, options) ->
    this.sync = this.sqliteSync
    super

  fetch: (options) ->
    this.sync = this.sqliteSync
    super

  pushToServer: (options) ->
    super.save()

  sqliteSync: (method, model, options) ->
    attrs = model.toJSON()

    sql = ""
    switch method
      when "create"
        fields = []
        values = []

        for attr, val of attrs
          if _.isArray(val) or _.isObject(val)
            val = JSON.stringify(val)

          fields.push(attr)
          values.push(val)

        sql =
          """
            INSERT INTO #{model.constructor.name}
            ( #{fields.join(", ")} )
            VALUES ( #{values.join(", ")} )
          """
      when "update"
        sql = []
        for attr, val of attrs
          sql.push(
            """
              UPDATE #{model.constructor.name}
              SET #{attr}=#{val}
              WHERE id="#{attrs['id']}"
{'fraction_non_alphanumeric': 0.1305300043802015, 'fraction_numerical': 0.0, 'mean_word_length': 1.6839012925969448, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3425350', 'n_tokens_mistral': 688, 'n_tokens_neox': 666, 'n_words': 167}