// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created :
//
// Last Modified By : Kimch
// Last Modified On :
// ***********************************************************************
//
//
// ***********************************************************************
using System.Collections.Generic;
public class Table
{
///
/// 行数据
///
private List _rowList = new List();
private Dictionary _rowDictionary = new Dictionary();
///
/// 列数据
///
private List _columnList = new List();
private Dictionary _columnDictionary = new Dictionary();
internal Table(Database database, Schema schema)
{
this.database = database;
this.name = schema.name;
foreach (var column in schema.columns)
{
AddColumn(new Column(column));
}
}
///
/// 表名
///
public string name
{
get;
private set;
}
///
/// 数据库
///
public Database database
{
get;
private set;
}
///
/// 行数
///
public int rowCount
{
get { return _rowDictionary.Count; }
}
///
/// 列数
///
public int columnCount
{
get { return _columnDictionary.Count; }
}
///
/// 增加列
///
///
private void AddColumn(Column column)
{
column.SetTable(this, _columnDictionary.Count);
if (!_columnDictionary.ContainsKey(column.name))
{
_columnDictionary.Add(column.name, column);
_columnList.Add(column);
}
}
///
/// 获取列(名字)
///
///
///
public Column GetColumn(string name)
{
Column ret;
_columnDictionary.TryGetValue(name, out ret);
return ret;
}
///
/// 获取列(序号)
///
///
///
public Column GetColumnByIndex(int index)
{
return _columnList[index];
}
///
/// 获取所有列
///
///
public Column[] GetColumns()
{
return _columnList.ToArray();
}
internal int GetColumnIndex(string name)
{
var column = this.GetColumn(name);
return column.index;
}
///
/// 增加行数据
///
///
///
///
public Row SetRow(int id, object[] values)
{
Row ret;
if (!_rowDictionary.TryGetValue(id, out ret))
{
ret = new Row(this, id);
_rowDictionary.Add(id, ret);
_rowList.Add(ret);
}
ret.SetValues(values);
return ret;
}
///
/// 增加行数据
///
///
///
///
///
public Row SetRow(int id, int column, object value)
{
Row ret;
if (!_rowDictionary.TryGetValue(id, out ret))
{
ret = new Row(this, id);
_rowDictionary.Add(id, ret);
_rowList.Add(ret);
}
ret.SetValue(column, value);
return ret;
}
///
/// 增加行数据
///
///
///
///
///
public Row SetRow(int id, string column, object value)
{
Row ret;
if (!_rowDictionary.TryGetValue(id, out ret))
{
ret = new Row(this, id);
_rowDictionary.Add(id, ret);
_rowList.Add(ret);
}
ret.SetValue(column, value);
return ret;
}
///
/// 获取行(id)
///
///
///
public Row GetRow(int id)
{
Row ret;
_rowDictionary.TryGetValue(id, out ret);
return ret;
}
///
/// 获取行(序号)
///
///
///
public Row GetRowByIndex(int index)
{
return _rowList[index];
}
///
/// 获取所有行
///
///
public Row[] GetRows()
{
return _rowList.ToArray();
}
///
/// 获取数据(高效不检查)
///
///
///
///
public object GetValue(int id, int column)
{
Row row;
if (_rowDictionary.TryGetValue(id, out row))
{
return row[column];
}
return null;
}
///
/// 获取数据(高效不检查)
///
///
///
///
public object GetValue(int id, string column)
{
Row row;
if (_rowDictionary.TryGetValue(id, out row))
{
return row[column];
}
return null;
}
}