static_postgres_orm 0.0.2-dev.2 copy "static_postgres_orm: ^0.0.2-dev.2" to clipboard
static_postgres_orm: ^0.0.2-dev.2 copied to clipboard

outdated

A static postgres ORM to abstract Database tables.

example/static_postgres_orm_example.dart

import 'package:static_postgres_orm/static_postgres_orm.dart';
import 'package:dartz/dartz.dart';

class _DataFields extends GenericDataFields{

  late final IntegerField_PG _company_id;
  late final IntegerField_PG _product_id;
  late final StringField_PG _code;
  late final StringField_PG _description;
  late final BooleanField_WithDefault_PG _is_active;
  late final NumericField_WithDefault_PG _price;
  late final StringField_PG _full_description;
  late final DateField_PG _insert_date;
  late final DateTimeField_PG _update_date;
  late final TimeField_PG _insert_time;

  _DataFields() : super() {
    fields = <GenericField>[];
    _createFields();
    _addFields();
  }

  _DataFields get _backup => getBackup(this) as _DataFields;
  set _backup(_DataFields v) => setBackup(this, v);

  void _createFields() {
    _company_id = IntegerField_PG(this, 'company_id', true, true);
    _product_id = IntegerField_PG(this, 'product_id', true, true);
    _code = StringField_PG(this, 'code', false, false, 50);
    _description = StringField_PG(this, 'description', false, false, 200);
    _is_active = BooleanField_WithDefault_PG(this, 'is_active', false, false, false);
    _price = NumericField_WithDefault_PG(this, 'price', false, false, 0);
    _full_description = StringField_PG(this, 'full_description', false, false, -1);
    _insert_date = DateField_PG(this, 'insert_date', false, false);
    _update_date = DateTimeField_PG(this, 'update_date', false, false);
    _insert_time = TimeField_PG(this, 'insert_time', false, false);
  }

  GenericField getCooField(_DataFields origin, String field) {
    return origin.fields
      .firstWhere((element) => getFieldName(element) == field);
  }

  void _cloneField(_DataFields origin) {
    fields.forEach((element) {
      element.copy(getCooField(origin, getFieldName(element)));
    });
  }

  @override
  void backup() {
    _backup = _DataFields();
    _backup._cloneField(this);
  }

  @override
  void restore() {
    if (assigned) {
      _cloneField(_backup);
      finalize();
    }
  }

  void _addFields() {
    fields.add(_company_id);
    fields.add(_product_id);
    fields.add(_code);
    fields.add(_description);
    fields.add(_is_active);
    fields.add(_price);
    fields.add(_full_description);
    fields.add(_insert_date);
    fields.add(_update_date);
    fields.add(_insert_time);
  }
}

class Public_Product_ORM extends PostgressORM{
  static final Event _addRecord = Event();
  _DataFields get _dataFields => getRecords(this)[rowIndex] as _DataFields;


  IntegerField_PG get company_id => _dataFields._company_id;
  IntegerField_PG get product_id => _dataFields._product_id;
  StringField_PG get code => _dataFields._code;
  StringField_PG get description => _dataFields._description;
  BooleanField_WithDefault_PG get is_active => _dataFields._is_active;
  NumericField_WithDefault_PG get price => _dataFields._price;
  StringField_PG get full_description => _dataFields._full_description;
  DateField_PG get insert_date => _dataFields._insert_date;
  DateTimeField_PG get update_date => _dataFields._update_date;
  TimeField_PG get insert_time => _dataFields._insert_time;

  Public_Product_ORM(Postgres_SqlConnection sqlConnection)
      : super(sqlConnection, 'public', 'product', _addRecord) {
    _addRecord.action = _executeAddRecord;
    getRecords(this).add(_DataFields());
  }

  void _executeAddRecord() {
    getRecords(this).add(_DataFields());
  }

  Future<Either<ErrorSqlResult, SelectSuccesSqlResult>> materialize(
      int company_id, int product_id) async {
    return getMaterialize(
        this,
        (<GenericField>[])
          ..add(IntegerField_PG.clone(_dataFields._company_id)
            ..setValue(company_id))
          ..add(IntegerField_PG.clone(_dataFields._product_id)
            ..setValue(product_id)));
  }

  Future<Either<ErrorSqlResult, ExecuteSuccesSqlResult>> deleteRecord(
    int company_id, int product_id) async{
    return getDeleteRecord(
         this,
         (<GenericField>[])
          ..add(IntegerField_PG.clone(_dataFields._company_id)
            ..setValue(company_id))
          ..add(IntegerField_PG.clone(_dataFields._product_id)
            ..setValue(product_id)));
  }
}
0
likes
0
points
158
downloads

Publisher

unverified uploader

Weekly Downloads

A static postgres ORM to abstract Database tables.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

buffer, collection, crypto, dartz, data_db, intl

More

Packages that depend on static_postgres_orm