/// <summary> /// Gets a new instance of A with the given properties. /// </summary> /// <param name="id">Id</param> /// <param name="b">B</param> /// <param name="ensureInDb">Ensure in db or not, by default it is true</param> /// <returns></returns> protected A GetA(int id, B b = null, bool ensureInDb = true) { A entity; if (b == null) { entity = new A { Id = id, Name = string.Format("A_{0}", id), B_Id = id, B_Id2 = 1 }; } else { entity = new A { Id = id, Name = string.Format("A_{0}", id), B_Id = b.Id, B_Id2 = b.Id2 }; } if (ensureInDb) { using (var ctx = new TdsContext()) { entity.Ensure(ctx); } } return(entity); }
public bool StandardQueryHandler(TdsContext ctx) { var res = ctx.Response; switch (ctx.Request.Query) { case FirstQuery: // first table res.NewTable(4) .Column(0, "DatabaseEngineType", typeof(int), 4) .Column(1, "DatabaseEngineEdition", typeof(string), 30) .Column(2, "ProductVersion", typeof(string), 30) .Column(3, "MicrosoftVersion", typeof(int), 4); res.NewRow() .ColumnData(0, 1) .ColumnData(1, "2") .ColumnData(2, "11.0.7493.4") .ColumnData(3, 184556869); // second table res.NewTable(3) .Column(0, "host_platform", typeof(string), 7); res.NewRow() .ColumnData(0, "Windows"); // third table res.NewTable(1) .Column(0, "ConnectionProtocol", typeof(string), 3); res.NewRow() .ColumnData(0, "TCP"); res.Done(); return(true); } return(false); }
public static TDSRET try_tds_login(ref TdsLogin login, out TdsSocket tds, bool verbose = false) { tds = null; if (!login.SetPasswd(PASSWORD) || !login.SetUser(USER) || !login.SetApp("app") || !login.SetHost("myhost") || !login.SetLibrary("TDS-Library") || !login.SetServer(SERVER) || !login.SetClientCharset(CHARSET) || !login.SetLanguage("us_english")) { return(G.TDS_FAIL); } if (verbose) { Console.WriteLine("Connecting to database"); } test_context = new TdsContext(); tds = test_context.AllocSocket(512); var connection = tds.ReadConfigInfo(ref login, test_context.Locale); if (connection == null || tds.ConnectAndLogin(connection) != G.TDS_SUCCESS) { if (connection != null) { TdsContext.FreeSocket(tds); tds = null; Tds.FreeLogin(connection); } return(G.TDS_FAIL); } Tds.FreeLogin(connection); return(G.TDS_SUCCESS); }
/// <summary> /// Gets a new instance of C with the given properties. /// </summary> /// <param name="id">Id</param> /// <param name="id2">Id2</param> /// <param name="a">A</param> /// <param name="d">D</param> /// <param name="ensureInDb">Ensure in db or not, by default it is true</param> /// <returns></returns> protected C GetC(int id, int id2, A a, D d = null, bool ensureInDb = true) { C entity; if (d == null) { entity = new C { A_Id = a.Id, Id = id, Id2 = id2, Name = string.Format("C_{0}_{1}_{2}", a.Id, id, id2), D_Id = id, D_Id2 = 1 }; } else { entity = new C { A_Id = a.Id, Id = id, Id2 = id2, Name = string.Format("C_{0}_{1}_{2}", a.Id, id, id2), D_Id = d.Id, D_Id2 = d.Id2 }; } if (ensureInDb) { using (var ctx = new TdsContext()) { entity.Ensure(ctx); } } return(entity); }
/// <summary> /// Deletes the given entity from the database /// </summary> /// <typeparam name="T">Entity type</typeparam> /// <param name="entity">Entity instance</param> protected void DeleteEntity <T>(T entity) where T : class { using (var ctx = new TdsContext()) { ctx.DeleteEntity(entity); } }
/// <summary> /// Modifies all non key properties of the given objeD. /// </summary> /// <param name="entity"></param> protected void Alter(D entity) { entity.Name += "modified"; using (var ctx = new TdsContext()) { ctx.EnsureEntity(entity, entity.GetCheckIfExistsExpression()); } }
public void InitializeTest() { using (var ctx = new TdsContext()) { ctx.TruncateAllTables(); } foreach (var filePath in Directory.GetFiles(TestSettings.Storage.EntitiesLocation)) { File.Delete(filePath); } }
public static TDSRET try_tds_logout(TdsLogin login, TdsSocket tds, bool verbose = false) { if (verbose) { Console.WriteLine("Entered tds_try_logout()"); } tds.CloseSocket(); tds.Dispose(); //TdsContext.FreeSocket(tds); login.Dispose(); // Tds.FreeLogin(login); test_context.Dispose(); // Tds.FreeContext(test_context); test_context = null; return(G.TDS_SUCCESS); }
/// <summary> /// Gets a new B instance with the given properties. /// </summary> /// <param name="id">Id</param> /// <param name="id2">Id2</param> /// <param name="ensureInDb">Ensure in db or not, by default it is true</param> /// <returns></returns> protected B GetB(int id, int id2, bool ensureInDb = true) { var entity = new B { Id = id, Id2 = id2, Name = string.Format("B_{0}_{1}", id, id2) }; if (ensureInDb) { using (var ctx = new TdsContext()) { entity.Ensure(ctx); } } return(entity); }
/// <summary> /// Gets a new instance of E with the given properties. /// </summary> /// <param name="b">B</param> /// <param name="d">D</param> /// <param name="id">Id</param> /// <param name="ensureInDb">Ensure in db or not, by default it is true</param> /// <returns></returns> protected E GetE(B b, D d, int id, bool ensureInDb = true) { var entity = new E { B_Id = b.Id, B_Id2 = b.Id2, D_Id = d.Id, D_Id2 = d.Id2, Id = id }; if (ensureInDb) { using (var ctx = new TdsContext()) { entity.Ensure(ctx); } } return(entity); }
public void QueryHandler(TdsContext ctx) { if (StandardQueryHandler(ctx)) { return; } var res = ctx.Response; res.NewTable(1) .Column(0, "content", typeof(string), 30); res.NewRow() .ColumnData(0, "content"); var info = res.Info; info.Columns[0].ColumnData = Marshal.StringToHGlobalAnsi("content"); res.Done(); }
/// <summary> /// Makes sure the given entity exists in the database with the desired properties. /// </summary> /// <typeparam name="T">Type of entity</typeparam> /// <param name="entity">Entity instance</param> /// <param name="ctx">Entity framework context instance</param> public static void Ensure <T>(this T entity, TdsContext ctx) where T : class, ISelfEnsuringEntity <T> { ctx.EnsureEntity <T>(entity, entity.GetCheckIfExistsExpression()); }
public void TestServer() { Tds.DumpOpen(@"C:\T_\dump.log"); void dump_login(TdsLogin login) { Console.WriteLine("host {0}", login.ClientHostName); Console.WriteLine("user {0}", login.UserName); Console.WriteLine("pass {0}", login.Password); Console.WriteLine("app {0}", login.AppName); Console.WriteLine("srvr {0}", login.ServerName); Console.WriteLine("vers {0}.{0}", login.TdsMajor(), login.TdsMinor()); Console.WriteLine("lib {0}", login.Library); Console.WriteLine("lang {0}", login.Language); Console.WriteLine("char {0}", login.ServerCharset); Console.WriteLine("bsiz {0}", login.BlockSize); } var abc = Marshal.SizeOf <TDSPOLLWAKEUP>(); var connection = Task.Run(() => { //Common.SERVER = "localhost"; //var login = new TdsLogin(false); //var ret = Common.try_tds_login(ref login, out var tds); //if (ret != G.TDS_SUCCESS) // throw new Exception("try_tds_login() failed"); //Common.run_query(tds, "Select * From Test;"); //Common.try_tds_logout(login, tds); using (var conn = new SqlConnection("Data Source=tcp:localhost,1433;Initial Catalog=Test;MultipleActiveResultSets=False;user=guest;pwd=sybase;Encrypt=false;trustservercertificate=false")) using (var com = new SqlCommand("Select * From Table", conn)) { conn.Open(); com.ExecuteNonQuery(); } }); //NativeMethods.tdsdump_log(@"C:\T_\dump.log", 1, $"A0: {tds.Value.state}\n"); using (var ctx = new TdsContext()) { //ctx.MsgHandler = (a, b, c) => //{ // return 0; //}; ctx.ErrHandler = (a, b, c) => { return(G.TDS_INT_CONTINUE); }; //ctx.IntHandler = (a) => //{ // return 0; //}; var tds = ctx.Listen() ?? throw new Exception("Error Listening"); //tds.Conn.Env.Language = "us_english"; //tds.Conn.Env.Charset = "ISO-8859-1"; //tds.Conn.Env.Database = "master"; using (var login = tds.AllocReadLogin(0x702) ?? throw new Exception("Error reading login")) { dump_login(login); if (true || (login.UserName == "guest" && login.Password == "sybase")) { tds.OutFlag = TDS_PACKET_TYPE.TDS_REPLY; //tds.EnvChange(P.TDS_ENV_DATABASE, "master", "pubs2"); //tds.SendMsg(5701, 2, 10, "Changed database context to 'pubs2'.", "JDBC", "ZZZZZ", 1); //if (!login.Value.suppress_language) //{ // tds.EnvChange(P.TDS_ENV_LANG, null, "us_english"); // tds.SendMsg(5703, 1, 10, "Changed language setting to 'us_english'.", "JDBC", "ZZZZZ", 1); //} //tds.EnvChange(P.TDS_ENV_PACKSIZE, null, "512"); tds.SendLoginAck("Microsoft SQL Server", G.TDS_MS_VER(10, 0, 6000)); if (G.IS_TDS50(tds.Conn.Value)) { tds.SendCapabilitiesToken(); } tds.SendDoneToken(0, 1); } else { return; // send nack before exiting } tds.FlushPacket(); } var query = tds.GetGenericQuery(); Console.WriteLine("query : {0}", query); tds.OutFlag = TDS_PACKET_TYPE.TDS_REPLY; if (false) { using (var resinfo = new TdsResultInfo(1)) { resinfo.Columns[0].ColumnType = TDS_SERVER_TYPE.SYBVARCHAR; resinfo.Columns[0].ColumnSize = 30; resinfo.Columns[0].ColumnName = "name"; resinfo.CurrentRow = Marshal.StringToHGlobalAnsi("pubs2"); resinfo.Columns[0].ColumnData = resinfo.CurrentRow; //var column = resinfo.Columns[0] = new TdsColumn //{ // ColumnType = TDS_SERVER_TYPE.SYBVARCHAR, // ColumnSize = 30, // ColumnName = "name", // ColumnData = resinfo.CurrentRow, //}; tds.SendResult(resinfo); tds.SendControlToken(1); tds.SendRow(resinfo); } } tds.SendDoneToken(16, 1); tds.FlushPacket(); //Thread.Sleep((int)(.5M * 1000M)); } }