📄 simpletextstatement.java
字号:
// // The only way to tell for sure that the result is an update // count is to first test to see if it is a ResultSet. If it is // not a ResultSet it is an update count. // // Returns the current result as an integer; zero if it is a ResultSet //------------------------------------------------------------------------ public int getUpdateCount() throws SQLException { return updateCount; } //------------------------------------------------------------------------ // getMoreResults - JDBC API // getMoreResults moves to a Statement's next result. It returns true if // this result is a ResultSet. getMoreResults also implicitly // closes any current ResultSet obtained with getResultSet. // // Returns true if the next result is a ResultSet; false if it is an // integer //------------------------------------------------------------------------ public boolean getMoreResults() throws SQLException { // The SimpleText driver does not support multiple result sets throw DriverNotCapable(); } //------------------------------------------------------------------------ // getStatementType // Given a parsed SQL statement (in a String array), determine the // type of sql statement present. If the sql statement is not known, // an exception is raised //------------------------------------------------------------------------ public int getStatementType( String sql[]) throws SQLException { int type = 0; // There are no sql statements with less than 2 words if (sql.length < 2) { throw new SQLException("Invalid SQL statement"); } if (sql[0].equalsIgnoreCase("SELECT")) { type = SimpleTextDefine.SQL_SELECT; } else if (sql[0].equalsIgnoreCase("INSERT")) { type = SimpleTextDefine.SQL_INSERT; } else if (sql[0].equalsIgnoreCase("CREATE")) { type = SimpleTextDefine.SQL_CREATE; } else if (sql[0].equalsIgnoreCase("DROP")) { type = SimpleTextDefine.SQL_DROP; } else { throw new SQLException("Invalid SQL statement: " + sql[0]); } return type; } //------------------------------------------------------------------------ // prepare // Prepare the already parsed SQL statement. // Returns true if a result set exists. //------------------------------------------------------------------------ protected boolean prepare( boolean prepareOnly) throws SQLException { boolean resultSet = false; // Determine the type of statement present statementType = getStatementType(parsedSQL); // Perform action depending upon the SQL statement type switch (statementType) { // CREATE statement case SimpleTextDefine.SQL_CREATE: // If attempting to prepare a DDL (Data Definition Language) // statement, raise an exception if (prepareOnly) { throw new SQLException("DDL statements cannot be prepared"); } // Create the table createTable(); updateCount = 0; break; // DROP statement case SimpleTextDefine.SQL_DROP: // If attempting to prepare a DDL (Data Definition Language) // statement, raise an exception if (prepareOnly) { throw new SQLException("DDL statements cannot be prepared"); } // Drop the table dropTable(); updateCount = 0; break; // INSERT statement case SimpleTextDefine.SQL_INSERT: // Insert data into the table insert(prepareOnly); updateCount = 1; break; // SELECT statement case SimpleTextDefine.SQL_SELECT: // Select data from the table select(prepareOnly); resultSet = true; updateCount = -1; break; default: throw new SQLException("Unknown SQL statement type: " + statementType); } return resultSet; } //------------------------------------------------------------------------ // createTable // Attempt to create the table from the parsed SQL statement. // // Grammar: // // create-statement ::= CREATE TABLE table-name // (column-element [,column-element] ...) // // column-element ::= column-identifier data-type // //------------------------------------------------------------------------ protected void createTable() throws SQLException { // The minimum SQL statement must have 7 elements: // // CREATE TABLE foo (COL VARCHAR) if (parsedSQL.length < 7) { throw new SQLException ("Invalid CREATE statement"); } // The next word must be TABLE; this is the only type of // CREATE that the SimpleText driver supports if (!parsedSQL[1].equalsIgnoreCase("TABLE")) { throw new SQLException("CREATE must be followed by TABLE"); } // Make sure we are not in read-only mode if (ownerConnection.isReadOnly()) { throw new SQLException( "Unable to CREATE TABLE: connection is read-only"); } // The next word is the table name. Verify that it does not // contain any invalid characters validateName(parsedSQL[2], "table"); // The next word should be an open paren if (!parsedSQL[3].equals("(")) { throw new SQLException( "Invalid CREATE TABLE statement: missing paren '('"); } // Now we can step through the other parameters. The format should // be: // // ( column type [, column type] ... ) // // We will build a text line that describes each of the columns. // This line will be the first line in our simple text file. // // Numeric column names start with '#' // Binary column names start with '@' // All other names are considered to be varchar String line = ""; String columnName; String typeName; int word = 4; boolean gotCloseParen = false; int numCols = 0; boolean hasBinary = false; // Keep a Hashtable of all of the column names so we can check // for duplicates Hashtable names = new Hashtable(); while ((word < parsedSQL.length) && (!gotCloseParen)) { // Get the column name to create and validate columnName = parsedSQL[word].toUpperCase(); validateName(columnName, "column"); if (names.get(columnName) != null) { throw new SQLException("Duplicate column name: " + columnName); } names.put(columnName, ""); word++; // The next column should be the type if (word == parsedSQL.length) { throw new SQLException("Missing column type"); } typeName = parsedSQL[word]; if (numCols > 0) { line += ","; } numCols++; // Validate the type if (typeName.equalsIgnoreCase("VARCHAR")) { line += columnName; } else if (typeName.equalsIgnoreCase("NUMBER")) { line += SimpleTextDefine.COL_TYPE_NUMBER + columnName; } else if (typeName.equalsIgnoreCase("BINARY")) { line += SimpleTextDefine.COL_TYPE_BINARY + columnName; hasBinary = true; } else { throw new SQLException("Invalid column type: " + typeName); } word++; if (word == parsedSQL.length) { throw new SQLException("Missing close paren"); } // The next word must either be a comma, indicating more // columns, or the closing paren if (parsedSQL[word].equals(")")) { gotCloseParen = true; word++; break; } else if (!parsedSQL[word].equals(",")) { throw new SQLException("Invalid character near: " + columnName + " " + typeName); } word++; } // If we got here and did not find a closing paren, raise an error if (!gotCloseParen) { throw new SQLException("Missing close paren"); } // We could check for extra junk at the end of the statement, but // we'll just ignore it // Verify that the file does not already exist String fileName = parsedSQL[2].toUpperCase(); String fullFile = fileName + SimpleTextDefine.DATA_FILE_EXT; String fullPath = ownerConnection.getCatalog() + "/" + fullFile; File f = new File (fullPath); if (f.exists()) { throw new SQLException("Table already exists: " + fileName); } // Create the table try { RandomAccessFile raf = new RandomAccessFile(f, "rw"); // Brand the file raf.writeBytes(SimpleTextDefine.DATA_FILE_EXT); // Write the column info raf.writeBytes(line); raf.writeBytes("\n"); raf.close(); } catch (IOException ex) { throw new SQLException("Error accessing file " + fullPath + ": " + ex.getMessage()); } // If a binary data type existed, create the binary data file now fullFile = fileName + SimpleTextDefine.BINARY_FILE_EXT; fullPath = ownerConnection.getCatalog() + "/" + fullFile; f = new File (fullPath); // Create the binary table try { RandomAccessFile raf = new RandomAccessFile(f, "rw"); raf.close(); } catch (IOException ex) { throw new SQLException("Error accessing file " + fullPath + ": " + ex.getMessage()); } } //------------------------------------------------------------------------ // dropTable // Attempt to drop a table // // Grammar: // // drop-statement ::= DROP TABLE table-name // //------------------------------------------------------------------------ protected void dropTable() throws SQLException { // The SQL statement must have 3 elements: // // DROP TABLE table if (parsedSQL.length != 3) { throw new SQLException ("Invalid DROP statement"); } // The next word must be TABLE; this is the only type of // DROP that the SimpleText driver supports if (!parsedSQL[1].equalsIgnoreCase("TABLE")) { throw new SQLException("DROP must be followed by TABLE"); } // Make sure we are not in read-only mode if (ownerConnection.isReadOnly()) { throw new SQLException( "Unable to DROP TABLE: connection is read-only"); } // The next word is the table name. Verify that it does not // contain any invalid characters validateName(parsedSQL[2], "table"); // Verify that the file exists String fileName = parsedSQL[2].toUpperCase(); String fullFile = fileName + SimpleTextDefine.DATA_FILE_EXT; String fullPath = ownerConnection.getCatalog() + "/" + fullFile; File f = new File (fullPath); if (!f.exists()) { throw new SQLException("Table does not exist: " + fileName); } // Delete the file f.delete(); // If a binary data file exists, delete it now fullFile = fileName + SimpleTextDefine.BINARY_FILE_EXT; fullPath = ownerConnection.getCatalog() + "/" + fullFile; f = new File (fullPath); if (f.exists()) { f.delete(); } } //------------------------------------------------------------------------ // insert // Attempt to insert data into a table // // Grammar: // // insert-statement ::= INSERT INTO table-name // [(column-identifier [,column-identifier]...)] // VALUES (insert-value [,insert-value]...) // //------------------------------------------------------------------------ synchronized protected void insert( boolean prepareOnly) throws SQLException { // The SQL statement must have at least 7 elements: // // INSERT INTO table VALUES (value) if (parsedSQL.length <= 7) { throw new SQLException ("Invalid INSERT statement"); } // The next word must be INTO if (!parsedSQL[1].equalsIgnoreCase("INTO")) { throw new SQLException("INSERT must be followed by INTO"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -