Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Adding methods to an object

Adding methods to an object

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Blainer   (191 posts)  Bio
Date Sat 13 Jun 2009 02:19 AM (UTC)

Amended on Sat 13 Jun 2009 03:04 AM (UTC) by Blainer

Message
If I have:

	assert (package.loadlib("sqlite3.dll","luaopen_luasql_sqlite3")) ()
	aard_db = assert(sqlite3.open_memory("aard"))

And I want to add:

	function aard_db:test(arg)
		self.nrows(arg)
		return something
	end

So I can do this:

	aard_db:test("something")


How do I do it? Can I create a class called aard_db and add the methods from sqlite3.dll?



Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 14 Jun 2009 09:17 PM (UTC)
Message
It's not going to be too easy. The variable aard_db is a userdata, not a table, and thus you can't add more things to it. A userdata is just a 4-byte pointer to an internal structure used by the database routines.

The userdata aard_db already has a metatable as well, so you can't just add one. I experimented with something like this:


mt = getmetatable (aard_db)


mt.__index.test = function (arg)
  print "in test"
  return self.nrows (arg)
end -- function


This indeed lets you add "test" as a function for the metatable in aard_db, however it doesn't know the self variable. Maybe you can get something along those lines to work, I'm not sure.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Erendir   Germany  (47 posts)  Bio
Date Reply #2 on Sun 14 Jun 2009 11:27 PM (UTC)
Message
just use this code:
mt.__index.test = function (self, arg)
  print "in test"
  return self.nrows (arg)
end -- function

or this:
function mt.__index:test(arg)
  print "in test"
  return self.nrows (arg)
end -- function
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Sun 14 Jun 2009 11:38 PM (UTC)

Amended on Sun 14 Jun 2009 11:40 PM (UTC) by Nick Gammon

Message
That gave this error:


Run-time error
World: smaug
Immediate execution
[string "Immediate"]:9: bad argument #1 to 'nrows' (:sqlite3 expected, got string)
stack traceback:
        [C]: in function 'nrows'
        [string "Immediate"]:9: in function 'test'
        [string "Immediate"]:13: in main chunk


However a slight modification seemed to work:


db=sqlite3.open(GetInfo (82))  -- open MUSHclient prefs file

mt = getmetatable (db)

mt.__index.test = function (self, arg)
  print "in test"
  return self:nrows (arg)
end -- function

require "tprint"

for row in db:test("SELECT * FROM sqlite_master") do
  tprint (row)
end

db:close()  -- close


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #4 on Sun 14 Jun 2009 11:39 PM (UTC)
Message
Quote:

assert (package.loadlib("sqlite3.dll","luaopen_luasql_sqlite3")) ()


SQLite is built into MUSHclient these days, you don't need this line.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


18,080 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.