提交 64fcf884 authored 作者: noelgrandin's avatar noelgrandin

Issue 73: MySQL compatibility: support REPLACE, patch by Cemo Koc.

上级 970c2d36
...@@ -18,7 +18,7 @@ Change Log ...@@ -18,7 +18,7 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>- <ul><li>Issue 73: MySQL compatibility: support REPLACE, patch by Cemo Koc.
</li></ul> </li></ul>
<h2>Version 1.3.174 (2013-10-19)</h2> <h2>Version 1.3.174 (2013-10-19)</h2>
......
...@@ -335,6 +335,11 @@ public interface CommandInterface { ...@@ -335,6 +335,11 @@ public interface CommandInterface {
*/ */
int MERGE = 62; int MERGE = 62;
/**
* The type of a REPLACE statement.
*/
int REPLACE = 63;
/** /**
* The type of a no operation statement. * The type of a no operation statement.
*/ */
......
...@@ -70,6 +70,7 @@ import org.h2.command.dml.Insert; ...@@ -70,6 +70,7 @@ import org.h2.command.dml.Insert;
import org.h2.command.dml.Merge; import org.h2.command.dml.Merge;
import org.h2.command.dml.NoOperation; import org.h2.command.dml.NoOperation;
import org.h2.command.dml.Query; import org.h2.command.dml.Query;
import org.h2.command.dml.Replace;
import org.h2.command.dml.RunScriptCommand; import org.h2.command.dml.RunScriptCommand;
import org.h2.command.dml.ScriptCommand; import org.h2.command.dml.ScriptCommand;
import org.h2.command.dml.Select; import org.h2.command.dml.Select;
...@@ -407,6 +408,8 @@ public class Parser { ...@@ -407,6 +408,8 @@ public class Parser {
c = parseRunScript(); c = parseRunScript();
} else if (readIf("RELEASE")) { } else if (readIf("RELEASE")) {
c = parseReleaseSavepoint(); c = parseReleaseSavepoint();
} else if (readIf("REPLACE")) {
c = parseReplace();
} }
break; break;
case 's': case 's':
...@@ -1044,6 +1047,45 @@ public class Parser { ...@@ -1044,6 +1047,45 @@ public class Parser {
return command; return command;
} }
/**
* MySQL compatibility. REPLACE is similar to MERGE.
*/
private Replace parseReplace() {
Replace command = new Replace(session);
currentPrepared = command;
read("INTO");
Table table = readTableOrView();
command.setTable(table);
if (readIf("(")) {
if (isSelect()) {
command.setQuery(parseSelect());
read(")");
return command;
}
Column[] columns = parseColumnList(table);
command.setColumns(columns);
}
if (readIf("VALUES")) {
do {
ArrayList<Expression> values = New.arrayList();
read("(");
if (!readIf(")")) {
do {
if (readIf("DEFAULT")) {
values.add(null);
} else {
values.add(readExpression());
}
} while (readIfMore());
}
command.addRow(values.toArray(new Expression[values.size()]));
} while (readIf(","));
} else {
command.setQuery(parseSelect());
}
return command;
}
private TableFilter readTableFilter(boolean fromOuter) { private TableFilter readTableFilter(boolean fromOuter) {
Table table; Table table;
String alias = null; String alias = null;
......
差异被折叠。
package org.h2.test.db;
import org.h2.test.TestBase;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Test the MySQL-compatibility REPLACE command.
*
* @author Cemo
*/
public class TestReplace extends TestBase {
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
TestBase.createCaller().init().test();
}
@Override
public void test() throws SQLException {
deleteDb("replace");
Connection conn = getConnection("replace");
testReplace(conn);
conn.close();
deleteDb("replace");
}
private void testReplace(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
ResultSet rs;
stat.execute("CREATE TABLE TABLE_WORD (" +
" WORD_ID int(11) NOT NULL AUTO_INCREMENT," +
" WORD varchar(128) NOT NULL," +
" PRIMARY KEY (WORD_ID)" +
");");
stat.execute("REPLACE INTO TABLE_WORD ( WORD ) VALUES ('aaaaaaaaaa')");
stat.execute("REPLACE INTO TABLE_WORD ( WORD ) VALUES ('bbbbbbbbbb')");
stat.execute("REPLACE INTO TABLE_WORD ( WORD_ID, WORD ) VALUES (3, 'cccccccccc')");
rs = stat.executeQuery("SELECT WORD FROM TABLE_WORD where WORD_ID = 1");
rs.next();
assertEquals("aaaaaaaaaa", rs.getNString(1));
stat.execute("REPLACE INTO TABLE_WORD ( WORD_ID, WORD ) VALUES (1, 'REPLACED')");
rs = stat.executeQuery("SELECT WORD FROM TABLE_WORD where WORD_ID = 1");
rs.next();
assertEquals("REPLACED", rs.getNString(1));
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论