예제 #1
0
        public async Task <bool> TableCreateNewColumnAsync(string tableName, string columnName, Type columnType, bool makeMatchIndex, bool makeRangeIndex, object defaultValue)
        {
            TableSetting ts             = null;
            bool         enterTableLock = false;

            try
            {
                if (defaultValue == null)
                {
                    return(false);
                }

                if (!CheckDataType(columnType, defaultValue.ToString()))
                {
                    return(false);
                }

                var db = this.redis.GetDatabase();

                ts = await TableGetSettingAsync(tableName);

                if (null == ts)
                {
                    return(false);
                }

                enterTableLock = await TableLockEnterAsync(ts, "");

                var cs = new ColumnSetting();
                cs.indexNumber  = ts.GetNextColumnIndexNumber();
                cs.dataType     = columnType;
                cs.isMatchIndex = makeMatchIndex;
                cs.isRangeIndex = makeRangeIndex;
                cs.defaultValue = defaultValue;

                if (cs.dataType == typeof(DateTime))
                {
                    var dvt = defaultValue.ToString().ToLower();
                    if (dvt.Equals("now"))
                    {
                        defaultValue = DateTime.Now.ToString();
                    }
                    else if (dvt.Equals("utcnow"))
                    {
                        defaultValue = DateTime.UtcNow.ToString();
                    }
                }

                ts.tableSchemaDic.Add(columnName, cs);

                if (makeMatchIndex)
                {
                    ts.matchIndexColumnDic.Add(columnName, cs.indexNumber);
                }

                if (makeRangeIndex)
                {
                    ts.rangeIndexColumnDic.Add(columnName, cs.indexNumber);
                }

                ts.columnIndexNameDic.Add(cs.indexNumber.ToString(), columnName);

                var tableSchemaName = RedisKey.GetRedisKey_TableSchema(tableName);

                var value = string.Format("{0},{1},{2},{3},{4},{5}", cs.indexNumber.ToString(), cs.dataType.ToString(), makeMatchIndex.ToString(), "False", makeRangeIndex.ToString(), defaultValue.ToString()); // fieldIndex, Type, IndexFlag, primaryKeyFlag, sortFlag
                await db.HashSetAsync(tableSchemaName, columnName, value);


                var tasklist = new List <Task <bool> >();
                var key      = RedisKey.GetRedisKey_TablePrimaryKeyList(tableName);

                foreach (var primaryKeyValue in db.SetScan(key, "*"))
                {
                    key = RedisKey.GetRedisKey_TableRow(ts.tableID, primaryKeyValue.ToString());
                    tasklist.Add(db.HashSetAsync(key, cs.indexNumber.ToString(), defaultValue.ToString()));

                    if (makeMatchIndex)
                    {
                        // make match index
                        key = RedisKey.GetRedisKey_TableMatchIndexColumn(ts.tableID, cs.indexNumber, defaultValue.ToString());
                        tasklist.Add(db.SetAddAsync(key, primaryKeyValue));
                    }

                    if (makeRangeIndex)
                    {
                        // make range index
                        key = RedisKey.GetRedisKey_TableRangeIndexColumn(ts.tableID, cs.indexNumber);
                        var score = ConvertToScore(cs.dataType, defaultValue.ToString());
                        tasklist.Add(db.SortedSetAddAsync(key, primaryKeyValue, score));
                    }
                }

                foreach (var t in tasklist)
                {
                    if (!await t)
                    {
                        return(false);
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
            finally
            {
                if (enterTableLock)
                {
                    await TableLockExit(ts, "");
                }
            }
        }
예제 #2
0
파일: Redisql.cs 프로젝트: sophnim/Redisql
        public async Task <TableSetting> TableGetSettingAsync(string tableName)
        {
            TableSetting ts;
            var          db = this.redis.GetDatabase();

            if (!this.tableSettingDic.TryGetValue(tableName, out ts))
            {
                // 아직 로드되지 않았다. redis로부터 읽어들인다.
                ts           = new TableSetting();
                ts.tableName = tableName;

                // get table id
                var tableID = await db.HashGetAsync(Consts.RedisKey_Hash_TableNameIds, tableName);

                if (RedisValue.Null == tableID)
                {
                    return(null);
                }

                ts.tableID = Convert.ToInt32(tableID.ToString());

                // read table schema
                var tableSchema = await db.HashGetAllAsync(RedisKey.GetRedisKey_TableSchema(tableName));

                if (null == tableSchema)
                {
                    return(null);
                }

                // get table info
                foreach (var e in tableSchema)
                {
                    var tokens = e.Value.ToString().Split(',');

                    var cs = new ColumnSetting();
                    cs.indexNumber = Convert.ToInt32(tokens[0]);

                    switch (tokens[1])
                    {
                    case "System.Byte": cs.dataType = typeof(Byte); break;

                    case "System.Int16": cs.dataType = typeof(Int16); break;

                    case "System.UInt16": cs.dataType = typeof(UInt16); break;

                    case "System.Int32": cs.dataType = typeof(Int32); break;

                    case "System.UInt32": cs.dataType = typeof(UInt32); break;

                    case "System.Int64": cs.dataType = typeof(Int64);  break;

                    case "System.UInt64": cs.dataType = typeof(UInt64); break;

                    case "System.Single": cs.dataType = typeof(Single); break;

                    case "System.Double": cs.dataType = typeof(Double); break;

                    case "System.String": cs.dataType = typeof(String); break;

                    case "System.DateTime": cs.dataType = typeof(DateTime); break;
                    }

                    if (tokens[5].Equals("null"))
                    {
                        cs.defaultValue = null;
                    }
                    else
                    {
                        switch (tokens[1])
                        {
                        case "System.Byte": cs.defaultValue = Convert.ToByte(tokens[5]); break;

                        case "System.Int16": cs.defaultValue = Convert.ToInt16(tokens[5]); break;

                        case "System.UInt16": cs.defaultValue = Convert.ToUInt16(tokens[5]); break;

                        case "System.Int32": cs.defaultValue = Convert.ToInt32(tokens[5]); break;

                        case "System.UInt32": cs.defaultValue = Convert.ToUInt32(tokens[5]); break;

                        case "System.Int64": cs.defaultValue = Convert.ToInt64(tokens[5]); break;

                        case "System.UInt64": cs.defaultValue = Convert.ToUInt64(tokens[5]); break;

                        case "System.Single": cs.defaultValue = Convert.ToSingle(tokens[5]); break;

                        case "System.Double": cs.defaultValue = Convert.ToDouble(tokens[5]); break;

                        case "System.String": cs.defaultValue = Convert.ToString(tokens[5]); break;

                        case "System.DateTime":
                            if (tokens[5].ToLower().Equals("now"))
                            {
                                cs.defaultValue = "now";
                            }
                            else if (tokens[5].ToLower().Equals("utcnow"))
                            {
                                cs.defaultValue = "utcnow";
                            }
                            else
                            {
                                cs.defaultValue = Convert.ToDateTime(tokens[5]);
                            }
                            break;
                        }
                    }

                    cs.isMatchIndex = Convert.ToBoolean(tokens[2]);
                    if (cs.isMatchIndex)
                    {
                        ts.matchIndexColumnDic.Add(e.Name.ToString(), cs.indexNumber);
                    }

                    var fieldPrimaryKeyFlag = Convert.ToBoolean(tokens[3]);
                    if (fieldPrimaryKeyFlag)
                    {
                        ts.primaryKeyColumnName = e.Name;
                    }

                    cs.isRangeIndex = Convert.ToBoolean(tokens[4]);
                    if (cs.isRangeIndex)
                    {
                        ts.rangeIndexColumnDic.Add(e.Name.ToString(), cs.indexNumber);
                    }

                    ts.columnIndexNameDic.Add(cs.indexNumber.ToString(), e.Name.ToString());
                    ts.tableSchemaDic.Add(e.Name.ToString(), cs);
                }

                this.tableSettingDic.TryAdd(tableName, ts);
            }

            return(ts);
        }