import os,strutils,parseutils,strfmt
import "/Nimborg/high_level.nim"
# attempt to connect to a Firebird 2.5 Superserver database via
# python fdb driver module
let fdb = pyImport("fdb")
let services = pyImport("from fdb import services")
let connect = pyImport("from fdb import connect")
proc serverInfo() {.discardable.} =
let ahost = toPy("127.0.0.1")
let auser = toPy("sysdba")
let apassword = toPy("masterkey")
echo "Server Information"
echo "--------------------\n"
let con = fdb.services.connect(host=ahost, user=auser, password=apassword)
echo "Server Version : ", con.get_server_version()
echo "Firebird Vers. : ", con.get_server_version()
echo "Architecture : ", con.get_architecture()
echo "ServiceManger : ", con.get_service_manager_version()
echo "Home Directory : ", con.get_home_directory()
echo "Security Db Path : ", con.get_security_database_path()
echo "Lock File Path : ", con.get_lock_file_directory()
echo "Server capability : ", con.get_server_capabilities()
echo "Multi Client : ", services.CAPABILITY_MULTI_CLIENT in con.get_server_capabilities()
echo "Quoted Filename : ", services.CAPABILITY_QUOTED_FILENAME in con.get_server_capabilities()
echo "Message File Path : ", con.get_message_file_directory()
echo "Connection Count : ", con.get_connection_count()
serverInfo()
This is the end of the error message :
Nimborg/low_level.nim(1662, 5) Warning: use 'defer'; standalone finally' is deprecated [Deprecated] Nimborg/high_level.nim(213, 20) Hint: 'T' is declared but not used [XDeclaredButNotUsed] firebirdT3.nim(20, 25) Error: type mismatch: got (PPyRef, PPyRef, host: PPyRef, user: PPyRef, password: PPyRef) but expected one of: high_level.()(f: PPyRef, args: varargs[PPyRef])
Try removing the name part of the named parameters, like so:
let con = fdb.services.connect(ahost, auser, apassword)
Actually, this bring up the question, could Nim have something like 'dictargs' or 'tableargs', to emulate something like Python's '**args'? I guess you could do something with macros...
import os,strutils,parseutils,strfmt
import python
# connect to a Firebird 2.5 Superserver service via
# python fdb driver module using Nim python
const pycode = """
import fdb
from fdb import services
def serverInfo():
print from_nim # display a message received from nim side
con = fdb.services.connect(host='127.0.0.1', user='sysdba', password='masterkey')
sv = "Server Version : " + str(con.get_server_version())
sa = "Architecture : " + str(con.get_architecture())
sm = "ServiceManger : " + str(con.get_service_manager_version() )
hd = "Home Directory : " + str(con.get_home_directory())
sp = "Security Db Path : " + str(con.get_security_database_path())
lp = "Lock File Path : " + str(con.get_lock_file_directory())
sc = "Server capability : " + str(con.get_server_capabilities())
mc = "Multi Client : " + str(services.CAPABILITY_MULTI_CLIENT in con.get_server_capabilities())
qf = "Quoted Filename : " + str(services.CAPABILITY_QUOTED_FILENAME in con.get_server_capabilities())
mp = "Message File Path : " + str(con.get_message_file_directory())
cc = "Connection Count : " + str(con.get_connection_count())
con.close()
return (sv,sa,sm,hd,sp,lp,sc,mc,qf,mp,cc)
resx = serverInfo()
"""
Py_Initialize()
var mainModule = PyImport_ImportModule("__main__")
var mainDict = PyModule_GetDict(mainModule)
# send stuff to python side
var pyString = PyString_FromString("\nNim says Hello in Python\n")
discard PyDict_SetItemString(main_dict, "from_nim", pyString)
# run above python code
discard PyRun_SimpleString(pycode)
# now get stuff back from python side
echo "\nBack in Nim unpacking data received from python\n"
var pyVariable = PyMapping_GetItemString(mainDict, "resx")
echo "Server Information"
echo "--------------------------------------\n"
for x in 0.. PySequence_Length(pyVariable)-1:
var pyData = PySequence_GetItem(pyVariable,x)
var pyItem = PyString_AsString(pyData)
echo pyItem
Py_XDECREF(mainModule)
Py_XDECREF(pyVariable)
Py_Finalize()
echo "\n\n...and Bye Bye"
Output :
Nim says Hello in Python
Back in Nim unpacking data received from python
Server Information
--------------------------------------
Server Version : LI-V2.5.4.26856 Firebird 2.5
Architecture : Firebird/linux AMD64
ServiceManger : 2
Home Directory : /opt/firebird/
Security Db Path : /opt/firebird/security2.fdb
Lock File Path : /tmp/firebird/
Server capability : (2, 4, 512, 256)
Multi Client : True
Quoted Filename : False
Message File Path : /opt/firebird/
Connection Count : 0
...and Bye Bye