git-svn-id: https://abrams/svn/LanSpider-repo@22 16f2e333-51b0-4855-8c5a-a66b7be24171
[LanSpider.git] / src / LanSpider / Database / Manager.cs
blob5666d49668f5d759a8184de60b32b7d173ba3313
1 using System;
2 using System.Collections.Generic;
3 using System.Data;
5 using System.IO;
7 using Extensions;
9 using LanSpider.Properties;
10 using System.Diagnostics;
11 using System.Data.SqlClient;
12 using System.Configuration;
14 namespace LanSpider.Database
16 public class Manager : IDisposable
18 private readonly SqlConnection _conn;
20 private readonly Epam_IndexDataContext _context;
22 private readonly int _rootId = 1;
24 public int RootId
26 get
28 return _rootId;
32 #region Static schema methods
34 /*private static bool SchemaExists( SQLiteConnection connection )
36 SQLiteCommand cmd = new SQLiteCommand( null, connection );
37 cmd.CommandText = "SELECT name FROM sqlite_master WHERE name=@table;";
38 cmd.Parameters.Add( new SQLiteParameter( "@table", "node" ) );
39 object tableName = cmd.ExecuteScalar();
41 return tableName != null && tableName != DBNull.Value;
44 private static void CreateSchema( SQLiteConnection connection )
46 SQLiteCommand cmd = new SQLiteCommand( null, connection );
48 cmd.CommandText = Resources.create_db;
49 cmd.ExecuteNonQuery();
53 #endregion Static schema methods
55 public Manager()
57 _conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
58 _conn.Open();
59 //_context = new Epam_IndexDataContext(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
60 // TO DO:
62 /*if ( !SchemaExists( _conn ) )
64 CreateSchema( _conn );
65 }*/
68 public void Dispose()
70 _conn.Close();
73 public List<string> GetRootFolders()
75 List<string> roots = new List<string>();
77 using (SqlCommand cmd = new SqlCommand("SELECT path FROM folder WHERE parent_id = @root_id AND parent_id != folder_id", _conn))
79 cmd.CommandType = CommandType.Text;
80 cmd.Parameters.Add(new SqlParameter("root_id", _rootId));
82 using (SqlDataReader results = cmd.ExecuteReader())
84 while (results.Read())
86 string x = (string)results.GetValue(0);
87 roots.Add(x);
92 return roots;
95 private void IndexFile(int shareId, FileInfo file)
97 IndexFile(shareId, file, null);
100 private void IndexFile(int shareId, FileInfo file, DataTable dt)
102 DataRow row = dt.NewRow();
103 row["parent_folder_id"] = shareId;
104 row["name"] = file.Name;
105 row["type"] = file.Extension;
108 row["size"] = file.Length;
110 catch (FileNotFoundException)
112 row["size"] = -1;
114 dt.Rows.Add(row);
115 /*using ( SqlCommand cmd = new SqlCommand(@"INSERT INTO ""file"" (parent_folder_id, name, type, size) VALUES (@parent_folder_id, @name, @type, @size)", _conn ) )
117 if ( trans != null )
119 cmd.Transaction = trans;
122 cmd.CommandType = CommandType.Text;
123 cmd.Parameters.Add( new SqlParameter( "parent_folder_id", shareId ) );
124 cmd.Parameters.Add(new SqlParameter("name", file.Name));
125 cmd.Parameters.Add(new SqlParameter("type", file.Extension));
129 cmd.Parameters.Add(new SqlParameter("size", file.Length));
131 catch ( FileNotFoundException )
133 cmd.Parameters.Add(new SqlParameter("size", -1));
136 cmd.ExecuteNonQuery();
140 private int GetShareId(string share)
142 return GetShareId(share, null);
145 private int GetShareId(string share, SqlTransaction trans)
147 int shareId = 0;
148 using (SqlCommand cmd = new SqlCommand("SELECT folder_id FROM folder WHERE path=@path;", _conn))
150 if (trans != null)
152 cmd.Transaction = trans;
155 cmd.CommandType = CommandType.Text;
156 cmd.Parameters.Add(new SqlParameter("path", share));
158 object val = cmd.ExecuteScalar();
159 if (val == null || val == DBNull.Value)
161 int parentId;
163 if (share.Count('\\') == 3) // top level share
165 parentId = RootId;
167 else
169 int index = share.LastIndexOf('\\');
170 string parentName = share.Substring(0, index);
171 parentId = GetShareId(parentName, trans);
173 cmd.CommandType = CommandType.StoredProcedure;
174 cmd.CommandText = "AddFolder";
175 cmd.Parameters.Add(new SqlParameter("parent_id", parentId));
176 cmd.Parameters.Add(new SqlParameter("folder_id", SqlDbType.Int)).Direction = ParameterDirection.Output;
177 cmd.ExecuteNonQuery();
178 shareId = (int)cmd.Parameters["folder_id"].Value;
180 else
181 return (int)val;
184 return shareId;
187 public void SaveShareStructure(string shareName, IEnumerable<FileInfo> fileInfos)
189 SqlTransaction trans = _conn.BeginTransaction();
193 SaveShareStructure(shareName, fileInfos, trans);
194 trans.Commit();
196 catch (Exception ex)
198 trans.Rollback();
199 Trace.WriteLine("Share " + shareName + " will not be saved: " + ex.Message);
200 Console.Error.WriteLine("Share " + shareName + " will not be saved: " + ex.Message);
201 //throw;
205 public void SaveShareStructure(string shareName, IEnumerable<FileInfo> fileInfos, SqlTransaction trans)
207 using (DataTable dt = new DataTable("[File]"))
209 SqlBulkCopy bulk = new SqlBulkCopy(_conn, SqlBulkCopyOptions.Default, trans);
210 bulk.DestinationTableName = dt.TableName;
211 bulk.BatchSize = 1000;
212 dt.Columns.Add(new DataColumn("file_id", typeof(int)));
213 dt.Columns.Add(new DataColumn("parent_folder_id", typeof(int)));
214 dt.Columns.Add(new DataColumn("name", typeof(string)));
215 dt.Columns.Add(new DataColumn("type", typeof(string)));
216 dt.Columns.Add(new DataColumn("size", typeof(long)));
218 foreach (FileInfo fileInfo in fileInfos)
220 // TO DO: GetShare as SP
221 int shareId = GetShareId(fileInfo.DirectoryName, trans);
222 IndexFile(shareId, fileInfo, dt);
224 bulk.WriteToServer(dt);
225 dt.Clear();
226 GC.Collect();
230 public void SaveKnownShareStructure(string shareName, IEnumerable<FileInfo> fileInfos)
232 SqlTransaction trans = _conn.BeginTransaction();
236 using (var cmd = new SqlCommand(@"DELETE FROM [FILE] WHERE parent_folder_id in (SELECT folder_id FROM Folder WHERE path like @share +'\%' or path = @share)", _conn))
238 cmd.CommandType = CommandType.Text;
239 cmd.Transaction = trans;
240 cmd.CommandTimeout = 0;
241 cmd.Parameters.Add(new SqlParameter("share", shareName));
242 cmd.ExecuteNonQuery();
243 //cmd.CommandText = @"DELETE FROM FOLDER WHERE folder_id in (select folder_id from FOLDER WHERE PATH like @share + '\%')";
244 //cmd.ExecuteNonQuery();
247 SaveShareStructure(shareName, fileInfos, trans);
248 trans.Commit();
250 catch (Exception ex)
252 trans.Rollback();
253 Trace.WriteLine("Share " + shareName + " will not be saved: " + ex.Message);
254 Console.Error.WriteLine("Share " + shareName + " will not be saved: " + ex.Message);
255 //throw;