⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 metakit quick and easy storage for your tcl application.mht

📁 TCL的数据库处理支撑库及一些示例
💻 MHT
📖 第 1 页 / 共 3 页
字号:
advantage of=20
the fact that this is the same format used by the Tcl "<CODE>array =
set</CODE>"=20
command. This allows you to store the contents of an entire row into a =
Tcl=20
array, and then use commands like "<CODE>info exists</CODE>", =
"<CODE>array=20
names</CODE>", etc. to examine the properties, as well as constructs =
like=20
"<CODE>$<I>arrayname</I>(<I>property</I>)</CODE>" to refer to a =
property. </P>
<P>Similarly, after you've read a row into an array and made some =
changes (or=20
constructed the array manually), you can write it back using a construct =
like=20
"<CODE>eval mk::set <I>db</I>.<I>viewname</I>!<I>rownum</I> [array get=20
<I>arrayname</I>]</CODE>". </P>
<H2>More Advanced Techniques</H2>
<P>This section will cover a few more advanced features of MetaKit that =
you'll=20
probably find yourself soon including in your programs, after you've =
mastered=20
the basics. </P>
<H3>Data Types</H3>
<P>Normally, every property you store in a view will be stored as a =
string. In=20
MetaKit, you don't have to predefine the lengths of the strings you'll =
store;=20
the data file will adjust to accommodate whatever length of strings you =
store.=20
</P>
<P>If you want, you can specify that some properties are stored not as =
strings,=20
but as other data types. This can be handy because it helps MetaKit =
store things=20
more efficiently. If all values assigned to a property are fairly small=20
integers, telling MetaKit that only integers will be stored in that =
property=20
makes for more efficient storage. As with strings, MetaKit will attempt =
to use=20
the smallest amount of storage possible. If you specify that a field =
will hold=20
integers, and you only ever store values of 0 or 1, MetaKit will pack =
this=20
integer property for eight different rows into a single byte. </P>
<P>Data types are specified by adding a "type specifier" to the name of =
the=20
property when you specify it in the <CODE>mk::view layout</CODE> =
command. You=20
can't have two properties with the same name but different type =
specifiers. For=20
example, the type specifier for an integer property is ":I", so you =
might use=20
something like: </P><PRE>mk::view layout db.employees "name employeeid:I =
salary"</PRE>
<P>Here is the list of valid data types and the corresponding type =
specifiers:=20
</P>
<TABLE cellPadding=3D5>
  <TBODY>
  <TR>
    <TD>:S </TD>
    <TD>A string property for storing strings of any size, but no null =
bytes=20
      (the default) </TD></TR>
  <TR>
    <TD>:I </TD>
    <TD>An integer property for efficiently storing values as integers =
(1-32=20
      bits) </TD></TR>
  <TR>
    <TD>:L </TD>
    <TD>A long integer property for storing values as 64 bit integers. =
</TD></TR>
  <TR>
    <TD>:F </TD>
    <TD>A float property for storing single precision (32 bit) floating =
point=20
      numbers. </TD></TR>
  <TR>
    <TD>:D </TD>
    <TD>A double property for storing double precision (64 bit) floating =
point=20
      numbers. </TD></TR>
  <TR>
    <TD>:B </TD>
    <TD>A binary property for untyped binary data which may include null =

      bytes. </TD></TR></TBODY></TABLE>
<H3>Changing Views</H3>
<P>One of the real strengths of MetaKit is that you can easily =
restructure data=20
that you've already stored in a data file. This is particularly handy =
given the=20
natural evolution of most programs, where requirements might change over =
time.=20
</P>
<P>To restructure a view, you simply open an existing data file, and =
issue a new=20
"<CODE>mk::view layout</CODE>" command that reflects the new structure =
you'd=20
like the view to have. If you specify new properties in the original =
view, they=20
will be added (with a default value, such as the empty string, =
assigned). If you=20
remove properties, they will be removed from the view in the data file. =
</P>
<H3>Subviews</H3>
<P>Sometimes you will want to attach multiple values of the same =
property values=20
to a row. For example, in an address book, you might want to record one =
or more=20
phone numbers for each entry. There are several ways that you might =
accomplish=20
this. A simple solution is to add properties like "homephone", =
"workphone",=20
"mobilephone" etc. to the main view. This would give you a fixed number =
of phone=20
numbers you could store with each entry, and for many entries most of =
these=20
might be empty. </P>
<P>A second approach is to define a completely separate view, with each =
row in=20
the new view containing a pointer to the corresponding entry in the =
address=20
book. For example, if there is a unique "addressid" property in the main =
address=20
book view, we might define a new view, add records, and search for all =
phone=20
numbers for a particular address book entry: </P><PRE>mk::view layout =
db.phones "addressid type number"
...
mk::row append db.phones addressid $addressid type home number 123-4567
mk::row append db.phones addressid $addressid type work number 987-6543
...
set rows [mk::select db.phones addressid $addressid]</PRE>
<P>MetaKit provides a third option, using a <I>subview</I> of the main =
view.=20
This allows you to store the phone numbers as a view within the same row =
you=20
store the rest of the address information. Like normal views, subviews =
contain=20
zero or more rows, each with the same set of properties, as defined in =
the=20
<CODE>mk::view layout</CODE> command. You can refer to rows in a subview =
by=20
extending the normal row syntax,=20
"<CODE>db.viewname!rownum.subview!subviewrownum</CODE>.." Here's an =
example of=20
how subviews can be used: </P><PRE># create the view, including a =
subview named phones
mk::view layout db.addresses {name city {phones {type number}}

# add a row (the first one, so it will be stored at index 0)
mk::row append db.addresses name Mark city Ancaster

# store two phone rows in the subview
mk::row append db.addresses!0.phones type home number 123-4567
mk::row append db.addresses!0.phones type work number 987-6543</PRE>
<P>You can use other commands like mk::select or mk::loop that operate =
on views=20
to operate on subviews as well. </P>
<P>If all you're doing is storing and retrieving data in a subview that =
is=20
associated with the parent row, subviews may be quicker and easier than =
using a=20
separate table. But if you plan on doing other searches (e.g. print out =
all=20
phone numbers), the separate table approach might be better. </P>
<H3>Deployment</H3>
<P>MetaKit data files are highly portable, so if you're moving your data =
file=20
between different platforms (e.g. Mac, Windows, Unix) you don't need to =
do=20
anything special to make your data file work on the new system. </P>
<P>When actually shipping your application, you can include MetaKit as a =
normal=20
Tcl extension via a shared library (e.g. mktcl.so or mktcl.dll), or =
build a=20
static library that you link directly into your executable. </P>
<P>You might also want to look at TclKit (see <A=20
href=3D"http://www.equi4.com/tclkit">http://www.equi4.com/tclkit</A>) =
which=20
bundles Tcl, Tk and MetaKit into a single platform-specific binary. You =
can then=20
bundle the rest of your application into a platform-neutral "scripted =
document".=20
Your users then need only download your scripted document and the TclKit =
binary=20
for their platform. This can save you some trouble in distributing your=20
applications on multiple platforms, particularly when you may not have=20
convenient access to each platform. </P>
<H2>Where to Go Next</H2>
<P>The official source for MetaKit information, including how to obtain =
and=20
install the latest versions, is the MetaKit web site, at <A=20
href=3D"http://www.equi4.com/metakit">http://www.equi4.com/metakit</A>. =
Make note=20
of the MetaKit mailing list, which can be a source of useful =
information. </P>
<P>What we've described here are the basic MetaKit commands you'll need =
in most=20
applications. There are many other commands and options you may find you =
could=20
take advantage of, and tricks that you may need as your data files grow =
large.=20
These can be found in the documentation included with the MetaKit =
distribution=20
(or available via the web site). In particular, you should consult the =
Tcl=20
reference manual at <A=20
href=3D"http://www.equi4.com/metakit/tcl.html">http://www.equi4.com/metak=
it/tcl.html</A>.=20
</P>
<H3>Acknowledgements</H3>
<P>Special thanks go of course to Jean-Claude Wippler for his =
never-ending and=20
tireless efforts in creating, extending, and supporting MetaKit, and for =
his=20
comments on this document. </P>
<H2>Appendix: Installing MetaKit</H2>
<P>If you're familiar with using Tcl and its extensions, then MetaKit =
should be=20
fairly straightforward to install. But if you're a bit less familiar =
with Tcl=20
extensions, and perhaps depending on the setup of your machine, you =
could run=20
into some troubles. Here we'll try to deal with some of the common =
cases. </P>
<H3>Extensions and Packages</H3>
<P>Tcl extensions can be composed of one or more binary shared libraries =
(e.g.=20
".so" or ".dll") and Tcl script files. MetaKit is pretty simple in that =
regard=20
in that it compiles into a single file, a platform-specific shared =
library (e.g.=20
libmk4tcl.so or libmk4tcl.dll) that contains all of the core MetaKit =
library=20
including the Tcl API. As you can see by the filename, this extension is =
known=20
as "Mk4tcl". </P>
<P>Tcl extensions are normally bundled into and distributed as =
"packages" and=20
loaded into your application via e.g. "package require Mk4tcl". A =
package is=20
installed in a directory alongside the Tcl distribution, where the =
directory is=20
given a name reflecting the name and version of the package (e.g.=20
/usr/lib/mk4tcl2.4). Inside this directory there is a special index file =
(always=20
named "pkgIndex.tcl"), which describes how to load the needed pieces (in =
this=20
case, just the MetaKit shared library), when an application does a =
"package=20
require". </P>
<H3>Compiling from Source</H3>
<P>If you're building and installing MetaKit from the source =
distribution, you=20
should have no problems; the extension will be built, and the package =
properly=20
constructed and installed. This is particularly true if you've installed =
Tcl on=20
your system yourself. </P>
<P>Problems to watch for here, especially when you're using an existing =
Tcl=20
version on the system, is that it's a fairly recent version of Tcl, and =
that the=20
Mk4tcl package is installed in a place where Tcl can find it (you can =
find out=20
where Tcl looks by examining the Tcl global variable "tcl_pkgPath"). =
</P>
<H3>Pre-Compiled Binaries</H3>
<P>Equi4 distributes pre-compiled binaries of MetaKit for a few =
platforms, so=20
you may also have the option of just downloading the libmk4tcl.so or=20
libmk4tcl.dll file for your platform. </P>
<P>This is just the shared library though, so to use it in your =
application via=20
"package require" you'll still need to create the package. One option is =
to=20
manually do this, creating the "mk4tcl2.4" directory where Tcl installs =
its=20
other packages, and using the Tcl "pkg_mkIndex" command (see the manual =
entry=20
for this command) to build the pkgIndex.tcl file needed to load the =
MetaKit=20
shared library. </P>
<P>There is a simpler option though, which you can use because MetaKit =
is just a=20
single shared library. Rather than loading it into your application with =

"package require", you can instead load the shared library directly. To =
do this,=20
first install the shared library in a known location on your machine. In =
your=20
program, replace the "package require mk4tcl" with the command "load=20
&lt;<I>path</I>&gt;/libmk4tcl.so" (or libmk4tcl.dll). </P>
<P>This document is Copyright 2002 by Mark Roseman. Permission is =
granted to=20
make unmodified copies of this document and to redistribute them. For =
the latest=20
version, see <A=20
href=3D"http://www.markroseman.com/tcl/">http://www.markroseman.com/tcl/<=
/A>=20
</P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -