@@ -6,6 +6,7 @@ import java.sql._
6
6
import ConnectionInfo .ConnectionMap
7
7
import jp .osscons .opensourcecobol .libcobj .data .CobolDataStorage
8
8
9
+ import java .nio .ByteBuffer
9
10
import scala .collection .immutable .Queue
10
11
11
12
object Common {
@@ -204,43 +205,27 @@ object Common {
204
205
def setLibErrorStatus (errorCode : SqlCode ): Operation [Int ] =
205
206
OCDB_PGSetLibErrorStatus (errorCode)
206
207
207
- def setResultStatus (id : Int ): Operation [Boolean ] = (for {
208
- keyAndValue <- operationCPure(lookUpConnList(id))
209
-
210
- _ <- whenExecuteAndExit(keyAndValue.isEmpty, operationPure(false ))
211
-
212
- kv <- operationCPure(operationPure(keyAndValue.getOrElse((" " , ConnectionInfo .defaultValue))))
213
- key <- operationCPure(operationPure(kv._1))
214
- pConn <- operationCPure(operationPure(kv._2))
215
-
216
- // [remark] temporary implementation
217
- // _ <- whenExecuteAndExit(pConn.result == OCDB_RES_DEFAULT_ADDRESS && pConn.result <= RESULT_FLAGBASE,
218
- _ <- whenExecuteAndExit(pConn.result.isLeft,
219
- operationPure(false ))
220
-
221
- // [remark] temporary implementation
222
- /* returnValue <- if(pConn.result == RESULT_FLAG1_PGSQL_DUMMYOPEN) {
223
- operationCPure[Int, Int](operationPure(RESULT_SUCCESS))
224
- } else {
225
- operationCPure[Int, Int](OCDB_PGSetResultStatus(pConn.result))
226
- }*/
227
- returnValue <- operationCPure(OCDB_PGSetResultStatus (pConn.result))
228
- } yield returnValue).eval
208
+ def setResultStatus (id : Int ): Operation [Boolean ] =
209
+ lookUpConnList(id).flatMap(_ match {
210
+ case None => operationPure(false )
211
+ case Some ((_, pConn)) => OCDB_PGSetResultStatus (pConn.result)
212
+ })
229
213
230
214
/**
231
- * [remark] temporary implementation
232
- * Equivalent to OCDB_PGSetResultStatus in dblib/ocpgsql.c
215
+ * Implementation of OCDB_PGSetResultStatus in dblib/ocpgsql.c (Open-COBOL-ESQL)
216
+ * Regardless of the pretious SQL execution, sqlErrorCode is zero.
217
+ * If the pretious SQL execution fails, sqlState equals to PSQLState.
218
+ * (https://github.com/pgjdbc/pgjdbc/blob/8be516d47ece60b7aeba5a9474b5cac1d538a04a/pgjdbc/src/main/java/org/postgresql/util/PSQLState.java)
233
219
* @param connAddr
234
- * @return
220
+ * @return true if and only if the previous SQL execution is successful.
235
221
*/
236
222
def OCDB_PGSetResultStatus (result : ExecResult ): Operation [Boolean ] = {
237
223
val (sqlCode, sqlState) = result match {
238
224
case Right (_) => (OCPG_NO_ERROR , " 00000" )
239
- case Left (e) => (e.getErrorCode, e.getSQLState)
225
+ case Left (e) => (e.getErrorCode, Option ( e.getSQLState).getOrElse( " 00000 " ) )
240
226
}
241
227
for {
242
- state <- getState
243
- _ <- setState({
228
+ _ <- updateState(state => {
244
229
val sqlCA = state.sqlCA
245
230
val newSqlCA = sqlCA.setCode(sqlCode).setState(sqlState.getBytes)
246
231
state.setSqlCA(newSqlCA)
@@ -365,22 +350,42 @@ object Common {
365
350
case OCDB_TYPE_JAPANESE_VARYING => createCobolDataJapaneseVarying(sv, addr, index, resultData)
366
351
}
367
352
}
368
-
369
- private def createCobolDataUnsignedNumber (sv : SQLVar , addr : CobolDataStorage , index : Int , data : scala.Array [Byte ]): Unit = {
353
+
354
+ // [TODO] improve the algorithm
355
+ private def createCobolDataUnsignedNumber (sv : SQLVar , addr : CobolDataStorage , index : Int , str : scala.Array [Byte ]): Unit = {
370
356
val finalBuf : scala.Array [Byte ] = new scala.Array (sv.length)
371
- val isNegative = data (0 ) == '-' .toByte
357
+ val isNegative = str (0 ) == '-' .toByte
372
358
val valueFirstIndex = if (isNegative) {1 } else {0 }
373
-
374
359
val indexOfDecimalPoint = {
375
- val index = data .indexOf('.' )
376
- if (index < 0 ) { data .length } else { index }
360
+ val index = str .indexOf('.' )
361
+ if (index < 0 ) { str .length } else { index }
377
362
}
363
+
378
364
for (i <- 0 until finalBuf.length) {
379
365
finalBuf(i) = '0' .toByte
380
366
}
381
- for (i <- valueFirstIndex until indexOfDecimalPoint) {
382
- finalBuf(i + finalBuf.length - (indexOfDecimalPoint + sv.power)) = data(i)
367
+
368
+ if (sv.power >= 0 ) {
369
+ for (i <- valueFirstIndex until indexOfDecimalPoint) {
370
+ finalBuf(i + finalBuf.length - (indexOfDecimalPoint + sv.power)) = str(i)
371
+ }
372
+ } else {
373
+ var i = sv.length + sv.power - 1
374
+ var j = indexOfDecimalPoint - 1
375
+ while (i >= 0 && j >= valueFirstIndex) {
376
+ finalBuf(i) = str(j)
377
+ i -= 1
378
+ j -= 1
379
+ }
380
+ i = sv.length + sv.power
381
+ j = indexOfDecimalPoint + 1
382
+ while (i < sv.length && j < str.length) {
383
+ finalBuf(i) = str(j)
384
+ i += 1
385
+ j += 1
386
+ }
383
387
}
388
+
384
389
for (i <- 0 until finalBuf.length) {
385
390
addr.setByte(i, finalBuf(i))
386
391
}
@@ -390,17 +395,36 @@ object Common {
390
395
val finalBuf : scala.Array [Byte ] = new scala.Array (sv.length)
391
396
val isNegative = str(0 ) == '-' .toByte
392
397
val valueFirstIndex = if (isNegative) {1 } else {0 }
393
-
394
398
val indexOfDecimalPoint = {
395
399
val index = str.indexOf('.' )
396
400
if (index < 0 ) { str.length } else { index }
397
401
}
402
+
398
403
for (i <- 0 until finalBuf.length) {
399
404
finalBuf(i) = '0' .toByte
400
405
}
401
- for (i <- valueFirstIndex until indexOfDecimalPoint) {
402
- finalBuf(i + finalBuf.length - (indexOfDecimalPoint + sv.power)) = str(i)
406
+
407
+ if (sv.power >= 0 ) {
408
+ for (i <- valueFirstIndex until indexOfDecimalPoint) {
409
+ finalBuf(i + finalBuf.length - (indexOfDecimalPoint + sv.power)) = str(i)
410
+ }
411
+ } else {
412
+ var i = sv.length + sv.power - 1
413
+ var j = indexOfDecimalPoint - 1
414
+ while (i >= 0 && j >= valueFirstIndex) {
415
+ finalBuf(i) = str(j)
416
+ i -= 1
417
+ j -= 1
418
+ }
419
+ i = sv.length + sv.power
420
+ j = indexOfDecimalPoint + 1
421
+ while (i < sv.length && j < str.length) {
422
+ finalBuf(i) = str(j)
423
+ i += 1
424
+ j += 1
425
+ }
403
426
}
427
+
404
428
if (isNegative) {
405
429
val finalByte = finalBuf(finalBuf.length - 1 )
406
430
finalBuf(finalBuf.length - 1 ) = (finalByte + 0x40 ).toByte
@@ -411,15 +435,58 @@ object Common {
411
435
}
412
436
413
437
private def createCobolDataSignedNumberLs (sv : SQLVar , addr : CobolDataStorage , i : Int , str : scala.Array [Byte ]): Unit = {
414
- // TODO Implement
438
+ val finalBuf : scala.Array [Byte ] = new scala.Array (sv.length)
439
+ val isNegative = str(0 ) == '-' .toByte
440
+ val valueFirstIndex = if (isNegative) {1 } else {0 }
441
+ val indexOfDecimalPoint = {
442
+ val index = str.indexOf('.' )
443
+ if (index < 0 ) { str.length } else { index }
444
+ }
445
+
446
+ for (i <- 0 until finalBuf.length) {
447
+ finalBuf(i) = '0' .toByte
448
+ }
449
+
450
+ if (sv.power >= 0 ) {
451
+ for (i <- valueFirstIndex until indexOfDecimalPoint) {
452
+ finalBuf(i + finalBuf.length - (indexOfDecimalPoint + sv.power)) = str(i)
453
+ }
454
+ } else {
455
+ var i = sv.length + sv.power
456
+ var j = indexOfDecimalPoint - 1
457
+ while (i >= 1 && j >= valueFirstIndex) {
458
+ finalBuf(i) = str(j)
459
+ i -= 1
460
+ j -= 1
461
+ }
462
+ i = sv.length + sv.power + 1
463
+ j = indexOfDecimalPoint + 1
464
+ while (i < sv.length && j < str.length) {
465
+ finalBuf(i) = str(j)
466
+ i += 1
467
+ j += 1
468
+ }
469
+ }
470
+
471
+ finalBuf(0 ) = (if (isNegative) {'-' } else {'+' }).toByte
472
+ for (i <- 0 until finalBuf.length) {
473
+ addr.setByte(i, finalBuf(i))
474
+ }
415
475
}
416
476
417
477
private def createCobolDataUnsignedNumberPd (sv : SQLVar , addr : CobolDataStorage , i : Int , str : scala.Array [Byte ]): Unit = {
418
478
// TODO Implement
419
479
}
420
480
421
481
private def createCobolDataSignedNumberPd (sv : SQLVar , addr : CobolDataStorage , i : Int , str : scala.Array [Byte ]): Unit = {
422
- // TODO Implement
482
+ println(" createCobolDataSignedNumberPd 1" )
483
+ val n = Integer .parseInt(new String (str))
484
+ println(s " n: ${n}" )
485
+ val bytes = new scala.Array [Byte ](4 )
486
+ ByteBuffer .wrap(bytes).putInt(n)
487
+ for (i <- 0 until 4 ) {
488
+ addr.setByte(i, bytes(i))
489
+ }
423
490
}
424
491
425
492
private def createCobolDataAlphanumeric (sv : SQLVar , addr : CobolDataStorage , i : Int , str : scala.Array [Byte ]): Unit = {
@@ -432,7 +499,12 @@ object Common {
432
499
}
433
500
434
501
private def createCobolDataJapanese (sv : SQLVar , addr : CobolDataStorage , i : Int , str : scala.Array [Byte ]): Unit = {
435
- // TODO Implement
502
+ if (str.length >= sv.length * 2 ) {
503
+ addr.memcpy(str, sv.length * 2 )
504
+ } else {
505
+ addr.memset(' ' .toByte, sv.length * 2 )
506
+ addr.memcpy(str, str.length)
507
+ }
436
508
}
437
509
438
510
private def createCobolDataAlphanumericVarying (sv : SQLVar , addr : CobolDataStorage , i : Int , str : scala.Array [Byte ]): Unit = {
0 commit comments