Skip to content

Commit

Permalink
comments on postgresql and mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlondono committed Nov 29, 2024
1 parent ffcb0eb commit cdca975
Show file tree
Hide file tree
Showing 27 changed files with 277 additions and 307 deletions.
11 changes: 0 additions & 11 deletions .idea/runConfigurations/melos_run_format.xml

This file was deleted.

5 changes: 0 additions & 5 deletions packages/athena_migrate/lib/athena_migrate.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/// Support for doing something awesome.
///
/// More dartdocs go here.
library athena_migrate;

export 'src/athena_migrate_base.dart';

// TODO: Export any libraries intended for clients of this package.
4 changes: 2 additions & 2 deletions packages/athena_migrate/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ homepage: https://docs.athena-sql.dev/migration/
repository: https://github.com/athena-sql/athena_sql/tree/master/packages/athena_migrate

environment:
sdk: '>=3.5.0 <4.0.0'
sdk: '>=3.0.0 <4.0.0'

dev_dependencies:
build_runner: ^2.3.0
lints: ^2.0.0
lints: ^5.0.0
mockito: ^5.4.0
test: ^1.21.0
dependencies:
Expand Down
30 changes: 0 additions & 30 deletions packages/athena_mysql/analysis_options.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion packages/athena_mysql/example/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:athena_mysql/athena_mysql.dart';

void main(List<String> args) async {
final athenaSql = await AthenaMySQL.open(AthenaMySqlEndpoint(
final athenaSql = await AthenaMySQL.open(const AthenaMySqlEndpoint(
host: 'localgost',
port: 3306,
userName: 'userName',
Expand Down
8 changes: 2 additions & 6 deletions packages/athena_mysql/lib/athena_mysql.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
/// Support for doing something awesome.
///
/// More dartdocs go here.
library athena_mysql;
export 'package:athena_sql/athena_sql.dart';

export 'src/athena_mysql_base.dart';
export 'src/database_config.dart';
export 'src/builders/builders.dart';
export 'package:athena_sql/athena_sql.dart';
export 'src/database_config.dart';
37 changes: 26 additions & 11 deletions packages/athena_mysql/lib/src/athena_mysql_base.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import 'package:athena_mysql/src/mapper.dart';
import 'package:athena_sql/athena_sql.dart';
import 'package:mysql_client/mysql_client.dart';

import 'database_config.dart';
import 'mapper.dart';

/// Represents a MySQL connection endpoint.
abstract class AthenaMySQLException implements Exception {
final String message;

/// Creates a new AthenaMySQLException with the given message.
const AthenaMySQLException(this.message);

/// The message for this exception.
final String message;

String get _prefix;

@override
Expand All @@ -17,19 +20,25 @@ abstract class AthenaMySQLException implements Exception {
}
}

/// Represents a MySQL connection endpoint.
class AthenaMySQLNoConnectionException extends AthenaMySQLException {
/// Creates a new AthenaMySQLNoConnectionException with the given message.
const AthenaMySQLNoConnectionException(
[super.message = "No connection found"]);
[super.message = 'No connection found']);

@override
String get _prefix => 'AthenaMySQLConnectionException';
}

/// Represents a MySQL connection endpoint.
class MySqlTransactionSQLDriver extends AthenaDatabaseDriver {
MySqlTransactionSQLDriver._(this.connection);

/// The connection to the database
MySQLConnection connection;
bool _isOpen = false;
MySqlTransactionSQLDriver._(this.connection);

/// Opens a new connection to the database.
static Future<MySqlTransactionSQLDriver> open(
AthenaMySqlEndpoint endpoint) async {
final connection = await MySQLConnection.createConnection(
Expand Down Expand Up @@ -76,7 +85,7 @@ class MySqlTransactionSQLDriver extends AthenaDatabaseDriver {
Map<String, dynamic>? mapValues,
bool? iterable,
}) async {
final mapper = QueryMapper(numered: false, prefixQuery: '?');
final mapper = QueryMapper(prefixQuery: '?');

final queryToExecute = mapper.getValues(queryString, mapValues ?? {});
if (queryToExecute.args.isEmpty) {
Expand All @@ -102,21 +111,24 @@ class MySqlTransactionSQLDriver extends AthenaDatabaseDriver {
}
}

/// Column options for MySQL Driver
class MySqlColumnsDriver extends AthenaColumnsDriver {
@override
ColumnDef boolean() => ColumnDef('BOOLEAN');
ColumnDef boolean() => const ColumnDef('BOOLEAN');

@override
ColumnDef integer() => ColumnDef('INTEGER');
ColumnDef integer() => const ColumnDef('INTEGER');

@override
ColumnDef string() => ColumnDef('VARCHAR', parameters: ['255']);
ColumnDef string() => const ColumnDef('VARCHAR', parameters: ['255']);
}

/// MySQL driver for Athena
class MySqlDriver extends MySqlTransactionSQLDriver
implements AthenaDatabaseConnectionDriver {
MySqlDriver._(MySQLConnection connection) : super._(connection);
MySqlDriver._(super.connection) : super._();

/// Opens a new connection to the database.
static Future<MySqlDriver> open(AthenaMySqlEndpoint endpoint) async {
final connection = await MySQLConnection.createConnection(
host: endpoint.host,
Expand Down Expand Up @@ -148,14 +160,17 @@ class MySqlDriver extends MySqlTransactionSQLDriver
}
}

/// MySQL Athena connection
class AthenaMySQL extends AthenaSQL<MySqlDriver> {
AthenaMySQL._(MySqlDriver driver) : super(driver);
AthenaMySQL._(super.driver);

/// Athena MySQL connection
static Future<AthenaMySQL> open(AthenaMySqlEndpoint endpoint) async {
final driver = await MySqlDriver.open(endpoint);
return AthenaMySQL._(driver);
}

/// Creates a new AthenaMySQL instance from a map connection
static Future<AthenaMySQL> fromMapConnection(
Map<String, dynamic> connection) async {
final config = AthenaMySqlEndpoint.fromMap(connection);
Expand Down
6 changes: 5 additions & 1 deletion packages/athena_mysql/lib/src/builders/column_options.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import 'package:athena_sql/athena_sql.dart';
import 'package:athena_sql/schemas.dart';
import 'package:athena_sql/query_printable.dart';
import 'package:athena_sql/schemas.dart';

/// Column options for Athena
typedef CSBuilder<D extends AthenaDriver> = AthenaQueryBuilder<D, ColumnSchema>;

/// Column options for Athena
extension ColumnOptionsBuilder<D extends AthenaDriver> on CSBuilder<D> {
/// Set Compression for the column
CSBuilder<D> compression(String compression) {
return $addingPreContrains(
QueryString().keyword('COMPRESSION ').userInput(compression));
}

/// Set Collate for the column
CSBuilder<D> collate(String collate) {
return $addingPreContrains(
QueryString().keyword('COLLATE ').userInput(collate));
Expand Down
53 changes: 38 additions & 15 deletions packages/athena_mysql/lib/src/builders/data_types.dart
Original file line number Diff line number Diff line change
@@ -1,105 +1,128 @@
import 'package:athena_sql/athena_sql.dart';
import 'package:athena_sql/schemas.dart';

/// Integer Data Types
extension IntegerDataTypes<D extends AthenaDriver>
on AthenaQueryBuilder<D, CreateColumnSchema> {
/// Create a column with type TINYINT
AthenaQueryBuilder<D, ColumnSchema> tinyint(String name) =>
$customType(name, type: 'TINYINT');

/// Create a column with type SMALLINT
AthenaQueryBuilder<D, ColumnSchema> smallint(String name) =>
$customType(name, type: 'SMALLINT');

/// Create a column with type MEDIUMINT
AthenaQueryBuilder<D, ColumnSchema> mediumint(String name) =>
$customType(name, type: "MEDIUMINT");
$customType(name, type: 'MEDIUMINT');

/// Create a column with type INT
AthenaQueryBuilder<D, ColumnSchema> int_(String name) =>
$customType(name, type: "INT");
$customType(name, type: 'INT');

AthenaQueryBuilder<D, ColumnSchema> _fixedPointType(String name, String type,
[int? digits, int? decimals]) {
assert(digits == null || digits > 0);
assert(digits == null || digits < 65);
assert(decimals == null || decimals > 0);
assert(digits == null || digits > 0, 'digits must be greater than 0');
assert(digits == null || digits < 65, 'digits must be less than 65');
assert(decimals == null || decimals > 0, 'decimals must be greater than 0');
// assert if decimals is not null, digits must be not null
assert(digits != null || decimals == null);
assert(digits != null || decimals == null,
'digits must be not null if decimals is not null');
return $customType(name,
type: type, parameters: ['$digits', if (decimals != null) '$decimals']);
}

/// Create a column with type DECIMAL
AthenaQueryBuilder<D, ColumnSchema> decimal(String name,
[int? digits, int? decimals]) =>
_fixedPointType(name, 'DECIMAL', digits, decimals);

/// Create a column with type NUMERIC
AthenaQueryBuilder<D, ColumnSchema> numeric(String name,
[int? digits, int? decimals]) =>
_fixedPointType(name, 'NUMERIC', digits, decimals);
}

/// Floating Point Data Types
extension FixedPointDataTypes<D extends AthenaDriver>
on AthenaQueryBuilder<D, CreateColumnSchema> {
AthenaQueryBuilder<D, ColumnSchema> _fixedPointType(String name, String type,
[int? digits, int? decimals]) {
assert(digits == null || digits > 0);
assert(digits == null || digits < 65);
assert(decimals == null || decimals > 0);
assert(digits == null || digits > 0, 'digits must be greater than 0');
assert(digits == null || digits < 65, 'digits must be less than 65');
assert(decimals == null || decimals > 0, 'decimals must be greater than 0');
// assert if decimals is not null, digits must be not null
assert(digits != null || decimals == null);
assert(digits != null || decimals == null,
'digits must be not null if decimals is not null');
return $customType(name, type: type, parameters: [
if (digits != null) '$digits',
if (digits != null && decimals != null) '$decimals'
]);
}

/// Create a column with type DECIMAL
AthenaQueryBuilder<D, ColumnSchema> decimal(String name,
[int? digits, int? decimals]) =>
_fixedPointType(name, 'DECIMAL', digits, decimals);

/// Create a column with type NUMERIC
AthenaQueryBuilder<D, ColumnSchema> numeric(String name,
[int? digits, int? decimals]) =>
_fixedPointType(name, 'NUMERIC', digits, decimals);

/// Create a column with type DEC
AthenaQueryBuilder<D, ColumnSchema> dec(String name,
[int? digits, int? decimals]) =>
_fixedPointType(name, 'DEC', digits, decimals);

/// Create a column with type FIXED
AthenaQueryBuilder<D, ColumnSchema> fixed(String name,
[int? digits, int? decimals]) =>
_fixedPointType(name, 'FIXED', digits, decimals);
}

/// Floating Point Data Types
extension FloatingPointDataTypes<D extends AthenaDriver>
on AthenaQueryBuilder<D, CreateColumnSchema> {
AthenaQueryBuilder<D, ColumnSchema> _floatingPointType(
String name, String type,
[int? precision, int? decimals]) {
assert(precision == null || precision > 0);
assert(precision == null || precision < 53);
assert(decimals == null || decimals > 0);
assert(
precision == null || precision > 0, 'precision must be greater than 0');
assert(
precision == null || precision < 53, 'precision must be less than 53');
assert(decimals == null || decimals > 0, 'decimals must be greater than 0');
// assert if decimals is not null, digits must be not null
assert(precision != null || decimals == null);
assert(precision != null || decimals == null,
'precision must be not null if decimals is not null');
return $customType(name, type: type, parameters: [
if (precision != null) '$precision',
if (precision != null && decimals != null) '$decimals'
]);
}

/// Create a column with type FLOAT
AthenaQueryBuilder<D, ColumnSchema> float(String name,
[int? precision, int? decimals]) =>
_floatingPointType(name, 'FLOAT', precision, decimals);

/// Create a column with type DOUBLE
AthenaQueryBuilder<D, ColumnSchema> double(String name,
[int? precision, int? decimals]) =>
_floatingPointType(name, 'DOUBLE', precision, decimals);

/// Create a column with type DOUBLE PRECISION
AthenaQueryBuilder<D, ColumnSchema> doublePrecision(String name,
[int? precision, int? decimals]) =>
_floatingPointType(name, 'DOUBLE PRECISION', precision, decimals);
}

/// Bits Data Types
extension BitValueDataTypes<D extends AthenaDriver>
on AthenaQueryBuilder<D, CreateColumnSchema> {
/// Create a column with type BIT
AthenaQueryBuilder<D, ColumnSchema> bit(String name, int values) {
assert(values >= 1 || values <= 64);
assert(values >= 1 || values <= 64, 'values must be between 1 and 64');
return $customType(name, type: 'BIT', parameters: ['$values']);
}
}
Loading

0 comments on commit cdca975

Please sign in to comment.