Call Stack in SAP Hana - multithreading

Are there any table/view/procedure where we access the call stack in a procedure?
For instance, I'm calling procedure B in procedure A and I would like to get which procedure is calling procedure B.
Thank you.

Nope, there is nothing that directly would provide you with this sort of call-hierarchy. The main reason for that is, that even though you call a procedure/function from another procedure and so on, SAP HANA will try and unfold these procedures to make up the optimal minimal data flow graphs.
That means (depending on your procedure coding), parts of procedures may be merged into a single SQL statement or compiled into a HANA internal language.
The closest thing for you to understand the actual call hierarchy will be to use the PlanViz visualisation on the top-most procedure/function call.
If you want to use the caller information actively in your code, you might want to think about using session variables to keep that information.

Related

Entity Framework database first approach using stored procedures

I am looking to build the data access layer of my MVC5 application. In our project we are going for database first approach with stored procedures only as team is more conversant with SQL and would like to perform all CRUD operations via stored procedures.
I am looking for good examples that show the implementation of this approach. I want to see how the entities are mapped. As this would be stored procedures in the database getting mapped to classes in .net.
I think its time for your team to become "conversant" with EF if you are going to use it. Doing every single CRUD operation with stored procedures is not the path I would take. If the stored procedure is doing something simple as:
Get the company record with ID 1
Then I would not use stored procedure and use EF. For more complex operations, stored procedures can be used. Therefore, you and your team may want to have team work session to decide on when to use stored procedures and when not to. Once you have decided, the whole team should stick to that approach. If you need to change it, have another meeting and make sure everyone is in the know. It is important for everyone to follow the same pattern once the team has agreed to it.
How to use stored procedures with EF?
I would start with one test stored procedure to see how the whole thing works. Once you and your team know exactly how the process works with EF, then put together a design, conventions etc. and then the whole team should follow the same pattern.
Write a test stored procedure which returns a resulset.
Create the EDMX by connecting to your database from Visual Studio.
Add the stored procedure to your EDMX.
Use the model browser to add a Function Import. This will create a method in your context which you can call like any other method, but underneath it will call your stored procedure. Please see this answer for more on how to do this step.
Step 4 will create a class based on your stored procedure's resultset.
Note
You may need to set this flag to off, TEMPORARILY, for EF to create the complex type based on your stored procedure result set.
SET FMTONLY OFF
See this answer for more about the flag.

How to execute "Stored Procedure" Query in node-oracledb if we are not aware of stored procedure bind parameters?

All the examples in documentation were given with bind variables.But what if, we are going to execute the query(Stored Procedure) which has written by the user.(In this case we will not be aware of what are all the input and output parameters to bind).
I am able to execute all the basic ddl and dml queries. But how to execute stored procedure like queries and what will be the way of retrieving?
Will there be any luck if we use "db-oracle"?
Note: I am new to nodejs and node-oracle-db
Have a look at the following examples:
https://github.com/oracle/node-oracledb/blob/master/examples/plsqlfunc.js
https://github.com/oracle/node-oracledb/blob/master/examples/plsqlproc.js
Also, I don't understand why you would not be aware of the input and output parameters to bind to. It would have to be a VERY dynamic situation for that to be true. It would be similar to saying: we don't know the names of the columns of the table we need to query. I'm not saying it doesn't happen or that there aren't unusual circumstances where this could be an issue, just that it's highly unusual.
In either case, whether you don't know the inputs and outputs of stored procedures or even if you don't know the names of the columns, that's where data dictionary views come in. Try running the following queries to begin exploring the views that may be relevant to you:
For procedures:
select *
from all_procedures;
For arguments:
select *
from all_arguments;

Putting permission and authorization logic in the database

I have a conceptual/architectural question I'd like to get some input on. Before I go into the details, I'd like to mention that I am well-aware of the arguments against putting application logic into the database, and the importance of maintaining abstraction and separation of concerns.
That being said, the application in question is a fairly simple one, in which performance is relatively more important than best practices. It is a new app, built with very modern technologies but on old school principles (stored procs, no ORM, etc).
I have a fairly complex "summary view" which is going to be driven by data provided by a stored procedure. Most of the elements of this view are going to have permission logic (not trivial, but nothing too complex) which will change both the appearance as well as the nature of the data based on the permissions of the currently logged in user (i.e. some data could be anonymized, other could be hidden, etc).
All data, as well as membership and ACL records, are stored in the same database.
So, the question is where to put the logic of applying user rights. The two options are:
1) Bring back all relevant data from the database into domain objects, then apply permissions in the middle-tier
2) Pass a user ID to the stored proc, and have it pass back an already prepared result to the middle-tier
At first look, conceptually, the no-brainer seems to be to throw it into the middle-tier (1st option) and leave the database concerned with what it does best - reading and writing data. However, the stored procedure will already be "tailored" to the specific view (think something along the lines of a report), and not used for anything else; thus, it just seems easier and lighter to process the permissions inside of the stored proc and bring back a prepared result with all the permissions already applied (a result that would have less data), rather than bringing back ALL the data into the middle tier and processing permissions there (eventually to discard half of the data anyway).
I am a bit torn and would appreciate some input. Intuitively, the 2nd option seems like a better fit, but "feels" very wrong.
Design the stored procedure to accept privileges enabled or features to use, e.g.
PROCEDURE retrieve_data (id_or_other_params,
anonymize := FALSE,
hide := FALSE,
some_other_feature := FALSE);
Procedure concerns now data retrieval only, with modifying options read from external source. Now let middle tier authenticate the user, decide which options to use and pass them to the procedure.

Code generation against Sprocs?

I'm trying to understand choices for code generation tools/ORM tools and discover what solution will best meet the requirements that I have and the limitations present.
I'm creating a foundational solution to be used for new projects. It consists of ASP.NET MVC 3.0, layers for business logic and data access. The data access layer will need to go against Oracle for now, and then switch to SQL this year as the db migration is finished.
From a DTO standpoint mapping to custom types in the solution, what ORM/code generation tool will work with creating my needed code but can ONLY access Stored Procs in Oracle and SQL.?
Meaning, I need to generate the custom objects that are the artifacts from and being pushed to the stored procedures as the parameters, I don't need to generate the sprocs themselves, they already exist. I'm looking for the representation of what the sproc needs and gives back to be generated into DTOs. In some cases I can go against views and generate DTOs. I'm assuming most tools already do this. But for 90% of the time, I don't have access directly to any tables or views, only stored procs.
Does this make sense?
ORMs are best at mapping objects to tables (and/or views), not mapping objects to sprocs.
Very few tools can do automated code generation against whatever output a sproc may generate, depending on the complexity of the sproc. It's much more straight-forward to code generate the input to a sproc as that is generally well defined and clear.
I would say if you are stuck with sprocs, your options for using third party code to help reduce your development and maintenance time are severely limited.
I believe either LinqToSql or EntityFramework (or both?) are capable of some magic with regards to SQL Server to try to mostly automatically figure out what a sproc may be returning. I don't think it works all the time, it's just sophisticated guess work and I seriously doubt it would work with Oracle. I am not aware of anything else software-wise that even attempts to figure out what a sproc may return.
A sproc can return multiple diverse record sets that can be built dynamically by the sproc depending on the input and data in the database. A technical solution to automatically anticipating sproc output seems like it would require the following:
A static set of underlying data in the database
The ability to pass all possible inputs to the sproc and execute the sproc without any negative impact or side effects
That would give you a static set of possible outputs for any given valid input. A small change in the data in the database could invalidate everything.
If I recall correctly, the magic Microsoft did was something like calling the sproc passing NULL for all input parameters and assuming the output is always exactly the first recordset that comes back from the database. That is clearly an incomplete solution to the problem, but in simple cases it appears to be magic because it can work very well some of the time.

A stored procedure returning string values

I'm wondering if this is a good practice or not. I have a database with a few stored procedures which I run using C# application.
Is it a good practice to return messages from stored procedures which can directly be shown to the application user?
For example:
CREATE PROCEDURE CheckParam
#Param int,
AS
BEGIN
IF (#Parem > 0)
BEGIN
SELECT 'Parameter is greater than 0'
END
ELSE SELECT 'Parameter is smaller than 0'
END
It would generally be considered best practice to have the code layer set the message text as this allows future maintainers of the code to clearly follow the intended logic.
No it's not. It's an awful practice. What if you'll need to support multiple languages or multiple display formats. Beside that, when project gets larger you'll start being confused when some part of presentation logic resides on DATABASE!!! and some parts on client side. Just return the DATA from DATABASE and nothing more.
It would be easier if you had a better example; the one you used would certainly impose a level of performance overhead that I wouldn't be happy with!
Typically, SQL Functions are better suited to returning 'scalar' values. I suggest you get familiar with those.

Resources