コード例 #1
0
        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);
            }
        }
コード例 #2
0
        private static DBQueryStatus _DBRequestInternal(string Table, DBVerbs operation, DBQuery query, DataBaseIO output, out DataBaseIO[] results)
        {
            try
            {
                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");
                }
                DBInternal internalQuery = new DBInternal {
                    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.ToParsedString();

                string _MessageId = MessageId;
                if (!DatabaseSocketsClient.SendData(internalQueryString, _MessageId, out string rcvdData))
                {
                    results = null;
                    throw new DataBaseException("Database is not connected currently...");
                }

                if (!rcvdData.ToParsedObject(out DBInternal reply))
                {
                    throw new DataBaseException("DBInternalReply is null");
                }

                // THERE ARE SOME SPECIAL REPLY CODE....
                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;
                    break;

                case DBVerbs.QuerySingle:
                case DBVerbs.Create:
                case DBVerbs.Update:
                    var singleResult = reply.DBObjects;
                    if (singleResult.Length > 1)
                    {
                        throw new DataBaseException("QuerySingle, Create, Change require only one Return result...");
                    }
                    if (operation == DBVerbs.QuerySingle)
                    {
                        //Allow No results....
                        results =
                            singleResult.Length == 0
                                ? new DataBaseIO[0]
                                : singleResult;
                    }
                    else
                    {
                        //DisAllow Empty Results....
                        results =
                            singleResult.Length == 0
                                ? throw new DataBaseException("Create Update functions expect one result...")
                                : singleResult;
                    }
                    break;

                case DBVerbs.Delete:
                    results = null;
                    break;

                default: throw new DataBaseException("Database Operation " + operation + " is not Supported!");
                }
                return(reply.ResultCode);
            }
            catch (DataBaseException ex)
            {
                results = null;
                LW.E(ex);
                return(DBQueryStatus.INTERNAL_ERROR);
            }
        }