Beispiel #1
0
        public object getHash(string hash, string field, IDBTableDesc desc)
        {
            if (_conn == null)
            {
                // error: without _conn
                Debug.logger.log(LogType.LOG_ERR, "DBRedisDB name[" + this.name + "] getHash hash[" + hash + "] field[" + field + "] without connection");
                return(null);
            }

            IDatabase db = _conn.GetDatabase();
            object    val;

            AEDBDataType dt = desc.getDataType(field);

            switch (dt)
            {
            case AEDBDataType.ADDT_INT:
                val = (int)db.HashGet(hash, field);
                break;

            case AEDBDataType.ADDT_UINT:
                val = (uint)db.HashGet(hash, field);
                break;

            case AEDBDataType.ADDT_FLOAT:
                val = (float)db.HashGet(hash, field);
                break;

            case AEDBDataType.ADDT_LONG:
                val = (long)db.HashGet(hash, field);
                break;

            case AEDBDataType.ADDT_ULONG:
                val = (ulong)db.HashGet(hash, field);
                break;

            case AEDBDataType.ADDT_DOUBLE:
                val = (double)db.HashGet(hash, field);
                break;

            case AEDBDataType.ADDT_BINARY:
            {
                val = AEDBHelper.unserializeObject(db.HashGet(hash, field));
            }
            break;

            //case AEDBDataType.ADDT_STRING: // deault is string
            default:
                val = (string)db.HashGet(hash, field);
                break;
            }

            if (_expireTime > 0)
            {
                // update expire time
                db.KeyExpire(hash, TimeSpan.FromSeconds(_expireTime));
            }

            return(val);
        }
Beispiel #2
0
        public object getHashs(string hash, IDBTableDesc desc)
        {
            if (_conn == null)
            {
                // error: without _conn
                Debug.logger.log(LogType.LOG_ERR, "DBRedisDB name[" + this.name + "] getHashs hash[" + hash + "] without connection!");
                return(null);
            }

            IDatabase     db  = _conn.GetDatabase();
            ExpandoObject ret = new ExpandoObject();
            IDictionary <string, object> IRet = (IDictionary <string, object>)ret;

            HashEntry[] hashs = db.HashGetAll(hash);
            for (int i = 0; i < hashs.Length; ++i)
            {
                AEDBDataType dt = desc.getDataType(hashs[i].Name);
                switch (dt)
                {
                case AEDBDataType.ADDT_INT:
                    IRet[hashs[i].Name] = (int)hashs[i].Value;
                    break;

                case AEDBDataType.ADDT_UINT:
                    IRet[hashs[i].Name] = (uint)hashs[i].Value;
                    break;

                case AEDBDataType.ADDT_FLOAT:
                    IRet[hashs[i].Name] = (float)hashs[i].Value;
                    break;

                case AEDBDataType.ADDT_LONG:
                    IRet[hashs[i].Name] = (long)hashs[i].Value;
                    break;

                case AEDBDataType.ADDT_ULONG:
                    IRet[hashs[i].Name] = (ulong)hashs[i].Value;
                    break;

                case AEDBDataType.ADDT_DOUBLE:
                    IRet[hashs[i].Name] = (double)hashs[i].Value;
                    break;

                case AEDBDataType.ADDT_BINARY:
                {
                    object o = AEDBHelper.unserializeObject(hashs[i].Value);
                    IRet[hashs[i].Name] = o;
                }
                break;

                //case AEDBDataType.ADDT_STRING: // deault is string
                default:
                    IRet[hashs[i].Name] = (string)hashs[i].Value;
                    break;
                }
            }

            if (_expireTime > 0)
            {
                // update expire time
                db.KeyExpire(hash, TimeSpan.FromSeconds(_expireTime));
            }

            return(ret);
        }
        public List <object> query(string query, IDBTableDesc desc)
        {
            if (_conn == null)
            {
                // error: without _conn
                // important: don't log val, or may expose sensitive data!!!
                Debug.logger.log(LogType.LOG_ERR, "mysql db[" + _conf.name + "] source[" + _conf.source + "] query[" + query + "] without connection!");
                return(null);
            }

            List <object> ret = new List <object>();

            try
            {
                //string sql = "SELECT Name, HeadOfState FROM Country WHERE Continent='Oceania'";
                MySqlCommand    cmd = new MySqlCommand(query, _conn);
                MySqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    ExpandoObject obj = new ExpandoObject();
                    IDictionary <string, object> IDic = obj;

                    for (int i = 0; i < rdr.FieldCount; ++i)
                    {
                        string       key = rdr.GetName(i);
                        AEDBDataType dt  = desc.getDataType(key);
                        switch (dt)
                        {
                        case AEDBDataType.ADDT_INT:
                            IDic.Add(key, rdr.GetInt32(i));
                            break;

                        case AEDBDataType.ADDT_UINT:
                            IDic.Add(key, rdr.GetUInt32(i));
                            break;

                        case AEDBDataType.ADDT_FLOAT:
                            IDic.Add(key, rdr.GetFloat(i));
                            break;

                        case AEDBDataType.ADDT_LONG:
                            IDic.Add(key, rdr.GetInt64(i));
                            break;

                        case AEDBDataType.ADDT_ULONG:
                            IDic.Add(key, rdr.GetUInt64(i));
                            break;

                        case AEDBDataType.ADDT_DOUBLE:
                            IDic.Add(key, rdr.GetDouble(i));
                            break;

                        case AEDBDataType.ADDT_BINARY:
                        {
                            int    length = (int)rdr.GetBytes(i, 0, null, 0, 0);
                            byte[] buffer = new byte[length];
                            int    index  = 0;

                            while (index < length)
                            {
                                int bytesRead = (int)rdr.GetBytes(i, index, buffer, index, length - index);
                                index += bytesRead;
                            }

                            IDic.Add(key, AEDBHelper.unserializeObject(buffer));
                        }
                        break;

                        //case AEDBDataType.ADDT_STRING: // deault is string
                        default:
                            IDic.Add(key, rdr.GetString(i));
                            break;
                        }
                    }

                    ret.Add(obj);
                }
                rdr.Close();
            }
            catch (Exception e)
            {
                Debug.logger.log(LogType.LOG_ERR, "Message[" + e.Message + "] Source[" + e.Source + "] Stack: " + e.StackTrace);
                Debug.logger.log(LogType.LOG_ERR, "mysql db[" + _conf.name + "] source[" + _conf.source + "] query[" + query + "] failed!");
            }

            return(ret);
        }