getResultCode method

  1. @override
int? getResultCode()

Get the (extended if available) result code.

This might involve parsing the sqlite native message to extract the code See https://www.sqlite.org/rescode.html for the list of result code.

iOS returns normal code while Android/ffi returns extended code for now The application should handle both.

Implementation

@override
int? getResultCode() => _resultCode ??= () {
      final message = _message!.toLowerCase();
      int? findCode(String patternPrefix) {
        final index = message.indexOf(patternPrefix);
        if (index != -1) {
          try {
            // Split at first space
            var code = message
                .substring(index + patternPrefix.length)
                .trim()
                .split(' ')[0];
            // Find ending parenthesis if any
            final endIndex = code.indexOf(')');
            if (endIndex != -1) {
              code = code.substring(0, endIndex);
            }

            final resultCode = int.tryParse(code);
            if (resultCode != null) {
              return resultCode;
            }
          } catch (_) {}
        }
        return null;
      }

      var code = findCode('(sqlite code ');
      if (code != null) {
        return code;
      }
      code = findCode('(code ');
      if (code != null) {
        return code;
      }
      // ios
      code = findCode('code=');
      if (code != null) {
        return code;
      }
      return null;
    }();