gmpassos/sql_commander

Does not work with flutter web

Closed this issue · 1 comments

Hi!

I tested the example running in Chrome and it says it is impossible to create a connection.

Does the package work with flutter web?

I tested it with your own example, modifying it for mysql and changing the table names and variables.

//import 'package:sql_commander/sql_commander_postgres.dart';
import 'package:flutter/foundation.dart';
import 'package:sql_commander/sql_commander_mysql.dart';

void main() async {
// DBCommand SQLs chain:
var dbCommandSQLs = [
// Provide the parameter %SYS_USER% in the INSERT below:
SQL(
'%SYS_USER%',
'alunos',
SQLType.SELECT,
where: SQLConditionValue('idalunos', '>', 0),
returnColumns: {'idalunos': 'id'},
orderBy: '>idalunos',
limit: 1,
),
// Provide the parameter %TAB_NUMBER% in the INSERT below:
SQL(
'%TAB_NUMBER%',
'cadfun',
SQLType.SELECT,

  where: SQLConditionGroup.and([
    SQLConditionValue('f_cod', '=', '5'),
  ]),
  returnColumns: {'f_nome': null},
  // ORDER BY num DESC:
  orderBy: '>f_cod',
  // LIMIT 1:
  limit: 1,
),
// INSERT into table `order` using `%SYS_USER%` and `%TAB_NUMBER%` as parameters:
SQL(
  // The ID of this SQL for references in the command chain: `#order:1001#`
  '1',
  'notas',
  SQLType.INSERT,
  parameters: {
    'idalunos': 5,
    'idmateria': 5,
    'anoletivo': 2024,
    'nota': 'O+-',
  },
  // Variables to resolve in this SQL:
  variables: {'SYS_USER': null, 'TAB_NUMBER': null},
  returnLastID: true,
),
// Another INSERT, using the INSERT above: `#order:1001#`
SQL(
  // The ID of this SQL for references:
  '1',
  'order_history',
  SQLType.INSERT,
  parameters: {
    // The order inserted above:
    'order': '#order:1001#',
    'date': DateTime.now(),
  },
  returnLastID: true,
),

];

// A DBCommand as JSON:
var commandJSON = {
"host": 'rxavier.mysql.uhserver.com',
"port": 3306,
"user": 'rxavier',
"pass": '@7Kf51brx',
"db": 'rxavier',
"software": 'mysql',
"sqls": dbCommandSQLs.map((e) => e.toJson()).toList(),
};

// Load a DBCommand from JSON:
var dbCommand = DBCommand.fromJson(commandJSON);

// Register the PostgreSQL connection implementation:
//DBConnectionPostgres.register();
// For MySQL:
DBConnectionMySQL.register();

// Execute the SQL chain:
var ok = await dbCommand.execute();

if (kDebugMode) {
print('SQL chain execution: $ok');
}
}

Show this error: Bad state: Can't create connection for DBConnectionCredential{host: rxavier.mysql.uhserver.com, port: 3306, user: rxavier, db: rxavier}

Best reguards,

Souza, Wellington

The browser can't connect to an arbitrary port, such as a 3306 TCP port (due to browser limitations).

In the browser, you can generate the commands and send them to a server/device that can connect to a DB and execute them.