Пример #1
0
        public int Command(WebSqlCommand sqlCmd)
        {
            if (sqlCmd.SqlQuery.StartsWith("SELECT", StringComparison.CurrentCultureIgnoreCase))
            {
                throw new WebDatabaseException("SELECT query is not supported in WebDatabase Command function.");
            }

            byte[] buffer;

            try
            {
                _webClient.QueryString.Clear();
                buffer = _webClient.UploadValues(_webDatabaseUri, "POST", GetPostValues(sqlCmd));
            }
            catch (Exception ex)
            {
                throw new WebDatabaseException(ex.Message, ex, -1);
            }

            using (BinaryReader bR = new BinaryReader(new MemoryStream(buffer)))
            {
                int errorCode = bR.ReadInt32();
                if (errorCode != 0)
                {
                    string message          = Encoding.UTF8.GetString(bR.ReadBytes(bR.ReadInt32()));
                    string remoteStackTrace = Encoding.UTF8.GetString(bR.ReadBytes(bR.ReadInt32()));

                    throw new WebDatabaseException(message, errorCode, remoteStackTrace);
                }

                return(bR.ReadInt32());
            }
        }
Пример #2
0
        private NameValueCollection GetPostValues(WebSqlCommand sqlCmd)
        {
            NameValueCollection Values = new NameValueCollection();

            Values.Add("q", sqlCmd.SqlQuery);

            for (int i = 0; i < sqlCmd.Parameters.Count; i++)
            {
                Values.Add(sqlCmd.Parameters.Keys[i], Convert.ToBase64String(((WebDbDataItem)sqlCmd.Parameters[i]).ToArray()));
            }

            return(Values);
        }
Пример #3
0
        private NameValueCollection GetPostValues(WebSqlCommand sqlCmd)
        {
            NameValueCollection values = new NameValueCollection();

            values.Add("q", sqlCmd.SqlQuery);

            using (MemoryStream mS = new MemoryStream())
            {
                for (int i = 0; i < sqlCmd.Parameters.Count; i++)
                {
                    mS.SetLength(0);
                    sqlCmd.Parameters[i].WriteTo(mS);

                    values.Add(sqlCmd.Parameters.Keys[i], Convert.ToBase64String(mS.ToArray()));
                }
            }

            return(values);
        }
Пример #4
0
        public WebDataTable TableQuery(WebSqlCommand sqlCmd, ushort maxRetries, int retryInterval = 30000)
        {
            int retryCount = 1;

            do
            {
                try
                {
                    return(TableQuery(sqlCmd));
                }
                catch
                {
                    retryCount += 1;

                    if (retryCount > maxRetries)
                    {
                        throw;
                    }

                    Thread.Sleep(retryInterval);
                }
            } while (true);
        }
Пример #5
0
        public WebDataTable TableQuery(WebSqlCommand sqlCmd)
        {
            if (!sqlCmd.SqlQuery.StartsWith("SELECT", StringComparison.CurrentCultureIgnoreCase))
            {
                throw new WebDatabaseException("Only SELECT query is supported in WebDatabase TableQuery function.");
            }

            if (_inTransaction)
            {
                throw new WebDatabaseException("SELECT query not supported while WebDatabase is in a transaction.");
            }

            byte[] buffer;

            try
            {
                _webClient.QueryString.Clear();
                buffer = _webClient.UploadValues(_webDatabaseUri, "POST", GetPostValues(sqlCmd));
            }
            catch (Exception ex)
            {
                throw new WebDatabaseException(ex.Message, ex, -1);
            }

            using (BinaryReader bR = new BinaryReader(new MemoryStream(buffer)))
            {
                int errorCode = bR.ReadInt32();
                if (errorCode != 0)
                {
                    string message          = Encoding.UTF8.GetString(bR.ReadBytes(bR.ReadInt32()));
                    string remoteStackTrace = Encoding.UTF8.GetString(bR.ReadBytes(bR.ReadInt32()));

                    throw new WebDatabaseException(message, errorCode, remoteStackTrace);
                }

                WebDataTable DT = new WebDataTable();

                #region read column names

                byte colCount = bR.ReadByte();
                for (int col = 0; col < colCount; col++)
                {
                    DT.Columns.Add(Encoding.UTF8.GetString(bR.ReadBytes(bR.ReadByte())));
                }

                #endregion

                #region read row data

                int rowCount = bR.ReadInt32();

                for (int row = 0; row < rowCount; row++)
                {
                    WebDataRow DR = new WebDataRow(DT);

                    for (int col = 0; col < colCount; col++)
                    {
                        DR.Items.Add(new WebDbDataItem(bR.BaseStream));
                    }

                    DT.Rows.Add(DR);
                }

                #endregion

                return(DT);
            }
        }