[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Adding methods to an object
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Adding methods to an object

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


Posted by Blainer   (191 posts)  [Biography] 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?



[Go to top] top

Posted by Nick Gammon   Australia  (21,609 posts)  [Biography] 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
[Go to top] top

Posted by Erendir   Germany  (47 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,609 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,609 posts)  [Biography] 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
[Go to top] 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.


5,170 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]