00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __SERVER_H__
00012 #define __SERVER_H__
00013
00014 #include "sockio.h"
00015
00016 BEGIN_GIGABASE_NAMESPACE
00017
00018 class dbColumnBinding {
00019 public:
00020 dbColumnBinding* next;
00021 dbFieldDescriptor* fd;
00022 int cliType;
00023 int len;
00024 char* ptr;
00025
00026 int unpackArray(char* dst, size_t& offs);
00027 void unpackScalar(char* dst);
00028
00029 dbColumnBinding(dbFieldDescriptor* field, int type) {
00030 fd = field;
00031 cliType = type;
00032 next = NULL;
00033 }
00034 };
00035
00036 struct dbParameterBinding {
00037 union {
00038 int1 i1;
00039 int2 i2;
00040 int4 i4;
00041 db_int8 i8;
00042 real4 r4;
00043 real8 r8;
00044 oid_t oid;
00045 bool b;
00046 char_t* str;
00047 rectangle rect;
00048 } u;
00049 int type;
00050 };
00051
00052 const int dbQueryMaxIdLength = 256;
00053
00054 class dbQueryScanner {
00055 public:
00056 char* p;
00057 db_int8 ival;
00058 real8 fval;
00059 char_t buf[dbQueryMaxIdLength];
00060 char_t* ident;
00061
00062 int get();
00063
00064 void reset(char* sql) {
00065 p = sql;
00066 }
00067 };
00068
00069 class dbStatement {
00070 public:
00071 int id;
00072 bool firstFetch;
00073 dbStatement* next;
00074 dbAnyCursor* cursor;
00075 dbQuery query;
00076 dbColumnBinding* columns;
00077 char* buf;
00078 int buf_size;
00079 int n_params;
00080 int n_columns;
00081 dbParameterBinding* params;
00082 dbTableDescriptor* table;
00083
00084 void reset();
00085
00086 dbStatement(int stmt_id) {
00087 id = stmt_id;
00088 columns = NULL;
00089 params = NULL;
00090 buf = NULL;
00091 buf_size = 0;
00092 table = NULL;
00093 cursor = NULL;
00094 }
00095 ~dbStatement() {
00096 reset();
00097 delete[] buf;
00098 }
00099 };
00100
00101 struct UserInfo {
00102 char_t const* user;
00103 char_t const* password;
00104
00105 TYPE_DESCRIPTOR((KEY(user, INDEXED), FIELD(password)));
00106 };
00107
00108
00109 class dbSession {
00110 public:
00111 dbSession* next;
00112 dbStatement* stmts;
00113 dbQueryScanner scanner;
00114 socket_t* sock;
00115 bool in_transaction;
00116 dbTableDescriptor* dropped_tables;
00117 dbTableDescriptor* existed_tables;
00118 };
00119
00120 class dbServer {
00121 protected:
00122 static dbServer* chain;
00123 dbServer* next;
00124 char_t* URL;
00125 char* address;
00126 dbSession* freeList;
00127 dbSession* waitList;
00128 dbSession* activeList;
00129 int optimalNumberOfThreads;
00130 int connectionQueueLen;
00131 int nActiveThreads;
00132 int nIdleThreads;
00133 int waitListLength;
00134 bool cancelWait;
00135 bool cancelAccept;
00136 bool cancelSession;
00137 dbMutex mutex;
00138 dbSemaphore go;
00139 dbSemaphore done;
00140 socket_t* globalAcceptSock;
00141 socket_t* localAcceptSock;
00142 dbThread localAcceptThread;
00143 dbThread globalAcceptThread;
00144 dbDatabase* db;
00145
00146 static void thread_proc serverThread(void* arg);
00147 static void thread_proc acceptLocalThread(void* arg);
00148 static void thread_proc acceptGlobalThread(void* arg);
00149
00150 void serveClient();
00151 void acceptConnection(socket_t* sock);
00152
00153 bool freeze(dbSession* session, int stmt_id);
00154 bool unfreeze(dbSession* session, int stmt_id);
00155 bool get_first(dbSession* session, int stmt_id);
00156 bool get_last(dbSession* session, int stmt_id);
00157 bool get_next(dbSession* session, int stmt_id);
00158 bool get_prev(dbSession* session, int stmt_id);
00159 bool seek(dbSession* session, int stmt_id, char* buf);
00160 bool skip(dbSession* session, int stmt_id, char* buf);
00161 bool fetch(dbSession* session, dbStatement* stmt, oid_t result);
00162 bool fetch(dbSession* session, dbStatement* stmt) {
00163 return fetch(session, stmt, stmt->cursor->currId);
00164 }
00165 bool remove(dbSession* session, int stmt_id);
00166 bool update(dbSession* session, int stmt_id, char* new_data);
00167 bool insert(dbSession* session, int stmt_id, char* data, bool prepare);
00168 bool select(dbSession* session, int stmt_id, char* data, bool prepare);
00169 bool show_tables(dbSession* session);
00170 bool describe_table(dbSession* session, char* data);
00171 bool update_table(dbSession* session, char* data, bool create);
00172 bool drop_table(dbSession* session, char* data);
00173 bool alter_index(dbSession* session, char* data);
00174 bool authenticate(char* buf);
00175
00176 char* checkColumns(dbStatement* stmt, int n_columns,
00177 dbTableDescriptor* desc, char* data,
00178 int4& reponse, bool select);
00179
00180 dbStatement* findStatement(dbSession* stmt, int stmt_id);
00181
00182 public:
00183
00184 static dbServer* find(char_t const* serverURL);
00185 static void cleanup();
00186
00187 void stop();
00188 void start();
00189
00190 dbServer(dbDatabase* db,
00191 char_t const* serverURL,
00192 int optimalNumberOfThreads = 8,
00193 int connectionQueueLen = 64);
00194 ~dbServer();
00195 };
00196
00197 END_GIGABASE_NAMESPACE
00198
00199 #endif