I need an efficient way to store IP address.
The query I would like to do is if IP is in the table (or in range).
I need to store ranges like,
192.168.0.1 - 192.168.1.255
And also individual IPs.
Since there are many records I think it will be a waste to store them one by one. But I would go for that if that would be the best.
Django does have GenericIPAddressField field, but it does not store ranges.
So what is my best solution?
You have two options, both involve the use of two columns.
Option one, store start IP end IP
class IPmodel(models.Model):
start_ip = models.GenericIPAddressField()
end_ip = models.GenericIPAddressField()
Option 2, store the IP and a network mask
class IPmodel(models.Model):
start_ip = models.GenericIPAddressField()
mask = models.IntegerField()
If you were to use option 1, your code might look like
IPmodel.objects.create(start_ip='192.168.1.0',end_ip='192.168.1.255')
IPmodel.objects.filter(end_ip__gt='192.168.1.10').filter(start_ip__lt='192.168.1.10')
If you used option 2, you would need to use F functions and the standard network address calculations to do your filtering.
Related
I have auto-increment ids as primary is all of my db tables, like users, orders etc. I do not want to expose these ids to end users, as they may iterate over IDs can get access to user details. Instead I want to use a 2-way maths function such that I can obfuscate and de-obfuscate an id without storing a DB mapping.
function obfuscate(id)
{
constSeed = 1203793
return (id*constSeed)
}
function deobfuscate(bigid)
{
constSeed = 1203793
return (bigid/constSeed)
}
I can even run the bigid through a base36 converter, to get a smaller alphanumeric id, publicly exposable.
Are issues with this approach? Any other suggestions?
If you don't want them with access to the ID's maybe use them only in $_SESSION variables or something along those lines.
If the data is visible to the end user, even if you hash or encrypt the data,
it will not be safe.
I have a database where a field (plate) has values like ABC123, ABB-123, ... Some users enter a - in between, some don't.
Now I'm building a search function where the user can perform a search, for example 'ABB123' which the search function transform to plate LIKE 'ABB123' but I would like that this search returns 'ABB-123' in this case.
Has someone any idea how to pull this off?
Note: database is InnoDB, so full-text search isn't possible. Since each user has his own database, I'm not planning to use a third-party search server either.
Can you search for both instances?
plate = 'ABB123' OR plate = 'ABB-123'
Or if the format of plate is rather consistent, could you do something like
plate LIKE 'ABB%123'
That would get both instances.
We want to set up a directory of all the organizations working with us. They are incredibly diverse (government, embassy, private companies, and organizations depending on them ). So, I've resolved to create 2 tables. Table 1 will treat all the organizations equally, i.e. it'll collect all the basic information (name, address, phone number, etc.). Table 2 will establish the hierarchy among all the organizations. For instance, Program for illiterate adults depends on the National Institute for Social Security which depends on the Labor Ministry.
In the Hierarchy table, each column represents a level. So, for the example above, (i)Labor Ministry - Level1(column1), (ii)National Institute for Social Security - Level2(column2), (iii)Program for illiterate adults - Level3(column3).
To attach an organization to an hierarchy, the user needs to go level by level(i.e. column by column). So, there will be at least 3 situations:
If an adequate hierarchy exists for an organization(for instance, level1: US Embassy), that organization can be added (For instance, level2: USAID).--> US Embassy/USAID, and so on.
How about if one or more levels are missing? - then they need to be added
How about if the hierarchy need to be modified? -- not every thing need to be modified.
I do not have any choice but working by level (i.e. column by column). I does not make sense to have all the levels in one form as the user need to navigate hierarchies to find the right one to attach an organization.
Let's say, I have those queries in my repository (just that you get the idea).
Query1
var orgHierarchy = (from orgH in db.Hierarchy
select orgH.Level1).FirstOrDefault;
Query2
var orgHierarchy = (from orgH in db.Hierarchy
select orgH.Level2).FirstOrDefault;
Query3, Query4, etc.
The above queries are the same except for the property queried (level1, level2, level3, etc.)
Question: Is there a general way of writing the above queries in one? So that the user can track an hierarchy level by level to attach an organization.
In other words, not knowing in advance which column to query, I still need to be able to do so depending on some conditions. For instance, an organization X depends on Y. Knowing that Y is somewhere on the 3rd level, I'll go to the 4th level, linking X to Y.
I need to select (not manually) a column with only one query that takes parameters.
=======================
EDIT
As I just said to #Mark Byers, all I want is just to be able to query a column not knowing in advance which one. Check this out:
How about this
Public Hierarchy GetHierarchy(string name)
{
var myHierarchy = from hierarc in db.Hierarchy
where (hierarc.Level1 == name)
select hierarc;
retuen myHierarchy;
}
Above, the query depends on name which is a variable. It mighbe Planning Ministry, Embassy, Local Phone, etc.
Can I write the same query, but this time instead of looking to much a value in the DB, I impose my query to select a particular column.
var myVar = from orgH in db.Hierarchy
where (orgH.Level1 == "Government")
select orgH.where(level == myVariable);
return myVar;
I don't pretend that select orgH.where(level == myVariable) is even close to be valid. But that is what I want: to be able to select a column depending on a variable (i.e. the value is not known in advance like with name).
Thanks for helping
How about using DynamicQueryable?
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Your database is not normalized so you should start by changing the heirarchy table to, for example:
OrganizationId Parent
1 NULL
2 1
3 1
4 3
To query this you might need to use recursive queries. This is difficult (but not impossible) using LINQ, so you might instead prefer to create a parameterized stored procedure using a recursive CTE and put the query there.
I am modifying my PHP network's code to have "user roles" like wordpress here is my plan so far
0 = registred non email verified user
1 = registed and verified email
2 = moderator
3-9 = nothing yet
10= admin
In my PHP code I will use an array like this that will set what a role number does.
$user_role['10']
I was thinking of storing which value a user has in my mysql DB, would this be the best way to store the 10 different role options as an enum or is there a better way or faster way? I read that enum is not the fastest sometimes.
enum('0','1','2','3','4','5','6','7','8','9','10')
I guess INT or TINYINT is enough to store user level (role). Also you may consider multiplting the numbers by 10, so you'll have a space to add more levels in future.
Use bitmasks for permissions / user levels.
See example.
Is there a way to use my own number in a table like an auto-number; that is to automatically assign the next available to a new record. We have system ID numbers for each employee that I want to tie into this database. I just want the table to auto assign the next number.
Can I do this?
Could this be done with a mixture of numeric and alpha?
Could criteria be used, like Code A = certain set of numbers, Code B = another?
Of course you can, but you will have to design this yourself.
There are hundreds of ways of doing this but one way might be that you may have a parameters table with "nextQuote", "nextEmployeeNo", "nextJob"... of course your table design could have anything, including prefix example;
PARAMETERS
Prefix Number
Q 1145
E 54
J 999
So now you can SELECT PreFix + MAX(Number) AS NextEmployee FROM Parameters WHERE Prefix = E
And in your code you can increment the number after dealing with it.
UPDATE Parameters SET Number = number + 1 WHERE Prefix = E
If this isn't up to the job then hopefully it will get you thinking in how you can do something similar.
Hope this helps.
For ADO users:
How To Implement Multiuser Custom Counters in Jet 4.0
For DAO users:
How To Implement Multi-user Custom Counters in DAO 3.5