<commit_before>﻿using System;
using MySql.Data.MySqlClient;
using System.Web.Security;

namespace FacebookSharp.MySqlProvider
{

    /// <remarks>
    /// 
    /// Table structure for MySqlFacebookMembershipProvider
    ///     CREATE TABLE `facebook_users` (
    ///		  `user_name` VARCHAR(60), -- membershipUsername, primary key already enforced as unique and not null
    ///		  `facebook_id` VARCHAR(50) NOT NULL UNIQUE,
    ///		  `access_token` VARCHAR(256),
    ///		  PRIMARY KEY (`user_name`)
    ///		);
    /// </remarks>
    public class MySqlFacebookMembershipProvider : IFacebookMembershipProvider
    {
        private readonly string _tableName;
        private readonly string _connectionString;

        public MySqlFacebookMembershipProvider(string connectionString)
            : this(connectionString, "facebook_users")
        {
        }

        public MySqlFacebookMembershipProvider(string connectionString, string tableName)
        {
            _connectionString = connectionString;
            _tableName = tableName;
        }

        #region Implementation of IFacebookMembershipProvider

        public bool HasLinkedFacebook(string membershipUsername)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd =
                    new MySqlCommand(string.Format("SELECT COUNT(*) FROM {0} WHERE user_name=@user_name", _tableName));
                cmd.Parameters.AddWithValue("@user_name", membershipUsername);
                cn.Open();

                return (long)cmd.ExecuteScalar() == 1;
            }
        }

        public bool HasLinkedFacebook(object membershipProviderUserKey)
        {
            MembershipUser user = Membership.GetUser(membershipProviderUserKey);
            if (user == null)
                throw new FacebookSharpException("User with given membershipProviderUserKey not found.");
            return HasLinkedFacebook(user.UserName);
        }

        public bool IsFacebookUserLinked(string facebookId)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd =
                    new MySqlCommand(string.Format("SELECT COUNT(*) FROM {0} WHERE facebook_id=@facebook_id", _tableName));
                cmd.Parameters.AddWithValue("@facebook_id", facebookId);
                cn.Open();

                return (long)cmd.ExecuteScalar() == 1;
            }
        }

        public void LinkFacebook(string membershipUsername, string facebookId, string accessToken)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd =
                    new MySqlCommand(
                        string.Format(
                            "INSERT INTO {0} (user_name,facebook_id,access_token) VALUES (@user_name,@facebook_id,@access_token)",
                            _tableName), cn);
                cmd.Parameters.AddWithValue("@user_name", membershipUsername);
                cmd.Parameters.AddWithValue("@facebook_id", facebookId);
                cmd.Parameters.AddWithValue("@access_token", accessToken);

                cn.Open();
                cmd.ExecuteNonQuery();
            }
        }

        public void LinkFacebook(object membershipProviderUserKey, string facebookId, string accessToken)
        {
            MembershipUser user = Membership.GetUser(membershipProviderUserKey);
            if (user == null)
                throw new FacebookSharpException("User with given membershipProviderUserKey not found.");

            LinkFacebook(user.UserName, facebookId, accessToken);
        }

        public void UnlinkFacebook(string membershipUsername)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd = new MySqlCommand(
                    string.Format("DELETE FROM {0} WHERE user_name=@user_name", _tableName), cn);
                cmd.Parameters.AddWithValue("@user_name", membershipUsername);

                cn.Open();
                cmd.ExecuteNonQuery();
            }
        }

        public void UnlinkFacebook(object membershipProviderUserKey)
        {
            MembershipUser user = Membership.GetUser(membershipProviderUserKey);
            if (user == null)
                throw new FacebookSharpException("User with given membershipProviderUserKey not found.");

            UnlinkFacebook(user.UserName);
        }

        public void UnlinkFacebookByFacebookId(string facebookId)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd = new MySqlCommand(
                    string.Format("DELETE FROM {0} WHERE facebook_id=@facebook_id", _tableName), cn);
                cmd.Parameters.AddWithValue("@facebook_id", facebookId);

                cn.Open();
                cmd.ExecuteNonQuery();
            }
        }

        public string GetFacebookAccessToken(string membershipUsername)
        {
            throw new NotImplementedException();
        }

        public string GetFacebookAccessToken(object membershipProviderUserKey)
        {
            throw new NotImplementedException();
        }

        public string GetFacebookAccessTokenByFacebookId(string facebookId)
        {
            throw new NotImplementedException();
        }

        public string GetFacebookId(string membershipUsername)
        {
            throw new NotImplementedException();
        }

        public string GetFacebookId(object membershipProviderUserKey)
        {
            throw new NotImplementedException();
        }<commit_msg>+ MySql GetFacebookAccessToken impl<commit_after>﻿using System;
using MySql.Data.MySqlClient;
using System.Web.Security;

namespace FacebookSharp.MySqlProvider
{

    /// <remarks>
    /// 
    /// Table structure for MySqlFacebookMembershipProvider
    ///     CREATE TABLE `facebook_users` (
    ///		  `user_name` VARCHAR(60), -- membershipUsername, primary key already enforced as unique and not null
    ///		  `facebook_id` VARCHAR(50) NOT NULL UNIQUE,
    ///		  `access_token` VARCHAR(256),
    ///		  PRIMARY KEY (`user_name`)
    ///		);
    /// </remarks>
    public class MySqlFacebookMembershipProvider : IFacebookMembershipProvider
    {
        private readonly string _tableName;
        private readonly string _connectionString;

        public MySqlFacebookMembershipProvider(string connectionString)
            : this(connectionString, "facebook_users")
        {
        }

        public MySqlFacebookMembershipProvider(string connectionString, string tableName)
        {
            _connectionString = connectionString;
            _tableName = tableName;
        }

        #region Implementation of IFacebookMembershipProvider

        public bool HasLinkedFacebook(string membershipUsername)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd =
                    new MySqlCommand(string.Format("SELECT COUNT(*) FROM {0} WHERE user_name=@user_name", _tableName));
                cmd.Parameters.AddWithValue("@user_name", membershipUsername);
                cn.Open();

                return (long)cmd.ExecuteScalar() == 1;
            }
        }

        public bool HasLinkedFacebook(object membershipProviderUserKey)
        {
            MembershipUser user = Membership.GetUser(membershipProviderUserKey);
            if (user == null)
                throw new FacebookSharpException("User with given membershipProviderUserKey not found.");
            return HasLinkedFacebook(user.UserName);
        }

        public bool IsFacebookUserLinked(string facebookId)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd =
                    new MySqlCommand(string.Format("SELECT COUNT(*) FROM {0} WHERE facebook_id=@facebook_id", _tableName));
                cmd.Parameters.AddWithValue("@facebook_id", facebookId);
                cn.Open();

                return (long)cmd.ExecuteScalar() == 1;
            }
        }

        public void LinkFacebook(string membershipUsername, string facebookId, string accessToken)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd =
                    new MySqlCommand(
                        string.Format(
                            "INSERT INTO {0} (user_name,facebook_id,access_token) VALUES (@user_name,@facebook_id,@access_token)",
                            _tableName), cn);
                cmd.Parameters.AddWithValue("@user_name", membershipUsername);
                cmd.Parameters.AddWithValue("@facebook_id", facebookId);
                cmd.Parameters.AddWithValue("@access_token", accessToken);

                cn.Open();
                cmd.ExecuteNonQuery();
            }
        }

        public void LinkFacebook(object membershipProviderUserKey, string facebookId, string accessToken)
        {
            MembershipUser user = Membership.GetUser(membershipProviderUserKey);
            if (user == null)
                throw new FacebookSharpException("User with given membershipProviderUserKey not found.");

            LinkFacebook(user.UserName, facebookId, accessToken);
        }

        public void UnlinkFacebook(string membershipUsername)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd = new MySqlCommand(
                    string.Format("DELETE FROM {0} WHERE user_name=@user_name", _tableName), cn);
                cmd.Parameters.AddWithValue("@user_name", membershipUsername);

                cn.Open();
                cmd.ExecuteNonQuery();
            }
        }

        public void UnlinkFacebook(object membershipProviderUserKey)
        {
            MembershipUser user = Membership.GetUser(membershipProviderUserKey);
            if (user == null)
                throw new FacebookSharpException("User with given membershipProviderUserKey not found.");

            UnlinkFacebook(user.UserName);
        }

        public void UnlinkFacebookByFacebookId(string facebookId)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd = new MySqlCommand(
                    string.Format("DELETE FROM {0} WHERE facebook_id=@facebook_id", _tableName), cn);
                cmd.Parameters.AddWithValue("@facebook_id", facebookId);

                cn.Open();
                cmd.ExecuteNonQuery();
            }
        }

        public string GetFacebookAccessToken(string membershipUsername)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd =
                    new MySqlCommand(
                        string.Format("SELECT access_token FROM {0} WHERE user_name=@user_name", _tableName), cn);
                cmd.Parameters.AddWithValue("@user_name", membershipUsername);

                cn.Open();
                var result = cmd.ExecuteScalar();
                return result == null ? "" : result.ToString();
            }
        }

        public string GetFacebookAccessToken(object membershipProviderUserKey)
        {
            MembershipUser user = Membership.GetUser(membershipProviderUserKey);
            if (user == null)
                throw new FacebookSharpException("User with given membershipProviderUserKey not found.");
            return GetFacebookAccessToken(user.UserName);
        }

        public string GetFacebookAccessTokenByFacebookId(string facebookId)
        {
            using (MySqlConnection cn = new MySqlConnection(_connectionString))
            {
                MySqlCommand cmd =
                    new MySqlCommand(
                        string.Format("SELECT access_token FROM {0} WHERE facebookId=@facebookId", _tableName), cn);
                cmd.Parameters.AddWithValue("@facebookId", facebookId);

                cn.Open();
                var result = cmd.ExecuteScalar();
                return result == null ? "" : result.ToString();
            }
        }

        public string GetFacebookId(string membershipUsername)
        {
            throw new NotImplementedException();
        }

        public string GetFacebookId(object membershipProviderUserKey)
        {
            throw new NotImplementedException();
        }
{'fraction_non_alphanumeric': 0.08151874852605927, 'fraction_numerical': 0.0023583051646883105, 'mean_word_length': 2.014691943127962, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 59, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3228123', 'n_tokens_mistral': 3192, 'n_tokens_neox': 2924, 'n_words': 664}