Class: LgPodPlugin::LSqliteDb

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/lg_pod_plugin/database.rb

Constant Summary collapse

K_USER_TABLE =
"user_tab"
K_USER_PROJECTS =
"user_projects"
K_POD_LATEST_REFS =
"user_pod_latest_refs"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLSqliteDb

初始化 db



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/lg_pod_plugin/database.rb', line 92

def initialize
  root_path = LFileManager.download_director.join("database")
  db_file_path = root_path.join("my.db")
  if !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)

  #添加项目表
  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)

  #添加项目表
  sql3 = "create table if not exists #{K_POD_LATEST_REFS}(
    id varchar(100) primary key not null,
    name varchar(100),
   git varchar(100),
   branch varchar(100),
    tag varchar(100),
    sha varchar(100)
    );"
  self.db.execute(sql3)

  super
end

Class Method Details

.sharedObject



87
88
89
# File 'lib/lg_pod_plugin/database.rb', line 87

def self.shared
  return LSqliteDb.instance
end

Instance Method Details

#insert_pod_refs(name, git, branch, tag, commit) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/lg_pod_plugin/database.rb', line 204

def insert_pod_refs(name, git, branch, tag , commit)
  id = LPodLatestRefs.get_pod_id(name, git, branch, tag)
  pod = LPodLatestRefs.new(id, name, git, branch, tag, commit)
  if self.query_pod_refs(id) != nil
    self.db.execute_batch(
      "UPDATE #{K_POD_LATEST_REFS} SET name = (:name), git = (:git), branch = (:branch), tag = (:tag), sha = (:sha) where (id = :id)", { "name" => pod.name, "git" => pod.git, "sha" => pod.commit, "branch" => pod.branch, :tag => pod.tag, :id => pod.id}
    )
  else
    self.db.execute("INSERT INTO #{K_POD_LATEST_REFS} (id, name, git, branch, tag, sha)
        VALUES (?, ?, ?, ?, ?, ?)", [pod.id, pod.name, pod.git, pod.branch, pod.tag, pod.commit])
  end

end

#insert_project(project) ⇒ Object

保存项目信息到数据库



173
174
175
176
177
178
179
180
181
182
# File 'lib/lg_pod_plugin/database.rb', line 173

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



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/lg_pod_plugin/database.rb', line 143

def (user)
  # pp "user.id => #{user.id}"
  if self.(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) 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 }
    )
  else
    self.db.execute("INSERT INTO #{K_USER_TABLE} (id, username, password, host, access_token,refresh_token, expires_in)
        VALUES (?, ?, ?, ?,?,?,?)", [user.id, user.username, user.password, user.host, user.access_token, user.refresh_token, user.expires_in])
  end

end

#query_pod_refs(id) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/lg_pod_plugin/database.rb', line 218

def query_pod_refs(id)
  pod_info = nil
  self.db.execute("select * from #{K_POD_LATEST_REFS} where id = '#{id}';") do |row|
    id = row[0]
    name = row[1]
    git = row[2]
    branch = row[3]
    tag = row[4]
    commit = row[5]
    pod_info = LPodLatestRefs.new(id, name, git, branch, tag, commit)
  end
  return pod_info
end

#query_project_info(name, git = nil) ⇒ Object

通过名称查询项目信息



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/lg_pod_plugin/database.rb', line 185

def query_project_info(name, git = nil)
  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
  return project_info
end

#query_user_info(user_id) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/lg_pod_plugin/database.rb', line 157

def (user_id)
   = nil
  self.db.execute("select * from #{K_USER_TABLE} where id = '#{user_id}';") do |row|
     = LUserAuthInfo.new
    .id = row[0]
    .username = row[1]
    .password = row[2]
    .host = row[3]
    .access_token = row[4]
    .refresh_token = row[5]
    .expires_in = row[6]
  end
  
end