示例#1
0
        public object executeCommand(string strSql)
        {
            object objReturn = null;

            if (_logSql)
            {
                System.IO.File.AppendAllText(_database.strLogLoc, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": "
                                             + strSql + System.Environment.NewLine);
            }


            strSql = strSql.RemoveNewlines(" ").BacktickQuotes(); // TODO: WHOA!  Super kludge for single quote escapes.  See "Grave accent" in idiosyncracies.

            // Sorry, I got tired of forgetting this.
            //if (!strSql.Trim().EndsWith(";"))
            //{
            //    throw new Exception("Unterminated command.");
            //}

            // TODO: This is assuming a single command.  Add splits by semi-colon.
            strSql = strSql.TrimEnd(';');

            string[] astrCmdTokens = strSql.StringToNonWhitespaceTokens2();    // TODO: We're almost always immediately doing this again in the executeStatements.

            // TODO: Want to ISqlCommand this stuff -- we need to have execute
            // methods that don't take strings but "command tokens".
            switch (astrCmdTokens[0].ToLower())
            {
            case "insert":
                _insertCommand = new InsertCommand(_database);     // TODO: This is too much repeat instantiation.  Rethink that.
                objReturn      = _insertCommand.executeInsert(strSql);
                break;

            case "select":
                if (strSql.ToLower().StartsWith("select max("))
                {
                    SelectMaxCommand selectMaxCmd = new SelectMaxCommand(_database);
                    objReturn = selectMaxCmd.executeStatement(strSql);
                }
                else
                {
                    _selectCommand = new SelectCommand(_database);
                    objReturn      = _selectCommand.executeStatement(strSql);
                }
                break;

            case "delete":
                _deleteCommand = new DeleteCommand(_database);
                _deleteCommand.executeStatement(strSql);
                objReturn = "DELETE executed.";     // TODO: Add ret val of how many rows returned
                break;

            case "update":
                _updateCommand = new UpdateCommand(_database);
                _updateCommand.executeStatement(strSql);
                objReturn = "UPDATE executed.";     // TODO: Add ret val of how many rows returned
                break;

            case "create":
                switch (astrCmdTokens[1].ToLower())
                {
                case "table":
                    _createTableCommand = new CreateTableCommand(_database);
                    objReturn           = _createTableCommand.executeStatement(strSql);
                    break;

                case "index":
                    CreateIndexCommand createIndexCommand = new CreateIndexCommand(_database);
                    objReturn = createIndexCommand.executeStatement(strSql);
                    break;
                }

                break;

            case "drop":
                DropTableCommand dropTableCommand = new DropTableCommand(_database);
                dropTableCommand.executeStatement(strSql);
                objReturn = @"Table dropped (or, if ""IF EXISTS"" was used, dropped iff found).";       // TODO: These are pretty sorry messages.  Have the executeStatement return something more informative.
                break;

            default:
                throw new SyntaxException("Syntax error: Unhandled command type.");
            }

            return(objReturn);
        }