30a6b40d025d0575542daf5119e6a7261cc9a888
[nit.git] / lib / linux / linux_data_store.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 module linux_data_store
18
19 import app::data_store
20 private import xdg_basedir
21 private import sqlite3
22 private import json_serialization
23
24 redef class App
25 redef var data_store = new LinuxStore
26 end
27
28 private class LinuxStore
29 super DataStore
30
31 # File path of the Sqlite3 DB file
32 fun db_file: String do return "data_store.db"
33
34 # Sqlite3 table used
35 fun db_table: String do return "data_store"
36
37 var db_cache: nullable Sqlite3DB = null
38
39 # Database to use to implement the `DataStore`
40 fun db: nullable Sqlite3DB
41 do
42 var db = db_cache
43 if db != null then return db
44
45 # Find DB path
46 var config_home = xdg_basedir.config_home.to_s
47 var config_dir = config_home.join_path(sys.program_name.basename(""))
48 if not config_dir.file_exists then config_dir.mkdir
49 var db_path = config_dir.join_path(db_file)
50
51 # Open DB connection
52 db = new Sqlite3DB.open(db_path)
53 if not db.is_open then
54 app.log_error "Data store unavaible, cannot load/save data. (at '{db_path}' with '{db.error or else "unknown"}')"
55 return null
56 end
57
58 # Create DB table
59 db.create_table "IF NOT EXISTS {db_table} (key TEXT PRIMARY KEY, value TEXT)"
60
61 db_cache = db
62 return db
63 end
64
65 redef fun [](key)
66 do
67 # Get DB
68 var db = self.db
69 if db == null then return null
70
71 # Prepare SELECT statement
72 var stmt = db.select("* FROM {db_table} WHERE key == {key.to_sql_string}")
73
74 # Execute statment
75 for row in stmt do
76 # Get from statement
77 var serialized = row[1].to_s
78 stmt.close
79
80 # Deserialize
81 var deserializer = new JsonDeserializer(serialized)
82 var deserialized = deserializer.deserialize
83
84 return deserialized
85 end
86
87 stmt.close
88 return null
89 end
90
91 redef fun []=(key, value)
92 do
93 # Get DB
94 var db = self.db
95 if db == null then return
96
97 # Serialize
98 var stream = new StringOStream
99 var serializer = new JsonSerializer(stream)
100 serializer.serialize value
101 var serialized = stream.to_s
102
103 # Save in DB
104 db.execute "BEGIN TRANSACTION"
105 db.insert "OR REPLACE INTO {db_table} VALUES({key.to_sql_string}, {serialized.to_sql_string})"
106 db.execute "COMMIT"
107 end
108 end