             fragments = parts
         }
 
-        var sql = ""
-        let escaped = params.map(escape)
-
-        escaped.enumerate().forEach{ index, part in
-            sql = sql + fragments[index] + part
-        }
-
-        sql = sql + fragments.last!
+        let sql = buildSQL(fragments, params: params)
         return sql
 
     }

        if action == .Insert {
            statements = statements + buildInsert(obj, upsert: isUpsert) + "\n"
        } else {
            statements = statements + buildUpdate(obj) + "\n"
        }
    }

    public func execute(){

            fragments = parts
        }

        let sql = buildSQL(fragments, params: values.queryParams)
        return sql
    }

    func buildDelete<T: AmigoModel>(obj: T) -> String {
        let model = obj.amigoModel
        let fragments: [String]
        let params = [model.primaryKey.modelValue(obj)!]

        if let parts = deleteCache[obj.qualifiedName] {
            fragments = parts
        } else {
            let (sql, _) = session.deleteSQL(obj)
            let parts = sql.componentsSeparatedByString("?")

            deleteCache[obj.qualifiedName] = parts
            fragments = parts
        }

        let sql = buildSQL(fragments, params: params)
        return sql
    }

    func buildSQL(queryFragments: [String], params: [AnyObject]) -> String{
        var sql = ""
        let escaped = params.map(escape)

        escaped.enumerate().forEach{ index, part in
            sql = sql + queryFragments[index] + part
        }

        sql = sql + queryFragments.last!
        return sql
    }

