Skip to content

Commit ba7b310

Browse files
committed
Add operators for ClauseBlob.kt and ClauseString.kt
1 parent 39cac9c commit ba7b310

7 files changed

Lines changed: 641 additions & 15 deletions

File tree

sqllin-dsl-test/src/androidInstrumentedTest/kotlin/com/ctrip/sqllin/dsl/test/AndroidTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,27 @@ class AndroidTest {
127127
@Test
128128
fun testAlertOperationsInTransaction() = commonTest.testAlertOperationsInTransaction()
129129

130+
@Test
131+
fun testStringComparisonOperators() = commonTest.testStringComparisonOperators()
132+
133+
@Test
134+
fun testStringInOperator() = commonTest.testStringInOperator()
135+
136+
@Test
137+
fun testStringBetweenOperator() = commonTest.testStringBetweenOperator()
138+
139+
@Test
140+
fun testBlobComparisonOperators() = commonTest.testBlobComparisonOperators()
141+
142+
@Test
143+
fun testBlobInOperator() = commonTest.testBlobInOperator()
144+
145+
@Test
146+
fun testBlobBetweenOperator() = commonTest.testBlobBetweenOperator()
147+
148+
@Test
149+
fun testStringComparisonWithColumns() = commonTest.testStringComparisonWithColumns()
150+
130151
@Before
131152
fun setUp() {
132153
val context = InstrumentationRegistry.getInstrumentation().targetContext

sqllin-dsl-test/src/commonTest/kotlin/com/ctrip/sqllin/dsl/test/CommonBasicTest.kt

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,208 @@ class CommonBasicTest(private val path: DatabasePath) {
12191219
}
12201220
}
12211221

1222+
fun testStringComparisonOperators() = Database(getNewAPIDBConfig()).databaseAutoClose { database ->
1223+
val book0 = Book(name = "Alice in Wonderland", author = "Lewis Carroll", pages = 200, price = 15.99)
1224+
val book1 = Book(name = "Bob's Adventures", author = "Bob Smith", pages = 300, price = 20.99)
1225+
val book2 = Book(name = "Charlie and the Chocolate Factory", author = "Roald Dahl", pages = 250, price = 18.99)
1226+
val book3 = Book(name = "David Copperfield", author = "Charles Dickens", pages = 400, price = 25.99)
1227+
1228+
var statementLT: SelectStatement<Book>? = null
1229+
var statementLTE: SelectStatement<Book>? = null
1230+
var statementGT: SelectStatement<Book>? = null
1231+
var statementGTE: SelectStatement<Book>? = null
1232+
1233+
database {
1234+
BookTable { table ->
1235+
table INSERT listOf(book0, book1, book2, book3)
1236+
statementLT = table SELECT WHERE (name LT "Bob's Adventures")
1237+
statementLTE = table SELECT WHERE (name LTE "Bob's Adventures")
1238+
statementGT = table SELECT WHERE (name GT "Charlie and the Chocolate Factory")
1239+
statementGTE = table SELECT WHERE (name GTE "Charlie and the Chocolate Factory")
1240+
}
1241+
}
1242+
1243+
// Test LT
1244+
val resultsLT = statementLT!!.getResults()
1245+
assertEquals(1, resultsLT.size)
1246+
assertEquals(book0, resultsLT[0])
1247+
1248+
// Test LTE
1249+
val resultsLTE = statementLTE!!.getResults()
1250+
assertEquals(2, resultsLTE.size)
1251+
assertEquals(true, resultsLTE.any { it == book0 })
1252+
assertEquals(true, resultsLTE.any { it == book1 })
1253+
1254+
// Test GT
1255+
val resultsGT = statementGT!!.getResults()
1256+
assertEquals(1, resultsGT.size)
1257+
assertEquals(book3, resultsGT[0])
1258+
1259+
// Test GTE
1260+
val resultsGTE = statementGTE!!.getResults()
1261+
assertEquals(2, resultsGTE.size)
1262+
assertEquals(true, resultsGTE.any { it == book2 })
1263+
assertEquals(true, resultsGTE.any { it == book3 })
1264+
}
1265+
1266+
fun testStringInOperator() = Database(getNewAPIDBConfig()).databaseAutoClose { database ->
1267+
val book0 = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
1268+
val book1 = Book(name = "Kotlin Cookbook", author = "Ken Kousen", pages = 251, price = 37.72)
1269+
val book2 = Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95)
1270+
val book3 = Book(name = "Modern Java Recipes", author ="Ken Kousen", pages = 322, price = 25.78)
1271+
1272+
var statementIN: SelectStatement<Book>? = null
1273+
1274+
database {
1275+
BookTable { table ->
1276+
table INSERT listOf(book0, book1, book2, book3)
1277+
statementIN = table SELECT WHERE (author IN listOf("Dan Brown", "Unknown Author"))
1278+
}
1279+
}
1280+
1281+
val results = statementIN!!.getResults()
1282+
assertEquals(2, results.size)
1283+
assertEquals(true, results.any { it == book0 })
1284+
assertEquals(true, results.any { it == book2 })
1285+
}
1286+
1287+
fun testStringBetweenOperator() = Database(getNewAPIDBConfig()).databaseAutoClose { database ->
1288+
val book0 = Book(name = "Alice in Wonderland", author = "Lewis Carroll", pages = 200, price = 15.99)
1289+
val book1 = Book(name = "Bob's Adventures", author = "Bob Smith", pages = 300, price = 20.99)
1290+
val book2 = Book(name = "Charlie and the Chocolate Factory", author = "Roald Dahl", pages = 250, price = 18.99)
1291+
val book3 = Book(name = "David Copperfield", author = "Charles Dickens", pages = 400, price = 25.99)
1292+
1293+
var statementBETWEEN: SelectStatement<Book>? = null
1294+
1295+
database {
1296+
BookTable { table ->
1297+
table INSERT listOf(book0, book1, book2, book3)
1298+
statementBETWEEN = table SELECT WHERE (name BETWEEN ("Bob's Adventures" to "Charlie and the Chocolate Factory"))
1299+
}
1300+
}
1301+
1302+
val results = statementBETWEEN!!.getResults()
1303+
assertEquals(2, results.size)
1304+
assertEquals(true, results.any { it == book1 })
1305+
assertEquals(true, results.any { it == book2 })
1306+
}
1307+
1308+
fun testBlobComparisonOperators() = Database(getNewAPIDBConfig()).databaseAutoClose { database ->
1309+
val file0 = FileData(id = null, fileName = "file0.bin", content = byteArrayOf(0x01, 0x02), metadata = "File 0")
1310+
val file1 = FileData(id = null, fileName = "file1.bin", content = byteArrayOf(0x03, 0x04), metadata = "File 1")
1311+
val file2 = FileData(id = null, fileName = "file2.bin", content = byteArrayOf(0x05, 0x06), metadata = "File 2")
1312+
val file3 = FileData(id = null, fileName = "file3.bin", content = byteArrayOf(0x07, 0x08), metadata = "File 3")
1313+
1314+
var statementLT: SelectStatement<FileData>? = null
1315+
var statementLTE: SelectStatement<FileData>? = null
1316+
var statementGT: SelectStatement<FileData>? = null
1317+
var statementGTE: SelectStatement<FileData>? = null
1318+
1319+
database {
1320+
FileDataTable { table ->
1321+
table INSERT listOf(file0, file1, file2, file3)
1322+
statementLT = table SELECT WHERE (content LT byteArrayOf(0x03, 0x04))
1323+
statementLTE = table SELECT WHERE (content LTE byteArrayOf(0x03, 0x04))
1324+
statementGT = table SELECT WHERE (content GT byteArrayOf(0x05, 0x06))
1325+
statementGTE = table SELECT WHERE (content GTE byteArrayOf(0x05, 0x06))
1326+
}
1327+
}
1328+
1329+
// Test LT
1330+
val resultsLT = statementLT!!.getResults()
1331+
assertEquals(1, resultsLT.size)
1332+
assertEquals("file0.bin", resultsLT[0].fileName)
1333+
1334+
// Test LTE
1335+
val resultsLTE = statementLTE!!.getResults()
1336+
assertEquals(2, resultsLTE.size)
1337+
assertEquals(true, resultsLTE.any { it.fileName == "file0.bin" })
1338+
assertEquals(true, resultsLTE.any { it.fileName == "file1.bin" })
1339+
1340+
// Test GT
1341+
val resultsGT = statementGT!!.getResults()
1342+
assertEquals(1, resultsGT.size)
1343+
assertEquals("file3.bin", resultsGT[0].fileName)
1344+
1345+
// Test GTE
1346+
val resultsGTE = statementGTE!!.getResults()
1347+
assertEquals(2, resultsGTE.size)
1348+
assertEquals(true, resultsGTE.any { it.fileName == "file2.bin" })
1349+
assertEquals(true, resultsGTE.any { it.fileName == "file3.bin" })
1350+
}
1351+
1352+
fun testBlobInOperator() = Database(getNewAPIDBConfig()).databaseAutoClose { database ->
1353+
val file0 = FileData(id = null, fileName = "file0.bin", content = byteArrayOf(0x01, 0x02), metadata = "File 0")
1354+
val file1 = FileData(id = null, fileName = "file1.bin", content = byteArrayOf(0x03, 0x04), metadata = "File 1")
1355+
val file2 = FileData(id = null, fileName = "file2.bin", content = byteArrayOf(0x05, 0x06), metadata = "File 2")
1356+
val file3 = FileData(id = null, fileName = "file3.bin", content = byteArrayOf(0x07, 0x08), metadata = "File 3")
1357+
1358+
var statementIN: SelectStatement<FileData>? = null
1359+
1360+
database {
1361+
FileDataTable { table ->
1362+
table INSERT listOf(file0, file1, file2, file3)
1363+
statementIN = table SELECT WHERE (content IN listOf(
1364+
byteArrayOf(0x01, 0x02),
1365+
byteArrayOf(0x05, 0x06),
1366+
byteArrayOf(0x09, 0x0A)
1367+
))
1368+
}
1369+
}
1370+
1371+
val results = statementIN!!.getResults()
1372+
assertEquals(2, results.size)
1373+
assertEquals(true, results.any { it.fileName == "file0.bin" })
1374+
assertEquals(true, results.any { it.fileName == "file2.bin" })
1375+
}
1376+
1377+
fun testBlobBetweenOperator() = Database(getNewAPIDBConfig()).databaseAutoClose { database ->
1378+
val file0 = FileData(id = null, fileName = "file0.bin", content = byteArrayOf(0x01, 0x02), metadata = "File 0")
1379+
val file1 = FileData(id = null, fileName = "file1.bin", content = byteArrayOf(0x03, 0x04), metadata = "File 1")
1380+
val file2 = FileData(id = null, fileName = "file2.bin", content = byteArrayOf(0x05, 0x06), metadata = "File 2")
1381+
val file3 = FileData(id = null, fileName = "file3.bin", content = byteArrayOf(0x07, 0x08), metadata = "File 3")
1382+
1383+
var statementBETWEEN: SelectStatement<FileData>? = null
1384+
1385+
database {
1386+
FileDataTable { table ->
1387+
table INSERT listOf(file0, file1, file2, file3)
1388+
statementBETWEEN = table SELECT WHERE (content BETWEEN (byteArrayOf(0x03, 0x04) to byteArrayOf(0x05, 0x06)))
1389+
}
1390+
}
1391+
1392+
val results = statementBETWEEN!!.getResults()
1393+
assertEquals(2, results.size)
1394+
assertEquals(true, results.any { it.fileName == "file1.bin" })
1395+
assertEquals(true, results.any { it.fileName == "file2.bin" })
1396+
}
1397+
1398+
fun testStringComparisonWithColumns() = Database(getNewAPIDBConfig()).databaseAutoClose { database ->
1399+
val book0 = Book(name = "Same Name", author = "Same Name", pages = 200, price = 15.99)
1400+
val book1 = Book(name = "Different", author = "Another", pages = 300, price = 20.99)
1401+
1402+
var statementEQ: SelectStatement<Book>? = null
1403+
var statementNEQ: SelectStatement<Book>? = null
1404+
1405+
database {
1406+
BookTable { table ->
1407+
table INSERT listOf(book0, book1)
1408+
statementEQ = table SELECT WHERE (name EQ BookTable.author)
1409+
statementNEQ = table SELECT WHERE (name NEQ BookTable.author)
1410+
}
1411+
}
1412+
1413+
// Test column comparison EQ
1414+
val resultsEQ = statementEQ!!.getResults()
1415+
assertEquals(1, resultsEQ.size)
1416+
assertEquals(book0, resultsEQ[0])
1417+
1418+
// Test column comparison NEQ
1419+
val resultsNEQ = statementNEQ!!.getResults()
1420+
assertEquals(1, resultsNEQ.size)
1421+
assertEquals(book1, resultsNEQ[0])
1422+
}
1423+
12221424
private fun getDefaultDBConfig(): DatabaseConfiguration =
12231425
DatabaseConfiguration(
12241426
name = DATABASE_NAME,

sqllin-dsl-test/src/jvmTest/kotlin/com/ctrip/sqllin/dsl/test/JvmTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ class JvmTest {
121121
@Test
122122
fun testAlertOperationsInTransaction() = commonTest.testAlertOperationsInTransaction()
123123

124+
@Test
125+
fun testStringComparisonOperators() = commonTest.testStringComparisonOperators()
126+
127+
@Test
128+
fun testStringInOperator() = commonTest.testStringInOperator()
129+
130+
@Test
131+
fun testStringBetweenOperator() = commonTest.testStringBetweenOperator()
132+
133+
@Test
134+
fun testBlobComparisonOperators() = commonTest.testBlobComparisonOperators()
135+
136+
@Test
137+
fun testBlobInOperator() = commonTest.testBlobInOperator()
138+
139+
@Test
140+
fun testBlobBetweenOperator() = commonTest.testBlobBetweenOperator()
141+
142+
@Test
143+
fun testStringComparisonWithColumns() = commonTest.testStringComparisonWithColumns()
144+
124145
@BeforeTest
125146
fun setUp() {
126147
deleteDatabase(path, CommonBasicTest.DATABASE_NAME)

sqllin-dsl-test/src/nativeTest/kotlin/com/ctrip/sqllin/dsl/test/NativeTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ class NativeTest {
137137
@Test
138138
fun testAlertOperationsInTransaction() = commonTest.testAlertOperationsInTransaction()
139139

140+
@Test
141+
fun testStringComparisonOperators() = commonTest.testStringComparisonOperators()
142+
143+
@Test
144+
fun testStringInOperator() = commonTest.testStringInOperator()
145+
146+
@Test
147+
fun testStringBetweenOperator() = commonTest.testStringBetweenOperator()
148+
149+
@Test
150+
fun testBlobComparisonOperators() = commonTest.testBlobComparisonOperators()
151+
152+
@Test
153+
fun testBlobInOperator() = commonTest.testBlobInOperator()
154+
155+
@Test
156+
fun testBlobBetweenOperator() = commonTest.testBlobBetweenOperator()
157+
158+
@Test
159+
fun testStringComparisonWithColumns() = commonTest.testStringComparisonWithColumns()
160+
140161
@BeforeTest
141162
fun setUp() {
142163
deleteDatabase(path, CommonBasicTest.DATABASE_NAME)

0 commit comments

Comments
 (0)