Added mysql initialization, added MySQL.Data dlls.
[LanSpider.git] / src / AltSpider / AltModel.cs
blob84ea0f06d214d3246473881536f930ad955699c6
1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Data.Common;
5 using System.Diagnostics;
7 using MySql.Data.MySqlClient;
9 namespace AltSpider
11 internal class AltModel : IDisposable
13 private readonly string _connectionString;
14 private readonly DbConnection _connection;
16 public AltModel()
18 DbConnectionStringBuilder dbcsb
19 = new DbConnectionStringBuilder
21 { "Data Source", "localhost" },
22 { "User ID", "root" },
23 { "Database", "shares_index" }
26 _connectionString = dbcsb.ConnectionString;
28 _connection = new MySqlConnection( _connectionString );
29 _connection.Open();
32 /// <summary>
33 /// Saves given path to the database
34 /// </summary>
35 /// <param name="path"></param>
36 /// <returns>Leaf node ID</returns>
37 public int SavePath( string path )
39 IEnumerable<string> getTokens = GetTokens( path );
41 int parentId = 1;
42 bool addingNodes = false;
44 foreach ( string token in getTokens )
46 if ( addingNodes == false )
48 parentId = FindNode( token, parentId );
49 if ( parentId == -1 )
51 addingNodes = true;
55 if ( addingNodes )
57 parentId = AddNode( token, parentId );
61 return parentId;
64 /// <summary>
65 /// Adds new node to the database.
66 /// </summary>
67 /// <param name="nodeName"></param>
68 /// <param name="parentId"></param>
69 /// <returns>Added node ID</returns>
70 private int AddNode( string nodeName, int parentId )
72 DbCommand cmd = _connection.CreateCommand();
73 cmd.CommandText = "proc_add_node";
74 cmd.CommandType = CommandType.StoredProcedure;
77 throw new NotImplementedException();
80 /// <summary>
81 /// Gets an ID of a node with name given and withc is a child node of
82 /// given parent.
83 /// </summary>
84 /// <param name="nodeName"></param>
85 /// <param name="parentNodeId"></param>
86 /// <returns></returns>
87 private int FindNode( string nodeName, int parentNodeId )
89 throw new NotImplementedException();
92 /// <summary>
93 /// Split given path into separate tokens (directory and file names).
94 /// </summary>
95 /// <param name="path"></param>
96 /// <returns></returns>
97 private static IEnumerable<string> GetTokens( string path )
99 return path.Substring( 1 ).Split( new[] { '/', '\\' } );
102 public void Dispose()
104 if ( _connection.State != ConnectionState.Closed )
108 _connection.Close();
110 catch ( DbException ex )
112 Trace.WriteLine( ex.Message, "Closing connection" );