📄 fastdbfields.cs
字号:
}
finally {
//Marshal.FreeCoTaskMem(name);
}
}
}
//-------------------------------------------------------------------------
/// <summary>
/// FastDbParameter implements a class that automatically manages the memory
/// associated with a parameter associated with a database statement.
/// </summary>
public class FastDbParameter: FastDbBuffer {
/// <summary>
/// Constructor that creates an instance of the FastDbField. You would
/// normally not use it directly, but by invoking: <see cref="FastDbFields.Add"/> method.
/// </summary>
/// <param name="name">The name of the field.</param>
/// <param name="var_type">The type of the field.</param>
/// <param name="capacity">Optional capacity of the field's buffer.</param>
public FastDbParameter(string name, CLI.FieldType var_type): this(name, var_type, 0) {}
public FastDbParameter(string name, CLI.FieldType var_type, int capacity) :
base(name, var_type, CLI.FieldFlags.cli_noindex) {}
public override void Assign(FastDbBuffer var) {
Debug.Assert(var is FastDbParameter, "Cannot assign " + var.GetType().Name + " type!");
base.Assign(var);
}
internal unsafe override void Bind(int StatementID) {
int res;
base.Bind(StatementID);
if (bound_to_statement == StatementID)
return;
else
bound_to_statement = StatementID;
if (CLI.IsArrayType(Type))
res = (int)CLI.ErrorCode.cli_not_implemented; // Array variables are not supported!
else {
string s = "%" + Name;
res = CLI.cli_parameter(StatementID, s, (int)Type, ((CLI.UnmanagedBuffer*)buffer.ToPointer())->data);
}
CLI.CliCheck(res);
}
}
//-------------------------------------------------------------------------
/// <summary>
/// FastDbCollection is the base class for FastDbFields and FastDbParameters. It
/// implements a collection of fields/parameters used to store
/// FastDbField/FastDbParameter objects.
/// </summary>
public abstract class FastDbCollection: IEnumerable {
protected ArrayList items = new ArrayList();
/// <summary>
/// Find an item by index.
/// </summary>
protected FastDbBuffer this[int index] { get { return (FastDbBuffer)items[index]; } }
/// <summary>
/// Find an item by name.
/// </summary>
protected FastDbBuffer this[string name] {
get {
for(int i=0; i<items.Count; i++)
if (String.Compare(name, ((FastDbBuffer)items[i]).Name, true) == 0)
return (FastDbBuffer)items[i];
throw new CliError("Parameter \"" + name + "\" not found!");
}
}
protected abstract FastDbBuffer Add(FastDbBuffer item);
/// <summary>
/// Get a count of items in the collection.
/// </summary>
public int Count { get { return items.Count; } }
/// <summary>
/// Clear the collection by removing all items.
/// </summary>
public void Clear() { UnBind(); items.Clear(); }
/// <summary>
/// Copy the contents of one collection to another.
/// </summary>
/// <param name="items">Source collection to copy items from.</param>
public virtual void Assign(FastDbCollection items) {
Clear();
for(int i=0; i<items.Count; ++i) this.Add(items[i]);
}
/// <summary>
/// Bind all fields/Parameters in the collection to a statement.
/// </summary>
/// <param name="statement"></param>
internal void Bind(int statement) {
for(int i=0; i<items.Count; ++i) this[i].Bind(statement);
}
/// <summary>
/// Unbind all fields from a statement.
/// </summary>
internal void UnBind() {
for(int i=0; i<items.Count; ++i) this[i].UnBind();
}
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public Iterator GetEnumerator() { return new Iterator(this); }
/// <summary>
/// This class implements IEnumerator interface. Use its methods within a
/// foreach() loop. Iterator.Index tells the field's index within a collection
/// and Iterator.IsLast is true if this is the last item.
/// </summary>
public class Iterator: IEnumerator {
#region Private Fields
int nIndex;
FastDbCollection collection;
#endregion
#region Constructors
public Iterator(FastDbCollection coll) {
collection = coll;
nIndex = -1;
}
#endregion
#region Methods
public void Reset() { nIndex = -1; }
public bool MoveNext() { return ++nIndex < collection.items.Count; }
public FastDbBuffer Current { get { return (FastDbBuffer)collection.items[nIndex]; } }
public int Index { get { return nIndex; } }
public bool IsLast { get { return nIndex == collection.items.Count-1; } }
// The current property on the IEnumerator interface:
object IEnumerator.Current { get { return Current; } }
#endregion
}
#endregion
}
//-------------------------------------------------------------------------
/// <summary>
/// A collection of fields belonging to a FastDbTable. It is usually accessed
/// by calling <see cref="FastDbCommand.Fields"/> property.
/// </summary>
public class FastDbFields: FastDbCollection {
protected override FastDbBuffer Add(FastDbBuffer item) { return this.Add((FastDbField)item); }
public FastDbField Add(FastDbField field) {
return Add(field.Name, field.Type, field.Flags, field.RefTable, field.InvRefField);
}
public FastDbField Add(string name, CLI.FieldType field_type) {
return Add(name, field_type, CLI.FieldFlags.cli_noindex, null, null);
}
public FastDbField Add(string name, CLI.FieldType field_type, CLI.FieldFlags index_type) {
return Add(name, field_type, index_type, null, null);
}
public FastDbField Add(string name, CLI.FieldType field_type, CLI.FieldFlags index_type, string ref_table) {
return Add(name, field_type, index_type, ref_table, null);
}
/// <summary>
/// Use this method to add a field to the collection.
/// </summary>
/// <param name="name">Name of the field</param>
/// <param name="field_type">Type of the field</param>
/// <param name="index_type">Optional index to be built on the field (default: CLI.FieldFlags.no_index)</param>
/// <param name="ref_table">Reference table name (used for inverse references). Default: null.</param>
/// <param name="inv_ref_field">Inverse reference field name (default: null)</param>
/// <returns></returns>
public FastDbField Add(string name, CLI.FieldType field_type, CLI.FieldFlags index_type, string ref_table, string inv_ref_field) {
int i = items.Add(new FastDbField(name, field_type, index_type, ref_table, inv_ref_field));
return (FastDbField)items[i];
}
/// <summary>
/// Get a field by index.
/// </summary>
public new FastDbField this[int index] { get { return (FastDbField)base[index]; } }
/// <summary>
/// Get a field by name.
/// </summary>
public new FastDbField this[string name] { get { return (FastDbField)base[name]; } }
}
/// <summary>
/// A collection of Parameters defined in a FastDb command.
/// <code>
/// FastDbCommand command =
/// new FastDbCommand(connection, "select * from tab where id = %id");
/// </code>
/// In the case above, the "id" is a parameter that can be associated with a
/// value using:
/// <code>
/// command.Parameters.Add("id", CLI.FieldType.cli_int4);
/// command.Parameters[0].Value = 10;
/// int num_rows = command.Execute();
/// </code>
/// </summary>
public class FastDbParameters: FastDbCollection {
protected override FastDbBuffer Add(FastDbBuffer param) {
return this.Add(param.Name, param.Type, param.Capacity);
}
public FastDbParameter Add(string name, CLI.FieldType param_type) {
Debug.Assert(param_type >= CLI.FieldType.cli_oid && param_type < CLI.FieldType.cli_array_of_oid,
String.Format("Parameter type {0} not supported!", Enum.GetName(typeof(CLI.FieldType), param_type)));
return this.Add(name, param_type, 0);
}
/// <summary>
/// Add a parameter to the FastDbParameters collection.
/// </summary>
/// <param name="name">Parameter name (as it appears in the SQL statement) without preceeding "%".</param>
/// <param name="param_type">Parameter type (it must match the type of the associated field).</param>
/// <param name="capacity">Optional capacity of the underlying memory buffer.</param>
/// <returns>A object representing the newly created parameter</returns>
public FastDbParameter Add(string name, CLI.FieldType param_type, int capacity) {
int i = items.Add(new FastDbParameter(name, param_type, capacity));
return (FastDbParameter)items[i];
}
/// <summary>
/// Get a parameter by index.
/// </summary>
public new FastDbParameter this[int index] { get { return (FastDbParameter)base[index]; } }
/// <summary>
/// Get a parameter by name.
/// </summary>
public new FastDbParameter this[string name] { get { return (FastDbParameter)base[name]; } }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -