> which hdbsqlexport PATH=$PATH:/usr/sap/hdbclient
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/sap/hdbclient> hdbsql -v> ls -lR /usr/sap/hdbclient/ruby/
total 8
dr-xr-xr-x+ 2 root root 4096 Mar 27 17:30 activerecord
dr-xr-xr-x+ 2 root root 4096 Mar 27 17:30 hanaclient
/usr/sap/hdbclient/ruby/activerecord:
total 80
-r--r--r-- 1 root root 79872 Mar 13 18:46 activerecord-hanaclient-adapter-2.3.74.gem> ruby -v> sudo gem install /usr/sap/hdbclient/ruby/hanaclient/hanaclient-2.3.74-x86_64-linux.gemCheck that the gem is properly installed. It should appear in the list of gems:
> gem list
*** LOCAL GEMS ***
...
hanaclient (2.3.74 x86_64-linux)
...require 'hanaclient'
include HANACLIENT
# Create and initialize an interface
@api = HANACLIENT::HANACLIENTInterface.new()
HANACLIENT::API.hanaclient_initialize_interface(@api)
@api.hanaclient_init()
# Create and set up new connection
@conn = @api.hanaclient_new_connection()
status = @api.hanaclient_connect( @conn,"ServerNode=server:3XX13;DATABASENAME=databasename;UID=USER;Pwd=Password")
# Check the status
# Display an error message in case the connection attempt failed, that is status=0
if status == 0
print "Connection failed: status = #{status}\n"
msg = @api.hanaclient_error( @conn )
print "Message=#{msg}\n"
exit
else
puts "Connection succeeded: you are connected to the database"
end
# Disconnect from the database and free the connection resources
@api.hanaclient_disconnect(@conn)
@api.hanaclient_free_connection(@conn)
@api.hanaclient_fini()
HANACLIENT::API.hanaclient_finalize_interface(@api)def create_table()
# Create a table
str = <<-EOC
CREATE TABLE "Product"
("ID" integer NOT NULL,
"Name" varchar(15) NOT NULL,
"Description" varchar(30) NOT NULL,
"Size" varchar(18) NOT NULL,
"Color" varchar(18) NOT NULL,
"Quantity" integer NOT NULL,
PRIMARY KEY("ID")
)
EOC
@api.hanaclient_execute_immediate(@conn, str)
puts "Created table 'Product'"
end
create_table()def insert_fixed_row()
@api.hanaclient_execute_immediate(@conn,<<-end_sql)
INSERT INTO "Product" VALUES
311,
'Jacket',
'Winter',
'Small',
'Red',
54
)
end_sql
# Commit the changes
@api.hanaclient_commit(@conn)
puts "Row inserted"
enddef update_row(size, id)
sql = <<-end_sql
UPDATE "Product"
SET "Size" = ?
WHERE "ID" = ?
end_sql
stmt = @api.hanaclient_prepare(@conn, sql)
# Describe the parameters
rc, param_size = @api.hanaclient_describe_bind_param(stmt, 0)
rc, param_id = @api.hanaclient_describe_bind_param(stmt, 1)
# Set the values
param_size.set_value(size)
param_id.set_value(id)
# Bind the parameters
rc = @api.hanaclient_bind_param(stmt, 0, param_size )
rc = @api.hanaclient_bind_param(stmt, 1, param_id )
rc = @api.hanaclient_execute(stmt)
# Commit the changes
@api.hanaclient_commit(@conn)
puts "Updated the row"
enddef query_products()
sql = <<-end_sql
SELECT "Name", "Size" FROM "Product"
end_sql
stmt = @api.hanaclient_execute_direct(@conn, sql)
num_rows = @api.hanaclient_num_rows(stmt)
for counter in 1..num_rows
@api.hanaclient_fetch_next(stmt)
rc, name = @api.hanaclient_get_column(stmt, 0)
rc, size = @api.hanaclient_get_column(stmt, 1)
print "The item retrieved is a ", name, " size ", size, "\n"
end
enddef create_stockentry_proc()
sql = <<-end_sql
CREATE OR REPLACE PROCEDURE STOCKENTRY(IN product_id INT)
LANGUAGE SQLSCRIPT AS
BEGIN
UPDATE "Product"
SET "Product"."Quantity" = "Product"."Quantity" - 1
WHERE "Product"."ID" = product_id ;
END
end_sql
@api.hanaclient_execute_immediate(@conn, sql)
end
def call_stockentry_proc(product_id)
stmt = @api.hanaclient_prepare(@conn, "CALL STOCKENTRY(?)")
# Describe the parameter
rc, product_id_param = @api.hanaclient_describe_bind_param(stmt, 0)
# Set the values
product_id_param.set_value(product_id)
# Bind the parameters
rc = @api.hanaclient_bind_param(stmt, 0, product_id_param)
# Call the procedure
rc = @api.hanaclient_execute(stmt)
# Commit the changes
@api.hanaclient_commit(@conn)
# Free statement resources
@api.hanaclient_free_stmt(stmt)
endYou must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 47 | |
| 21 | |
| 18 | |
| 16 | |
| 15 | |
| 14 | |
| 13 | |
| 13 | |
| 13 | |
| 13 |