<commit_before>//
//  Select.swift
//  SQLiteStORM
//
//  Created by <NAME> on 2016-10-07.
//
//

import StORM
import PerfectLogger

/// Provides select functions as an extension to the main class.
extension SQLiteStORM {

	/// Retrieves all rows in the table, only limited by the cursor (9,999,999 rows)
	public func findAll() throws {
		do {
			let cursor = StORMCursor(limit: 9999999,offset: 0)
			try select(
				columns: [],
				whereclause: "1",
				params: [],
				orderby: [],
				cursor: cursor
			)
		} catch {
			throw StORMError.error("\(error)")
		}
	}

	/// Select function with specific where clause.
	/// Parameterized statements are used, so all params should be passed in using the [Any] params array.
	/// The whereclause should be specified in the following format: "name = $1 AND email LIKE $2"
	/// An orderby array can be specified in a String array like ["name DESC","email ASC"]
	/// A StORMCursor can be supplied, otherwise the default values are used.
	/// Note that the joins, having and groupBy functionality is unimplemented at this time.
	/// The select function will populate the object with the results of the query.
	public func select(
		whereclause:	String,
		params:			[Any],
		orderby:		[String],
		cursor:			StORMCursor = StORMCursor(),
		joins:			[StORMDataSourceJoin] = [],
		having:			[String] = [],
		groupBy:		[String] = []
		) throws {
		do {
			try select(
					columns: [],
					whereclause: whereclause,
					params: params,
					orderby: orderby,
					cursor: cursor,
					joins: joins,
					having: having,
					groupBy: groupBy
			)
		} catch {
			throw StORMError.error("\(error)")
		}
	}

	/// Select function with specific where clause, and spefified columns to return.
	/// Parameterized statements are used, so all params should be passed in using the [Any] params array.
	/// The whereclause should be specified in the following format: "name = $1 AND email LIKE $2"
	/// An orderby array can be specified in a String array like ["name DESC","email ASC"]
	/// A StORMCursor can be supplied, otherwise the default values are used.
	/// Note that the joins, having and groupBy functionality is unimplemented at this time.
	/// The select function will populate the object with the results of the query.
	public func select(
		columns:		[String],
		whereclause:	String,
		params:			[Any],
		orderby:		[String],
		cursor:			StORMCursor = StORMCursor(),
		joins:			[StORMDataSourceJoin] = [],
		having:			[String] = [],
		groupBy:		[String] = []
		) throws {

		let clauseCount = "COUNT(*) AS counter"
		var clauseSelectList = "*"
		var clauseWhere = ""
		var clauseOrder = ""

		if columns.count > 0 {
			clauseSelectList = columns.joined(separator: ",")
		} else {
			var keys = [String]()
			for i in cols() {
				keys.append(i.0)
			}
			clauseSelectList = keys.joined(separator: ",")
		}
		if whereclause.characters.count > 0 {
			clauseWhere = " WHERE \(whereclause)"
		}

		var paramsString = [String]()
		for i in 0..<params.count {
			paramsString.append(String(describing: params[i]))
		}
		if orderby.count > 0 {
			clauseOrder = " ORDER BY \(orderby.joined(separator: ", "))"
		}
		do {
			//print("EXEC: SELECT \(clauseCount) FROM \(table()) \(clauseWhere)")
			let getCount = try execRows("SELECT \(clauseCount) FROM \(table()) \(clauseWhere)", params: paramsString)
			let numrecords = getCount.first?.data["counter"]! as? Int ?? 0
			results.cursorData = StORMCursor(
				limit: cursor.limit,
				offset: cursor.offset,
				totalRecords: numrecords)


			// SELECT ASSEMBLE
			var str = "SELECT \(clauseSelectList) FROM \(table()) \(clauseWhere) \(clauseOrder)"


			// TODO: Add joins, having, groupby

			if cursor.limit > 0 {
				str += " LIMIT \(cursor.limit)"
			}
			if cursor.offset > 0 {
				str += " OFFSET \(cursor.offset)"
			}

			//print("EXEC: \(str)")
			// save results into ResultSet
			results.rows = try execRows(str, params: paramsString)

			// id no records found throw an error .noRecordFound
			if results.cursorData.totalRecords == 0 {
//				print("************ NO RECORDS FOUND *****************")
				self.error = StORMError.noRecordFound
//				print("************ \(self.error.string()) *****************")
				throw StORMError.noRecordFound
			}

			// if just one row returned, act like a "GET"
			if results.cursorData.totalRecords == 1 { makeRow() }

		} catch {
			LogFile.error("Error msg: \(error)", logFile: "./StORMlog.txt")
			self.errorMsg = "\(error)"
			throw error
		}
	}

}<commit_msg>removing legacy no records found as an error<commit_after>//
//  Select.swift
//  SQLiteStORM
//
//  Created by <NAME> on 2016-10-07.
//
//

import StORM
import PerfectLogger

/// Provides select functions as an extension to the main class.
extension SQLiteStORM {

	/// Retrieves all rows in the table, only limited by the cursor (9,999,999 rows)
	public func findAll() throws {
		do {
			let cursor = StORMCursor(limit: 9999999,offset: 0)
			try select(
				columns: [],
				whereclause: "1",
				params: [],
				orderby: [],
				cursor: cursor
			)
		} catch {
			throw StORMError.error("\(error)")
		}
	}

	/// Select function with specific where clause.
	/// Parameterized statements are used, so all params should be passed in using the [Any] params array.
	/// The whereclause should be specified in the following format: "name = $1 AND email LIKE $2"
	/// An orderby array can be specified in a String array like ["name DESC","email ASC"]
	/// A StORMCursor can be supplied, otherwise the default values are used.
	/// Note that the joins, having and groupBy functionality is unimplemented at this time.
	/// The select function will populate the object with the results of the query.
	public func select(
		whereclause:	String,
		params:			[Any],
		orderby:		[String],
		cursor:			StORMCursor = StORMCursor(),
		joins:			[StORMDataSourceJoin] = [],
		having:			[String] = [],
		groupBy:		[String] = []
		) throws {
		do {
			try select(
					columns: [],
					whereclause: whereclause,
					params: params,
					orderby: orderby,
					cursor: cursor,
					joins: joins,
					having: having,
					groupBy: groupBy
			)
		} catch {
			throw StORMError.error("\(error)")
		}
	}

	/// Select function with specific where clause, and spefified columns to return.
	/// Parameterized statements are used, so all params should be passed in using the [Any] params array.
	/// The whereclause should be specified in the following format: "name = $1 AND email LIKE $2"
	/// An orderby array can be specified in a String array like ["name DESC","email ASC"]
	/// A StORMCursor can be supplied, otherwise the default values are used.
	/// Note that the joins, having and groupBy functionality is unimplemented at this time.
	/// The select function will populate the object with the results of the query.
	public func select(
		columns:		[String],
		whereclause:	String,
		params:			[Any],
		orderby:		[String],
		cursor:			StORMCursor = StORMCursor(),
		joins:			[StORMDataSourceJoin] = [],
		having:			[String] = [],
		groupBy:		[String] = []
		) throws {

		let clauseCount = "COUNT(*) AS counter"
		var clauseSelectList = "*"
		var clauseWhere = ""
		var clauseOrder = ""

		if columns.count > 0 {
			clauseSelectList = columns.joined(separator: ",")
		} else {
			var keys = [String]()
			for i in cols() {
				keys.append(i.0)
			}
			clauseSelectList = keys.joined(separator: ",")
		}
		if whereclause.characters.count > 0 {
			clauseWhere = " WHERE \(whereclause)"
		}

		var paramsString = [String]()
		for i in 0..<params.count {
			paramsString.append(String(describing: params[i]))
		}
		if orderby.count > 0 {
			clauseOrder = " ORDER BY \(orderby.joined(separator: ", "))"
		}
		do {
			//print("EXEC: SELECT \(clauseCount) FROM \(table()) \(clauseWhere)")
			let getCount = try execRows("SELECT \(clauseCount) FROM \(table()) \(clauseWhere)", params: paramsString)
			let numrecords = getCount.first?.data["counter"]! as? Int ?? 0
			results.cursorData = StORMCursor(
				limit: cursor.limit,
				offset: cursor.offset,
				totalRecords: numrecords)


			// SELECT ASSEMBLE
			var str = "SELECT \(clauseSelectList) FROM \(table()) \(clauseWhere) \(clauseOrder)"


			// TODO: Add joins, having, groupby

			if cursor.limit > 0 {
				str += " LIMIT \(cursor.limit)"
			}
			if cursor.offset > 0 {
				str += " OFFSET \(cursor.offset)"
			}

			// save results into ResultSet
			results.rows = try execRows(str, params: paramsString)

			// if just one row returned, act like a "GET"
			if results.cursorData.totalRecords == 1 { makeRow() }

		} catch {
			LogFile.error("Error msg: \(error)", logFile: "./StORMlog.txt")
			self.errorMsg = "\(error)"
			throw error
		}
	}

}
{'fraction_non_alphanumeric': 0.13433179723502303, 'fraction_numerical': 0.008640552995391706, 'mean_word_length': 3.0622367805334583, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1263144', 'n_tokens_mistral': 3219, 'n_tokens_neox': 2724, 'n_words': 910}