public StorageAccountSetting Create(StorageAccountSetting setting)
        {
            if (setting == null)
                throw new ArgumentNullException("setting");

            return SqlQueryExecutor.Execute(() =>
            {
                String query = string.Format(CultureInfo.InvariantCulture, "INSERT INTO {1} {0}; SELECT LAST_INSERT_ID();", InsertFieldList, TableName);
                int insertedId = (int)_connectionProvider.CurrentConnection.Query<long>(query, new { setting.StorageAccountId, setting.SettingName, setting.SettingValue }).FirstOrDefault();
                if (insertedId <= 0)
                    throw new MonsterDbException("Account setting insertion failed");

                String idAndStampQuery = string.Format(CultureInfo.InvariantCulture, "SELECT id AS Id, stamp AS Stamp FROM {0} WHERE id=@Id;", TableName);
                IdAndStamp idAndStamp = _connectionProvider.CurrentConnection.Query<IdAndStamp>(idAndStampQuery, new { Id = insertedId }).FirstOrDefault();

                if (idAndStamp == null)
                    throw new MonsterDbException("Account insertion failed");

                setting.Id = idAndStamp.Id;
                setting.Stamp = idAndStamp.Stamp;
                return setting;
            });
        }
        public UpdateResult SaveSettings(IDictionary<string, string> storageAccountSettingsList, int storageAccountId, DateTime storageAccountStamp)
        {
            if (storageAccountSettingsList == null)
                throw new ArgumentNullException("storageAccountSettingsList");

            if (storageAccountSettingsList.Count <= 0)
                return UpdateResult.Success;

            return SqlQueryExecutor.Execute(() =>
            {
                return _connectionProvider.DoInTransaction(()=>
                        {
                        const string selectAccountWithStampQuery = "SELECT a.id FROM storage_accounts a WHERE a.id = @Id AND a.stamp = @Stamp LIMIT 1;";

                        int? accountIdCheck = _connectionProvider.CurrentConnection.Query<int?>(selectAccountWithStampQuery, new { Id = storageAccountId, Stamp = storageAccountStamp }).FirstOrDefault();

                        if (accountIdCheck == null)
                        {
                            const string selectAccountQueryWithoutStamp = "SELECT a.id FROM storage_accounts a WHERE a.id = @Id LIMIT 1;";
                            accountIdCheck = _connectionProvider.CurrentConnection.Query<int?>(selectAccountQueryWithoutStamp, new { Id = storageAccountId }).FirstOrDefault();
                            if (accountIdCheck == null)
                                return UpdateResult.ItemNotExists;
                            return UpdateResult.Stalled;
                        }

                        foreach (var accountSetting in storageAccountSettingsList)
                        {
                            StorageAccountSetting setting = LoadByName(accountSetting.Key, storageAccountId);
                            if (setting == null)
                            {
                                setting = new StorageAccountSetting
                                    {
                                        StorageAccountId = storageAccountId,
                                        SettingName = accountSetting.Key,
                                        SettingValue = accountSetting.Value
                                    };
                                Create(setting);
                            }
                            else
                            {
                                setting.SettingName = accountSetting.Key;
                                setting.SettingValue = accountSetting.Value;
                                Update(setting);
                            }
                        }

                        const string insertQuery = "UPDATE storage_accounts SET stamp = CURRENT_TIMESTAMP() WHERE id = @Id;";
                        _connectionProvider.CurrentConnection.Execute(insertQuery, new { Id = storageAccountId });

                        return UpdateResult.Success;
                    });
            });
        }
        public StorageAccountSetting Update(StorageAccountSetting setting)
        {
            if (setting == null)
                throw new ArgumentNullException("setting");

            return SqlQueryExecutor.Execute(() =>
            {
                String query = string.Format(CultureInfo.InvariantCulture, "UPDATE {1} SET {0} WHERE Id=@Id", UpdateFieldList, TableName);
                _connectionProvider.CurrentConnection.Execute(query, new { setting.Id, setting.StorageAccountId, setting.SettingName, setting.SettingValue });

                String idAndStampQuery = string.Format(CultureInfo.InvariantCulture, "SELECT id AS Id, stamp AS Stamp FROM {0} WHERE id=@Id;", TableName);
                IdAndStamp idAndStamp = _connectionProvider.CurrentConnection.Query<IdAndStamp>(idAndStampQuery, new { setting.Id }).FirstOrDefault();

                if (idAndStamp == null)
                    throw new MonsterDbException("Account setting update failed");

                setting.Id = idAndStamp.Id;
                setting.Stamp = idAndStamp.Stamp;
                return setting;
            });
        }