Ejemplo n.º 1
0
        /// <summary>セルにアクセスするためのインデクサ</summary>
        /// <param name="index">インデックス</param>
        /// <returns>セルの値</returns>
        public object this[int index]
        {
            get
            {
                return(this._row[index]);
            }
            set
            {
                //object temp = null;

                // 列を確認し
                DTColumn dtCol = (DTColumn)this._cols[index];

                // 値の null チェック
                if (value == null)
                {
                    // セットする値が null または DBNull の場合は、null をセットする

                    // 2011.08.22 もともと null だった場合は何もしないように修正
                    if (this._row[index] == null)
                    {
                        // もともと null だった場合は何もしない
                        return;
                    }
                    else
                    {
                        // 設定
                        this._row[index] = null;
                    }
                }
                else
                {
                    // セットする値が null でも DBNull でもない場合は、値の型チェックを行う

                    // 型が
                    if (DTColumn.CheckType(value, dtCol.ColType))
                    {
                        // 一致
                    }
                    else
                    {
                        // 一致しない

                        // 自動変換
                        value = DTColumn.AutoCast(dtCol.ColType, value);
                    }

                    // 2011.08.22 変更前後で値が変わらなかった場合は何もしないように修正
                    if (dtCol.ColType == DTType.ByteArray)
                    {
                        if (((byte[])value).SequenceEqual((byte[])this._row[index]))
                        {
                            // 変更前後で値が変わらなかった場合は何もしない
                            return;
                        }
                        else
                        {
                            // 設定
                            this._row[index] = value;
                        }
                    }
                    else
                    {
                        if (value.Equals(this._row[index]))
                        {
                            // 変更前後で値が変わらなかった場合は何もしない
                            return;
                        }
                        else
                        {
                            // 設定
                            this._row[index] = value;
                        }
                    }
                }

                // 行ステータス
                if (this.RowState == DataRowState.Added || this.RowState == DataRowState.Deleted)
                {
                    // 行ステータスが "Added"(追加された行) または "Deleted"(削除された行) の場合は、行ステータスを変更しない
                }
                else
                {
                    // 上記以外の場合は、行ステータスを "Modified"(変更された行) に変更
                    this.RowState = DataRowState.Modified;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>セルにアクセスするためのインデクサ</summary>
        /// <param name="index">インデックス</param>
        /// <returns>セルの値</returns>
        public object this[int index]
        {
            get
            {
                return(this._row[index]);
            }
            set
            {
                //object temp = null;

                // 列を確認し
                DTColumn dtCol = (DTColumn)this._cols[index];

                // 値の null チェック
                if (value == null || value is System.DBNull)
                {
                    // セットする値が null または DBNull の場合は、null をセットする

                    // 2011.08.22 もともと null だった場合は何もしないように修正
                    if (this._row[index] == null)
                    {
                        // もともと null だった場合は何もしない
                        return;
                    }
                    else
                    {
                        // 設定
                        this._row[index] = null;
                    }
                }
                else
                {
                    // セットする値が null でも DBNull でもない場合は、値の型チェックを行う

                    // 型が
                    if (DTColumn.CheckType(value, dtCol.ColType))
                    {
                        // 一致
                    }
                    else
                    {
                        // 一致しない

                        // 自動変換
                        value = DTColumn.AutoCast(dtCol.ColType, value);
                    }

                    // 2011.08.22 変更前後で値が変わらなかった場合は何もしないように修正
                    if (dtCol.ColType == DTType.ByteArray)
                    {
                        int len = ((byte[])value).Length;

                        // nullチェック
                        if (this._row[index] == null)
                        {
                            // nullの場合、設定
                            this._row[index] = value;
                        }
                        else
                        {
                            // nullでない場合、チェック
                            if (((byte[])this._row[index]).Length == len)
                            {
                                // 変更フラグ
                                bool isChanged = false;

                                // ループ
                                for (int i = 0; i < len; i++)
                                {
                                    if (((byte)(((byte[])this._row[index])[i])) != ((byte)((byte[])value)[i]))
                                    {
                                        // 異なる場合、設定して
                                        this._row[index] = value;
                                        // フラグを立てて
                                        isChanged = true;
                                        // ブレイク
                                        break;
                                    }
                                }

                                // 変更されていない場合、
                                if (!isChanged)
                                {
                                    // 変更前後で値が変わらなかった場合は何もしない
                                    return;
                                }
                            }
                            else
                            {
                                // 異なる場合、設定
                                this._row[index] = value;
                            }

                            //if (((byte[])value).SequenceEqual((byte[])this._row[index]))
                            //{
                            //    // 変更前後で値が変わらなかった場合は何もしない
                            //    return;
                            //}
                            //else
                            //{
                            //    // 設定
                            //    this._row[index] = value;
                            //}
                        }
                    }
                    else
                    {
                        if (value.Equals(this._row[index]))
                        {
                            // 変更前後で値が変わらなかった場合は何もしない
                            return;
                        }
                        else
                        {
                            // 設定
                            this._row[index] = value;
                        }
                    }
                }

                // 行ステータス
                if (this.RowState == DataRowState.Added || this.RowState == DataRowState.Deleted)
                {
                    // 行ステータスが "Added"(追加された行) または "Deleted"(削除された行) の場合は、行ステータスを変更しない
                }
                else
                {
                    // 上記以外の場合は、行ステータスを "Modified"(変更された行) に変更
                    this.RowState = DataRowState.Modified;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>テキストからロードする</summary>
        /// <param name="tr">任意のTextReader </param>
        public void Load(TextReader tr)
        {
            // 初期化
            this._tbls             = new List <DTTable>();
            this._tblsNameIndexMap = new Dictionary <string, int>();

            // ワーク
            string temp = "";

            DTTable  tbl = null;
            DTColumn col = null;
            DTRow    row = null;

            int bkColIndex = 0;

            while (true)
            {
                string line = tr.ReadLine();

                // 入力がなくなったら、ループを抜ける
                if (line == null)
                {
                    break;
                }

                if (line.Length >= 4) // rnn:,rnrも考慮し「>=」とした。
                {
                    switch (line.Substring(0, 4))
                    {
                    case "tbl:":

                        // 表名
                        string tblName = line.Substring(4);

                        // 表を生成
                        tbl = new DTTable(tblName);

                        // 表を追加
                        this.Add(tbl);

                        break;

                    case "col:":

                        // 列情報
                        temp = line.Substring(4);
                        string colName = temp.Split(',')[0];
                        string colType = temp.Split(',')[1];

                        // 列を生成
                        col = new DTColumn(colName, DTColumn.StringToEnum(colType));

                        // 列を追加
                        tbl.Cols.Add(col);

                        break;

                    case "row:":

                        // 行ステータス
                        row.RowState = (DataRowState)int.Parse(line.Substring(4));

                        break;

                    case "cel:":

                        // セル情報
                        temp = line.Substring(4);
                        int    clnIndex  = temp.IndexOf(":");
                        int    colIndex  = int.Parse(temp.Substring(0, clnIndex).Split(',')[2]);
                        string celString = temp.Substring(clnIndex + 1);

                        // 列インデックスをチェック
                        if (colIndex == 0)
                        {
                            // 新しい行
                            row = tbl.Rows.AddNew();
                        }
                        else
                        {
                            // 継続行
                        }

                        // セルに値を設定
                        if (celString == "null")
                        {
                            // row[colIndex] = null;
                        }
                        else
                        {
                            // 列情報
                            col = (DTColumn)tbl.Cols.ColsInfo[colIndex];

                            if (col.ColType == DTType.String)
                            {
                                // そのまま
                                row[colIndex] = celString;
                                // インデックスを退避
                                bkColIndex = colIndex;
                            }
                            else if (col.ColType == DTType.ByteArray)
                            {
                                // バイト配列は、Base64デコードする。
                                byte[] celByte = Convert.FromBase64String(celString);
                                row[colIndex] = celByte;
                            }
                            else if (col.ColType == DTType.DateTime)
                            {
                                // DateTimeは、yyyy/M/d-H:m:s.fff
                                string ymd  = celString.Split('-')[0];
                                string hmsf = celString.Split('-')[1];

                                DateTime cellDttm = new DateTime(
                                    int.Parse(ymd.Split('/')[0]),
                                    int.Parse(ymd.Split('/')[1]),
                                    int.Parse(ymd.Split('/')[2]),
                                    int.Parse(hmsf.Split(':')[0]),
                                    int.Parse(hmsf.Split(':')[1]),
                                    int.Parse(hmsf.Split(':')[2].Split('.')[0]),
                                    int.Parse(hmsf.Split(':')[2].Split('.')[1]));

                                row[colIndex] = cellDttm;
                            }
                            else
                            {
                                // 型変換を試みる。
                                row[colIndex] = DTColumn.AutoCast(col.ColType, celString);
                            }
                        }

                        break;

                    case "rnr:":

                        // 文字列の続き

                        temp             = line.Substring(4);
                        row[bkColIndex] += "\r" + temp;

                        break;

                    case "rnn:":

                        // 文字列の続き

                        temp             = line.Substring(4);
                        row[bkColIndex] += "\n" + temp;

                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    // 捨て
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// LoadJson Method(To Load records from Json format)
        /// </summary>
        /// <param name="sr">StreamReader</param>
        public void LoadJson(StreamReader sr)
        {
            List <TableRecords> lstTableRecords = new List <TableRecords>();
            string json = sr.ReadToEnd();

            lstTableRecords = JsonConvert.DeserializeObject <List <TableRecords> >(json);

            DTTable  tbl        = null;
            DTColumn col        = null;
            DTRow    row        = null;
            int      bkColIndex = 0;

            for (int i = 0; i < lstTableRecords.Count; i++)
            {
                string tblName = lstTableRecords[i].tbl;

                //add the DTTable present in lstTableRecords into tbl
                tbl = new DTTable(tblName);

                // add the tbl into DTTables
                this.Add(tbl);

                Dictionary <string, string> tempCol = lstTableRecords[i].col;

                foreach (string key in tempCol.Keys)
                {
                    string colName = key;
                    string colType = tempCol[key];

                    //add the colName and colValue into DTColumn
                    col = new DTColumn(colName, DTColumn.StringToEnum(colType));

                    // add the col into tbl
                    tbl.Cols.Add(col);
                }

                ArrayList tempRow = lstTableRecords[i].row;
                for (int j = 0; j < lstTableRecords[i].row.Count; j++)
                {
                    //deserialize the first value inside row of lstTableRecords
                    object rowJson = JsonConvert.DeserializeObject(lstTableRecords[i].row[j].ToString());

                    //Convert the deserialized value into dictionary
                    Dictionary <string, object> rowDetails = JsonConvert.DeserializeObject <Dictionary <string, object> >(rowJson.ToString());

                    int colIndex = 0;
                    if (colIndex == 0)
                    {
                        // add new row
                        row = tbl.Rows.AddNew();
                    }
                    else
                    {
                        // do nothing
                    }
                    foreach (var key in rowDetails)
                    {
                        string colName  = key.Key;
                        object colValue = rowDetails[colName];
                        // getting the values and adding it to rows
                        if (colName != "rowstate")
                        {
                            col = (DTColumn)tbl.Cols.ColsInfo[colIndex];
                            if (colValue == null)
                            {
                                row[colIndex] = null;
                            }
                            else if (col.ColType == DTType.String)
                            {
                                // そのまま
                                row[colIndex] = colValue;
                                // インデックスを退避
                                bkColIndex = colIndex;
                            }
                            else if (col.ColType == DTType.ByteArray)
                            {
                                // バイト配列は、Base64デコードする。
                                byte[] celByte = Convert.FromBase64String(colValue.ToString());
                                row[colIndex] = celByte;
                            }
                            else if (col.ColType == DTType.DateTime)
                            {
                                // DateTimeは、yyyy/M/d-H:m:s.fff
                                string ymd  = colValue.ToString().Split('-')[0];
                                string hmsf = colValue.ToString().Split('-')[1];

                                DateTime cellDttm = new DateTime(
                                    int.Parse(ymd.Split('/')[0]),
                                    int.Parse(ymd.Split('/')[1]),
                                    int.Parse(ymd.Split('/')[2]),
                                    int.Parse(hmsf.Split(':')[0]),
                                    int.Parse(hmsf.Split(':')[1]),
                                    int.Parse(hmsf.Split(':')[2].Split('.')[0]),
                                    int.Parse(hmsf.Split(':')[2].Split('.')[1]));

                                row[colIndex] = cellDttm;
                            }
                            else
                            {
                                // 型変換を試みる。
                                row[colIndex] = DTColumn.AutoCast(col.ColType, colValue.ToString());
                            }

                            colIndex = colIndex + 1;
                        }
                        else
                        {
                            row.RowState = (DataRowState)(int.Parse)(colValue.ToString());
                        }
                    }
                }
            }
        }