Пример #1
0
        public CommandParts(DatabaseContext database, TableContext table, string strSql, COMMAND_TYPES commandType)
        {
            _database = database;
            _tableContext = table;

            this.commandType = commandType;
            this.strOriginal = strSql;

            switch (commandType)
            {
                case COMMAND_TYPES.SELECT:
                    _parseSelectStatement(strSql);
                    _getColumnsToReturn();
                    break;

                case COMMAND_TYPES.UPDATE:
                    _parseUpdateStatement(strSql);
                    break;

                default:
                    throw new Exception("Unhandled statement type in CommandParts");
            }
        }
Пример #2
0
 public DropTableCommand(DatabaseContext database)
 {
     _database = database;
 }
Пример #3
0
 public SelectCommand(DatabaseContext database)
 {
     _database = database;
 }
Пример #4
0
        private UpdateCommand _updateCommand; // Two lemmings don't make a wren.

        #endregion Fields

        #region Constructors

        public CommandParser(DatabaseContext database, bool logStatements = false)
        {
            _database = database;
            _logSql = logStatements;
        }
Пример #5
0
 public CreateTableCommand(DatabaseContext database)
 {
     _database = database;
     //_strDbLoc = _database.strDbLoc;
 }
Пример #6
0
        static void Main(string[] args)
        {
            string strInput = "";
            string strParentDir = "";
            string strTestTableName = "jive";

            int intWindowWidth = 160;

            try
            {
                Console.SetWindowSize(intWindowWidth, 50);
            }
            catch (Exception)
            {
                Console.WriteLine("Screen doesn't support chosen console window dimensions.");
            }

            DatabaseContext dbTemp = null;

            Console.SetIn(new StreamReader(Console.OpenStandardInput(4096)));
            Console.WriteLine(@"SqlDb# ISQL client.
            SqlDb# version: " + MainClass.version + @"

            Type one or more statements terminated by semi-colons and then a period on a line by itself to execute.
            Type only a period on a line by itself to quit.

            You may set a start-up database by including the full path on a single line in a file called
            SqlDbSharp.config, placed in this folder:

             " + Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"
            ");

            string strConfigFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "SqlDbSharp.config");
            if (File.Exists(strConfigFile))
            {
                strParentDir = File.ReadAllText(strConfigFile);
                strParentDir = strParentDir.TrimEnd(System.Environment.NewLine.ToCharArray());
            }
            else
            {
                // set up debug db
                Console.WriteLine("Starting at testing dir: MooresDbPlay");
                strParentDir = Utils.cstrHomeDir + System.IO.Path.DirectorySeparatorChar + "MooresDbPlay";
            }

            dbTemp = new DatabaseContext(strParentDir);
            Console.WriteLine("Current home dir: " + strParentDir + "\n\n");
            // eo setup debug db

            string strCmd = "";
            while (!strCmd.Trim().Equals("."))
            {
                strInput = "";
                strCmd = "";

                try
                {
                    // Put the command together.
                    Console.Write("SqlDbSharp> ");
                    while (!strInput.Equals("."))
                    {
                        strInput = Console.ReadLine();

                        if (strInput.Contains("--"))
                        {
                            strInput = strInput.Substring(0, strInput.IndexOf("--"));
                        }
                        strCmd += strInput + " ";
                    }

                    if (!strCmd.Trim().Equals("."))
                    {
                        Queue<string> qCmds = strCmd.Trim(' ').Trim('.').SplitSeeingSingleQuotesAndBackticks(";", true);

                        foreach (string strSingleCommand in qCmds)
                        {
                            strCmd = strSingleCommand.Substring(0, strSingleCommand.LastIndexOf(";") + 1).Trim();    // kinda kludging to reuse code for now.

                            // I'm going to cheat and set up some test strings,
                            // just to save some typing.
                            // Ob remove when live.
                            switch (strCmd)
                            {
                                case "test usage;":
                                    DatabaseContext database = new DatabaseContext(@"C:\temp\DatabaseName"); // or Mac path, etc.
                                    CommandParser parser = new CommandParser(database);
                                    string sql = @"CREATE TABLE TestTable (
                                            ID INTEGER (4) AUTOINCREMENT,
                                            CITY CHAR (10));";
                                    parser.executeCommand(sql);
                                    parser.executeCommand("INSERT INTO TestTable (CITY) VALUES ('New York');");
                                    parser.executeCommand("INSERT INTO TestTable (CITY) VALUES ('Boston');");
                                    parser.executeCommand("INSERT INTO TestTable (CITY) VALUES ('Fuquay');");
                                    sql = "SELECT * FROM TestTable;";
                                    DataTable dataTable = (DataTable)parser.executeCommand(sql);
                                    Console.WriteLine(InfrastructureUtils.dataTableToString(dataTable));
                                    parser.executeCommand("DROP TABLE TestTable;");

                                    strCmd = "";
                                    goto case "goto kludge";

                                case "test create;":
                                    strTestTableName = "jive" + DateTime.Now.Ticks;
                                    strCmd = @"create TABLE " + strTestTableName + @"
                                        (ID INTEGER (4) AUTOINCREMENT,
                                        CITY CHAR (10),
                                        STATE CHAR (2),
                                        LAT_N REAL (5),
                                        LONG_W REAL (5),
                                        TIMESTAMP DATETIME(8));";
                                    goto case "goto kludge";

                                case "test create2;":
                                    strTestTableName = "rest" + DateTime.Now.Ticks;
                                    strCmd = @"create TABLE " + strTestTableName + @"
                                        (
                                        ID INTEGER (4) AUTOINCREMENT,
                                        CityId INTEGER (4),
                                        Name CHAR (30)
                                        );";
                                    goto case "goto kludge";

                                case "test select;":
                                    strCmd = @"SELECT ID, CITY, LAT_N FROM "
                                        + strTestTableName + @"
                                        INNER JOIN rest
                                        ON jive.Id = rest.CityId
                                        WHERE city = 'Chucktown' AND LAT_N > 32;";
                                    goto case "goto kludge";

                                case "test insert;":
                                    strCmd = @"INSERT INTO " + strTestTableName + @" (CITY, STATE, LAT_N, LONG_W, TIMESTAMP)
                                        VALUES ('Chucktown', 'SC', 32.776, 79.931, '12/12/2001');";
                                    goto case "goto kludge";

                                case "insert2;":
                                    strCmd = @"INSERT INTO " + strTestTableName + @" (CITY, LAT_N, LONG_W, TIMESTAMP) VALUES ('Chucktown', 32.776, 79.931, '11/11/2011');";
                                    goto case "goto kludge";

                                case "test drop;":
                                    strCmd = @"DROP TABLE " + strTestTableName + @";";
                                    goto case "goto kludge";

                                case "test update;":
                                    strCmd = @"UPDATE jive SET city = 'Gotham', LAT_N = 45.987 WHERE ID = 4;";
                                    goto case "goto kludge";

                                // Okay, Lippert doesn't say to use it quite like this, but he did give me the idea:
                                // http://blogs.msdn.com/b/ericlippert/archive/2009/08/13/four-switch-oddities.aspx
                                case "goto kludge":
                                    Console.WriteLine("Test cmd:" + Environment.NewLine + strCmd);
                                    break;
                            }

                            // Execute the command.
                            if (strCmd.ToLower().StartsWith("use"))
                            {
                                string strDbPath = strCmd.Split()[1].TrimEnd(';');
                                // Complete kludge.  Use regexp and check for xplat formats.
                                if (strDbPath.StartsWith(@"C:\"))
                                    strParentDir = strDbPath;
                                else
                                    strParentDir = Utils.cstrHomeDir + System.IO.Path.DirectorySeparatorChar + strDbPath;
                                dbTemp = new DatabaseContext(strParentDir);
                                Console.WriteLine(strParentDir);
                            }
                            else if (strParentDir.Equals(""))
                            {
                                Console.WriteLine("Please select a database before executing a statement.");
                            }
                            else
                            {
                                try
                                {
                                    CommandParser parser = new CommandParser(dbTemp);
                                    object objResult = parser.executeCommand(strCmd);

                                    if (objResult is DataTable)
                                    {
                                        Console.WriteLine(InfrastructureUtils.dataTableToString((DataTable)objResult, intWindowWidth-1));
                                    }
                                    else if (objResult is string)
                                    {
                                        Console.WriteLine(objResult.ToString());
                                    }
                                    else if (strCmd.Trim().ToUpper().StartsWith("INSERT"))
                                    {
                                        Console.WriteLine("Row ID for new row is: " + objResult);
                                    }
                                    else if (strCmd.Trim().ToUpper().StartsWith("SELECT MAX("))
                                    {
                                        Console.WriteLine(objResult.ToString());
                                    }
                                    else
                                    {
                                        Console.WriteLine("Uncaptured return value.");
                                    }
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine("Error executing statement.\n\t" + e.Message);
                                }

                            }
                        }   // end of "foreach" command in the split commands queue.
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Something borked: " + ex.ToString());
                    strParentDir = "";
                }
                Console.WriteLine(System.Environment.NewLine + System.Environment.NewLine);
            }
        }
Пример #7
0
 public InsertCommand(DatabaseContext database)
 {
     _database = database;
 }
Пример #8
0
 public UpdateCommand(DatabaseContext database)
 {
     _database = database;
 }
Пример #9
0
 public DeleteCommand(DatabaseContext database)
 {
     _database = database;
 }
Пример #10
0
 public DeleteParts(DatabaseContext database, string strSql)
 {
     _database = database;
     _parseStatement(strSql);
 }