parseDumpResult static method

  1. @visibleForTesting
MariaDbDatabase parseDumpResult({
  1. required String result,
  2. required String database,
})

Implementation

@visibleForTesting
static MariaDbDatabase parseDumpResult({
  required String result,
  required String database,
}) {
  final doc = XmlDocument.parse(result);

  // There can only be a single database node, because we explicitly provide
  // the name of the database
  final dbNode = doc.findAllElements("database").singleOrNull;

  if (null == dbNode)
    throw FormatException(
      "The dump result contains no 'database' element",
      result,
    );

  final tableDataNodes = dbNode.findElements("table_data");

  final parsedDb = MariaDbDatabase(name: database);

  // parse each table node
  for (final tblNode in tableDataNodes) {
    final name = tblNode.getAttribute("name");
    if (null == name)
      throw FormatException("Each table must have a name attribute", tblNode);

    final parsedTable = MariaDbTable(name: name);
    final rowNodes = tblNode.findElements("row");

    // If a field can be null, then it has an attribute with this key with
    // the value "true" if it is null.
    const nullAttributeKey = "xsi:nil";

    for (final r in rowNodes) {
      final fields = r.findElements("field");

      for (final f in fields) {
        final colName = f.getAttribute("name");
        if (null == colName)
          throw FormatException("Each field must have a column name", f);

        final isNullAttr = f.getAttribute(nullAttributeKey);
        if (null != isNullAttr &&
            bool.parse(isNullAttr, caseSensitive: false)) {
          parsedTable
              .putIfAbsent(colName, () => MariaDbColumn(name: colName))
              .add(null);
        } else {
          parsedTable
              .putIfAbsent(colName, () => MariaDbColumn(name: colName))
              .add(f.innerText);
        }
      }
    }

    parsedDb[name] = parsedTable;
  }

  return parsedDb;
}