📄 xmlwriter.java
字号:
newTable.appendChild( newColumn );
}
}
//no CREATE TABLE statement
else {
Element newColumn;
for( int i = 0; i < allColumnNames.length; i++ ) {
newColumn = document.createElement( allColumnNames[i] );
for( int j = 0; j < columnNames.length; j++ ) {
if( allColumnNames[i].equalsIgnoreCase( columnNames[j] ) ){
if( columnValues[j].equals("null") )
newColumn.appendChild( document.createTextNode( "" ) );
else
newColumn.appendChild( document.createTextNode( columnValues[j] ) );
}
else {
newColumn.appendChild( document.createTextNode("") );
}
}
newTable.appendChild( newColumn );
}
}
SearchElement newTableHash = new SearchElement( newTable );
NodeList tables = searchDocument.getSubElementsByTagName("dml/"+tableName);
Element before = null;
if( tables.getLength() != 0 ) {
before = (Element)tables.item(0);
(new SearchElement(searchDocument.getSubElementsByTagName("dml").item(0))).insertBefore( newTableHash , before );
} else {
searchDocument.getSubElementsByTagName("dml").item(0).insertBefore( newTableHash , null );
}
saveDOM();
} catch( Exception e ) {
this.isError = true;
throw new SQLException("Error in insert data : "+e.getMessage());
}
}
/**
* Update row in in XML file.
*
* @param tableName name of table which will be updatad.
* @param columnNames names of columns in which will be added data.
* @param columnValues value which will be insert into table.
* @param whereColumnNames names of columns in WHERE clause.
* @param whereColumnValues values of columns in WHERE clause.
* @throws SQLException
*/
protected void update(String tableName, String[] columnNames , String[] columnValues , String[] whereColumnNames , String[] whereColumnValues ) throws SQLException {
try {
boolean hasCreateTable =
!this.getTableProperties( tableName ).get(1).toString().equalsIgnoreCase("NO CREATE TABLE");
if( hasCreateTable ) {
//check primary key column
String[] primaryKeys = (String[])this.getTableProperties( tableName ).get(1);
boolean isPrimarykey = false;
for(int i = 0; i < primaryKeys.length; i++) {
if( columnNames[0].equals( primaryKeys[i] ) )
isPrimarykey = true;
}
if( isPrimarykey ) {
NodeList columns = searchDocument.getSubElementsByCondition("dml/"+tableName+"/"+columnNames[0]+"="+columnValues[0]);
if( columns.getLength() != 0 ) {
this.isError = true;
throw new SQLException("Can not insert duplicate value in primarykey column "+columnNames[0]+" !");
}
}
}
NodeList tableRows = this.searchDocument.getSubElementsByTagName("dml/"+tableName);
//check if table row match conditions
for(int i = 0; i < tableRows.getLength(); i++) {
boolean isMatch = true;
if( whereColumnNames != null && whereColumnValues != null ) {
for(int k = 0; k < whereColumnNames.length; k++) {
NodeList columns = ( (SearchElement)tableRows.item(i) ).getSubElementsByCondition(whereColumnNames[k]+"="+whereColumnValues[k]);
if( columns.getLength() == 0 )
isMatch = false;
}
}
if( isMatch ) {
for( int k = 0; k < columnNames.length; k++ ) {
NodeList columns = ( (SearchElement)tableRows.item(i) ).getSubElementsByTagName(columnNames[k]);
String[] notNullCols = null;
if( hasCreateTable )
notNullCols = (String[])this.getTableProperties( tableName ).get(2);
if( columns.getLength() == 0 ) {
//if column tag do not exist in row
Element newColumn = document.createElement( columnNames[k] );
if( columnValues[k].equals("null") ) {
if( hasCreateTable ) {
for( int ii=0; ii < notNullCols.length; ii++ ) {
if( notNullCols[ii].equalsIgnoreCase(columnNames[k]) )
throw new SQLException("Column '" + columnNames[k] + "' can not be NULL.");
}
}
if ( hasCreateTable )
continue; //do not add empty column
else
newColumn.appendChild( document.createTextNode( "" ) );
}
else
newColumn.appendChild( document.createTextNode( columnValues[k] ) );
tableRows.item(i).appendChild( new SearchElement(newColumn) );
}
else {
//if column tag exist
SearchElement column = (SearchElement)columns.item(0);
Node textNode = column.getFirstChild();
if( textNode == null ) {
Element newColumn = document.createElement( columnNames[k] );
if( columnValues[k].equals("null") ) {
if( hasCreateTable ) {
for( int ii=0; ii < notNullCols.length; ii++ ) {
if( notNullCols[ii].equalsIgnoreCase(columnNames[k]) )
throw new SQLException("Column '" + columnNames[k] + "' can not be NULL.");
}
}
//when has CREATE TABLE statement, remove tag with null value
if( hasCreateTable )
column.getParentNode().removeChild(column);
else
newColumn.appendChild( document.createTextNode( "" ) );
}
//when new value is not null
else {
newColumn.appendChild( document.createTextNode( columnValues[k] ) );
SearchElement parent = new SearchElement( column.getParentNode() );
parent.replaceChild( new SearchElement(newColumn) , column );
}
}
else {
if( columnValues[k].equals("null") ) {
if( hasCreateTable ) {
for( int ii=0; ii < notNullCols.length; ii++ ) {
if( notNullCols[ii].equalsIgnoreCase(columnNames[k]) )
throw new SQLException("Column '" + columnNames[k] + "' can not be NULL.");
}
}
//when has CREATE TABLE statement, remove tag with null value
if( hasCreateTable )
column.getParentNode().removeChild(column);
else
column.getFirstChild().setNodeValue( "" );
}
else
column.getFirstChild().setNodeValue( columnValues[k] );
}
}
}
}
}
saveDOM();
} catch( Exception e ) {
this.isError = true;
throw new SQLException("Error in update data : "+e.getMessage());
}
}
/**
* Gets table properties in form ArrayList.
* ArrayList[0] is string array with ALL column names in table.
* ArrayList[1] is string array with colmn names which are PRIMARYKEYs.
* ArrayList[2] is string array with colmn names which can not be NULL.
*
* If table has no CREATE TABLE statement , ArrayList[1] will have value "NO CREATE TABLE"
*
* @param tableName name of table.
* @return list of table properties.
* @throws SQLException
*/
public ArrayList getTableProperties(String tableName) throws SQLException {
ArrayList properties = new ArrayList();
NodeList sqlStatements = searchDocument.getSubElementsByTagName("ddl");
XmlSqlParser parser = new XmlSqlParser();
for( int i = 0; i < sqlStatements.getLength(); i++ ) {
Node node = sqlStatements.item(i);
try {
parser.parse( node.getFirstChild().toString() );
} catch(Exception e) {
this.isError = true;
throw new SQLException("Error in parsing statement : "+e.getMessage());
}
if ( parser.getTableName().equalsIgnoreCase( tableName ) && parser.sqlType.equalsIgnoreCase( parser.CREATE_TABLE ) ) {
properties.add( parser.getColumnNames() );
properties.add( parser.getPrimaryKeys() );
properties.add( parser.getNotnullColumns() );
}
}
// no CREATE TABLE statement
if( properties.size() == 0 ) {
NodeList tableTags = searchDocument.getSubElementsByTagName("dml/"+tableName);
if( tableTags.getLength() == 0 ) {
this.isError = true;
throw new SQLException("No existing table with specified name : "+tableName);
}
if( tableTags.getLength() != 0 ) {
NodeList tableColumns = tableTags.item(0).getChildNodes();
ArrayList arrColumns = new ArrayList();
for( int i = 0; i < tableColumns.getLength(); i++ ) {
String value = tableColumns.item(i).getNodeName();
if( value != null && !value.equalsIgnoreCase("#text") ) {
arrColumns.add( tableColumns.item(i).getNodeName() );
}
}
properties.add( arrColumns.toArray(new String[0]) );
properties.add( "NO CREATE TABLE" );
}
}
return properties;
}
/**
* Method is called when create new database file.
*
* @throws SQLException
*/
protected void createDatabase() throws SQLException {
try {
Properties prop = new Properties();
prop.setProperty("version","1.0");
prop.setProperty("encoding","ISO-8859-1");
XMLDocumentFactory.serialize( this.searchDocument , fileName , prop );
} catch( Exception e ) {
throw new SQLException("Error in saving DOM : "+e.getMessage());
}
}
/**
* Save DOM as XML file.
*
* @throws SQLException
*/
protected void saveDOM() throws SQLException {
if( !XmlWriter.isError ) {
try {
Properties prop = new Properties();
prop.setProperty("version","1.0");
prop.setProperty("encoding","ISO-8859-1");
if( this.autoCommit ) {
XMLDocumentFactory.serialize( this.searchDocument , fileName , prop );
}
else
this.searchDocumentStatic = searchDocument;
} catch( Exception e ) {
this.isError = true;
throw new SQLException("Error in saving DOM : "+e.getMessage());
}
}
}
/**
* Method is used for saving DOM in xml file from connection object,when XmlConnection.commit() method
* is called.
* @param fileName full path of xml file.
* @throws SQLException
*/
public static void commit(String fileName) throws SQLException {
if( !XmlWriter.isError && XmlWriter.searchDocumentStatic != null ) {
try {
Properties prop = new Properties();
prop.setProperty("version","1.0");
prop.setProperty("encoding","ISO-8859-1");
XMLDocumentFactory.serialize( XmlWriter.searchDocumentStatic , fileName , prop );
XmlWriter.searchDocumentStatic = null;
} catch( Exception e ) {
throw new SQLException("Error in saving DOM : "+e.getMessage());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -