<commit_before>using System;
using System.Collections;

namespace System.Data.LightDatamodel
{
	/// <summary>
	/// Summary description for SQLiteDataProvider.
	/// </summary>
	public class SQLiteDataProvider : GenericDataProvider
	{
		public SQLiteDataProvider(IDbConnection cmd)
		{
			m_connection = cmd;
		}

		public override string GetPrimaryKey(string tablename)
		{
			if(m_connection.State != ConnectionState.Open) m_connection.Open();
			IDbCommand cmd = m_connection.CreateCommand();
			cmd.CommandText = "SELECT Name FROM SQLITE_MASTER WHERE name=" + AddParameter(cmd, tablename) + " AND type='table'";
			IDataReader rd = cmd.ExecuteReader();
			if (!rd.Read())
				throw new Exception("Failed to read SQL from SQLITE_MASTER for table " + tablename);
			string sql = rd.GetValue(0).ToString();

			//TODO: How do we read the primary key, in case we don't use Autonumbers?
			//Do we parse the "create" sql?
			return "OID";
		}

		public override string[] GetTablenames()
		{
			if(m_connection.State != ConnectionState.Open) m_connection.Open();
			IDbCommand cmd = m_connection.CreateCommand();
			cmd.CommandText = "SELECT Name FROM SQLITE_MASTER WHERE type='table' AND name NOT LIKE 'sqlite_%'";
			IDataReader rd = cmd.ExecuteReader();
			ArrayList tb = new ArrayList();
			while(rd.Read())
				tb.Add(rd.GetValue(0).ToString());
			return (string[])tb.ToArray(typeof(string));
		}

		public override string QuoteColumnname(string columnname)
		{
			return "\"" + columnname.Replace("\"", "\\\"") + "\"";
		}

		public override string QuoteTablename(string tablename)
		{
			//TODO: Is this the correct way to escape the table names?
			return "\"" + tablename.Replace("\"", "\\\"") + "\"";
		}

		public override void BeginTransaction(Guid id)
		{
			IDbCommand cmd = m_connection.CreateCommand();
			cmd.CommandText = "BEGIN TRANSACTION '" + id.ToString() + "'";
			cmd.ExecuteNonQuery();
		}

		public override void CommitTransaction(Guid id)
		{
			IDbCommand cmd = m_connection.CreateCommand();
			cmd.CommandText = "COMMIT TRANSACTION '" + id.ToString() + "'";
			cmd.ExecuteNonQuery();
		}

		public override void RollbackTransaction(Guid id)
		{
			IDbCommand cmd = m_connection.CreateCommand();
			cmd.CommandText = "ROLLBACK TRANSACTION '" + id.ToString() + "'";
			cmd.ExecuteNonQuery();
		}

		public override string GetLastAutogeneratedValue(string tablename)
		{
			return "SELECT last_insert_rowid()";
		}


	}
}
<commit_msg>Fixed some bugs in the SQLite provider, and implemented a "guessing" model for detecting the primary key of a table<commit_after>using System;
using System.Collections;

namespace System.Data.LightDatamodel
{
	/// <summary>
	/// Summary description for SQLiteDataProvider.
	/// </summary>
	public class SQLiteDataProvider : GenericDataProvider
	{
		public SQLiteDataProvider(IDbConnection cmd)
		{
			m_connection = cmd;
		}

		public override string GetPrimaryKey(string tablename)
		{
			if(m_connection.State != ConnectionState.Open) m_connection.Open();
			IDbCommand cmd = m_connection.CreateCommand();
			cmd.CommandText = "SELECT SQL FROM SQLITE_MASTER WHERE name=" + AddParameter(cmd, tablename) + " AND type='table'";
			IDataReader rd = null; 
			try
			{
				rd = cmd.ExecuteReader();
				if (!rd.Read())
					throw new Exception("Failed to read SQL from SQLITE_MASTER for table " + tablename);
				string sql = rd.GetValue(0).ToString();
				int p = sql.ToLower().IndexOf(" primary");

				if (p > 0)
				{
					sql = sql.Substring(0, p + 1).Trim();
					p = sql.LastIndexOfAny(new char[] {',', '('});
					if (p >= 0)
						sql = sql.Substring(p + 1).Trim();
					p = sql.LastIndexOf(" ");
					p = sql.LastIndexOfAny(new char[] {'"', '\'', ']' });
					if (p > 0)
						sql = sql.Substring(0, p + 1);
					else
					{
						p = sql.IndexOf(" ");
						if (p > 0)
							sql = sql.Substring(0, p);
					}
						



					if (sql.IndexOfAny(new char[] {'"', '\'', '[' }) == 0)
						return sql.Substring(1, sql.Length - 2);
					else
						return sql;
				}
	
			} 
			finally 
			{
				try { if (rd != null) rd.Close(); }
				catch {}
			}

			return "";
		}

		public override string[] GetTablenames()
		{
			if(m_connection.State != ConnectionState.Open) m_connection.Open();
			IDbCommand cmd = m_connection.CreateCommand();
			cmd.CommandText = "SELECT Name FROM SQLITE_MASTER WHERE type='table' AND name NOT LIKE 'sqlite_%'";
			IDataReader rd = null;
			ArrayList tb = new ArrayList();
			try
			{
				rd = cmd.ExecuteReader();
				while(rd.Read())
					tb.Add(rd.GetValue(0).ToString());
				rd.Close();
			}
			finally
			{
				try { if (rd != null) rd.Close(); }
				catch {}
			}
			return (string[])tb.ToArray(typeof(string));
		}

		public override string QuoteColumnname(string columnname)
		{
			return "\"" + columnname.Replace("\"", "\\\"") + "\"";
		}

		public override string QuoteTablename(string tablename)
		{
			//TODO: Is this the correct way to escape the table names?
			return "\"" + tablename.Replace("\"", "\\\"") + "\"";
		}

		public override void BeginTransaction(Guid id)
		{
			IDbCommand cmd = m_connection.CreateCommand();
			cmd.CommandText = "BEGIN TRANSACTION '" + id.ToString() + "'";
			cmd.ExecuteNonQuery();
			cmd.Dispose();
		}

		public override void CommitTransaction(Guid id)
		{
			IDbCommand cmd = m_connection.CreateCommand();
			cmd.CommandText = "COMMIT TRANSACTION '" + id.ToString() + "'";
			cmd.ExecuteNonQuery();
			cmd.Dispose();
		}

		public override void RollbackTransaction(Guid id)
		{
			IDbCommand cmd = m_connection.CreateCommand();
			cmd.CommandText = "ROLLBACK TRANSACTION '" + id.ToString() + "'";
			cmd.ExecuteNonQuery();
			cmd.Dispose();
		}

		public override string GetLastAutogeneratedValue(string tablename)
		{
			return "SELECT last_insert_rowid()";
		}


	}
}

{'fraction_non_alphanumeric': 0.13896729204715258, 'fraction_numerical': 0.0028225136974929436, 'mean_word_length': 2.881443298969072, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '7132400', 'n_tokens_mistral': 2310, 'n_tokens_neox': 1870, 'n_words': 452}