This seems to be a duplicate of the other question with the same title, but it actually isn't.
We have our business logic implemented mostly as DB2 stored procedures (I see that H2 has a DB2-compatibility mode - nice!).
How can we use H2 for in-memory unit testing with these procedures?
Unfortunately H2 seems to lack the CREATE PROCEDURE command from its grammar.
I don't want to use Java functions as stored procedures. It would be best if the very same sql files could be used for testing and production as well... am I asking too much?
EDIT: we also use SQL cursors... again, no sign of support :-(
Unfortunately, the compatibility mode doesn't go as far as supporting SQL prodecures. Currently, the only solution is to use Java functions. SQL cursors are also not supported, sorry. But I will add these feature requests to the roadmap. Patches are welcome of course :-)
Related
Are there any libraries, open source or otherwise, that can be installed into a SQL Server instance (2008 or later) that can enforce AOP standards? I'd really like to avoid enforcing cross-cutting concerns with templates across our development staff. AOP seems like the best option, if it's available.
If it doesn't exist already, I'll try to roll my own.
EDIT:
Some examples might be subclassing Table to make specific kinds of tables, like mixin characteristics. I'm in a data warehouse environment with a lot of audit requirements so we create bitemporal tables a lot. It would be awesome to have a
CREATE BITEMPORAL TABLE
statement that would add transaction and valid time and modify CRUD statements against those tables. (Yes, I know that views and triggers can do this, somewhat.) A harder thing to accomplish would be stored procedures with specific logging or transaction characteristics, like
CREATE PROC FOO /* VERBOSE, ATOMIC, SERIALIZABLE */
and have the body automatically wrapped with the appropriate T-SQL to do those things. Yes, it's possible to add stored procedures to take those arguments and do SQL generation and compile those artifacts. But the drawback is that there's no enforcement - a developer may bypass the procedure and use CREATE PROC directly - and that the content in syscomments is the generated code, not the AOP annotated version, which breaks the abstraction.
Maybe you are interested in having a look at AO4SQL - a programming language that brings AO concepts to SQL. Conceptually, the tool works with any SQL server.
You can download my paper "AO4SQL: Towards an Aspect-Oriented Extension for SQL" that was published at the RAM-SE 2011: http://www-users.cs.york.ac.uk/~manuel/Events/RAM-SE11/RAMSE11papers.zip
Keep in mind that AO4SQL is a prototype tool, but if you would likt to join an open source project ... get in contact with me.
Interesting. I've never even thought about applying AOP techniques to a SQL Server.
With SQL Server 2008, I believe you have the ability to call .NET code, so you could maybe work in a standard AOP tool like PostSharp or Castle DynamicProxy that way.
I am developing a data driven website and quite a lot of programming logic resides in database stored procedures and database functions. I found myself changing the stored proc/functions quite a lot in order to fix bugs or add new functionality. The data (tables) have remained mostly untouched.
The issue I am having is keeping track of versions of stored proc/functions. Currently I am incrementing version of whole database when I do a set of changes. As data is huge (10 Gb) I get issues having to run development version and release versions of databases in parallel.
I wish to put all the stored procs and functions in one database and keep data in one database, so that I can better manage the changes.
I am sure others would have encountered similar suggest and request suggestions on how to best handle this situation.
I would also recommend using source control keyword expansion in your stored procedures ($Version:$)
That way you can eyeball, grep, search syscomments, etc to see what version you have on your deployed database.
You can version just the schema dumps. In combination with source control keword expansion (as suggested by Rawheiser), you just take a look at what version you have in the database, generate a diff and apply it.
Also, there are several excellent tools to compare databases and their schemas, generate DDL scripts etc.: SQL Workbench, Power Architect, DDLUtils and Redgate SQL Compare, to name a few. SQL Compare is likely to work best with SQL Server, although all the others are FOSS and provide a higher ROI (in terms of time spent learning and what you can do with them) as they are platoform and RDBMS independent.
Finally, I have to say...I understand that the immediate results you get with logic in the DB are tempting, but if you've gone beyond more than a couple of procedures in the database, you're setting your self up for quite a lot of pain, sifting through what easily turns into spaghetti code and locking your application to a single database vendor. You might have your reasons, but I've been there and didn't like it very much. Logic can live very nicely in a different layer.
For source control you have several options:
Use a Visual Studio Database project.
Use SQL Server 2005's built-in support for source control
Use a third part tool such as SQL Compare
IMO Option 1. is preferable.
I am looking to insert and update records in a database using functions and logic that are not available in SQL Server or any other RDBMS for that matter. After Googling around a bit this morning, I have come across the concept of Extended Stored Procedures. As far as I can tell, I should be able to compile my desired functionality into a dll, make a stored proc utilizing that dll to do the inserting/updating.
However, most of the articles and examples I have come across are somewhat dated (~2000). Are extended stored procedures still an acceptable practice? I am far from an expert in this area, so any other suggestions or comments would be greatly appreciated.
If you're using SQL Server 2005 or later, SQL CLR is the area to look at. You can call .NET code from within SQL Server.
This article on MSDN is a good place to start.
Are extended stored procedures still
an acceptable practice?
No, they are officialy deprecated and will be dicontinued in a future release. See Deprecated Database Engine Features in SQL Server 2008 , in the Features Not Supported in a Future Version of SQL Server table:
Extended stored procedure programming: Use CLR Integration instead.
I usually recommend against using CLR procedures, in most cases you can refactor the problem you are facing, into something that Transact Sql can handle.
Of most concern is the procedural approach that often accompanies the use of CLR procedures, when a relation database performs best when performing set based operations.
So the first question I always ask, is there anyway to refactor the problem into a set based operation.
If not, then I ask why would you want to execute the code inside of the database server, instead of in an application layer? Think about the performance impact you might have by placing the logic inside the database. (This might not be an issue if your db server has plenty of extra processing time).
If you do go head with CLR procedures, I think they are best applied to intensive calculations and complex logic.
I'm developing an asp.net application with Database factory pattern which allows the application to support both Sql Server and Oracle. I've created an abstract class that has the methods common to Sql Server and Oracle, like the CreateConnection and CreateCommand methods. This class is implemented by SqlServer and Oracle classes. Now, is there an easy way to write in-line sql queries with parameters common to both Sql Server and Oracle. I mean, I understand that we use "#" symbol in Sql Server and ":" in Oracle for parameters. Just for this reason, I'm writing queries twice in each of the class. Is there a way to write such queries common to both the databases? (or interpret the parameters from one common query?)
Thanks.
The only way to write one query that will work for both Oracle and Sql Server is to use only the syntax that is common to both platforms. Once you use features that are different between the two languages (like parameters or joins), you either have to write two different queries or hack together a "translator" class that converts a query from one platform to the other.
I've done a lot of this type of programming (database-agnostic software), and with .Net a relatively pain-free way of doing this is to write your main application to work entirely with ADO.Net DataTables/DataSets, with a wrapper class that handles generating the DataTables from either Oracle or Sql Server tables under-the-hood, and also handles persisting changes made to the DataTables back into Oracle or Sql Server. This approach isolates your DB-specific code in one place, although it's not necessarily a viable approach if the data your application needs access to is large.
You could write some kind of translator, but I would suggest that in some cases you'll need to write db-specific code for performance reasons anyway, so you'll have to put up with the maintenance burden of two versions of some queries.
What is the point of using ORACLE and not using all its non standard functions (analytics, pivots etc) ? ORACLE is a powerful tool.
Other DBs have there own strenght also, so why use the lowest common denominator just to be able to work on ALL of them? You will just lose in performance.
Just pick one DB, and use it fully with all its functionalities !
Pardon my ignorance here, but can't something like an ORM (object relational mapper) work for both SQL and Oracle?
I had similar requirements, to support both Sql Server and Oracle, and summarized my two years of experience with such problems in these articles:
Writing ANSI Standard SQL is not practical.
Think ANSI Standard SQL Is Fully Portable Between Databases? Think Again.
I'm currently planning the infrastructure for my future web project. I want to go the way Joel went with having one DB per client and now thinking which DB engine will be good for me. The best would be of course SQL Server, but I can't afford a full-blown version at this moment and I don't think SQL Server Express will be a good choice for the loaded service. Now I'm thinking of using PostgreSQL instead. Given that my development environment will be ASP.NET 3.5 with say NHibernate or LINQ to SQL, how much trouble will I have if I use PostgreSQL instead of SQL Server?
Thanks!
NHibernate works OK with PostgreSQL (whether the db is on Windows or UNIX-like OSes) and .NET works well with it using the Npgsql db provider.
The only "trouble" you'll get is of course PostgreSQL doesn't do T-SQL. In fact its PL/pgSQL stored proc language is closer to Oracle's PL/SQL than it is to MS SQL Server's T-SQL. So you'll have to recode your stored procs, and there will be some gotchas to watch out for if you do ADO.NET. If you use NHibernate, you probably won't have to worry much about that. No LINQ to SQL though, so tough luck for you.
PostgreSQL is scalable and works OK now with Windows (earlier versions didn't support Windows formally), and pgAdmin is a good management tool for it, you'll be able to do most of the stuff you can do with SQL Server's GUI tools with it in a short time.
I don't think it is a bad idea, but a great experience.
By the way NHibernate is the way to go Linq to Nhibernate is under heavy development and available in the trunk so if you do care "which I don't care" about Linq don't be scare to use it.
Why not start with SQL Server Express and migrate when you have the money? That way you can move toward what you consider ideal and reduce conversion costs.
If you go with PostgreSQL you won't be able to use LINQ to SQL. Currently LINQ only works with SQL Server (possibly Oracle). I'm not sure about NHibernate. Also, if you use PostgreSQL, last time I checked, they had dropped windows support. So you'll be looking into having a second box running Linux for the DB.
[EDIT]
It turns out PostgreSQL is supported on windows. I can't recall where I saw support being cancelled. Anyway, I've heard it runs better on Linux anyway, so you might want to look into doing that regardless.
These days,postgres works really fast with .net and it is as good or even better than the proprietary mssql