Skip to content

Commit 56d1023

Browse files
author
Isaque Neves
committed
teste teste_insert_get_id
1 parent 9e3a25d commit 56d1023

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

example/bin/postgre_v3_example2.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:io';
2-
32
import 'package:eloquent/eloquent.dart';
43

54
void main(List<String> args) async {

example/bin/teste_insert_get_id.dart

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import 'dart:io';
2+
import 'package:eloquent/eloquent.dart';
3+
4+
Future<Connection> getConn() async {
5+
final manager = Manager();
6+
manager.addConnection({
7+
'driver': 'pgsql',
8+
'driver_implementation': 'postgres_v3',
9+
'host': 'localhost',
10+
'port': '5435',
11+
'database': 'cracha',
12+
'username': 'sisadmin',
13+
'password': 's1sadm1n',
14+
'charset': 'utf8',
15+
'prefix': '',
16+
'schema': ['public'],
17+
//'sslmode' : 'require',
18+
// 'pool': true,
19+
// 'poolsize': 50,
20+
});
21+
manager.setAsGlobal();
22+
final db = await manager.connection();
23+
return db;
24+
}
25+
26+
var data = {
27+
'numero': -1,
28+
'nome': '123',
29+
'sexo': '',
30+
'logradouro': '123',
31+
'rua': '123',
32+
'numero_casa': 123,
33+
'complemento': '',
34+
'cidade': 'cidade',
35+
'bairro': 'bairro',
36+
'estado': 'RJ',
37+
'pais': 'pais',
38+
'cep': '123',
39+
'ddd': 'ddd',
40+
'telefone1': 'telefone1',
41+
'telefone2': 'telefone2',
42+
'fax': 'fax',
43+
'celular': 'celular',
44+
'email': 'email',
45+
'observacao': 'observacao',
46+
'dtnascimento': DateTime.now(),
47+
'dtcadastro': DateTime.now(),
48+
'matricula': '123',
49+
'dtadesao': DateTime.now(),
50+
'campo1': 'campo1',
51+
'campo2': 'campo2',
52+
'campo3': 'campo3',
53+
'campo4': 'campo4',
54+
'campo5': '13128250731',
55+
'campo6': 'campo6',
56+
'campo7': 'campo7',
57+
'campo8': 'campo8',
58+
'campo9': 'campo9',
59+
'campo10': 'campo10',
60+
'campo11': 'campo11',
61+
'campo12': 'campo12',
62+
};
63+
void main(List<String> args) async {
64+
final db = await getConn();
65+
66+
final id = await db.transaction((ctx) async {
67+
final lastCod = await ctx
68+
.table('public.clientes')
69+
.selectRaw('MAX(numero) AS cod')
70+
.first();
71+
72+
final nextCod = lastCod == null || lastCod['cod'] == null
73+
? 1
74+
: (lastCod['cod'] as int) + 1;
75+
76+
final query = ctx.table('public.clientes');
77+
78+
data['numero'] = nextCod;
79+
80+
final numero = await query.insertGetId(data, 'numero');
81+
82+
return numero;
83+
});
84+
85+
print('insertGetId: $id');
86+
}

0 commit comments

Comments
 (0)