Exemple #1
0
        static void Main(string[] args)
        {
            var con = new MongoDBConnection("Server=127.0.0.1;Port=27017;Database=cdactraining;");
            var cmd = new MongoDBCommand("SELECT * FROM employees", con);

            try
            {
                con.Open();
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(reader["empName"]);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public override bool Delete(string id)
        {
            using (MongoDBConnection dbcon = new MongoDBConnection(m_connectionString))
            {
                dbcon.Open();

                using (MongoDBCommand cmd = new MongoDBCommand("delete from assets where id=?id", dbcon))
                {
                    cmd.Parameters.AddWithValue("?id", id);
                    cmd.ExecuteNonQuery();
                }
            }

            return true;
        }
        /// <summary>
        /// Check if the assets exist in the database.
        /// </summary>
        /// <param name="uuidss">The assets' IDs</param>
        /// <returns>For each asset: true if it exists, false otherwise</returns>
        public override bool[] AssetsExist(UUID[] uuids)
        {
            if (uuids.Length == 0)
                return new bool[0];

            HashSet<UUID> exist = new HashSet<UUID>();

            string ids = "'" + string.Join("','", uuids) + "'";
            string sql = string.Format("SELECT id FROM assets WHERE id IN ({0})", ids);

            using (MongoDBConnection dbcon = new MongoDBConnection(m_connectionString))
            {
                dbcon.Open();
                using (MongoDBCommand cmd = new MongoDBCommand(sql, dbcon))
                {
                    using (MongoDBDataReader dbReader = cmd.ExecuteReader())
                    {
                        while (dbReader.Read())
                        {
                            UUID id = DBGuid.FromDB(dbReader["id"]);
                            exist.Add(id);
                        }
                    }
                }
            }

            bool[] results = new bool[uuids.Length];
            for (int i = 0; i < uuids.Length; i++)
                results[i] = exist.Contains(uuids[i]);

            return results;
        }
        /// <summary>
        /// Returns a list of AssetMetadata objects. The list is a subset of
        /// the entire data set offset by <paramref name="start" /> containing
        /// <paramref name="count" /> elements.
        /// </summary>
        /// <param name="start">The number of results to discard from the total data set.</param>
        /// <param name="count">The number of rows the returned list should contain.</param>
        /// <returns>A list of AssetMetadata objects.</returns>
        public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
        {
            List<AssetMetadata> retList = new List<AssetMetadata>(count);

            using (MongoDBConnection dbcon = new MongoDBConnection(m_connectionString))
            {
                dbcon.Open();

                using (MongoDBCommand cmd
                    = new MongoDBCommand(
                        "SELECT name,description,assetType,temporary,id,asset_flags,CreatorID FROM assets LIMIT ?start, ?count",
                        dbcon))
                {
                    cmd.Parameters.AddWithValue("?start", start);
                    cmd.Parameters.AddWithValue("?count", count);

                    try
                    {
                        using (MongoDBDataReader dbReader = cmd.ExecuteReader())
                        {
                            while (dbReader.Read())
                            {
                                AssetMetadata metadata = new AssetMetadata();
                                metadata.Name = (string)dbReader["name"];
                                metadata.Description = (string)dbReader["description"];
                                metadata.Type = (sbyte)dbReader["assetType"];
                                metadata.Temporary = Convert.ToBoolean(dbReader["temporary"]); // Not sure if this is correct.
                                metadata.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]);
                                metadata.FullID = DBGuid.FromDB(dbReader["id"]);
                                metadata.CreatorID = dbReader["CreatorID"].ToString();

                                // Current SHA1s are not stored/computed.
                                metadata.SHA1 = new byte[] { };

                                retList.Add(metadata);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        m_log.Error(
                            string.Format(
                                "[ASSETS DB]: MongoDB failure fetching asset set from {0}, count {1}.  Exception  ", 
                                start, count), 
                            e);
                    }
                }
            }

            return retList;
        }
        private void UpdateAccessTime(AssetBase asset)
        {

            using (MongoDBConnection dbcon = new MongoDBConnection(m_connectionString))
            {
                dbcon.Open();

                using (MongoDBCommand cmd
                    = new MongoDBCommand("update assets set access_time=?access_time where id=?id", dbcon))
                {
                    try
                    {
                        using (cmd)
                        {
                            // create unix epoch time
                            int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
                            cmd.Parameters.AddWithValue("?id", asset.ID);
                            cmd.Parameters.AddWithValue("?access_time", now);
                            cmd.ExecuteNonQuery();
                        }
                    }
                    catch (Exception e)
                    {
                        m_log.Error(
                            string.Format(
                                "[ASSETS DB]: Failure updating access_time for asset {0} with name {1}.  Exception  ", 
                                asset.FullID, asset.Name), 
                            e);
                    }
                }
            }
        }