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); }
public bool update(string table, string keyName, string keyVal, List <KeyValuePair <string, object> > obj, IDBTableDesc desc) { if (_conn == null) { // error: without _conn // important: don't log query, or may expose sensitive data!!! Debug.logger.log(LogType.LOG_ERR, "mysql db[" + _conf.name + "] source[" + _conf.source + "] update[" + table + "] keyname[" + keyName + "] without connection!"); return(false); } bool ret = true; try { //update privatepropsinfo set propsbuffer = '%s' where uin = %llu string sql = "UPDATE " + table + " SET "; int i = 0; foreach (var item in obj) { if (i > 0) { sql += ", "; } sql += item.Key + "=@v" + i; ++i; } sql = sql + " WHERE " + keyName + "=" + keyVal + ";"; MySqlCommand cmd = new MySqlCommand(sql, _conn); i = 0; foreach (var item in obj) { string pname = "@v" + i; if (desc.getDataType(item.Key) == AEDBDataType.ADDT_BINARY) { cmd.Parameters.AddWithValue(pname, AEDBHelper.serializeObject(item.Value)); } else { cmd.Parameters.AddWithValue(pname, item.Value); } ++i; } int rowaffect = cmd.ExecuteNonQuery(); ret = (rowaffect > 0); } 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 + "] upate[" + table + "] keyname[" + keyName + "] failed!"); } return(ret); }
public bool setHash(string hash, string field, object val, IDBTableDesc desc) { if (_conn == null) { // error: without _conn // important: don't log val, or may expose sensitive data!!! Debug.logger.log(LogType.LOG_ERR, "DBRedisDB name[" + this.name + "] setHash hash[" + hash + "] field[" + field + "] without connection!"); return(false); } IDatabase db = _conn.GetDatabase(); bool ret = true; AEDBDataType dt = desc.getDataType(field); switch (dt) { case AEDBDataType.ADDT_INT: ret = db.HashSet(hash, field, (int)val); break; case AEDBDataType.ADDT_UINT: ret = db.HashSet(hash, field, (uint)val); break; case AEDBDataType.ADDT_FLOAT: ret = db.HashSet(hash, field, (float)val); break; case AEDBDataType.ADDT_LONG: ret = db.HashSet(hash, field, (long)val); break; case AEDBDataType.ADDT_ULONG: ret = db.HashSet(hash, field, (ulong)val); break; case AEDBDataType.ADDT_DOUBLE: ret = db.HashSet(hash, field, (double)val); break; case AEDBDataType.ADDT_BINARY: { byte[] objBytes = AEDBHelper.serializeObject(val); ret = db.HashSet(hash, field, objBytes); } break; //case AEDBDataType.ADDT_STRING: // deault is string default: ret = db.HashSet(hash, field, (string)val); break; } if (!ret) { // error: without _conn // important: don't log val, or may expose sensitive data!!! Debug.logger.log(LogType.LOG_ERR, "DBRedisDB name[" + this.name + "] setHash hash[" + hash + "] field[" + field + "] failed!"); return(false); } if (_expireTime > 0) { // update expire time db.KeyExpire(hash, TimeSpan.FromSeconds(_expireTime)); } return(ret); }
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 bool setHashs(string hash, List <KeyValuePair <string, object> > par, IDBTableDesc desc) { if (_conn == null) { // error: without _conn // important: don't log par, or may expose sensitive data!!! Debug.logger.log(LogType.LOG_ERR, "DBRedisDB name[" + this.name + "] setHashs hash[" + hash + "] without connection!"); return(false); } IDatabase db = _conn.GetDatabase(); bool ret = true; HashEntry[] hashs = new HashEntry[par.Count]; for (int i = 0; i < par.Count; ++i) { AEDBDataType dt = desc.getDataType(par[i].Key); switch (dt) { case AEDBDataType.ADDT_INT: hashs[i] = new HashEntry(par[i].Key, (int)par[i].Value); break; case AEDBDataType.ADDT_UINT: hashs[i] = new HashEntry(par[i].Key, (uint)par[i].Value); break; case AEDBDataType.ADDT_FLOAT: hashs[i] = new HashEntry(par[i].Key, (float)par[i].Value); break; case AEDBDataType.ADDT_LONG: hashs[i] = new HashEntry(par[i].Key, (long)par[i].Value); break; case AEDBDataType.ADDT_ULONG: hashs[i] = new HashEntry(par[i].Key, (ulong)par[i].Value); break; case AEDBDataType.ADDT_DOUBLE: hashs[i] = new HashEntry(par[i].Key, (double)par[i].Value); break; case AEDBDataType.ADDT_BINARY: { byte[] objBytes = AEDBHelper.serializeObject(par[i].Value); hashs[i] = new HashEntry(par[i].Key, objBytes); } break; //case AEDBDataType.ADDT_STRING: // deault is string default: hashs[i] = new HashEntry(par[i].Key, (string)par[i].Value); break; } } if (_expireTime > 0) { db.HashSet(hash, hashs); db.KeyExpire(hash, TimeSpan.FromSeconds(_expireTime)); } else { db.HashSet(hash, hashs); } 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); }
public bool insert(string table, List <KeyValuePair <string, object> > obj, IDBTableDesc desc) { if (_conn == null) { // error: without _conn // important: don't log query, or may expose sensitive data!!! Debug.logger.log(LogType.LOG_ERR, "mysql db[" + _conf.name + "] source[" + _conf.source + "] insert[" + table + "] without connection!"); return(false); } bool ret = true; try { //"INSERT INTO testtab(TestCol1,TestCol2) VALUES(@test1,@test2)"; string sql = "INSERT INTO " + table + "("; string sql1 = ") VALUES("; int i = 0; foreach (var item in obj) { if (i > 0) { sql += ","; sql1 += ","; } sql += item.Key; sql1 += "@v" + i; ++i; } sql = sql + sql1 + ");"; MySqlCommand cmd = new MySqlCommand(sql, _conn); i = 0; foreach (var item in obj) { string pname = "@v" + i; if (desc.getDataType(item.Key) == AEDBDataType.ADDT_BINARY) { cmd.Parameters.AddWithValue(pname, AEDBHelper.serializeObject(item.Value)); } else { cmd.Parameters.AddWithValue(pname, item.Value); } ++i; } int rowaffect = cmd.ExecuteNonQuery(); ret = (rowaffect > 0); } 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 + "] insert[" + table + "] failed!"); } return(ret); }