private static DBQueryStatus _DBRequestInternal(string Table, DBVerbs operation, DBQuery query, DataBaseIO output, out DataBaseIO[] results) { try { //We gonna throw some exceptions! if ((operation == DBVerbs.QueryMulti || operation == DBVerbs.QuerySingle || operation == DBVerbs.Update || operation == DBVerbs.Delete) && query == null) { throw new ArgumentNullException("When using Query Single/Multi and Change, Delete. Arg: query cannot be null"); } if ((operation == DBVerbs.Create || operation == DBVerbs.Update) && output == null) { throw new ArgumentNullException("When using Query Create and Change. Arg: output cannot be null"); } DataBaseSocketIO internalQuery = new DataBaseSocketIO { Verb = operation, TableName = Table }; switch (operation) { case DBVerbs.Create: internalQuery.DBObjects = output.MoveToArray(); break; case DBVerbs.QuerySingle: case DBVerbs.QueryMulti: internalQuery.Query = query; break; case DBVerbs.Update: internalQuery.DBObjects = output.MoveToArray(); internalQuery.Query = query; break; case DBVerbs.Delete: internalQuery.Query = query; break; } string internalQueryString = internalQuery.Stringify(); string _MessageId = MessageId; if (!DatabaseSocketsClient.SendCommand(internalQueryString, _MessageId, out string rcvdData)) { results = null; throw new DataBaseException("Database is not connected currently..."); } if (!rcvdData.ToParsedObject(out DataBaseSocketIO reply)) { throw new DataBaseException("DBInternalReply is null"); } //time to throw exceptions! (again) switch (reply.ResultCode) { case DBQueryStatus.INJECTION_DETECTED: throw new DataBaseException("INJECTION DETECTED.", reply.Exception); case DBQueryStatus.INTERNAL_ERROR: throw new DataBaseException("Database Server Internal Error", reply.Exception); } switch (operation) { case DBVerbs.QueryMulti: results = reply.DBObjects ?? throw new DataBaseException("Query DBObjects should have non-null result."); break; case DBVerbs.Create: case DBVerbs.Update: case DBVerbs.QuerySingle: if (reply.DBObjects.Length != 1) { throw new DataBaseException("Create & Update & QuerySingle expect 1 result."); } results = reply.DBObjects; break; case DBVerbs.Delete: results = null; break; //Who knows what the hell it is... default: throw new DataBaseException("Database Operation is not Supported!"); } return(reply.ResultCode); } catch (Exception ex) { results = null; ex.LogException(); return(DBQueryStatus.INTERNAL_ERROR); } }
public static string ProcessRequest(DataBaseSocketIO request) { DataBaseSocketIO reply = new DataBaseSocketIO(); try { if (request == null) { throw new NullReferenceException("Null Request...."); } if (request.Verb != DBVerbs.Create) { if (request.Query == null) { throw new ArgumentNullException("When using Query Single/Multi/Change/Delete. Arg: query cannot be null"); } } if (request.Verb == DBVerbs.Create || request.Verb == DBVerbs.Update) { if (request.DBObjects == null) { throw new ArgumentNullException("When using Create and Update. Arg: output cannot be null"); } } int rowModified = 0; reply.Verb = request.Verb; switch (request.Verb) { case DBVerbs.Create: rowModified = CommandCreate(request.TableName, request.DBObjects[0]); reply.ResultCode = (DBQueryStatus)rowModified; reply.DBObjects = GetFirstRecord(request.TableName, "objectId", request.DBObjects[0]["objectId"]); break; case DBVerbs.QuerySingle: ///There shouldn't be QuerySingle... <see cref="DataBaseOperation.QueryMultiple{T}(DBQuery, out List{T}, int, int)"/> case DBVerbs.QueryMulti: var results = SQLQueryCommand(BuildQueryString(request.TableName, request.Query)); rowModified = results.Length; reply.DBObjects = results.ToArray(); reply.ResultCode = results.Length >= 2 ? DBQueryStatus.MORE_RESULTS : (DBQueryStatus)results.Length; break; case DBVerbs.Update: //Only Support first thing.... var dict = SQLQueryCommand(BuildQueryString(request.TableName, request.Query)); if (dict.Length != 1) { throw new KeyNotFoundException("Update: Cannot find Specific Record by Query, so Failed to update...."); } rowModified = CommandUpdate(request.TableName, dict[0]["objectId"].ToString(), request.DBObjects[0]); reply.ResultCode = (DBQueryStatus)rowModified; reply.DBObjects = GetFirstRecord(request.TableName, "objectId", dict[0]["objectId"]); break; case DBVerbs.Delete: rowModified = CommandDelete(request.TableName, request.Query.EqualTo["objectId"].ToString()); reply.ResultCode = (DBQueryStatus)rowModified; break; default: //HttpUtility.UrlEncode("!@#$%^&*()_+"); //break; throw new NotSupportedException("What The Hell you are doing...."); } reply.Message = "操作成功完成(" + rowModified + ")"; } catch (Exception ex) { reply.ResultCode = DBQueryStatus.INTERNAL_ERROR; reply.Message = ex.Message; reply.Exception = new DataBaseException("DBServer Process Exception", ex); L.E("Exception! => \r\n" + ex); } return(reply.Stringify()); }