Skip to content

Commit

Permalink
teste teste_insert_get_id
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaque Neves committed Jan 24, 2024
1 parent 9e3a25d commit 56d1023
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
1 change: 0 additions & 1 deletion example/bin/postgre_v3_example2.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:io';

import 'package:eloquent/eloquent.dart';

void main(List<String> args) async {
Expand Down
86 changes: 86 additions & 0 deletions example/bin/teste_insert_get_id.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:io';
import 'package:eloquent/eloquent.dart';

Future<Connection> 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<String> 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');
}

0 comments on commit 56d1023

Please sign in to comment.