패키지, 저장소 및 앱 카테고리 검색

패키지 상세v5.5.0

fluent_query_builder

PostgreSQL 데이터베이스에서 SQL 쿼리를 유창하게 실행할 수 있는 Dart 라이브러리로, 실행이 매우 간단합니다.

AndroidiOSmacOSWindowsLinux
30일 다운로드
237
좋아요
51
Pub 점수
140

패키지 설치

flutter pub add fluent_query_builder

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

Fluent Query Builder는 PostgreSQL 데이터베이스에 대한 SQL 쿼리 실행을 유연하고 체인 가능한 API를 사용하여 간소화하도록 설계된 Dart 라이브러리입니다. 개발자들이 원시 SQL 문자열을 수동으로 구성하지 않고도 깔끔하고 가독성 있으며 유지보수가 쉬운 데이터베이스 상호작용을 작성할 수 있게 해줍니다. 이 라이브러리는 여러 플랫폼에서 작동하는 Flutter 앱과 원활하게 통합되어 강력한 데이터베이스 작업이 필요한 프로젝트에 이상적입니다. 최소한의 설정과 직관적인 문법을 통해 SQL 인젝션 위험을 줄이고 쿼리 관리를 간소화합니다.

사용 사례

  • Flutter 앱에서 동적 데이터베이스 쿼리 만들기
  • PostgreSQL로 CRUD 작업 간소화하기
  • SQL 문 생성 시 반복 코드 줄이기
  • 크로스 플랫폼 모바일 및 데스크톱 앱과 PostgreSQL 통합
  • 성능을 희생하지 않고 안전하고 가독성이 높은 SQL 코드를 작성하기

주요 기능

  • 유창하고 체인 가능한 쿼리 구문
  • SQL 인젝션에 대한 내장 보호
  • PostgreSQL 전용 기능 지원
  • Flutter 프로젝트와의 간편한 통합
  • 크로스 플랫폼 호환성 (Android, iOS, macOS, Windows, Linux)

적합한 대상

  • Flutter 개발자가 PostgreSQL과 함께 작업 중
  • 코드 가독성과 보안을 우선시하는 팀
  • 자주 데이터베이스 상호작용을 필요로 하는 프로젝트
  • 원시 SQL 또는 ORM 프레임워크의 대안을 찾는 개발자

고려 사항

  • 백엔드 데이터베이스로 PostgreSQL가 필요합니다
  • SQL 기반 작업에 제한됨
  • 내장된 마이그레이션 또는 스키마 관리 없음
  • 중간에서 복잡한 쿼리 요구 사항을 가진 애플리케이션에 가장 적합
  • 외부 유지보수에 따라 달라지는 GitHub 저장소

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

fluent_query_builder

Build Status

CI

A dart library that allows you to execute SQL queries in the PostgreSQL and MySql database in a fluent way, is very easy to execute. This library implements POOL of connections. This lib implements the vast majority of SQL statements and clauses

Soon it will also support ORM without reflection and without code generation in a simple and consistent way.

Usage

A simple query builder usage example:

import 'package:fluent_query_builder/fluent_query_builder.dart';

void main() {
  //PostgreSQL connection information
  final pgsqlCom = DBConnectionInfo(
    host: 'localhost',
    database: 'banco_teste',
    driver: ConnectionDriver.pgsql,
    port: 5432,
    username: 'user',
    password: 'pass',
    charset: 'utf8',
    schemes: ['public'],
  );

  //MySQL connection information
  final mysqlCom = DBConnectionInfo(
    host: 'localhost',
    database: 'banco_teste',
    driver: ConnectionDriver.mysql,
    port: 3306,
     username: 'user',
    password: 'pass',
    charset: 'utf8',
  );

  DbLayer().connect(mysqlCom).then((db) {
    //mysql insert
    db
        .insert()
        .into('pessoas')
        .set('nome', 'Isaque Neves Sant\'Ana')
        .set('telefone', '(22) 2771-6265')
        .exec()
        .then((result) => print('mysql insert $result'));

    //mysql update
    db
        .update()
        .table('pessoas')
        .set('nome', 'João')
        .where('id=?', 13)
        .exec()
        .then((result) => print('mysql update $result'));

    //mysql update with setAll
    db
        .update()
        .whereSafe('id', '=', 13)
        .table('pessoas')
        .setAll({
          'nome': 'Jon Doe',
          'telefone': '171171171',
        })
        .exec()
        .then((result) => print('mysql update with setAll $result'));

    //mysql delete
    db.delete().from('pessoas')
    .where('id=?', 14)
    .exec()
    .then((result) => print('mysql delete $result'));

    //mysql select
    db
        .select()
        //.fields(['login', 'idSistema', 's.sigla'])
        //.fieldRaw('SELECT COUNT(*)')
        .from('pessoas')
        .whereSafe('nome', 'like', '%Sant\'Ana%')
        //.limit(1)
        .firstAsMap()
        .then((result) => print('mysql select $result'));

    //mysql raw query SELECT * FROM `pessoas` or SELECT COUNT(*) FROM pessoas
    db  
        .raw("SELECT * FROM `pessoas`")
        .firstAsMap()
        .then((result) => print('mysql raw $result'));
        
    //mysql count records 
    db
        .select()
        .from('pessoas')
        .orWhereSafe('nome', 'like', '%Sant\'Ana%')
        .orWhereSafe('id', '<', 20)
        .count()
        .then((result) => print('mysql count $result'));
  });

  DbLayer().connect(pgsqlCom).then((db) {
    //pgsql insert
    db
        .insert()
        .into('usuarios')
        .set('username', 'isaque')
        .set('password', '123456')
        .exec()
        .then((result) => print('pgsql insert $result'));

  //pgsql insertGetAll
  db
      .insertGetAll()
      .into('usuarios')
      .set('username', 'isaque')
      .set('password', '123456')
      .exec()
      .then((result) => print('pgsql insertGetAll $result'));

 //pgsql insertGetId
  db
      .insertGetId()
      .into('usuarios')
      .set('username', 'isaque')
      .set('password', '123456')
      .exec()
      .then((result) => print('pgsql insertGetId $result'));

    //pgsql count records
         db
        .select()
        .from('pessoas')       
        .count()
        .then((result) => print('pgsql count $result'));

     //Complex selection With whereGroup, whereSafe, where whereRaw
      await db.raw('DROP TABLE IF EXISTS notificacoes').exec();
      await db.raw('''CREATE TABLE notificacoes (
                      "id" serial NOT NULL ,
                      "mensagem" text COLLATE "pg_catalog"."default",
                      "dataCriado" timestamp(0) DEFAULT now(),
                      "link" text COLLATE "pg_catalog"."default" DEFAULT NULL::character varying,
                      "idPessoa" int4,
                      "idSistema" int4,
                      "userAgent" text COLLATE "pg_catalog"."default" DEFAULT NULL::character varying,
                      "idOrganograma" int4,
                      "isLido" bool,
                      "toAll" bool,
                      "icon" text COLLATE "pg_catalog"."default",
                      CONSTRAINT "notificacoes_pkey" PRIMARY KEY ("id")
                    )''').exec();
      await db.insert().into('notificacoes').setAll({
        'mensagem': 'Teste',
        'link': 'https://pub.dev',
        'dataCriado': '2021-05-04 17:53:55',
        'idPessoa': 2,
        'idSistema': 1,
        'userAgent': 'Google',
        'idOrganograma': 19,
        'isLido': false,
        'toAll': true,
        'icon': ''
      }).exec();

      final query = db.select().fromRaw('notificacoes');
      query.where('"dataCriado"::TIMESTAMP  > \'?\'::TIMESTAMP ', '2021-05-04 17:53:55');
      query.whereGroup((q) {
        q.where('"idPessoa"=?', 2, 'or');
        q.where('"idOrganograma"=?', 19, 'or');
        q.where('"toAll"=?', "'t'", 'or');
        q.whereSafe('"toAll"', '=', 'true');
        q.whereRaw('"toAll"= true');
        return q;
      });
      query.orWhereGroup((q) {
        q.whereSafe('"toAll"', '=', 'true');
        query.orWhereGroup((q) {
          q.whereSafe('"toAll"', '=', 'true');
          return q;
        });
        return q;
      });
      query.order('dataCriado', dir: SortOrder.DESC);
      final listMap = await query.limit(1).getAsMap();

      print(listMap)
       /*Resullt
       [
        {
          'id': 1,
          'mensagem': 'Teste',
          'dataCriado': DateTime.tryParse('2021-05-04 17:53:55.000Z'),
          'link': 'https://pub.dev',
          'idPessoa': 2,
          'idSistema': 1,
          'userAgent': 'Google',
          'idOrganograma': 19,
          'isLido': false,
          'toAll': true,
          'icon': ''
        }
      ]*/
   

        
  });
  
  //pgsql transaction example
  var db = await DbLayer().connect(pgsqlCom);
  await db.transaction((ctx) async {
    
    var result = await ctx.insert().into('usuarios')
    .set('username', 'isaque')
    .set('password', '123456')
    .exec();
    //pgsql use of Where Group => WHERE (nome ilike '%5%' or cpf ilike '%5%') AND id > 0
    await ctx
    .select()
    .from('pessoas')     
    .orWhereGroup((query) {
      return query
      .orWhereSafe('nome', 'ilike', '%5%')
      .orWhereSafe('cpf', 'ilike', '%5%');
    })
    .whereSafe('id', '>', 0)
    .getAsMap();

    print('pgsql transaction $result');
  });

  //pgsql getRelationFromMaps example
   var data = await db
      .select()
      .from('pessoas') 
      .orWhereGroup((query) {
        return query.orWhereSafe('nome', 'ilike', '%5%').orWhereSafe('cpf', 'ilike', '%5%');
      })
      .whereSafe('id', '>', 0)
      .getAsMap();

  data = await db.getRelationFromMaps(data, 'usuarios', 'idPessoa', 'id');

  print('pgsql select \r\n ${jsonEncode(data)}');
  /* pgsql select 
    [
      {
        "id":2,
        "nome":"Isaque Neves Sant'Ana",
        "telefone":"(22) 99701-5305",
        "cpf":"54654",
        "usuarios":[
            {
              "id":16,
              "username":"isaque",
              "password":"123456",
              "ativo":null,
              "idPessoa":2
            }
        ]
      },
      {
        "id":1,
        "nome":"João da Silva 5",
        "telefone":"27772339",
        "cpf":"1111",
        "usuarios":[

        ]
      }
    ]  */
}

A simple beta pre ORM usage example:

import 'package:fluent_query_builder/fluent_query_builder.dart';

class Usuario implements FluentModelBase {
  Usuario({this.username});

  Usuario.fromMap(Map<String, dynamic> map) {
    id = map['id'] as int;
    username = map['username'] as String;
    password = map['password'] as String;
    ativo = map['ativo'] as bool;
    idPerfil = map['idPerfil'] as int;
  }

  int id;
  String username;
  String password;
  bool ativo;
  int idPerfil;

  @override
  Map<String, dynamic> toMap() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    if (id != null) {
      data['id'] = id;
    }
    data['username'] = username;
    data['password'] = password;
    data['ativo'] = ativo;
    data['idPerfil'] = idPerfil;
    return data;
  }

  @override
  String get tableName => 'usuarios';
}

void main() {
  //connection settings
  final com = DBConnectionInfo(
    host: '192.168.133.13',
    database: 'sistemas',
    port: 5432,
    username: 'user',
    password: 'pass',
    charset: 'utf8',
    schemes: ['riodasostrasapp'],
  );
  
  //connect on database and set factories for construct instance of model
   DbLayer(factories: [
    {Usuario: (x) => Usuario.fromMap(x)}
  ]).connect(com).then((db) {
    //insert Usuario
    db.putSingle<Usuario>(Usuario(username: 'jon.doe', password: '123456'));
    //update Usuario
    db.update().where('id=?', 20).updateSingle<Usuario>(Usuario(username: 'jon.doe', password: '987'));
    //select Usuario
    db.select().from(Usuario().tableName).where('id>?', 2).fetchAll<Usuario>().then((result) {
      print(result);
    });
    //delete Usuario
    db.delete().deleteSingle<Usuario>(Usuario(id: 20, username: 'jon.doe', password: '123456'));
  });
}

카테고리

의존성

의존성 목록 5 개

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.