From 56d1023aa2a52ac4b5a498e01887d937c98a0adb Mon Sep 17 00:00:00 2001 From: Isaque Neves Date: Wed, 24 Jan 2024 12:28:02 -0300 Subject: [PATCH] teste teste_insert_get_id --- example/bin/postgre_v3_example2.dart | 1 - example/bin/teste_insert_get_id.dart | 86 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 example/bin/teste_insert_get_id.dart diff --git a/example/bin/postgre_v3_example2.dart b/example/bin/postgre_v3_example2.dart index 1014410..58c4c78 100644 --- a/example/bin/postgre_v3_example2.dart +++ b/example/bin/postgre_v3_example2.dart @@ -1,5 +1,4 @@ import 'dart:io'; - import 'package:eloquent/eloquent.dart'; void main(List args) async { diff --git a/example/bin/teste_insert_get_id.dart b/example/bin/teste_insert_get_id.dart new file mode 100644 index 0000000..52fa98b --- /dev/null +++ b/example/bin/teste_insert_get_id.dart @@ -0,0 +1,86 @@ +import 'dart:io'; +import 'package:eloquent/eloquent.dart'; + +Future getConn() async { + final manager = Manager(); + manager.addConnection({ + 'driver': 'pgsql', + 'driver_implementation': 'postgres_v3', + 'host': 'localhost', + 'port': '5435', + 'database': 'cracha', + 'username': 'sisadmin', + 'password': 's1sadm1n', + 'charset': 'utf8', + 'prefix': '', + 'schema': ['public'], + //'sslmode' : 'require', + // 'pool': true, + // 'poolsize': 50, + }); + manager.setAsGlobal(); + final db = await manager.connection(); + return db; +} + +var data = { + 'numero': -1, + 'nome': '123', + 'sexo': '', + 'logradouro': '123', + 'rua': '123', + 'numero_casa': 123, + 'complemento': '', + 'cidade': 'cidade', + 'bairro': 'bairro', + 'estado': 'RJ', + 'pais': 'pais', + 'cep': '123', + 'ddd': 'ddd', + 'telefone1': 'telefone1', + 'telefone2': 'telefone2', + 'fax': 'fax', + 'celular': 'celular', + 'email': 'email', + 'observacao': 'observacao', + 'dtnascimento': DateTime.now(), + 'dtcadastro': DateTime.now(), + 'matricula': '123', + 'dtadesao': DateTime.now(), + 'campo1': 'campo1', + 'campo2': 'campo2', + 'campo3': 'campo3', + 'campo4': 'campo4', + 'campo5': '13128250731', + 'campo6': 'campo6', + 'campo7': 'campo7', + 'campo8': 'campo8', + 'campo9': 'campo9', + 'campo10': 'campo10', + 'campo11': 'campo11', + 'campo12': 'campo12', +}; +void main(List args) async { + final db = await getConn(); + + final id = await db.transaction((ctx) async { + final lastCod = await ctx + .table('public.clientes') + .selectRaw('MAX(numero) AS cod') + .first(); + + final nextCod = lastCod == null || lastCod['cod'] == null + ? 1 + : (lastCod['cod'] as int) + 1; + + final query = ctx.table('public.clientes'); + + data['numero'] = nextCod; + + final numero = await query.insertGetId(data, 'numero'); + + return numero; + }); + + print('insertGetId: $id'); +}