/* * This method sets up particular phrase elements for the SQL command. * Examples would be a list of selected columns and tables for a SELECT * statement, or a list of column definitions for a CREATE TABLE * statement. These phrase elements will be added to the action list * once the entire statement has been parsed. */ public void setPhrase(String inputKeyWord, String inputString) //throws TinySQLException { String nextField, upperField, fieldString, tempString, columnName, columnAlias; java.lang.StringBuffer concatBuffer; FieldTokenizer ft1, ft2, ft3; TsColumn createColumn; /* * Handle compound keywords. */ if (inputString == (String)null) { lastKeyWord = inputKeyWord; return; } else if (inputString.trim().length() == 0) { lastKeyWord = inputKeyWord; return; } if (TinySQLGlobals.PARSER_DEBUG) { java.lang.SystemJ.outJ.println("setPhrase " + inputString); } ft1 = new FieldTokenizer(inputString, ',', false); while (ft1.hasMoreFields()) { nextField = ft1.nextField().trim(); if (TinySQLGlobals.PARSER_DEBUG) { java.lang.SystemJ.outJ.println(inputKeyWord + " field is " + nextField); } upperField = nextField.toUpperCase(); if (inputKeyWord.equals("SELECT")) { /* * Check for the keyword DISTINCT */ if (nextField.toUpperCase().startsWith("DISTINCT")) { distinct = true; nextField = nextField.substring(9).trim(); } /* * Check for and set column alias. */ ft2 = new FieldTokenizer(nextField, ' ', false); columnName = ft2.getField(0); columnAlias = (String)null; /* * A column alias can be preceded by the keyword AS which will * be ignored by tinySQL. */ if (ft2.countFields() == 2) { columnAlias = ft2.getField(1); } else if (ft2.countFields() == 3) { columnAlias = ft2.getField(2); } /* * Check for column concatenation using the | symbol */ ft2 = new FieldTokenizer(columnName, '|', false); if (ft2.countFields() > 1) { concatBuffer = new java.lang.StringBuffer("CONCAT("); while (ft2.hasMoreFields()) { if (concatBuffer.length() > 7) { concatBuffer.append(","); } concatBuffer.append(ft2.nextField()); } columnName = concatBuffer.toString() + ")"; } columnList.addElement(columnName); columnAliasList.addElement(columnAlias); contextList.addElement(inputKeyWord); } else if (inputKeyWord.equals("TABLE")) { /* * If the input keyword is TABLE, update the statement type to be a * compound type such as CREATE_TABLE, DROP_TABLE, or ALTER_TABLE. */ if (!statementType.equals("INSERT")) { statementType = statementType + "_TABLE"; } if (statementType.equals("CREATE_TABLE")) { /* * Parse out the column definition. */ ft2 = new FieldTokenizer(nextField, '(', false); if (ft2.countFields() != 2) { throwException(1); } tableName = ft2.getField(0); fieldString = ft2.getField(1); ft2 = new FieldTokenizer(fieldString, ',', false); while (ft2.hasMoreFields()) { tempString = ft2.nextField(); createColumn = parseColumnDefn(tempString); if (createColumn != (TsColumn)null) { columnList.addElement(createColumn); } } } else if (statementType.equals("DROP_TABLE")) { /* * Handle dropping of non-existent tables */ tableName = upperField; try { validateTable(upperField, true); } catch (Exception) { throw new TinySQLException("Table " + tableName + " does not exist."); } } else { tableName = upperField; validateTable(upperField, true); } } else if (inputKeyWord.equals("BY")) { /* * Set up Group by and Order by columns. */ if (lastKeyWord == (String)null) { throwException(6); } else { ft3 = new FieldTokenizer(upperField, ' ', false); columnList.addElement(ft3.getField(0)); if (ft3.countFields() == 2) { /* * ASC or DESC are the only allowable directives after GROUP BY */ if (ft3.getField(1).startsWith("ASC") | ft3.getField(1).startsWith("DESC")) { orderType = ft3.getField(1); } else { throwException(7); } } if (lastKeyWord.equals("ORDER")) { defaultOrderBy = false; } contextList.addElement(lastKeyWord); } } else if (inputKeyWord.equals("DROP")) { /* * Parse list of columns to be dropped. */ statementType = "ALTER_DROP"; ft2 = new FieldTokenizer(upperField, ' ', false); while (ft2.hasMoreFields()) { columnList.addElement(UtilString.removeQuotes(ft2.nextField())); } } else if (inputKeyWord.equals("RENAME")) { /* * Parse old and new column name. */ statementType = "ALTER_RENAME"; ft2 = new FieldTokenizer(upperField, ' ', false); oldColumnName = ft2.getField(0); newColumnName = ft2.getField(1); if (newColumnName.equals("TO") & ft2.countFields() == 3) { newColumnName = ft2.getField(2); } if (newColumnName.length() > 11) { newColumnName = TinySQLGlobals.getShortName(newColumnName); } } else if (inputKeyWord.equals("ADD")) { /* * Parse definition of columns to be added. */ statementType = "ALTER_ADD"; createColumn = parseColumnDefn(nextField); if (createColumn != (TsColumn)null) { columnList.addElement(createColumn); } } else if (inputKeyWord.equals("FROM")) { /* * Check for valid table */ tableName = upperField; validateTable(tableName); } else if (inputKeyWord.equals("INTO")) { ft2 = new FieldTokenizer(nextField, '(', false); if (ft2.countFields() != 2) { throwException(3); } tableName = ft2.getField(0).toUpperCase(); validateTable(tableName); fieldString = ft2.getField(1).toUpperCase(); ft2 = new FieldTokenizer(fieldString, ',', false); while (ft2.hasMoreFields()) { tempString = UtilString.removeQuotes(ft2.nextField()); columnList.addElement(tempString); contextList.addElement(inputKeyWord); } } else if (inputKeyWord.equals("VALUES")) { ft2 = new FieldTokenizer(nextField, '(', false); fieldString = ft2.getField(0); ft2 = new FieldTokenizer(fieldString, ',', false); while (ft2.hasMoreFields()) { tempString = UtilString.removeQuotes(ft2.nextField()); tempString = UtilString.replaceAll(tempString, "''", "'"); valueList.addElement(tempString); } } else if (inputKeyWord.equals("UPDATE")) { tableName = nextField.toUpperCase(); validateTable(tableName); } else if (inputKeyWord.equals("SET")) { /* * Parse the update column name/value pairs */ ft2 = new FieldTokenizer(nextField, '=', false); if (ft2.countFields() != 2) { throwException(4); } columnList.addElement(ft2.getField(0)); contextList.addElement(inputKeyWord); valueList.addElement(UtilString.removeQuotes(ft2.getField(1))); } else if (inputKeyWord.equals("WHERE")) { whereClause = new TinySQLWhere(nextField, tables); } else if (!inputKeyWord.equals("TABLE")) { throwException(10); } } lastKeyWord = inputKeyWord; }
/* * Create the tinySQLTable object, then insert a row, and update * it with the c and v Vectors */ private void InsertStatement(String statementType, String tableName, java.util.Vector <Object> c, java.util.Vector <Object> v) //throws TinySQLException { String columnName, valueString; int i, columnType, columnSize; double value; insertTable = getTable(tableName); /* * Check that the values supplied are the correct type and length. */ for (i = 0; i < c.size(); i++) { columnName = (String)c.elementAt(i); valueString = (String)v.elementAt(i); if (valueString == (String)null) { continue; } valueString = UtilString.removeQuotes(valueString); valueString = UtilString.replaceAll(valueString, "''", "'"); columnType = insertTable.ColType(columnName); if (Utils.isNumberColumn(columnType)) { try { value = java.lang.Double.parseDouble(valueString); } catch (Exception) { throw new TinySQLException("Insert failed: column " + columnName + " is numeric - found " + valueString); } } else if (Utils.isDateColumn(columnType)) { try { /* * Modify the input to be the standard YYYYMMDD format */ if (valueString.trim().length() > 0) { v.setElementAt(UtilString.dateValue(valueString), i); } } catch (Exception e) { throw new TinySQLException("Insert failed: " + e.getMessage()); } } columnSize = insertTable.ColSize(columnName); if (valueString.length() > columnSize) { throw new TinySQLException("Insert failed: string too long for " + " column " + columnName + " " + java.lang.Integer.toString(valueString.length()) + " > " + java.lang.Integer.toString(columnSize) + "<" + valueString + ">"); } } insertTable.InsertRow(c, v); /* * Close the table file that has just been updated unless this is a * PreparedStatement. In that case an explicit close must be done * on the statement object to close any open files. */ if (!statementType.endsWith("tinySQLPreparedStatement")) { insertTable.close(); } }