Class: LgPodPlugin::LSqliteDb
- Inherits:
-
Object
- Object
- LgPodPlugin::LSqliteDb
- Includes:
- Singleton
- Defined in:
- lib/lg_pod_plugin/db/database.rb
Constant Summary collapse
- K_USER_TABLE =
"user_tab"- K_USER_PROJECTS =
"user_projects"- K_POD_LATEST_REFS =
"user_pod_latest_refs"- K_POD_SHA_BRANCH =
"user_pod_sha_with_branch"
Class Method Summary collapse
Instance Method Summary collapse
- #delete_project_by_id(project_id) ⇒ Object
-
#delete_user_info(id) ⇒ Object
删除用户信息.
-
#init_database ⇒ Object
初始化database.
-
#initialize ⇒ LSqliteDb
constructor
初始化 db.
-
#insert_project(project) ⇒ Object
保存项目信息到数据库.
- #insert_user_info(user) ⇒ Object
-
#query_project_info(name, git) ⇒ Object
通过名称查询项目信息.
- #query_user_info(user_id) ⇒ Object
Constructor Details
#initialize ⇒ LSqliteDb
初始化 db
62 63 |
# File 'lib/lg_pod_plugin/db/database.rb', line 62 def initialize end |
Class Method Details
.shared ⇒ Object
57 58 59 |
# File 'lib/lg_pod_plugin/db/database.rb', line 57 def self.shared return LSqliteDb.instance end |
Instance Method Details
#delete_project_by_id(project_id) ⇒ Object
188 189 190 191 |
# File 'lib/lg_pod_plugin/db/database.rb', line 188 def delete_project_by_id(project_id) ps = @db.prepare("DELETE FROM #{K_USER_PROJECTS} WHERE id = :n") ps.execute('n' => project_id) end |
#delete_user_info(id) ⇒ Object
删除用户信息
151 152 153 154 |
# File 'lib/lg_pod_plugin/db/database.rb', line 151 def delete_user_info(id) ps = @db.prepare("DELETE FROM #{K_USER_TABLE} WHERE id = :n") ps.execute('n' => id) end |
#init_database ⇒ Object
初始化database
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/lg_pod_plugin/db/database.rb', line 66 def init_database root_path = LFileManager.download_director.join("database") db_file_path = root_path.join("my.db") unless root_path.exist? && db_file_path.exist? FileUtils.mkdir(root_path) FileUtils.chdir(root_path) FileUtils.touch("my.db") end # FileUtils.chdir(root_path) self.db = SQLite3::Database.new(db_file_path.to_path) #添加用户表 sql1 = "create table if not exists #{K_USER_TABLE}( id varchar(100) primary key not null, username varchar(100), password varchar(100), host varchar(100), access_token varchar(100), refresh_token varchar(100), expires_in TimeStamp NOT NULL DEFAULT CURRENT_TIMESTAMP);" self.db.execute(sql1) sel_sql = "select * from sqlite_master where name = '#{K_USER_TABLE}' and sql like '%update_time%'; " resultSetPrincipal = self.db.execute(sel_sql) if resultSetPrincipal.count == 0 alter = "ALTER TABLE #{K_USER_TABLE} ADD 'update_time' TimeStamp;" self.db.execute(alter) end user_sel_sql2 = "select * from sqlite_master where name = '#{K_USER_TABLE}' and sql like '%type%'; " resultSetPrincipal2 = self.db.execute(user_sel_sql2) if resultSetPrincipal2.count == 0 alter = "ALTER TABLE #{K_USER_TABLE} ADD 'type' INT;" self.db.execute(alter) end #添加项目表 sql2 = "create table if not exists #{K_USER_PROJECTS}( id varchar(100) primary key not null, name varchar(100), desc varchar(100), path varchar(100), ssh_url_to_repo varchar(100), http_url_to_repo varchar(100), web_url varchar(100), name_with_namespace varchar(100), path_with_namespace varchar(100) );" self.db.execute(sql2) end |
#insert_project(project) ⇒ Object
保存项目信息到数据库
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/lg_pod_plugin/db/database.rb', line 157 def insert_project(project) if self.query_project_info(project.name, project.http_url_to_repo) != nil self.db.execute_batch( "UPDATE #{K_USER_PROJECTS} SET name = (:name), desc = (:desc), path = (:path), ssh_url_to_repo = (:ssh_url_to_repo), http_url_to_repo = (:http_url_to_repo), web_url = (:web_url), name_with_namespace = (:name_with_namespace), path_with_namespace = (:path_with_namespace) where (id = :id)", { "name" => project.name, "desc" => project.description, "path" => project.path, "ssh_url_to_repo" => project.ssh_url_to_repo, :http_url_to_repo => project.http_url_to_repo, :web_url => project.web_url, :id => project.id, :path_with_namespace => project.path_with_namespace, :name_with_namespace => project.name_with_namespace } ) else self.db.execute("INSERT INTO #{K_USER_PROJECTS} (id, name, desc, path, ssh_url_to_repo, http_url_to_repo, web_url,name_with_namespace, path_with_namespace) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", [project.id, project.name, project.description, project.path, project.ssh_url_to_repo, project.http_url_to_repo, project.web_url, project.name_with_namespace, project.path_with_namespace]) end end |
#insert_user_info(user) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/lg_pod_plugin/db/database.rb', line 118 def insert_user_info(user) # pp "user.id => #{user.id}" if self.query_user_info(user.id) != nil self.db.execute_batch( "UPDATE #{K_USER_TABLE} SET username = (:username), password = (:password), host = (:host), access_token = (:access_token), expires_in = (:expires_in), refresh_token = (:refresh_token), update_time = (:update_time), type = (:type) where (id = :id)", { "username" => user.username, "password" => user.password, "host" => user.host, "access_token" => user.access_token, :expires_in => user.expires_in, :id => user.id, :refresh_token => user.refresh_token , :update_time => user.update_time, :type => user.type} ) else self.db.execute("INSERT INTO #{K_USER_TABLE} (id, username, password, host, access_token,refresh_token, expires_in, update_time, type) VALUES (?, ?, ?, ?,?,?,?, ?, ?)", [user.id, user.username, user.password, user.host, user.access_token, user.refresh_token, user.expires_in, user.update_time, user.type]) end end |
#query_project_info(name, git) ⇒ Object
通过名称查询项目信息
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/lg_pod_plugin/db/database.rb', line 169 def query_project_info(name, git) project_info = nil self.db.execute("select * from #{K_USER_PROJECTS} where name = '#{name}' or path = '#{name}' ;") do |row| name_with_namespace = row[7] path_with_namespace = row[8] next unless git.include?(name_with_namespace) || git.include?(path_with_namespace) id = row[0] path = row[3] name = row[1] web_url = row[6] description = row[2] ssh_url_to_repo = row[4] http_url_to_repo = row[5] project_info = ProjectModel.new(id, name, description, path, ssh_url_to_repo, http_url_to_repo, web_url, name_with_namespace, path_with_namespace) return project_info end project_info end |
#query_user_info(user_id) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/lg_pod_plugin/db/database.rb', line 133 def query_user_info(user_id) user_info = nil self.db.execute("select * from #{K_USER_TABLE} where id = '#{user_id}';") do |row| user_info = LUserAuthInfo.new user_info.id = row[0] user_info.username = row[1] user_info.password = row[2] user_info.host = row[3] user_info.access_token = row[4] user_info.refresh_token = row[5] user_info.expires_in = row[6] user_info.update_time = row[7] user_info.type = row[8] end user_info end |