public long Count <T>(string sql, object parameterObject) { sql = SqlStore.GetSql(sql); sql = string.Format("select count(*) from ({0}) t_count", sql); IEnumerable <DbParameter> parameters = BuildParameters(parameterObject, ref sql); DbCommand command = _database.CreateCommand(sql, Connection); command.Transaction = Transaction; foreach (DbParameter dbParameter in parameters) { command.Parameters.Add(dbParameter); } List <DiscriminatorAttribute> attrs = typeof(T).GetAttributes <DiscriminatorAttribute>(false); if (attrs.Count == 0) { command.Parameters.Add(_database.CreateParameter("Discriminator", typeof(T).Name)); } else { command.Parameters.Add(_database.CreateParameter("Discriminator", null)); } return(Convert.ToInt64(command.ExecuteScalar() ?? 0)); }
public static void SQLDropDatabase() { string connectionString = string.Empty; using (SqlStore sqlStore = CreateSqlStore()) { connectionString = string.Format("server={0};Integrated Security=True", sqlStore.ServerName); sqlStore.DropDbSchema(); } // Drop database if (!string.IsNullOrEmpty(connectionString)) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); try { using (SqlCommand command = conn.CreateCommand()) { command.CommandText = string.Format("DROP DATABASE {0}", DatabaseName); command.ExecuteNonQuery(); } } catch (SqlException exc) { if (exc.ErrorCode != sqlErrDatabaseExists) { throw exc; } } } } }
public DataTable Table(string sqlName, object parameterObject, int skip, int take, string orderBy) { string sql = SqlStore.GetSql(sqlName); orderBy = orderBy.ToLower(); bool asc = true; if (orderBy.EndsWith("desc")) { orderBy = orderBy.Substring(0, orderBy.Length - 4); asc = false; } orderBy = orderBy.Replace(" ", ""); while (orderBy.IndexOf(" ") >= 0) { orderBy = orderBy.Trim(); } // select * //from ( // select row_number()over(order by tempColumn)tempRowNumber,* // from (select top 1510 tempColumn=0,* from u1club_bom_orders // order by orderid desc // )t //)tt sql = "select Convert(nvarchar(50), tt.rownum) rownumName, tt.* from (select row_number()over(order by {0}) rownum, * from(" + sql + ") t) tt where rownum between {1} and {2}"; sql = string.Format(sql, orderBy + (asc ? "" : " desc"), skip + 1, skip + take); return(Table(sql, parameterObject)); }
public SqlStoreTests() { var configuration = MockBuilder.BuildConfiguration(); _sqlStore = new SqlStore(new List <Type> { typeof(TestSqlStoreEntity) }, configuration.SqlStore, false); }
private Configuration() { _bus = new CommandBus(); var eventStore = new SqlStore(_bus); var repository = new DomainRepository(eventStore); //Registering my Commands to corresponding CommandHandlers var commandService = new UserCommandHandler(repository); _bus.RegisterHandler <CreateUserCommand>(commandService.Handle); _bus.RegisterHandler <UpdateUserCommand>(commandService.Handle); }
public long Count(string sql, object parameterObject) { sql = SqlStore.GetSql(sql); sql = string.Format("select count(*) from ({0}) t_count", sql); IEnumerable <DbParameter> parameters = BuildParameters(parameterObject, ref sql); DbCommand command = _database.CreateCommand(sql, Connection); command.Transaction = Transaction; foreach (DbParameter dbParameter in parameters) { command.Parameters.Add(dbParameter); } return(Convert.ToInt64(command.ExecuteScalar() ?? 0)); }
public int ExecuteNonQuery(string sqlName, object parameterObject, CommandType commandType = CommandType.Text) { string sql = SqlStore.GetSql(sqlName); IEnumerable <DbParameter> parameters = BuildParameters(parameterObject, ref sql); DbCommand command = _database.CreateCommand(sql, Connection); command.Transaction = Transaction; command.CommandType = commandType; foreach (DbParameter dbParameter in parameters) { command.Parameters.Add(dbParameter); } return(command.ExecuteNonQuery()); }
public static void SQLCreateDatabase(string databaseName, int version, IEnumerable <string> libraryNames) { using (SqlStore sqlStore = CreateSqlStore(databaseName)) { // Create database string connectionString = string.Format("server={0};Integrated Security=True", sqlStore.ServerName); using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); try { SqlCommand command = conn.CreateCommand(); command.CommandText = string.Format("CREATE DATABASE {0}", databaseName); command.ExecuteNonQuery(); } catch (SqlException exc) { // Ignore "Database already exists" error if (exc.ErrorCode != sqlErrDatabaseExists) { throw exc; } } } // Create Repository CachedRepository repository = new CachedRepository(); repository.Version = version; repository.Store = CreateSqlStore(databaseName); // Create project Project project = new Project(); project.AutoLoadLibraries = true; project.Name = "..."; project.Repository = repository; // Add and register libraries project.RemoveAllLibraries(); project.AddLibrary(typeof(ValueDevice).Assembly, true); foreach (string libName in libraryNames) { project.AddLibraryByName(libName, true); } project.RegisterEntityTypes(); // Create schema sqlStore.CreateDbCommands(repository); sqlStore.CreateDbSchema(repository); // Close project project.Close(); } }
public void FullTests() { var cacheKey = new CacheKey("http://google.com", new string[0]); var sqlStore = new SqlStore(); var httpClient = new HttpClient(); sqlStore.AddOrUpdate(cacheKey, httpClient.GetAsync("http://google.com").Result ); HttpResponseMessage responseMessage; Assert.IsTrue(sqlStore.TryGetValue(cacheKey, out responseMessage), "TryGetValue returned false"); Assert.IsNotNull(responseMessage, "responseMessage null"); Assert.IsTrue(responseMessage.Headers.Count() > 0); Assert.IsTrue(sqlStore.TryRemove(cacheKey)); }
public object ExecuteScalar(string sqlName, object parameterObject) { string sql = SqlStore.GetSql(sqlName); IEnumerable <DbParameter> parameters = BuildParameters(parameterObject, ref sql); DbCommand command = _database.CreateCommand(sql, Connection); if (Transaction != null) { command.Transaction = Transaction; } foreach (DbParameter dbParameter in parameters) { command.Parameters.Add(dbParameter); } return(command.ExecuteScalar()); }
public void GetDomains() { var cacheKey = new CacheKey("http://google.com", new string[] {"Accept"}); var cacheKey2 = new CacheKey("http://facebook.com", new string[] {"Accept"} ); var sqlStore = new SqlStore(); var httpClient = new HttpClient(); sqlStore.AddOrUpdate(cacheKey, httpClient.GetAsync("http://google.com").Result ); sqlStore.AddOrUpdate(cacheKey2, httpClient.GetAsync("http://facebook.com").Result ); var domainSizes = sqlStore.GetDomainSizes(); Assert.AreEqual(2, domainSizes.Count); }
public IEnumerable <T> Query <T>(string sqlName, object parameterObject) { string sql = SqlStore.GetSql(sqlName); IEnumerable <DbParameter> parameters = BuildParameters(parameterObject, ref sql); DbCommand command = _database.CreateCommand(sql, Connection); command.Transaction = Transaction; foreach (DbParameter dbParameter in parameters) { command.Parameters.Add(dbParameter); } List <DiscriminatorAttribute> attrs = typeof(T).GetAttributes <DiscriminatorAttribute>(false); if (attrs.Count == 0) { command.Parameters.Add(_database.CreateParameter("Discriminator", typeof(T).Name)); } else { command.Parameters.Add(_database.CreateParameter("Discriminator", null)); } using (DbDataReader reader = command.ExecuteReader()) { bool hasDiscriminator = HasDiscriminator(reader); while (reader.Read()) { Type type = typeof(T); if (hasDiscriminator) { foreach (DiscriminatorAttribute attr in attrs) { if (string.Equals(attr.Discriminator, reader["Discriminator"].ToString(), StringComparison.OrdinalIgnoreCase)) { type = attr.DiscriminatorType; } } } yield return((T)ReaderTranslator.Map(reader, Activator.CreateInstance(type))); } } }
public DataTable Table(string sqlName, object parameterObject) { string sql = SqlStore.GetSql(sqlName); var dt = new DataTable(); IEnumerable <DbParameter> parameters = BuildParameters(parameterObject, ref sql); DbDataAdapter adapter = _database.CreateDataAdapter(sql, Connection); DbCommand command = adapter.SelectCommand; if (Transaction != null) { command.Transaction = Transaction; } foreach (DbParameter dbParameter in parameters) { command.Parameters.Add(dbParameter); } adapter.Fill(dt); return(dt); }
public void GetDomains() { var cacheKey = new CacheKey("http://google.com", new string[] { "Accept" }); var cacheKey2 = new CacheKey("http://facebook.com", new string[] { "Accept" } ); var sqlStore = new SqlStore(); var httpClient = new HttpClient(); sqlStore.AddOrUpdate(cacheKey, httpClient.GetAsync("http://google.com").Result ); sqlStore.AddOrUpdate(cacheKey2, httpClient.GetAsync("http://facebook.com").Result ); var domainSizes = sqlStore.GetDomainSizes(); Assert.AreEqual(2, domainSizes.Count); }
public void TestClear() { var sqlStore = new SqlStore(); sqlStore.Clear(); }
private AdoNetStore GetAdoNetStore(out string projectName, OpenAdoNetRepositoryDialog.Mode mode) { projectName = string.Empty; AdoNetStore result = null; try { Cursor = Cursors.WaitCursor; Application.DoEvents(); using (OpenAdoNetRepositoryDialog dlg = new OpenAdoNetRepositoryDialog(this, DefaultServerName, DefaultDatabaseName, mode)) { if (dlg.ShowDialog() == DialogResult.OK && CloseProject()) { if (dlg.ProviderName == SqlServerProviderName) { result = new SqlStore(dlg.ServerName, dlg.DatabaseName); projectName = dlg.ProjectName; } else MessageBox.Show(this, "Unsupported database repository.", "Unsupported repository", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } finally { Cursor = Cursors.Default; } return result; }
public static Store CreateStore(RepositoryInfo repositoryInfo) { Store store = null; if (repositoryInfo.typeName == RepositoryInfo.XmlStoreTypeName) { store = new XmlStore(Path.GetDirectoryName(repositoryInfo.location), Path.GetExtension(repositoryInfo.location)); } else if (repositoryInfo.typeName == RepositoryInfo.SqlServerStoreTypeName) { store = new SqlStore(); ((SqlStore)store).DatabaseName = repositoryInfo.location; ((SqlStore)store).ServerName = repositoryInfo.computerName; } else { Debug.Fail(string.Format("Unsupported {0} value '{1}'", typeof(RepositoryInfo).Name, repositoryInfo)); } return store; }