<commit_before>//
//  Insert.swift
//  PostgresStORM
//
//  Created by <NAME> on 2016-09-24.
//
//

import Foundation
import StORM
import PostgreSQL

extension PostgresStORM {


	public func insert(cols: [String], params: [Any]) throws -> Any {
		let (idname, _) = firstAsKey()
		do {
			return try insert(cols: cols, params: params, idcolumn: idname)
		} catch {
			throw StORMError.error(error.localizedDescription)
		}
	}

	public func insert(cols: [String], params: [Any], idcolumn: String) throws -> Any {

		// PostgreSQL specific insert staement exec
		var paramString = [String]()
		var substString = [String]()
		for i in 0..<params.count {
			paramString.append(String(describing: params[i]))
			substString.append("$\(i+1)")
		}
		let str = "INSERT INTO \(self.table) (\(cols.joined(separator: ","))) VALUES(\(substString.joined(separator: ","))) RETURNING \(idcolumn)"

		do {
			let response = try exec(str, params: paramString)<commit_msg>insert as data<commit_after>//
//  Insert.swift
//  PostgresStORM
//
//  Created by <NAME> on 2016-09-24.
//
//

import Foundation
import StORM
import PostgreSQL

extension PostgresStORM {


	@discardableResult
	public func insert(_ data: [(String, Any)]) throws -> Any {

		var keys = [String]()
		var vals = [String]()
		for i in data {
			keys.append(i.0)
			vals.append(String(describing: i.1))
		}
		do {
			return try insert(cols: keys, params: vals)
		} catch {
			throw StORMError.error(error.localizedDescription)
		}
	}


	public func insert(cols: [String], params: [Any]) throws -> Any {
		let (idname, _) = firstAsKey()
		do {
			return try insert(cols: cols, params: params, idcolumn: idname)
		} catch {
			throw StORMError.error(error.localizedDescription)
		}
	}

	public func insert(cols: [String], params: [Any], idcolumn: String) throws -> Any {

		// PostgreSQL specific insert staement exec
		var paramString = [String]()
		var substString = [String]()
		for i in 0..<params.count {
			paramString.append(String(describing: params[i]))
			substString.append("$\(i+1)")
		}
		let str = "INSERT INTO \(self.table()) (\(cols.joined(separator: ","))) VALUES(\(substString.joined(separator: ","))) RETURNING \(idcolumn)"

		do {
			let response = try exec(str, params: paramString)
{'fraction_non_alphanumeric': 0.15422222222222223, 'fraction_numerical': 0.009777777777777778, 'mean_word_length': 3.69937369519833, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '6232559', 'n_tokens_mistral': 855, 'n_tokens_neox': 757, 'n_words': 199}