Esempio n. 1
0
        public bool RemoveKey(MetadataKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (!key.IsValid())
            {
                throw new ArgumentException(@"Key is invalid.", "key");
            }
            var typeId = (int)key.Type;

            if (typeId != -1)
            {
                lock (lockObject)
                {
                    if (connection != null)
                    {
                        string query = String.Format(@"DELETE FROM `Keys` WHERE `Keys`.`TypeId` = '{0}' AND `Keys`.`Name` = '{1}'", typeId, key.Name);
                        return(ExecuteNonQuery(query) == 1);
                    }
                }
            }
            return(false);
        }
Esempio n. 2
0
        public IEnumerable <MetadataKey> FetchAllKeys()
        {
            var          keysToRemove = new List <MetadataKey>(keyIds.Keys);
            const string Query        = @"SELECT * FROM `Keys`";

            DataTable dataTable = ExecuteReader(Query);

            foreach (DataRow row in dataTable.Rows)
            {
                var typeId = (int)(long)row["TypeId"];
                var keyId  = (long)row["KeyId"];
                if (typeId >= 0 && typeId < Enum.GetValues(typeof(MetadataKey.DatabaseType)).Length)
                {
                    var key = new MetadataKey((string)row["Name"], (MetadataKey.DatabaseType)typeId);
                    keyIds[key] = keyId;
                    keysToRemove.Remove(key);
                }
            }
            // Also remove keys that has been removed
            foreach (var key in keysToRemove)
            {
                keyIds.Remove(key);
            }
            return(keyIds.Keys.ToArray());
        }
Esempio n. 3
0
        public bool AddKey(MetadataKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (!key.IsValid())
            {
                throw new ArgumentException(@"Key is invalid.", "key");
            }
            var typeId = (int)key.Type;

            if (typeId != -1)
            {
                lock (lockObject)
                {
                    if (connection != null)
                    {
                        // TODO/Benlitz: a transaction that first try to fetch the key. If it exists, it should return false
                        string query = String.Format(@"INSERT INTO `Keys` (`TypeId`, `Name`) VALUES ('{0}', '{1}')", typeId, key.Name);
                        return(ExecuteNonQuery(query) == 1);
                    }
                }
            }
            return(false);
        }
Esempio n. 4
0
        private long GetKeyId(MetadataKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            string query  = String.Format(@"SELECT `KeyId` FROM `Keys` WHERE `Name` = '{0}' AND `TypeId` = '{1}'", key.Name, (int)key.Type);
            var    result = ExecuteScalar(query);

            if (result != null)
            {
                keyIds[key] = (long)result;
            }
            return(result != null ? (long)result : 0);
        }
Esempio n. 5
0
        private IEnumerable <IObjectMetadata> ParseResult(DataTable dataTable)
        {
            var result = new List <IObjectMetadata>();

            foreach (DataRow row in dataTable.Rows)
            {
                var url         = (string)row["Location"];
                var name        = (string)row["Name"];
                var type        = (MetadataKey.DatabaseType)(long) row["TypeId"];
                var key         = new MetadataKey(name, type);
                var keyId       = (long)row["KeyId"];
                var objectUrlId = (long)row["ObjectUrlId"];
                keyIds[key] = keyId;
                objectUrlIds[FormatUrl(url)] = objectUrlId;
                object value = key.ConvertValue(row["Value"].ToString());
                result.Add(new ObjectMetadata(url, key, value));
            }
            return(result);
        }
Esempio n. 6
0
 public Task <IObjectMetadata> FetchAsync(string objectUrl, MetadataKey key)
 {
     return(Task.Run(() => Fetch(objectUrl, key)));
 }
Esempio n. 7
0
        public IObjectMetadata Fetch(string objectUrl, MetadataKey key)
        {
            string query = String.Format(@"SELECT * FROM `Metadata` INNER JOIN ObjectUrls ON `ObjectUrls`.`ObjectUrlId` = `Metadata`.`ObjectUrlId` INNER JOIN Keys ON `Keys`.`KeyId` = `Metadata`.`KeyId` WHERE `ObjectUrls`.`Location` = '{0}' AND `Keys`.`Name` = '{1}' AND `Keys`.`TypeId` = '{2}'", FormatUrl(objectUrl), key.Name, (int)key.Type);

            return(ParseResult(ExecuteReader(query)).SingleOrDefault());
        }
Esempio n. 8
0
 public Task <IEnumerable <IObjectMetadata> > FetchAsync(MetadataKey key)
 {
     return(Task.Run(() => Fetch(key)));
 }
Esempio n. 9
0
        public IEnumerable <IObjectMetadata> Fetch(MetadataKey key)
        {
            string query = String.Format(@"SELECT * FROM `Metadata` INNER JOIN ObjectUrls ON `ObjectUrls`.`ObjectUrlId` = `Metadata`.`ObjectUrlId` INNER JOIN Keys ON `Keys`.`KeyId` = `Metadata`.`KeyId` WHERE `Keys`.`Name` = '{0}' AND `Keys`.`TypeId` = '{1}'", key.Name, (int)key.Type);

            return(ParseResult(ExecuteReader(query)));
        }