📄 database.cs
字号:
/// <param name="table">class corresponding to the table
/// </param>
/// <param name="key">field of the class to be indexed
/// </param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// the specified class
/// </exception>
/// <returns> <code>true</code> if index is deleted, <code>false</code> if index
/// is not found
/// </returns>
public bool DropIndex(Type table, string key)
{
Table t = locateTable(table, true);
FieldIndex index = (FieldIndex)t.indicesMap[key];
if (index != null)
{
t.indicesMap.Remove(key);
t.indices.Remove(index);
return true;
}
return false;
}
/// <summary>
/// <p>
/// Exclude record from specified index. This method is needed to perform update of indexed
/// field (key). Before updating the record, it is necessary to exclude it from indices
/// which keys are affected. After updating the field, record should be reinserted in these indices
/// using includeInIndex method.</p><p>
/// If there is not table associated with class of this object, then
/// database will search for table associated with superclass and so on...</p><p>
/// This method does nothing if there is no index for the specified field.
/// </p></summary>
/// <param name="record">object to be excluded from the specified index
/// </param>
/// <param name="key">name of the indexed field
/// </param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// record class
/// </exception>
/// <returns> <code>true</code> if record is excluded from index, <code>false</code> if
/// there is no such index
/// </returns>
public bool ExcludeFromIndex(IPersistent record, string key)
{
return ExcludeFromIndex(record.GetType(), record, key);
}
/// <summary>
/// <p>
/// Exclude record from specified index. This method is needed to perform update of indexed
/// field (key). Before updating the record, it is necessary to exclude it from indices
/// which keys are affected. After updating the field, record should be reinserted in these indices
/// using includeInIndex method.</p><p>
/// If there is not table associated with class of this object, then
/// database will search for table associated with superclass and so on...</p><p>
/// This method does nothing if there is no index for the specified field.
/// </p></summary>
/// <param name="table">class corresponding to the table
/// </param>
/// <param name="record">object to be excluded from the specified index
/// </param>
/// <param name="key">name of the indexed field
/// </param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// the specified class
/// </exception>
/// <returns> <code>true</code> if record is excluded from index, <code>false</code> if
/// there is no such index
/// </returns>
public bool ExcludeFromIndex(Type table, IPersistent record, string key)
{
Table t = locateTable(table, true);
FieldIndex index = (FieldIndex)t.indicesMap[key];
if (index != null)
{
index.Remove(record);
return true;
}
return false;
}
/// <summary>
/// <p>
/// Include record in the specified index. This method is needed to perform update of indexed
/// field (key). Before updating the record, it is necessary to exclude it from indices
/// which keys are affected using excludeFromIndex method. After updating the field, record should be
/// reinserted in these indices using this method.</p><p>
/// If there is not table associated with class of this object, then
/// database will search for table associated with superclass and so on...</p><p>
/// This method does nothing if there is no index for the specified field.
/// </p></summary>
/// <param name="record">object to be excluded from the specified index
/// </param>
/// <param name="key">name of the indexed field
/// </param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// the specified class
/// </exception>
/// <returns> <code>true</code> if record is included in index, <code>false</code> if
/// there is no such index
/// </returns>
public bool IncludeInIndex(IPersistent record, string key)
{
return IncludeInIndex(record.GetType(), record, key);
}
/// <summary>
/// <p>
/// Include record in the specified index. This method is needed to perform update of indexed
/// field (key). Before updating the record, it is necessary to exclude it from indices
/// which keys are affected using excludeFromIndex method. After updating the field, record should be
/// reinserted in these indices using this method.</p><p>
/// If there is not table associated with class of this object, then
/// database will search for table associated with superclass and so on...</p><p>
/// This method does nothing if there is no index for the specified field.
/// </p></summary>
/// <param name="table">class corresponding to the table
/// </param>
/// <param name="record">object to be excluded from the specified index
/// </param>
/// <param name="key">name of the indexed field
/// </param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// the specified class
/// </exception>
/// <returns> <code>true</code> if record is included in index, <code>false</code> if
/// there is no such index
/// </returns>
public bool IncludeInIndex(Type table, IPersistent record, string key)
{
Table t = locateTable(table, true);
FieldIndex index = (FieldIndex)t.indicesMap[key];
if (index != null)
{
index.Put(record);
return true;
}
return false;
}
/// <summary>
/// Select record from specified table
/// </summary>
/// <param name="table">class corresponding to the table
/// </param>
/// <param name="predicate">search predicate
/// </param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// the specified class
/// </exception>
/// <exception cref="CompileError"> exception is thrown if predicate is not valid JSQL exception
/// </exception>
/// <exception cref="JSQLRuntimeException"> exception is thrown if there is runtime error during query execution
/// </exception>
public IEnumerable Select(Type table, string predicate)
{
return Select(table, predicate, false);
}
/// <summary>
/// Select record from specified table
/// </summary>
/// <param name="table">class corresponding to the table
/// </param>
/// <param name="predicate">search predicate
/// </param>
/// <param name="forUpdate"><code>true</code> if records are selected for update -
/// in this case eclusive lock is set for the table to avoid deadlock.
/// </param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// the specified class
/// </exception>
/// <exception cref="CompileError"> exception is thrown if predicate is not valid JSQL exception
/// </exception>
/// <exception cref="JSQLRuntimeException"> exception is thrown if there is runtime error during query execution
/// </exception>
public IEnumerable Select(Type table, string predicate, bool forUpdate)
{
Query q = Prepare(table, predicate, forUpdate);
return q.Execute(GetRecords(table));
}
/// <summary>
/// <p>
/// Prepare JSQL query. Prepare is needed for queries with parameters. Also
/// preparing query can improve speed if query will be executed multiple times
/// (using prepare, it is compiled only once).</p><p>
/// To execute prepared query, you should use Query.Execute(db.GetRecords(typeof(XYZ))) method
/// </p></summary>
/// <param name="table">class corresponding to the table
/// </param>
/// <param name="predicate">search predicate
/// </param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// the specified class
/// </exception>
/// <exception cref="CompileError"> exception is thrown if predicate is not valid JSQL exception
/// </exception>
public Query Prepare(Type table, string predicate)
{
return Prepare(table, predicate, false);
}
/// <summary>
/// <p>
/// Prepare JSQL query. Prepare is needed for queries with parameters. Also
/// preparing query can improve speed if query will be executed multiple times
/// (using prepare, it is compiled only once).</p><p>
/// To execute prepared query, you should use Query.Execute(db.GetRecords(typeof(XYZ))) method
/// </p></summary>
/// <param name="table">class corresponding to the table
/// </param>
/// <param name="predicate">search predicate
/// </param>
/// <param name="forUpdate"><code>true</code> if records are selected for update -
/// in this case eclusive lock is set for the table to avoid deadlock.
/// </param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// the specified class
/// </exception>
/// <exception cref="CompileError"> exception is thrown if predicate is not valid JSQL exception
/// </exception>
public Query Prepare(Type table, string predicate, bool forUpdate)
{
Table t = locateTable(table, forUpdate);
Query q = storage.CreateQuery();
q.Prepare(table, predicate);
foreach (DictionaryEntry entry in t.indicesMap) {
FieldIndex index = (FieldIndex)entry.Value;
string key = (string)entry.Key;
q.AddIndex(key, index);
}
return q;
}
/// <summary>
/// Get iterator through all table records
/// </summary>
/// <param name="table">class corresponding to the table</param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// the specified class
/// </exception>
public IEnumerable GetRecords(Type table)
{
return GetRecords(table, false);
}
/// <summary>
/// Get iterator through all table records
/// </summary>
/// <param name="table">class corresponding to the table</param>
/// <param name="forUpdate"><code>true</code> if records are selected for update -
/// in this case eclusive lock is set for the table to avoid deadlock.
/// </param>
/// <exception cref="StorageError">StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
/// the specified class
/// </exception>
public IEnumerable GetRecords(Type table, bool forUpdate)
{
Table t = locateTable(table, forUpdate);
return t.extent;
}
/// <summary>
/// Get storage associated with this database
/// </summary>
/// <returns> underlying storage</returns>
public Storage Storage
{
get
{
return storage;
}
}
class Table : Persistent
{
internal ISet extent;
internal Link indices;
[NonSerialized()]
internal Hashtable indicesMap = new Hashtable();
public override void OnLoad()
{
for (int i = indices.Count; --i >= 0;)
{
FieldIndex index = (FieldIndex)indices[i];
indicesMap[index.KeyField.Name] = index;
}
}
}
Hashtable tables;
Storage storage;
Index metadata;
bool multithreaded;
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -