📄 fastdbcli.cs
字号:
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace FastDbNet
{
/// <summary>
/// Exception thrown by Gigabase/FastDB CLI implementation.
/// </summary>
public class CliError : Exception {
public int ErrorCode;
///<summary>
/// Constructor of the exception object.
/// </summary>
/// <param name="Msg">message describing the reason of the fault</param>
public CliError(string Msg) : this((int)CLI.ErrorCode.cli_unknown_error, Msg) {}
///<summary>
/// Constructor of the exception object.
/// </summary>
/// <param name="Code">Error code describing the reason of the fault</param>
public CliError(int Code) : this(Code, "") {}
///<summary>
/// Constructor of the exception object.
/// </summary>
/// <param name="Code">Error code describing the reason of the fault</param>
/// <param name="Msg">Message describing the reason of the fault</param>
public CliError(int Code, string Msg): base(FormatMessage(Code, Msg)) {
ErrorCode = Code;
}
private static string FormatMessage(int Code, string Msg) {
if (Msg != "")
Msg += ". " + CLI.CliErrorToStr(Code);
else
Msg = CLI.CliErrorToStr(Code);
return Msg;
}
}
/// <summary>
/// FastDb CLI interface.
/// </summary>
public class CLI {
#if GIGABASE
#if LINUX
public const string libname = "libgigabase_r.so";
#else
public const string libname = "GigaBase.dll";
#endif
#else
#if LINUX
public const string libname = "libfastdb_r.so";
#else
public const string libname = "FastDB.dll";
#endif
#endif
//-----------------------------------------
// cli_result_code
//-----------------------------------------
public enum ErrorCode : int {
cli_ok = 0,
cli_bad_address = -1,
cli_connection_refused = -2,
cli_database_not_found = -3,
cli_bad_statement = -4,
cli_parameter_not_found = -5,
cli_unbound_parameter = -6,
cli_column_not_found = -7,
cli_incompatible_type = -8,
cli_network_error = -9,
cli_runtime_error = -10,
cli_bad_descriptor = -11,
cli_unsupported_type = -12,
cli_not_found = -13,
cli_not_update_mode = -14,
cli_table_not_found = -15,
cli_not_all_columns_specified = -16,
cli_not_fetched = -17,
cli_already_updated = -18,
cli_table_already_exists = -19,
cli_not_implemented = -20,
#if GIGABASE
cli_login_failed = -21,
cli_empty_parameter = -22,
cli_closed_connection = -23
#endif
//-----------------------------------------
// cli_error_class
// Note: When calling CliErrorToStr in TCliErrorHandler subtract 100 from the
// code passed as ErrorClassCode in order to get correct description
//-----------------------------------------
cli_query_error = 1 -100,
cli_arithmetic_error = 2 -100,
cli_index_out_of_range_error = 3 -100,
cli_database_open_error = 4 -100,
cli_file_error = 5 -100,
cli_out_of_memory_error = 6 -100,
cli_deadlock = 7 -100,
cli_null_reference_error = 8 -100,
cli_lock_revoked = 9 -100,
cli_file_limit_exeeded = 10 -100,
//-----------------------------------------
// Extended Error Codes
//-----------------------------------------
cli_error_loading_library = -200,
cli_session_not_assigned = -201,
cli_database_already_open = -202,
cli_invalid_field_size = -203,
cli_empty_query = -204,
cli_item_already_defined = -205,
cli_wrong_inverse_reference = -206,
cli_no_fields_defined = -207,
cli_access_violation = -208,
cli_unknown_error = -999
}
//-----------------------------------------
// Field Types
//-----------------------------------------
public enum FieldType : int {
cli_oid = 0,
cli_bool = 1,
cli_int1 = 2,
cli_int2 = 3,
cli_int4 = 4,
cli_int8 = 5,
cli_real4 = 6,
cli_real8 = 7,
cli_decimal = 8, //{ not supported in FastDB }
cli_asciiz = 9, //{ zero terminated string (Get/Set function can be used) }
cli_pasciiz = 10, //{ pointer to zero terminated string }
cli_cstring = 11, //{ not supported in FastDB }
cli_array_of_oid = 12,
cli_array_of_bool = 13,
cli_array_of_int1 = 14,
cli_array_of_int2 = 15,
cli_array_of_int4 = 16,
cli_array_of_int8 = 17,
cli_array_of_real4 = 18,
cli_array_of_real8 = 19,
cli_array_of_decimal = 20, //{ not supported in FastDB }
cli_array_of_string = 21,
cli_any = 22, //{ not supported in FastDB }
cli_datetime = 23, //{ not supported in FastDB }
cli_autoincrement = 24,
cli_rectangle = 25, //{ not supported in FastDB }
cli_unknown = 26,
//--- Custom types not supported by the database directly -----
cli_subst // Reserved for substitution variables
};
internal static int[] SizeOfCliType =
new int[] {
Marshal.SizeOf(typeof(int)), // cli_oid
Marshal.SizeOf(typeof(bool)), // cli_bool
Marshal.SizeOf(typeof(sbyte)), // cli_int1
Marshal.SizeOf(typeof(short)), // cli_int2
Marshal.SizeOf(typeof(int)), // cli_int4
Marshal.SizeOf(typeof(long)), // cli_int8_t
Marshal.SizeOf(typeof(float)), // cli_real4_t
Marshal.SizeOf(typeof(double)), // cli_real8_t
0, // cli_decimal
0, // cli_asciiz,
0, // cli_pasciiz,
0, // cli_cstring,
0, // cli_array_of_oid,
0, // cli_array_of_bool,
0, // cli_array_of_int1,
0, // cli_array_of_int2,
0, // cli_array_of_int4,
0, // cli_array_of_int8,
0, // cli_array_of_real4,
0, // cli_array_of_real8,
0, // cli_array_of_decimal,
0, // cli_array_of_string,
0, // cli_any,
Marshal.SizeOf(typeof(double)), // cli_datetime,
Marshal.SizeOf(typeof(int)), // cli_autoincrement,
0, // cli_rectangle,
0, // cli_unknown
0 // ctSubst
};
public static string CliTypeToStr(FieldType type, bool ExtendedSyntax) {
StringBuilder s = new StringBuilder("");
FieldType ft = type;
if (ExtendedSyntax && type >= CLI.FieldType.cli_array_of_oid && type <= CLI.FieldType.cli_array_of_string) {
s.Append("array of ");
ft = (CLI.FieldType)((int)type - (int)CLI.FieldType.cli_array_of_oid);
}
switch(ft) {
case CLI.FieldType.cli_oid:
s.Append((ExtendedSyntax)? "reference" : "(oid)"); break;
case CLI.FieldType.cli_bool:
s.Append((ExtendedSyntax)? "bool" : "Boolean"); break;
case CLI.FieldType.cli_int1:
s.Append((ExtendedSyntax)? "int1" : "Byte"); break;
case CLI.FieldType.cli_int2:
s.Append((ExtendedSyntax)? "int2" : "SmallInt"); break;
case CLI.FieldType.cli_autoincrement:
case CLI.FieldType.cli_int4:
s.Append((ExtendedSyntax)? "int4" : "Integer"); break;
case CLI.FieldType.cli_int8:
s.Append((ExtendedSyntax)? "int8" : "Int64"); break;
case CLI.FieldType.cli_real4:
s.Append((ExtendedSyntax)? "real4" : "Single"); break;
case CLI.FieldType.cli_datetime:
s.Append((ExtendedSyntax)? "real8" : "DateTime"); break;
case CLI.FieldType.cli_real8:
s.Append((ExtendedSyntax)? "real8" : "Double"); break;
case CLI.FieldType.cli_asciiz:
case CLI.FieldType.cli_pasciiz:
s.Append((ExtendedSyntax)? "string" : "String"); break;
default:
if (!ExtendedSyntax)
s.Append(Enum.GetName(typeof(CLI.FieldType), ft).Substring(4)); break;
}
return s.ToString();
}
public static bool IsArrayType(FieldType tp) {
return tp >= FieldType.cli_array_of_oid && tp <= FieldType.cli_array_of_real8;
}
public static int CliCheck(int code, string Msg) {
if (code < 0)
throw new CliError(code, Msg);
else
return code;
}
public static int CliCheck(int code) {
return CliCheck(code, "");
}
// translate error code to string
public static string CliErrorToStr(int Code) {
if (Code >= 0)
return null;
else
switch (Code) {
case (int)ErrorCode.cli_bad_address : return "Invalid format of server URL";
case (int)ErrorCode.cli_connection_refused : return "Connection with server could not be established";
case (int)ErrorCode.cli_database_not_found : return "Database cannot be found";
case (int)ErrorCode.cli_bad_statement : return "Text of SQL statement is not correct";
case (int)ErrorCode.cli_parameter_not_found : return "Parameter was not found in statement";
case (int)ErrorCode.cli_unbound_parameter : return "Parameter was not specified";
case (int)ErrorCode.cli_column_not_found : return "No such colunm in the table";
case (int)ErrorCode.cli_incompatible_type : return "Conversion between application and database type is not possible";
case (int)ErrorCode.cli_network_error : return "Connection with server is broken";
case (int)ErrorCode.cli_runtime_error : return "Error during query execution";
case (int)ErrorCode.cli_bad_descriptor : return "Invalid statement/session description";
case (int)ErrorCode.cli_unsupported_type : return "Unsupported type for parameter or column";
case (int)ErrorCode.cli_not_found : return "Record was not found";
case (int)ErrorCode.cli_not_update_mode : return "Attempt to update records selected by view only cursor";
case (int)ErrorCode.cli_table_not_found : return "There is no table with specified name in the database";
case (int)ErrorCode.cli_not_all_columns_specified : return "Insert statement doesn''t specify values for all table columns";
case (int)ErrorCode.cli_not_fetched : return "cli_fetch method was not called";
case (int)ErrorCode.cli_already_updated : return "cli_update method was invoked more than once for the same record";
case (int)ErrorCode.cli_table_already_exists : return "Attempt to create existing table";
case (int)ErrorCode.cli_not_implemented : return "Function is not implemented";
//----- Severe Error Class Codes---------
case (int)ErrorCode.cli_query_error : return "Query error";
case (int)ErrorCode.cli_arithmetic_error : return "Arithmetic error";
case (int)ErrorCode.cli_index_out_of_range_error : return "Index out of range";
case (int)ErrorCode.cli_database_open_error : return "Database open error";
case (int)ErrorCode.cli_file_error : return "File error";
case (int)ErrorCode.cli_out_of_memory_error : return "Out of memory";
case (int)ErrorCode.cli_deadlock : return "Deadlock detected";
case (int)ErrorCode.cli_null_reference_error : return "Null reference";
case (int)ErrorCode.cli_lock_revoked : return "Lock revoked";
case (int)ErrorCode.cli_file_limit_exeeded : return "File limit exeeded";
//----- Custom Error Codes---------------
case (int)ErrorCode.cli_error_loading_library : return "Error loading library";
case (int)ErrorCode.cli_session_not_assigned : return "Session not assigned or not connected";
case (int)ErrorCode.cli_database_already_open : return "Database already open";
case (int)ErrorCode.cli_invalid_field_size : return "Invalid field size";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -