<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Common;
using System.Data;
using Biggy.Extensions;
using System.Dynamic;
using System.Text.RegularExpressions;

namespace Biggy
{
  public abstract class BiggyRelationalStore<T> : IBiggyStore<T>, IUpdateableBiggyStore<T>, IQueryableBiggyStore<T> where T : new() {
    public DbCache Cache { get; set; }

    public abstract DbConnection OpenConnection();
    public abstract string GetInsertReturnValueSQL(string delimitedPkColumn);
    public abstract string GetSingleSelect(string delimitedTableName, string where);
    public abstract string BuildSelect(string where, string orderBy = "", int limit = 0);
    public virtual  string ConnectionString { get { return this.Cache.ConnectionString; } }


    public DBTableMapping tableMapping { get; set; }
    public DbColumnMapping PrimaryKeyMapping { get; set; }

    protected BiggyRelationalStore() { }

    public BiggyRelationalStore(DbCache dbCache) {
      this.Cache = dbCache;
      this.tableMapping = this.getTableMappingForT();

      // Is there an auto PK? If so, set the member variable:
      if(this.tableMapping.PrimaryKeyMapping.Count == 1) {
        if(this.tableMapping.PrimaryKeyMapping[0].IsAutoIncementing) {
          this.PrimaryKeyMapping = this.tableMapping.PrimaryKeyMapping[0];
        }
      }
    }


    public virtual DBTableMapping getTableMappingForT() {
      return this.Cache.getTableMappingFor<T>();
    }


    /// <summary>
    /// Returns all records complying with the passed-in WHERE clause and arguments, 
    /// ordered as specified, limited (TOP) by limit.
    /// </summary>
    public virtual IEnumerable<T> All<T>(string where = "", string orderBy = "", int limit = 0, string columns = "*", params object[] args) where T : new() {
      string sql = this.BuildSelect(where, orderBy, limit);
      var formatted = string.Format(sql, columns, this.tableMapping.DelimitedTableName);
      return Query<T>(formatted, args);
    }

    public virtual T Insert(T item) {
      if(this.BeforeSave(item)) {
        using (var conn = Cache.OpenConnection()) {
          var cmd = (DbCommand)this.CreateInsertCommand(item);
          cmd.CommandText += this.GetInsertReturnValueSQL(this.PrimaryKeyMapping.DelimitedColumnName);<commit_msg>BiggyRelationalStore: Fix Primary Key Mapping to set non-serial PKs. Close #87<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Common;
using System.Data;
using Biggy.Extensions;
using System.Dynamic;
using System.Text.RegularExpressions;

namespace Biggy
{
  public abstract class BiggyRelationalStore<T> : IBiggyStore<T>, IUpdateableBiggyStore<T>, IQueryableBiggyStore<T> where T : new() {
    public DbCache Cache { get; set; }

    public abstract DbConnection OpenConnection();
    public abstract string GetInsertReturnValueSQL(string delimitedPkColumn);
    public abstract string GetSingleSelect(string delimitedTableName, string where);
    public abstract string BuildSelect(string where, string orderBy = "", int limit = 0);
    public virtual  string ConnectionString { get { return this.Cache.ConnectionString; } }


    public DBTableMapping tableMapping { get; set; }
    public DbColumnMapping PrimaryKeyMapping { get; set; }

    protected BiggyRelationalStore() { }

    public BiggyRelationalStore(DbCache dbCache) {
      this.Cache = dbCache;
      this.tableMapping = this.getTableMappingForT();

      // Is there a PK? If so, set the member variable:
      if(this.tableMapping.PrimaryKeyMapping.Count == 1) {
        this.PrimaryKeyMapping = this.tableMapping.PrimaryKeyMapping[0];
      }
    }


    public virtual DBTableMapping getTableMappingForT() {
      return this.Cache.getTableMappingFor<T>();
    }


    /// <summary>
    /// Returns all records complying with the passed-in WHERE clause and arguments, 
    /// ordered as specified, limited (TOP) by limit.
    /// </summary>
    public virtual IEnumerable<T> All<T>(string where = "", string orderBy = "", int limit = 0, string columns = "*", params object[] args) where T : new() {
      string sql = this.BuildSelect(where, orderBy, limit);
      var formatted = string.Format(sql, columns, this.tableMapping.DelimitedTableName);
      return Query<T>(formatted, args);
    }

    public virtual T Insert(T item) {
      if(this.BeforeSave(item)) {
        using (var conn = Cache.OpenConnection()) {
          var cmd = (DbCommand)this.CreateInsertCommand(item);
          cmd.CommandText += this.GetInsertReturnValueSQL(this.PrimaryKeyMapping.DelimitedColumnName);
{'fraction_non_alphanumeric': 0.10194174757281553, 'fraction_numerical': 0.002321654706627269, 'mean_word_length': 3.825865580448065, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '4911859', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1292, 'n_words': 361}