Schema constructor

Schema(
  1. String sql
)

Implementation

Schema(String sql)
    : sql = sql.trim(),
      table = RegExp(tablePattern).firstMatch(sql)!.group(1),
      columns = RegExp(columnsPattern).allMatches(sql).map((match) {
        final defention = match.group(0)!;

        return Column(
          name: match.group(1),
          isNullable: defention.contains('NOT NULL'),
          isUnique: defention.contains('UNIQUE'),
          defaultValue: defention.contains('DEFAULT')
              ? defention
                  .substring(defention.indexOf('DEFAULT'))
                  .replaceAll('DEFAULT', '')
                  .replaceAll(',', '')
                  .trim()
              : null,
        );
      }).toList(),
      primaryKey = RegExp(primaryKeysPattern)
          .firstMatch(sql)
          ?.groups([1, 2]).firstWhere((item) => item != null),
      foreignKeys = RegExp(foreignKeyPattern).allMatches(sql).map((match) {
        return ForeignKey(
          parent: match.group(1),
          reference: Reference(
            table: match.group(2),
            primaryKey: match.group(3),
          ),
        );
      }).toList();