Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2 from adzymaniac/devel

Devel
  • Loading branch information...
commit a41c4a20e16c3e9c3d25cb6a0a257fab83223f77 2 parents 423ccfb + 3462974
@adzymaniac adzymaniac authored
Showing with 127 additions and 7 deletions.
  1. +44 −6 warsi-catalog.vala
  2. +80 −0 warsi-database.vala
  3. +3 −1 warsi-exceptions.vala
View
50 warsi-catalog.vala
@@ -24,14 +24,22 @@ using Gee;
private const string PACKAGES_DIR = "/var/lib/apt/lists";
private const string STATUS_PACKAGES = "/var/lib/dpkg/status";
+private const long MAX_REC_PER_PAGE = 100;
public struct PackageRow {
public string name;
public string version;
- public long offset;
+ public string offset;
public int64 repository;
}
+public struct PackageList {
+ public string name;
+ public string version;
+ public string offset;
+ public string repository;
+}
+
public class WarsiCatalog : GLib.Object {
private long offset;
@@ -45,8 +53,7 @@ public class WarsiCatalog : GLib.Object {
var enumerator = directory.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, 0);
FileInfo file_info;
- var db = new WarsiDatabase ();
- db.prepare ();
+ WarsiDatabase.instance().prepare ();
while ((file_info = enumerator.next_file ()) != null) {
if ("Packages" in file_info.get_name ()) {
@@ -58,7 +65,7 @@ public class WarsiCatalog : GLib.Object {
PackageRow row = PackageRow ();
var timestamp = new DateTime.now_local ();
- var repo_id = db.insert_repository (file_info.get_name (), timestamp.to_string ());
+ var repo_id = WarsiDatabase.instance().insert_repository (file_info.get_name (), timestamp.to_string ());
row.repository = repo_id;
try {
@@ -85,7 +92,7 @@ public class WarsiCatalog : GLib.Object {
if (line.length == 0) {
if (row.name.length != 0 && row.version.length != 0) {
row.offset = ("%xld").printf ((uint)offset);
- db.insert (row);
+ WarsiDatabase.instance().insert (row);
}
}
}
@@ -94,7 +101,38 @@ public class WarsiCatalog : GLib.Object {
}
}
}
- db.save ();
+ WarsiDatabase.instance().save ();
+ }
+
+ public string list (string package = "", long start) throws WarsiCatalogError {
+ string packages = "";
+ bool need_comma = false;
+ var packageslist = WarsiDatabase.instance().list (package, start, MAX_REC_PER_PAGE);
+
+ packages += "[\n";
+ foreach ( PackageList? packagelist in packageslist ) {
+ if (need_comma) packages += ",\n";
+ packages += " { 'name' : '%s',\n".printf (packagelist.name);
+ packages += " 'version' : '%s',\n".printf (packagelist.version);
+ packages += " 'offset' : '%s',\n".printf (packagelist.offset);
+ packages += " 'repository' : '%s',\n".printf (packagelist.repository);
+ need_comma = true;
+ }
+ packages += "]";
+ return packages;
+ }
+
+ public long get_size () {
+ return WarsiDatabase.instance().get_list_size ();
+ }
+
+ public string get_info (string name, string version) {
+ var packagerow = WarsiDatabase.instance().get_info (name, version);
+
+ string package = "{ 'name' : '%s', 'version' : '%s', 'offset' : '%s', 'repository' : '%s' }"
+ .printf (packagerow.name, packagerow.version, packagerow.offset, packagerow.repository);
+
+ return package;
}
public status () {
View
80 warsi-database.vala
@@ -29,6 +29,8 @@ public class WarsiDatabase : GLib.Object {
private Sqlite.Statement stmt;
private bool prepared = false;
+ static WarsiDatabase _instance = null;
+
public WarsiDatabase () throws WarsiDatabaseError {
int res = db.open_v2(WARSI_DB, out db, Sqlite.OPEN_READWRITE | Sqlite.OPEN_CREATE,
null);
@@ -38,6 +40,14 @@ public class WarsiDatabase : GLib.Object {
}
}
+ public static WarsiDatabase instance () {
+ if (_instance == null) {
+ _instance = new WarsiDatabase ();
+ }
+
+ return _instance;
+ }
+
public void prepare () throws WarsiDatabaseError {
int res = db.exec ("BEGIN TRANSACTION");
@@ -168,4 +178,74 @@ public class WarsiDatabase : GLib.Object {
prepared = false;
}
}
+
+ public Gee.ArrayList<PackageList?> list (string package = "", long start, long length) {
+ int res;
+
+ if (package != "") {
+ res = db.prepare_v2 ("SELECT name, version, offset, Repositories.repository as repository FROM Packages INNER JOIN Repositories ON Repositories.id=Packages.repository WHERE name LIKE ?% LIMIT ? OFFSET ?", -1, out stmt);
+ res = stmt.bind_text (1, package);
+ res = stmt.bind_int64 (2, length);
+ res = stmt.bind_int64 (3, start);
+ } else {
+ res = db.prepare_v2 ("SELECT name, version, offset, Repositories.repository as repository FROM Packages INNER JOIN Repositories ON Repositories.id=Packages.repository LIMIT ? OFFSET ?", -1, out stmt);
+ res = stmt.bind_int64 (1, length);
+ res = stmt.bind_int64 (2, start);
+ }
+
+ Gee.ArrayList<PackageList?> all = new Gee.ArrayList<PackageList?> ();
+
+ if ( res == 1 ) {
+ throw new WarsiDatabaseError.DATABASE_LIST_ERROR ("Unable to list packages: %s\n", db.errmsg ());
+ } else {
+ while (( res = stmt.step() ) == Sqlite.ROW) {
+ PackageList row = PackageList ();
+ row.name = stmt.column_text (0);
+ row.version = stmt.column_text (1);
+ row.offset = stmt.column_text (2);
+ row.repository = stmt.column_text (3);
+
+ all.add (row);
+ }
+ }
+ return all;
+ }
+
+ public long get_list_size (string package = "%") {
+ int res;
+
+ res = db.prepare_v2 ("SELECT COUNT(name) FROM Packages WHERE name LIKE ?%", -1, out stmt);
+ res = stmt.bind_text (1, package);
+
+ res = stmt.step();
+ if (res != Sqlite.ROW) {
+ throw new WarsiDatabaseError.DATABASE_LIST_ERROR ("Unable to retrieve row count on Packages: (%d) %s\n", res, db.errmsg());
+
+ return 0;
+ }
+
+ return stmt.column_int(0);
+ }
+
+ public PackageList? get_info (string name, string version) {
+ int res;
+
+ res = db.prepare_v2 ("SELECT name, version, offset, Repositories.repository as repository FROM Packages INNER JOIN Repositories ON Repositories.id=Packages.repository WHERE name = ? AND version = ?", -1, out stmt);
+ res = stmt.bind_text (1, name);
+ res = stmt.bind_text (2, version);
+
+ if (res == 1) {
+ throw new WarsiDatabaseError.DATABASE_GETINFO_ERROR ("Unable to search: %s\n", db.errmsg ());
+ }
+
+ PackageList row = PackageList ();
+ if ((res = stmt.step ()) == Sqlite.ROW) {
+ row.name = stmt.column_text (0);
+ row.version = stmt.column_text (1);
+ row.offset = stmt.column_text (2);
+ row.repository = stmt.column_text (3);
+ }
+
+ return row;
+ }
}
View
4 warsi-exceptions.vala
@@ -33,5 +33,7 @@ public errordomain WarsiCatalogError {
public errordomain WarsiDatabaseError {
DATABASE_PREPARE_ERROR,
- DATABASE_INSERT_ERROR
+ DATABASE_INSERT_ERROR,
+ DATABASE_LIST_ERROR,
+ DATABASE_GETINFO_ERROR
}
Please sign in to comment.
Something went wrong with that request. Please try again.