2 using System
.Collections
.Generic
;
4 using System
.Data
.Common
;
5 using System
.Diagnostics
;
7 using MySql
.Data
.MySqlClient
;
11 public class AltModel
: IDisposable
13 private readonly string _connectionString
;
14 private readonly DbConnection _connection
;
18 DbConnectionStringBuilder dbcsb
19 = new DbConnectionStringBuilder
21 { "Data Source", "localhost" }
,
22 { "User ID", "root" }
,
23 { "Database", "share_index" }
,
27 _connectionString
= dbcsb
.ConnectionString
;
29 _connection
= new MySqlConnection( _connectionString
);
34 /// Saves given path to the database.
36 /// <param name="path"></param>
37 /// <returns>Leaf node ID</returns>
38 /// <exception cref="ApplicationException">Thrown if saving failed.</exception>
39 public int SavePath( string path
)
41 IEnumerable
<string> getTokens
= GetTokens( path
);
45 foreach ( string token
in getTokens
)
47 parentId
= SaveNode( token
, parentId
);
50 throw new ApplicationException( String
.Format( "Saving node {0} of {1} failed.", token
, path
) );
58 /// Adds new node to the if does not exist in the database otherwise, loads the node.
60 /// <param name="nodeName"></param>
61 /// <param name="parentNodeId"></param>
62 /// <returns>Child node ID</returns>
63 private int SaveNode( string nodeName
, int parentNodeId
)
65 DbCommand cmd
= _connection
.CreateCommand();
66 cmd
.CommandText
= "proc_save_node";
67 cmd
.CommandType
= CommandType
.StoredProcedure
;
69 DbParameter nameParam
= cmd
.CreateParameter();
70 nameParam
.Direction
= ParameterDirection
.Input
;
71 nameParam
.ParameterName
= "$name";
72 nameParam
.Value
= nodeName
;
74 DbParameter parendIdParam
= cmd
.CreateParameter();
75 parendIdParam
.Direction
= ParameterDirection
.Input
;
76 parendIdParam
.ParameterName
= "$parent_id";
77 parendIdParam
.Value
= parentNodeId
;
79 DbParameter childIdParam
= cmd
.CreateParameter();
80 childIdParam
.Direction
= ParameterDirection
.Output
;
81 childIdParam
.ParameterName
= "$child_node_id";
83 cmd
.Parameters
.AddRange( new[] { nameParam, parendIdParam, childIdParam }
);
85 cmd
.ExecuteNonQuery();
88 if ( childIdParam
.Value
!= null && childIdParam
.Value
!= DBNull
.Value
)
90 childNodeId
= (int) childIdParam
.Value
;
99 /// Split given path into separate tokens (directory and file names).
101 /// <param name="path"></param>
102 /// <returns></returns>
103 private static IEnumerable
<string> GetTokens( string path
)
105 string[] patrs
= path
.Substring( 1 ).Split( new[] { '/', '\\' }
, StringSplitOptions
.RemoveEmptyEntries
);
106 patrs
[ 0 ] = path
[ 0 ] + patrs
[ 0 ];
110 public void Dispose()
112 if ( _connection
.State
!= ConnectionState
.Closed
)
118 catch ( DbException ex
)
120 Trace
.WriteLine( ex
.Message
, "Closing connection" );
126 /// Save file info into the database.
128 /// <param name="nodeId">File node id</param>
129 /// <param name="size">File size in bytes</param>
130 /// <param name="ctime">File creation time</param>
131 /// <param name="mtime">File's last modification time</param>
132 /// <param name="atime">File's last access time</param>
133 public void SaveFileInfo( int nodeId
, long size
, DateTime ctime
, DateTime mtime
, DateTime atime
)
135 DbCommand cmd
= _connection
.CreateCommand();
136 cmd
.CommandText
= "proc_save_file_info";
137 cmd
.CommandType
= CommandType
.StoredProcedure
;
139 DbParameter idParam
= cmd
.CreateParameter();
140 idParam
.Direction
= ParameterDirection
.Input
;
141 idParam
.ParameterName
= "$id";
142 idParam
.Value
= nodeId
;
144 DbParameter sizeParam
= cmd
.CreateParameter();
145 sizeParam
.Direction
= ParameterDirection
.Input
;
146 sizeParam
.ParameterName
= "$size";
147 sizeParam
.Value
= size
;
149 DbParameter ctimeParam
= cmd
.CreateParameter();
150 ctimeParam
.Direction
= ParameterDirection
.Input
;
151 ctimeParam
.ParameterName
= "$ctime";
152 ctimeParam
.Value
= ctime
;
154 DbParameter mtimeParam
= cmd
.CreateParameter();
155 mtimeParam
.Direction
= ParameterDirection
.Input
;
156 mtimeParam
.ParameterName
= "$mtime";
157 mtimeParam
.Value
= mtime
;
159 DbParameter atimeParam
= cmd
.CreateParameter();
160 atimeParam
.Direction
= ParameterDirection
.Input
;
161 atimeParam
.ParameterName
= "$atime";
162 atimeParam
.Value
= atime
;
164 cmd
.Parameters
.AddRange( new[] { idParam, sizeParam, ctimeParam, mtimeParam, atimeParam }
);
166 cmd
.ExecuteNonQuery();
171 /// Get list of shares stored in the database.
173 /// <returns>List of shares</returns>
174 public List
<string> GetShares()
176 List
<string> shares
= new List
<string>();
178 DbCommand cmd
= _connection
.CreateCommand();
179 cmd
.CommandText
= "SELECT path FROM view_shares";
180 cmd
.CommandType
= CommandType
.Text
;
182 DbDataReader reader
= cmd
.ExecuteReader();
183 if ( reader
.HasRows
)
185 while ( reader
.Read() )
187 shares
.Add( reader
[ "path" ] as string );