Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions res/gtk/connection-form.blp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ menu primary_menu {
}
section {
item {
label: _("_Import Connections");
label: _("_Import");
action: "win.import";
}

item {
label: _("_Export Connections");
label: _("_Export");
action: "win.export";
}
}
Expand Down
9 changes: 7 additions & 2 deletions res/gtk/query-editor.blp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ template $PsequelQueryEditor : Adw.Bin {
spinning: bind template.query-history-viewmodel as <$PsequelQueryHistoryViewModel>.is-loading;
}

// Label query_time {
// label: bind template.query-history-viewmodel as <$PsequelQueryHistoryViewModel>.query-time;
// }

Button run_query_btn {
styles ["suggested-action"]
label: "Run Query";
Expand Down Expand Up @@ -152,8 +156,9 @@ template $PsequelQueryEditor : Adw.Bin {
hexpand: true;
}

Label query_time {
label: bind template.query-history-viewmodel as <$PsequelQueryHistoryViewModel>.query-time;
Button export {
label: "Export";
clicked => $on_export_csv();
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions res/gtk/schema-main.blp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ template $PsequelSchemaMain : Gtk.Box {
StackPage {
name: "view";
child: $PsequelViewStructureView {
viewstructure-viewmodel: bind template.view-viewmodel as <$PsequelViewViewModel>.viewstructure-viewmodel;
};
}
};
Expand All @@ -106,14 +105,12 @@ template $PsequelSchemaMain : Gtk.Box {
StackPage {
name: "table";
child: $PsequelTableDataView {
tabledata-viewmodel: bind template.table-viewmodel as <$PsequelTableViewModel>.tabledata-viewmodel;
};
}

StackPage {
name: "view";
child: $PsequelViewDataView {
viewdata-viewmodel: bind template.view-viewmodel as <$PsequelViewViewModel>.viewdata-viewmodel;
};
}
};
Expand Down
20 changes: 9 additions & 11 deletions src/application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ namespace Psequel {
public int color_scheme { get; set; }
public const int MAX_COLUMNS = 100;

private PreferencesWindow preference;


public Application () {
Object (application_id: "me.ppvan.psequel", flags: ApplicationFlags.DEFAULT_FLAGS);
}
Expand Down Expand Up @@ -191,14 +188,13 @@ namespace Psequel {

private void on_preferences_action () {

if (this.preference == null) {
this.preference = new PreferencesWindow () {
transient_for = this.active_window,
modal = true,
application = this,
};
}
this.preference.present ();
var preference = new PreferencesWindow () {
transient_for = this.active_window,
modal = true,
application = this,
};

preference.present ();
}

/**
Expand All @@ -225,6 +221,7 @@ namespace Psequel {
var schema_service = new SchemaService (sql_service);
var repository = new ConnectionRepository (Application.settings);
var navigation = new NavigationService ();
var export = new ExportService ();

// viewmodels
var conn_vm = new ConnectionViewModel (repository, sql_service, navigation);
Expand All @@ -240,6 +237,7 @@ namespace Psequel {

container.register (sql_service);
container.register (schema_service);
container.register (export);
container.register (repository);
container.register (navigation);
container.register (conn_vm);
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ psequel_sources = [
# 'models/utils.vala',

'services/SQLService.vala',
'services/ExportService.vala',
'services/NavigationService.vala',
'services/ConnectionService.vala',
'services/Container.vala',
Expand Down
18 changes: 13 additions & 5 deletions src/models/Query.vala
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
namespace Psequel {
public class Query : Object, Json.Serializable {
public string sql { get; private set; }
public Variant[] params;

// Properties must be public, get, set inorder to Json.Serializable works
public string sql { get; set; }
public string[] params {get; set;}
public Query (string sql) {
base();
this.sql = sql;
}

public Query.with_params (string sql, Variant[] params) {
public Query.with_params (string sql, string[] params) {
this(sql);
this.params = params;
}

public void set_limit (int limit) {
if (!is_select ()) {
if (!is_dql ()) {
return;
}
sql += @" LIMIT $limit";
}

private inline bool is_select () {
public bool is_dql () {
return sql.up (6) == "SELECT";
}

public bool is_ddl () {
var regex = /CREATE|DROP|RENAME|ALTER|INSERT|UPDATE|DELETE/;
var query = sql.up (6);
return regex.match (query, 0, null);
}

public Query clone () {
return (Query)Json.gobject_deserialize (typeof (Query), Json.gobject_serialize (this));
}
Expand Down
8 changes: 0 additions & 8 deletions src/models/Schema.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@ namespace Psequel {
public class Schema : Object {
public string name { get; private set; }

public List<Table> tables { get; owned set; }
public List<View> views {get; owned set;}

public Schema (string name) {
Object ();
this.name = name;
}

construct {
tables = new List<Table> ();
views = new List<View> ();
}
}

/** Base type for hold info about Table */
Expand Down
49 changes: 49 additions & 0 deletions src/services/ExportService.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Psequel{
public class ExportService : Object {

public const string DELIMETER = ",";
public const string NEWLINE = "\n";

public ExportService () {
}

// Implement accordding to https://en.wikipedia.org/wiki/Comma-separated_values?useskin=vector#Basic_rules
public async void export_csv (File dest, Relation relation) throws PsequelError {

string[] rows = new string[relation.rows + 1];
string[] cols = new string[relation.cols];

// headers
for (int j = 0; j < relation.cols; j++) {
cols[j] = quote (relation.get_header (j));
}
rows[0] = string.joinv (DELIMETER, cols);

for (int i = 0; i < relation.rows; i++) {
cols = new string[relation.cols];
var row = relation[i];
for (int j = 0; j < relation.cols; j++) {
cols[j] = quote (row[j]);
}

rows[i + 1] = string.joinv (DELIMETER, cols);
}

var bytes = new Bytes.take (string.joinv (NEWLINE, rows).data);

try {
yield dest.replace_contents_bytes_async (bytes, null, false, FileCreateFlags.PRIVATE, null, null);
} catch (GLib.Error err) {
throw new PsequelError.EXPORT_ERROR(err.message);
}
}

private string quote (string str) {
if (str.contains (DELIMETER)) {
return @"\"$(str)\"";
}

return str;
}
}
}
14 changes: 7 additions & 7 deletions src/services/SQLCompletionProvider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ namespace Psequel {
/*
Query viewmodel is not set until the query view is created.
*/
yield schema_viewmodel.load_schema (schema_viewmodel.current_schema);
// yield schema_viewmodel.load_schema (schema_viewmodel.current_schema);
dynamic_candidates = new List<Model> ();
schema_viewmodel.current_schema.tables.foreach ((table) => {
dynamic_candidates.append (new Model (table.name, "TABLE"));
});
// schema_viewmodel.current_schema.tables.foreach ((table) => {
// dynamic_candidates.append (new Model (table.name, "TABLE"));
// });

schema_viewmodel.current_schema.views.foreach ((view) => {
dynamic_candidates.append (new Model (view.name, "VIEW"));
});
// schema_viewmodel.current_schema.views.foreach ((view) => {
// dynamic_candidates.append (new Model (view.name, "VIEW"));
// });

var candidates = new ObservableList<Model> ();
candidates.append_all (static_candidates);
Expand Down
20 changes: 3 additions & 17 deletions src/services/SQLService.vala
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ namespace Psequel {
var status = result.get_status ();

switch (status) {
case ExecStatus.TUPLES_OK, ExecStatus.COMMAND_OK:
case ExecStatus.TUPLES_OK, ExecStatus.COMMAND_OK, ExecStatus.COPY_OUT:
// success
break;
case ExecStatus.FATAL_ERROR:
Expand Down Expand Up @@ -145,21 +145,7 @@ namespace Psequel {
}
}

private async Result exec_query_params_internal (string query, Variant[] params) throws PsequelError {

int n_params = params.length;
string[] values = new string[n_params];

for (int i = 0; i < n_params; i++) {
if (params[i].is_of_type (VariantType.STRING)) {
values[i] = params[i].get_string ();
} else if (params[i].get_type ().is_basic ()) {
values[i] = params[i].print (false);
} else {
warning ("Programming error, got type '%s'", params[i].get_type_string ());
assert_not_reached ();
}
}
private async Result exec_query_params_internal (string query, string[] params) throws PsequelError {

debug ("Exec Param: %s", query);
TimePerf.begin ();
Expand All @@ -169,7 +155,7 @@ namespace Psequel {

try {
var worker = new Worker ("exec query params", () => {
result = active_db.exec_params (query, n_params, null, values, null, null, 0);
result = active_db.exec_params (query, params.length, null, params, null, null, 0);
// Jump to yield
Idle.add ((owned) callback);
});
Expand Down
Loading