Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.90 KB | None | 0 0
  1. /**
  2.  
  3.     Jordi Puigdellívol
  4.  
  5.     Marlin Tagging system
  6.  
  7.     libsqlite3-dev
  8.  
  9.     create table tags (uri TEXT, color INT, tags TEXT);
  10.  
  11.     valac --pkg sqlite3 -o sqlitesample SqliteSample.vala
  12.  */
  13.  
  14. using GLib;
  15. using Sqlite;
  16.  
  17. public class MarlinTags : GLib.Object {
  18.  
  19.  
  20.     private Database db;
  21.  
  22.     public static int callback (int n_columns, string[] values, string[] column_names){
  23.         for (int i = 0; i < n_columns; i++) {
  24.             stdout.printf ("%s = %s\n", column_names[i], values[i]);
  25.         }
  26.         stdout.printf ("\n");
  27.  
  28.         return 0;
  29.     }
  30.  
  31.     public bool openDB(string dbName){
  32.  
  33.         if (!FileUtils.test (dbName, FileTest.IS_REGULAR)) {
  34.             stderr.printf ("Database %s does not exist or is directory\n", dbName);
  35.             return false;
  36.         }
  37.  
  38.         int rc = Database.open (dbName, out db);
  39.  
  40.         if (rc != Sqlite.OK) {
  41.             stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ());
  42.             return false;
  43.         }
  44.  
  45.         return true;
  46.     }
  47.  
  48.  
  49.     public bool addColor(string uri, int color){
  50.  
  51.         //TODO: Find if the uri already exists in database, if so, then update
  52.         //TODO: Convert int to string
  53.         string c = "insert into tags(uri,color) values ("+uri+",0)";
  54.         int   rc = db.exec (c, null, null);
  55.  
  56.         if (rc != Sqlite.OK) {
  57.             stderr.printf ("[addColor: SQL error]  %d, %s\n", rc, db.errmsg ());
  58.             return false;
  59.         }
  60.  
  61.         return true;
  62.     }
  63.  
  64.     public int getColor(string uri)
  65.     {
  66.         string c = "select color from tags where uri='" + uri + "'";
  67.         Statement stmt;
  68.         int rc = 0;
  69.         int col, cols;
  70.         string txt = "-1";
  71.  
  72.         if ((rc = db.prepare_v2 (c, -1, out stmt, null)) == 1) {
  73.                 printerr ("SQL error: %d, %s\n", rc, db.errmsg ());
  74.                 return -1;
  75.             }
  76.             cols = stmt.column_count();
  77.             do {
  78.                 rc = stmt.step();
  79.                 switch (rc) {
  80.                 case Sqlite.DONE:
  81.                     break;
  82.                 case Sqlite.ROW:
  83.                     for (col = 0; col < cols; col++) {
  84.                         txt = stmt.column_text(col);
  85.                         //print ("%s = %s\n", stmt.column_name (col), txt);
  86.                     }
  87.                     break;
  88.                 default:
  89.                     printerr ("Error: %d, %s\n", rc, db.errmsg ());
  90.                     break;
  91.                 }
  92.             } while (rc == Sqlite.ROW);
  93.  
  94.         return (int)txt.to_long();
  95.     }
  96.  
  97.     public bool showTable(string table){
  98.  
  99.         string consult = "select * from " + table;
  100.         int rc = db.exec (consult, callback, null);
  101.  
  102.         if (rc != Sqlite.OK) {
  103.             stderr.printf ("[showTable: SQL error]: %d, %s\n", rc, db.errmsg ());
  104.             return false;
  105.         }
  106.  
  107.         return true;
  108.     }
  109.  
  110.  
  111.     /*Main*/
  112.     public static int main (string[] args) {
  113.  
  114.         MarlinTags t = new MarlinTags();
  115.  
  116.         //The database must exists, add here the full path
  117.         string marlinDB = "testdb";
  118.  
  119.         //string consult = "select * from tags";
  120.  
  121.  
  122.         t.openDB(marlinDB);
  123.         //t.addColor("/home/diana",0);
  124.         //t.showTable("tags");
  125.         print("Color for file is %i\n", t.getColor("/home/jordi"));
  126.  
  127.  
  128.         return 0;
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement